1 // SPDX-License-Identifier: GPL-2.0-only << 2 /* 1 /* 3 * Copyright 2002-2004, Instant802 Networks, I 2 * Copyright 2002-2004, Instant802 Networks, Inc. 4 * Copyright 2008, Jouni Malinen <j@w1.fi> 3 * Copyright 2008, Jouni Malinen <j@w1.fi> 5 * Copyright (C) 2016-2017 Intel Deutschland G !! 4 * Copyright (C) 2016 Intel Deutschland GmbH 6 * Copyright (C) 2020-2023 Intel Corporation !! 5 * >> 6 * This program is free software; you can redistribute it and/or modify >> 7 * it under the terms of the GNU General Public License version 2 as >> 8 * published by the Free Software Foundation. 7 */ 9 */ 8 10 9 #include <linux/netdevice.h> 11 #include <linux/netdevice.h> 10 #include <linux/types.h> 12 #include <linux/types.h> 11 #include <linux/skbuff.h> 13 #include <linux/skbuff.h> 12 #include <linux/compiler.h> 14 #include <linux/compiler.h> 13 #include <linux/ieee80211.h> 15 #include <linux/ieee80211.h> 14 #include <linux/gfp.h> 16 #include <linux/gfp.h> 15 #include <linux/unaligned.h> !! 17 #include <asm/unaligned.h> 16 #include <net/mac80211.h> 18 #include <net/mac80211.h> 17 #include <crypto/aes.h> 19 #include <crypto/aes.h> 18 #include <crypto/utils.h> << 19 20 20 #include "ieee80211_i.h" 21 #include "ieee80211_i.h" 21 #include "michael.h" 22 #include "michael.h" 22 #include "tkip.h" 23 #include "tkip.h" 23 #include "aes_ccm.h" 24 #include "aes_ccm.h" 24 #include "aes_cmac.h" 25 #include "aes_cmac.h" 25 #include "aes_gmac.h" 26 #include "aes_gmac.h" 26 #include "aes_gcm.h" 27 #include "aes_gcm.h" 27 #include "wpa.h" 28 #include "wpa.h" 28 29 29 ieee80211_tx_result 30 ieee80211_tx_result 30 ieee80211_tx_h_michael_mic_add(struct ieee8021 31 ieee80211_tx_h_michael_mic_add(struct ieee80211_tx_data *tx) 31 { 32 { 32 u8 *data, *key, *mic; 33 u8 *data, *key, *mic; 33 size_t data_len; 34 size_t data_len; 34 unsigned int hdrlen; 35 unsigned int hdrlen; 35 struct ieee80211_hdr *hdr; 36 struct ieee80211_hdr *hdr; 36 struct sk_buff *skb = tx->skb; 37 struct sk_buff *skb = tx->skb; 37 struct ieee80211_tx_info *info = IEEE8 38 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 38 int tail; 39 int tail; 39 40 40 hdr = (struct ieee80211_hdr *)skb->dat 41 hdr = (struct ieee80211_hdr *)skb->data; 41 if (!tx->key || tx->key->conf.cipher ! 42 if (!tx->key || tx->key->conf.cipher != WLAN_CIPHER_SUITE_TKIP || 42 skb->len < 24 || !ieee80211_is_dat 43 skb->len < 24 || !ieee80211_is_data_present(hdr->frame_control)) 43 return TX_CONTINUE; 44 return TX_CONTINUE; 44 45 45 hdrlen = ieee80211_hdrlen(hdr->frame_c 46 hdrlen = ieee80211_hdrlen(hdr->frame_control); 46 if (skb->len < hdrlen) 47 if (skb->len < hdrlen) 47 return TX_DROP; 48 return TX_DROP; 48 49 49 data = skb->data + hdrlen; 50 data = skb->data + hdrlen; 50 data_len = skb->len - hdrlen; 51 data_len = skb->len - hdrlen; 51 52 52 if (unlikely(info->flags & IEEE80211_T 53 if (unlikely(info->flags & IEEE80211_TX_INTFL_TKIP_MIC_FAILURE)) { 53 /* Need to use software crypto 54 /* Need to use software crypto for the test */ 54 info->control.hw_key = NULL; 55 info->control.hw_key = NULL; 55 } 56 } 56 57 57 if (info->control.hw_key && 58 if (info->control.hw_key && 58 (info->flags & IEEE80211_TX_CTL_DO 59 (info->flags & IEEE80211_TX_CTL_DONTFRAG || 59 ieee80211_hw_check(&tx->local->hw 60 ieee80211_hw_check(&tx->local->hw, SUPPORTS_TX_FRAG)) && 60 !(tx->key->conf.flags & (IEEE80211 !! 61 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) { 61 IEEE80211 !! 62 /* hwaccel - with no need for SW-generated MMIC */ 62 /* hwaccel - with no need for << 63 return TX_CONTINUE; 63 return TX_CONTINUE; 64 } 64 } 65 65 66 tail = MICHAEL_MIC_LEN; 66 tail = MICHAEL_MIC_LEN; 67 if (!info->control.hw_key) 67 if (!info->control.hw_key) 68 tail += IEEE80211_TKIP_ICV_LEN 68 tail += IEEE80211_TKIP_ICV_LEN; 69 69 70 if (WARN(skb_tailroom(skb) < tail || 70 if (WARN(skb_tailroom(skb) < tail || 71 skb_headroom(skb) < IEEE80211 71 skb_headroom(skb) < IEEE80211_TKIP_IV_LEN, 72 "mmic: not enough head/tail ( 72 "mmic: not enough head/tail (%d/%d,%d/%d)\n", 73 skb_headroom(skb), IEEE80211_ 73 skb_headroom(skb), IEEE80211_TKIP_IV_LEN, 74 skb_tailroom(skb), tail)) 74 skb_tailroom(skb), tail)) 75 return TX_DROP; 75 return TX_DROP; 76 76 77 mic = skb_put(skb, MICHAEL_MIC_LEN); << 78 << 79 if (tx->key->conf.flags & IEEE80211_KE << 80 /* Zeroed MIC can help with de << 81 memset(mic, 0, MICHAEL_MIC_LEN << 82 return TX_CONTINUE; << 83 } << 84 << 85 key = &tx->key->conf.key[NL80211_TKIP_ 77 key = &tx->key->conf.key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY]; >> 78 mic = skb_put(skb, MICHAEL_MIC_LEN); 86 michael_mic(key, hdr, data, data_len, 79 michael_mic(key, hdr, data, data_len, mic); 87 if (unlikely(info->flags & IEEE80211_T 80 if (unlikely(info->flags & IEEE80211_TX_INTFL_TKIP_MIC_FAILURE)) 88 mic[0]++; 81 mic[0]++; 89 82 90 return TX_CONTINUE; 83 return TX_CONTINUE; 91 } 84 } 92 85 93 86 94 ieee80211_rx_result 87 ieee80211_rx_result 95 ieee80211_rx_h_michael_mic_verify(struct ieee8 88 ieee80211_rx_h_michael_mic_verify(struct ieee80211_rx_data *rx) 96 { 89 { 97 u8 *data, *key = NULL; 90 u8 *data, *key = NULL; 98 size_t data_len; 91 size_t data_len; 99 unsigned int hdrlen; 92 unsigned int hdrlen; 100 u8 mic[MICHAEL_MIC_LEN]; 93 u8 mic[MICHAEL_MIC_LEN]; 101 struct sk_buff *skb = rx->skb; 94 struct sk_buff *skb = rx->skb; 102 struct ieee80211_rx_status *status = I 95 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 103 struct ieee80211_hdr *hdr = (struct ie 96 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 104 97 105 /* 98 /* 106 * it makes no sense to check for MIC 99 * it makes no sense to check for MIC errors on anything other 107 * than data frames. 100 * than data frames. 108 */ 101 */ 109 if (!ieee80211_is_data_present(hdr->fr 102 if (!ieee80211_is_data_present(hdr->frame_control)) 110 return RX_CONTINUE; 103 return RX_CONTINUE; 111 104 112 /* 105 /* 113 * No way to verify the MIC if the har 106 * No way to verify the MIC if the hardware stripped it or 114 * the IV with the key index. In this 107 * the IV with the key index. In this case we have solely rely 115 * on the driver to set RX_FLAG_MMIC_E 108 * on the driver to set RX_FLAG_MMIC_ERROR in the event of a 116 * MIC failure report. 109 * MIC failure report. 117 */ 110 */ 118 if (status->flag & (RX_FLAG_MMIC_STRIP 111 if (status->flag & (RX_FLAG_MMIC_STRIPPED | RX_FLAG_IV_STRIPPED)) { 119 if (status->flag & RX_FLAG_MMI 112 if (status->flag & RX_FLAG_MMIC_ERROR) 120 goto mic_fail_no_key; 113 goto mic_fail_no_key; 121 114 122 if (!(status->flag & RX_FLAG_I 115 if (!(status->flag & RX_FLAG_IV_STRIPPED) && rx->key && 123 rx->key->conf.cipher == WL 116 rx->key->conf.cipher == WLAN_CIPHER_SUITE_TKIP) 124 goto update_iv; 117 goto update_iv; 125 118 126 return RX_CONTINUE; 119 return RX_CONTINUE; 127 } 120 } 128 121 129 /* 122 /* 130 * Some hardware seems to generate Mic 123 * Some hardware seems to generate Michael MIC failure reports; even 131 * though, the frame was not encrypted 124 * though, the frame was not encrypted with TKIP and therefore has no 132 * MIC. Ignore the flag them to avoid 125 * MIC. Ignore the flag them to avoid triggering countermeasures. 133 */ 126 */ 134 if (!rx->key || rx->key->conf.cipher ! 127 if (!rx->key || rx->key->conf.cipher != WLAN_CIPHER_SUITE_TKIP || 135 !(status->flag & RX_FLAG_DECRYPTED 128 !(status->flag & RX_FLAG_DECRYPTED)) 136 return RX_CONTINUE; 129 return RX_CONTINUE; 137 130 138 if (rx->sdata->vif.type == NL80211_IFT 131 if (rx->sdata->vif.type == NL80211_IFTYPE_AP && rx->key->conf.keyidx) { 139 /* 132 /* 140 * APs with pairwise keys shou 133 * APs with pairwise keys should never receive Michael MIC 141 * errors for non-zero keyidx 134 * errors for non-zero keyidx because these are reserved for 142 * group keys and only the AP 135 * group keys and only the AP is sending real multicast 143 * frames in the BSS. 136 * frames in the BSS. 144 */ 137 */ 145 return RX_DROP_U_AP_RX_GROUPCA !! 138 return RX_DROP_UNUSABLE; 146 } 139 } 147 140 148 if (status->flag & RX_FLAG_MMIC_ERROR) 141 if (status->flag & RX_FLAG_MMIC_ERROR) 149 goto mic_fail; 142 goto mic_fail; 150 143 151 hdrlen = ieee80211_hdrlen(hdr->frame_c 144 hdrlen = ieee80211_hdrlen(hdr->frame_control); 152 if (skb->len < hdrlen + MICHAEL_MIC_LE 145 if (skb->len < hdrlen + MICHAEL_MIC_LEN) 153 return RX_DROP_U_SHORT_MMIC; !! 146 return RX_DROP_UNUSABLE; 154 147 155 if (skb_linearize(rx->skb)) 148 if (skb_linearize(rx->skb)) 156 return RX_DROP_U_OOM; !! 149 return RX_DROP_UNUSABLE; 157 hdr = (void *)skb->data; 150 hdr = (void *)skb->data; 158 151 159 data = skb->data + hdrlen; 152 data = skb->data + hdrlen; 160 data_len = skb->len - hdrlen - MICHAEL 153 data_len = skb->len - hdrlen - MICHAEL_MIC_LEN; 161 key = &rx->key->conf.key[NL80211_TKIP_ 154 key = &rx->key->conf.key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY]; 162 michael_mic(key, hdr, data, data_len, 155 michael_mic(key, hdr, data, data_len, mic); 163 if (crypto_memneq(mic, data + data_len !! 156 if (memcmp(mic, data + data_len, MICHAEL_MIC_LEN) != 0) 164 goto mic_fail; 157 goto mic_fail; 165 158 166 /* remove Michael MIC from payload */ 159 /* remove Michael MIC from payload */ 167 skb_trim(skb, skb->len - MICHAEL_MIC_L 160 skb_trim(skb, skb->len - MICHAEL_MIC_LEN); 168 161 169 update_iv: 162 update_iv: 170 /* update IV in key information to be 163 /* update IV in key information to be able to detect replays */ 171 rx->key->u.tkip.rx[rx->security_idx].i !! 164 rx->key->u.tkip.rx[rx->security_idx].iv32 = rx->tkip_iv32; 172 rx->key->u.tkip.rx[rx->security_idx].i !! 165 rx->key->u.tkip.rx[rx->security_idx].iv16 = rx->tkip_iv16; 173 166 174 return RX_CONTINUE; 167 return RX_CONTINUE; 175 168 176 mic_fail: 169 mic_fail: 177 rx->key->u.tkip.mic_failures++; 170 rx->key->u.tkip.mic_failures++; 178 171 179 mic_fail_no_key: 172 mic_fail_no_key: 180 /* 173 /* 181 * In some cases the key can be unset 174 * In some cases the key can be unset - e.g. a multicast packet, in 182 * a driver that supports HW encryptio 175 * a driver that supports HW encryption. Send up the key idx only if 183 * the key is set. 176 * the key is set. 184 */ 177 */ 185 cfg80211_michael_mic_failure(rx->sdata 178 cfg80211_michael_mic_failure(rx->sdata->dev, hdr->addr2, 186 is_multic 179 is_multicast_ether_addr(hdr->addr1) ? 187 NL80211_K 180 NL80211_KEYTYPE_GROUP : 188 NL80211_K 181 NL80211_KEYTYPE_PAIRWISE, 189 rx->key ? 182 rx->key ? rx->key->conf.keyidx : -1, 190 NULL, GFP 183 NULL, GFP_ATOMIC); 191 return RX_DROP_U_MMIC_FAIL; !! 184 return RX_DROP_UNUSABLE; 192 } 185 } 193 186 194 static int tkip_encrypt_skb(struct ieee80211_t 187 static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb) 195 { 188 { 196 struct ieee80211_hdr *hdr = (struct ie 189 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 197 struct ieee80211_key *key = tx->key; 190 struct ieee80211_key *key = tx->key; 198 struct ieee80211_tx_info *info = IEEE8 191 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 199 unsigned int hdrlen; 192 unsigned int hdrlen; 200 int len, tail; 193 int len, tail; 201 u64 pn; 194 u64 pn; 202 u8 *pos; 195 u8 *pos; 203 196 204 if (info->control.hw_key && 197 if (info->control.hw_key && 205 !(info->control.hw_key->flags & IE 198 !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) && 206 !(info->control.hw_key->flags & IE 199 !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) { 207 /* hwaccel - with no need for 200 /* hwaccel - with no need for software-generated IV */ 208 return 0; 201 return 0; 209 } 202 } 210 203 211 hdrlen = ieee80211_hdrlen(hdr->frame_c 204 hdrlen = ieee80211_hdrlen(hdr->frame_control); 212 len = skb->len - hdrlen; 205 len = skb->len - hdrlen; 213 206 214 if (info->control.hw_key) 207 if (info->control.hw_key) 215 tail = 0; 208 tail = 0; 216 else 209 else 217 tail = IEEE80211_TKIP_ICV_LEN; 210 tail = IEEE80211_TKIP_ICV_LEN; 218 211 219 if (WARN_ON(skb_tailroom(skb) < tail | 212 if (WARN_ON(skb_tailroom(skb) < tail || 220 skb_headroom(skb) < IEEE80 213 skb_headroom(skb) < IEEE80211_TKIP_IV_LEN)) 221 return -1; 214 return -1; 222 215 223 pos = skb_push(skb, IEEE80211_TKIP_IV_ 216 pos = skb_push(skb, IEEE80211_TKIP_IV_LEN); 224 memmove(pos, pos + IEEE80211_TKIP_IV_L 217 memmove(pos, pos + IEEE80211_TKIP_IV_LEN, hdrlen); 225 pos += hdrlen; 218 pos += hdrlen; 226 219 227 /* the HW only needs room for the IV, 220 /* the HW only needs room for the IV, but not the actual IV */ 228 if (info->control.hw_key && 221 if (info->control.hw_key && 229 (info->control.hw_key->flags & IEE 222 (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) 230 return 0; 223 return 0; 231 224 232 /* Increase IV for the frame */ 225 /* Increase IV for the frame */ 233 pn = atomic64_inc_return(&key->conf.tx 226 pn = atomic64_inc_return(&key->conf.tx_pn); 234 pos = ieee80211_tkip_add_iv(pos, &key- 227 pos = ieee80211_tkip_add_iv(pos, &key->conf, pn); 235 228 236 /* hwaccel - with software IV */ 229 /* hwaccel - with software IV */ 237 if (info->control.hw_key) 230 if (info->control.hw_key) 238 return 0; 231 return 0; 239 232 240 /* Add room for ICV */ 233 /* Add room for ICV */ 241 skb_put(skb, IEEE80211_TKIP_ICV_LEN); 234 skb_put(skb, IEEE80211_TKIP_ICV_LEN); 242 235 243 return ieee80211_tkip_encrypt_data(&tx !! 236 return ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm, 244 key 237 key, skb, pos, len); 245 } 238 } 246 239 247 240 248 ieee80211_tx_result 241 ieee80211_tx_result 249 ieee80211_crypto_tkip_encrypt(struct ieee80211 242 ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data *tx) 250 { 243 { 251 struct sk_buff *skb; 244 struct sk_buff *skb; 252 245 253 ieee80211_tx_set_protected(tx); 246 ieee80211_tx_set_protected(tx); 254 247 255 skb_queue_walk(&tx->skbs, skb) { 248 skb_queue_walk(&tx->skbs, skb) { 256 if (tkip_encrypt_skb(tx, skb) 249 if (tkip_encrypt_skb(tx, skb) < 0) 257 return TX_DROP; 250 return TX_DROP; 258 } 251 } 259 252 260 return TX_CONTINUE; 253 return TX_CONTINUE; 261 } 254 } 262 255 263 256 264 ieee80211_rx_result 257 ieee80211_rx_result 265 ieee80211_crypto_tkip_decrypt(struct ieee80211 258 ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data *rx) 266 { 259 { 267 struct ieee80211_hdr *hdr = (struct ie 260 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data; 268 int hdrlen, res, hwaccel = 0; 261 int hdrlen, res, hwaccel = 0; 269 struct ieee80211_key *key = rx->key; 262 struct ieee80211_key *key = rx->key; 270 struct sk_buff *skb = rx->skb; 263 struct sk_buff *skb = rx->skb; 271 struct ieee80211_rx_status *status = I 264 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 272 265 273 hdrlen = ieee80211_hdrlen(hdr->frame_c 266 hdrlen = ieee80211_hdrlen(hdr->frame_control); 274 267 275 if (!ieee80211_is_data(hdr->frame_cont 268 if (!ieee80211_is_data(hdr->frame_control)) 276 return RX_CONTINUE; 269 return RX_CONTINUE; 277 270 278 if (!rx->sta || skb->len - hdrlen < 12 271 if (!rx->sta || skb->len - hdrlen < 12) 279 return RX_DROP_U_SHORT_TKIP; !! 272 return RX_DROP_UNUSABLE; 280 273 281 /* it may be possible to optimize this 274 /* it may be possible to optimize this a bit more */ 282 if (skb_linearize(rx->skb)) 275 if (skb_linearize(rx->skb)) 283 return RX_DROP_U_OOM; !! 276 return RX_DROP_UNUSABLE; 284 hdr = (void *)skb->data; 277 hdr = (void *)skb->data; 285 278 286 /* 279 /* 287 * Let TKIP code verify IV, but skip d 280 * Let TKIP code verify IV, but skip decryption. 288 * In the case where hardware checks t 281 * In the case where hardware checks the IV as well, 289 * we don't even get here, see ieee802 282 * we don't even get here, see ieee80211_rx_h_decrypt() 290 */ 283 */ 291 if (status->flag & RX_FLAG_DECRYPTED) 284 if (status->flag & RX_FLAG_DECRYPTED) 292 hwaccel = 1; 285 hwaccel = 1; 293 286 294 res = ieee80211_tkip_decrypt_data(&rx- !! 287 res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm, 295 key, 288 key, skb->data + hdrlen, 296 skb- 289 skb->len - hdrlen, rx->sta->sta.addr, 297 hdr- 290 hdr->addr1, hwaccel, rx->security_idx, 298 &rx- !! 291 &rx->tkip_iv32, 299 &rx- !! 292 &rx->tkip_iv16); 300 if (res != TKIP_DECRYPT_OK) 293 if (res != TKIP_DECRYPT_OK) 301 return RX_DROP_U_TKIP_FAIL; !! 294 return RX_DROP_UNUSABLE; 302 295 303 /* Trim ICV */ 296 /* Trim ICV */ 304 if (!(status->flag & RX_FLAG_ICV_STRIP !! 297 skb_trim(skb, skb->len - IEEE80211_TKIP_ICV_LEN); 305 skb_trim(skb, skb->len - IEEE8 << 306 298 307 /* Remove IV */ 299 /* Remove IV */ 308 memmove(skb->data + IEEE80211_TKIP_IV_ 300 memmove(skb->data + IEEE80211_TKIP_IV_LEN, skb->data, hdrlen); 309 skb_pull(skb, IEEE80211_TKIP_IV_LEN); 301 skb_pull(skb, IEEE80211_TKIP_IV_LEN); 310 302 311 return RX_CONTINUE; 303 return RX_CONTINUE; 312 } 304 } 313 305 314 /* !! 306 315 * Calculate AAD for CCMP/GCMP, returning qos_ !! 307 static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad) 316 * need that in CCMP also for b_0. << 317 */ << 318 static u8 ccmp_gcmp_aad(struct sk_buff *skb, u << 319 { 308 { 320 struct ieee80211_hdr *hdr = (void *)sk << 321 __le16 mask_fc; 309 __le16 mask_fc; 322 int a4_included, mgmt; 310 int a4_included, mgmt; 323 u8 qos_tid; 311 u8 qos_tid; 324 u16 len_a = 22; !! 312 u16 len_a; >> 313 unsigned int hdrlen; >> 314 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 325 315 326 /* 316 /* 327 * Mask FC: zero subtype b4 b5 b6 (if 317 * Mask FC: zero subtype b4 b5 b6 (if not mgmt) 328 * Retry, PwrMgt, MoreData, Order (if !! 318 * Retry, PwrMgt, MoreData; set Protected 329 */ 319 */ 330 mgmt = ieee80211_is_mgmt(hdr->frame_co 320 mgmt = ieee80211_is_mgmt(hdr->frame_control); 331 mask_fc = hdr->frame_control; 321 mask_fc = hdr->frame_control; 332 mask_fc &= ~cpu_to_le16(IEEE80211_FCTL 322 mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY | 333 IEEE80211_FCTL 323 IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA); 334 if (!mgmt) 324 if (!mgmt) 335 mask_fc &= ~cpu_to_le16(0x0070 325 mask_fc &= ~cpu_to_le16(0x0070); 336 mask_fc |= cpu_to_le16(IEEE80211_FCTL_ 326 mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED); 337 327 >> 328 hdrlen = ieee80211_hdrlen(hdr->frame_control); >> 329 len_a = hdrlen - 2; 338 a4_included = ieee80211_has_a4(hdr->fr 330 a4_included = ieee80211_has_a4(hdr->frame_control); 339 if (a4_included) << 340 len_a += 6; << 341 331 342 if (ieee80211_is_data_qos(hdr->frame_c !! 332 if (ieee80211_is_data_qos(hdr->frame_control)) 343 qos_tid = *ieee80211_get_qos_c !! 333 qos_tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK; >> 334 else >> 335 qos_tid = 0; 344 336 345 if (spp_amsdu) !! 337 /* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC 346 qos_tid &= IEEE80211_Q !! 338 * mode authentication are not allowed to collide, yet both are derived 347 IEEE80211_Q !! 339 * from this vector b_0. We only set L := 1 here to indicate that the 348 else !! 340 * data size can be represented in (L+1) bytes. The CCM layer will take 349 qos_tid &= IEEE80211_Q !! 341 * care of storing the data length in the top (L+1) bytes and setting >> 342 * and clearing the other bits as is required to derive the two IVs. >> 343 */ >> 344 b_0[0] = 0x1; 350 345 351 mask_fc &= ~cpu_to_le16(IEEE80 !! 346 /* Nonce: Nonce Flags | A2 | PN 352 len_a += 2; !! 347 * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7) 353 } else { !! 348 */ 354 qos_tid = 0; !! 349 b_0[1] = qos_tid | (mgmt << 4); 355 } !! 350 memcpy(&b_0[2], hdr->addr2, ETH_ALEN); >> 351 memcpy(&b_0[8], pn, IEEE80211_CCMP_PN_LEN); 356 352 357 /* AAD (extra authenticate-only data) 353 /* AAD (extra authenticate-only data) / masked 802.11 header 358 * FC | A1 | A2 | A3 | SC | [A4] | [QC 354 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */ 359 put_unaligned_be16(len_a, &aad[0]); 355 put_unaligned_be16(len_a, &aad[0]); 360 put_unaligned(mask_fc, (__le16 *)&aad[ 356 put_unaligned(mask_fc, (__le16 *)&aad[2]); 361 memcpy(&aad[4], &hdr->addrs, 3 * ETH_A !! 357 memcpy(&aad[4], &hdr->addr1, 3 * ETH_ALEN); 362 358 363 /* Mask Seq#, leave Frag# */ 359 /* Mask Seq#, leave Frag# */ 364 aad[22] = *((u8 *) &hdr->seq_ctrl) & 0 360 aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f; 365 aad[23] = 0; 361 aad[23] = 0; 366 362 367 if (a4_included) { 363 if (a4_included) { 368 memcpy(&aad[24], hdr->addr4, E 364 memcpy(&aad[24], hdr->addr4, ETH_ALEN); 369 aad[30] = qos_tid; 365 aad[30] = qos_tid; 370 aad[31] = 0; 366 aad[31] = 0; 371 } else { 367 } else { 372 memset(&aad[24], 0, ETH_ALEN + 368 memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN); 373 aad[24] = qos_tid; 369 aad[24] = qos_tid; 374 } 370 } 375 << 376 return qos_tid; << 377 } 371 } 378 372 379 static void ccmp_special_blocks(struct sk_buff << 380 bool spp_amsdu << 381 { << 382 struct ieee80211_hdr *hdr = (struct ie << 383 u8 qos_tid = ccmp_gcmp_aad(skb, aad, s << 384 << 385 /* In CCM, the initial vectors (IV) us << 386 * mode authentication are not allowed << 387 * from this vector b_0. We only set L << 388 * data size can be represented in (L+ << 389 * care of storing the data length in << 390 * and clearing the other bits as is r << 391 */ << 392 b_0[0] = 0x1; << 393 << 394 /* Nonce: Nonce Flags | A2 | PN << 395 * Nonce Flags: Priority (b0..b3) | Ma << 396 */ << 397 b_0[1] = qos_tid | (ieee80211_is_mgmt( << 398 memcpy(&b_0[2], hdr->addr2, ETH_ALEN); << 399 memcpy(&b_0[8], pn, IEEE80211_CCMP_PN_ << 400 } << 401 373 402 static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn 374 static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id) 403 { 375 { 404 hdr[0] = pn[5]; 376 hdr[0] = pn[5]; 405 hdr[1] = pn[4]; 377 hdr[1] = pn[4]; 406 hdr[2] = 0; 378 hdr[2] = 0; 407 hdr[3] = 0x20 | (key_id << 6); 379 hdr[3] = 0x20 | (key_id << 6); 408 hdr[4] = pn[3]; 380 hdr[4] = pn[3]; 409 hdr[5] = pn[2]; 381 hdr[5] = pn[2]; 410 hdr[6] = pn[1]; 382 hdr[6] = pn[1]; 411 hdr[7] = pn[0]; 383 hdr[7] = pn[0]; 412 } 384 } 413 385 414 386 415 static inline void ccmp_hdr2pn(u8 *pn, u8 *hdr 387 static inline void ccmp_hdr2pn(u8 *pn, u8 *hdr) 416 { 388 { 417 pn[0] = hdr[7]; 389 pn[0] = hdr[7]; 418 pn[1] = hdr[6]; 390 pn[1] = hdr[6]; 419 pn[2] = hdr[5]; 391 pn[2] = hdr[5]; 420 pn[3] = hdr[4]; 392 pn[3] = hdr[4]; 421 pn[4] = hdr[1]; 393 pn[4] = hdr[1]; 422 pn[5] = hdr[0]; 394 pn[5] = hdr[0]; 423 } 395 } 424 396 425 397 426 static int ccmp_encrypt_skb(struct ieee80211_t 398 static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb, 427 unsigned int mic_l 399 unsigned int mic_len) 428 { 400 { 429 struct ieee80211_hdr *hdr = (struct ie 401 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 430 struct ieee80211_key *key = tx->key; 402 struct ieee80211_key *key = tx->key; 431 struct ieee80211_tx_info *info = IEEE8 403 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 432 int hdrlen, len, tail; 404 int hdrlen, len, tail; 433 u8 *pos; 405 u8 *pos; 434 u8 pn[6]; 406 u8 pn[6]; 435 u64 pn64; 407 u64 pn64; 436 u8 aad[CCM_AAD_LEN]; 408 u8 aad[CCM_AAD_LEN]; 437 u8 b_0[AES_BLOCK_SIZE]; 409 u8 b_0[AES_BLOCK_SIZE]; 438 410 439 if (info->control.hw_key && 411 if (info->control.hw_key && 440 !(info->control.hw_key->flags & IE 412 !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) && 441 !(info->control.hw_key->flags & IE 413 !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) && 442 !((info->control.hw_key->flags & 414 !((info->control.hw_key->flags & 443 IEEE80211_KEY_FLAG_GENERATE_IV_ 415 IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) && 444 ieee80211_is_mgmt(hdr->frame_con 416 ieee80211_is_mgmt(hdr->frame_control))) { 445 /* 417 /* 446 * hwaccel has no need for pre 418 * hwaccel has no need for preallocated room for CCMP 447 * header or MIC fields 419 * header or MIC fields 448 */ 420 */ 449 return 0; 421 return 0; 450 } 422 } 451 423 452 hdrlen = ieee80211_hdrlen(hdr->frame_c 424 hdrlen = ieee80211_hdrlen(hdr->frame_control); 453 len = skb->len - hdrlen; 425 len = skb->len - hdrlen; 454 426 455 if (info->control.hw_key) 427 if (info->control.hw_key) 456 tail = 0; 428 tail = 0; 457 else 429 else 458 tail = mic_len; 430 tail = mic_len; 459 431 460 if (WARN_ON(skb_tailroom(skb) < tail | 432 if (WARN_ON(skb_tailroom(skb) < tail || 461 skb_headroom(skb) < IEEE80 433 skb_headroom(skb) < IEEE80211_CCMP_HDR_LEN)) 462 return -1; 434 return -1; 463 435 464 pos = skb_push(skb, IEEE80211_CCMP_HDR 436 pos = skb_push(skb, IEEE80211_CCMP_HDR_LEN); 465 memmove(pos, pos + IEEE80211_CCMP_HDR_ 437 memmove(pos, pos + IEEE80211_CCMP_HDR_LEN, hdrlen); 466 438 467 /* the HW only needs room for the IV, 439 /* the HW only needs room for the IV, but not the actual IV */ 468 if (info->control.hw_key && 440 if (info->control.hw_key && 469 (info->control.hw_key->flags & IEE 441 (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) 470 return 0; 442 return 0; 471 443 >> 444 hdr = (struct ieee80211_hdr *) pos; 472 pos += hdrlen; 445 pos += hdrlen; 473 446 474 pn64 = atomic64_inc_return(&key->conf. 447 pn64 = atomic64_inc_return(&key->conf.tx_pn); 475 448 476 pn[5] = pn64; 449 pn[5] = pn64; 477 pn[4] = pn64 >> 8; 450 pn[4] = pn64 >> 8; 478 pn[3] = pn64 >> 16; 451 pn[3] = pn64 >> 16; 479 pn[2] = pn64 >> 24; 452 pn[2] = pn64 >> 24; 480 pn[1] = pn64 >> 32; 453 pn[1] = pn64 >> 32; 481 pn[0] = pn64 >> 40; 454 pn[0] = pn64 >> 40; 482 455 483 ccmp_pn2hdr(pos, pn, key->conf.keyidx) 456 ccmp_pn2hdr(pos, pn, key->conf.keyidx); 484 457 485 /* hwaccel - with software CCMP header 458 /* hwaccel - with software CCMP header */ 486 if (info->control.hw_key) 459 if (info->control.hw_key) 487 return 0; 460 return 0; 488 461 489 pos += IEEE80211_CCMP_HDR_LEN; 462 pos += IEEE80211_CCMP_HDR_LEN; 490 ccmp_special_blocks(skb, pn, b_0, aad, !! 463 ccmp_special_blocks(skb, pn, b_0, aad); 491 key->conf.flags & << 492 return ieee80211_aes_ccm_encrypt(key-> 464 return ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, b_0, aad, pos, len, 493 skb_p !! 465 skb_put(skb, mic_len), mic_len); 494 } 466 } 495 467 496 468 497 ieee80211_tx_result 469 ieee80211_tx_result 498 ieee80211_crypto_ccmp_encrypt(struct ieee80211 470 ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data *tx, 499 unsigned int mic 471 unsigned int mic_len) 500 { 472 { 501 struct sk_buff *skb; 473 struct sk_buff *skb; 502 474 503 ieee80211_tx_set_protected(tx); 475 ieee80211_tx_set_protected(tx); 504 476 505 skb_queue_walk(&tx->skbs, skb) { 477 skb_queue_walk(&tx->skbs, skb) { 506 if (ccmp_encrypt_skb(tx, skb, 478 if (ccmp_encrypt_skb(tx, skb, mic_len) < 0) 507 return TX_DROP; 479 return TX_DROP; 508 } 480 } 509 481 510 return TX_CONTINUE; 482 return TX_CONTINUE; 511 } 483 } 512 484 513 485 514 ieee80211_rx_result 486 ieee80211_rx_result 515 ieee80211_crypto_ccmp_decrypt(struct ieee80211 487 ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx, 516 unsigned int mic 488 unsigned int mic_len) 517 { 489 { 518 struct ieee80211_hdr *hdr = (struct ie 490 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data; 519 int hdrlen; 491 int hdrlen; 520 struct ieee80211_key *key = rx->key; 492 struct ieee80211_key *key = rx->key; 521 struct sk_buff *skb = rx->skb; 493 struct sk_buff *skb = rx->skb; 522 struct ieee80211_rx_status *status = I 494 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 523 u8 pn[IEEE80211_CCMP_PN_LEN]; 495 u8 pn[IEEE80211_CCMP_PN_LEN]; 524 int data_len; 496 int data_len; 525 int queue; 497 int queue; 526 498 527 hdrlen = ieee80211_hdrlen(hdr->frame_c 499 hdrlen = ieee80211_hdrlen(hdr->frame_control); 528 500 529 if (!ieee80211_is_data(hdr->frame_cont 501 if (!ieee80211_is_data(hdr->frame_control) && 530 !ieee80211_is_robust_mgmt_frame(sk 502 !ieee80211_is_robust_mgmt_frame(skb)) 531 return RX_CONTINUE; 503 return RX_CONTINUE; 532 504 533 if (status->flag & RX_FLAG_DECRYPTED) 505 if (status->flag & RX_FLAG_DECRYPTED) { 534 if (!pskb_may_pull(rx->skb, hd 506 if (!pskb_may_pull(rx->skb, hdrlen + IEEE80211_CCMP_HDR_LEN)) 535 return RX_DROP_U_SHORT !! 507 return RX_DROP_UNUSABLE; 536 if (status->flag & RX_FLAG_MIC 508 if (status->flag & RX_FLAG_MIC_STRIPPED) 537 mic_len = 0; 509 mic_len = 0; 538 } else { 510 } else { 539 if (skb_linearize(rx->skb)) 511 if (skb_linearize(rx->skb)) 540 return RX_DROP_U_OOM; !! 512 return RX_DROP_UNUSABLE; 541 } 513 } 542 514 543 /* reload hdr - skb might have been re << 544 hdr = (void *)rx->skb->data; << 545 << 546 data_len = skb->len - hdrlen - IEEE802 515 data_len = skb->len - hdrlen - IEEE80211_CCMP_HDR_LEN - mic_len; 547 if (!rx->sta || data_len < 0) 516 if (!rx->sta || data_len < 0) 548 return RX_DROP_U_SHORT_CCMP; !! 517 return RX_DROP_UNUSABLE; 549 518 550 if (!(status->flag & RX_FLAG_PN_VALIDA 519 if (!(status->flag & RX_FLAG_PN_VALIDATED)) { 551 int res; 520 int res; 552 521 553 ccmp_hdr2pn(pn, skb->data + hd 522 ccmp_hdr2pn(pn, skb->data + hdrlen); 554 523 555 queue = rx->security_idx; 524 queue = rx->security_idx; 556 525 557 res = memcmp(pn, key->u.ccmp.r 526 res = memcmp(pn, key->u.ccmp.rx_pn[queue], 558 IEEE80211_CCMP_PN 527 IEEE80211_CCMP_PN_LEN); 559 if (res < 0 || 528 if (res < 0 || 560 (!res && !(status->flag & 529 (!res && !(status->flag & RX_FLAG_ALLOW_SAME_PN))) { 561 key->u.ccmp.replays++; 530 key->u.ccmp.replays++; 562 return RX_DROP_U_REPLA !! 531 return RX_DROP_UNUSABLE; 563 } 532 } 564 533 565 if (!(status->flag & RX_FLAG_D 534 if (!(status->flag & RX_FLAG_DECRYPTED)) { 566 u8 aad[2 * AES_BLOCK_S 535 u8 aad[2 * AES_BLOCK_SIZE]; 567 u8 b_0[AES_BLOCK_SIZE] 536 u8 b_0[AES_BLOCK_SIZE]; 568 /* hardware didn't dec 537 /* hardware didn't decrypt/verify MIC */ 569 ccmp_special_blocks(sk !! 538 ccmp_special_blocks(skb, pn, b_0, aad); 570 ke << 571 539 572 if (ieee80211_aes_ccm_ 540 if (ieee80211_aes_ccm_decrypt( 573 key->u.ccm 541 key->u.ccmp.tfm, b_0, aad, 574 skb->data 542 skb->data + hdrlen + IEEE80211_CCMP_HDR_LEN, 575 data_len, 543 data_len, 576 skb->data !! 544 skb->data + skb->len - mic_len, mic_len)) 577 return RX_DROP !! 545 return RX_DROP_UNUSABLE; 578 } 546 } 579 547 580 memcpy(key->u.ccmp.rx_pn[queue 548 memcpy(key->u.ccmp.rx_pn[queue], pn, IEEE80211_CCMP_PN_LEN); 581 if (unlikely(ieee80211_is_frag << 582 memcpy(rx->ccm_gcm.pn, << 583 } 549 } 584 550 585 /* Remove CCMP header and MIC */ 551 /* Remove CCMP header and MIC */ 586 if (pskb_trim(skb, skb->len - mic_len) 552 if (pskb_trim(skb, skb->len - mic_len)) 587 return RX_DROP_U_SHORT_CCMP_MI !! 553 return RX_DROP_UNUSABLE; 588 memmove(skb->data + IEEE80211_CCMP_HDR 554 memmove(skb->data + IEEE80211_CCMP_HDR_LEN, skb->data, hdrlen); 589 skb_pull(skb, IEEE80211_CCMP_HDR_LEN); 555 skb_pull(skb, IEEE80211_CCMP_HDR_LEN); 590 556 591 return RX_CONTINUE; 557 return RX_CONTINUE; 592 } 558 } 593 559 594 static void gcmp_special_blocks(struct sk_buff !! 560 static void gcmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *j_0, u8 *aad) 595 bool spp_amsdu << 596 { 561 { 597 struct ieee80211_hdr *hdr = (void *)sk !! 562 __le16 mask_fc; >> 563 u8 qos_tid; >> 564 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 598 565 599 memcpy(j_0, hdr->addr2, ETH_ALEN); 566 memcpy(j_0, hdr->addr2, ETH_ALEN); 600 memcpy(&j_0[ETH_ALEN], pn, IEEE80211_G 567 memcpy(&j_0[ETH_ALEN], pn, IEEE80211_GCMP_PN_LEN); 601 j_0[13] = 0; 568 j_0[13] = 0; 602 j_0[14] = 0; 569 j_0[14] = 0; 603 j_0[AES_BLOCK_SIZE - 1] = 0x01; 570 j_0[AES_BLOCK_SIZE - 1] = 0x01; 604 571 605 ccmp_gcmp_aad(skb, aad, spp_amsdu); !! 572 /* AAD (extra authenticate-only data) / masked 802.11 header >> 573 * FC | A1 | A2 | A3 | SC | [A4] | [QC] >> 574 */ >> 575 put_unaligned_be16(ieee80211_hdrlen(hdr->frame_control) - 2, &aad[0]); >> 576 /* Mask FC: zero subtype b4 b5 b6 (if not mgmt) >> 577 * Retry, PwrMgt, MoreData; set Protected >> 578 */ >> 579 mask_fc = hdr->frame_control; >> 580 mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY | >> 581 IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA); >> 582 if (!ieee80211_is_mgmt(hdr->frame_control)) >> 583 mask_fc &= ~cpu_to_le16(0x0070); >> 584 mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED); >> 585 >> 586 put_unaligned(mask_fc, (__le16 *)&aad[2]); >> 587 memcpy(&aad[4], &hdr->addr1, 3 * ETH_ALEN); >> 588 >> 589 /* Mask Seq#, leave Frag# */ >> 590 aad[22] = *((u8 *)&hdr->seq_ctrl) & 0x0f; >> 591 aad[23] = 0; >> 592 >> 593 if (ieee80211_is_data_qos(hdr->frame_control)) >> 594 qos_tid = *ieee80211_get_qos_ctl(hdr) & >> 595 IEEE80211_QOS_CTL_TID_MASK; >> 596 else >> 597 qos_tid = 0; >> 598 >> 599 if (ieee80211_has_a4(hdr->frame_control)) { >> 600 memcpy(&aad[24], hdr->addr4, ETH_ALEN); >> 601 aad[30] = qos_tid; >> 602 aad[31] = 0; >> 603 } else { >> 604 memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN); >> 605 aad[24] = qos_tid; >> 606 } 606 } 607 } 607 608 608 static inline void gcmp_pn2hdr(u8 *hdr, const 609 static inline void gcmp_pn2hdr(u8 *hdr, const u8 *pn, int key_id) 609 { 610 { 610 hdr[0] = pn[5]; 611 hdr[0] = pn[5]; 611 hdr[1] = pn[4]; 612 hdr[1] = pn[4]; 612 hdr[2] = 0; 613 hdr[2] = 0; 613 hdr[3] = 0x20 | (key_id << 6); 614 hdr[3] = 0x20 | (key_id << 6); 614 hdr[4] = pn[3]; 615 hdr[4] = pn[3]; 615 hdr[5] = pn[2]; 616 hdr[5] = pn[2]; 616 hdr[6] = pn[1]; 617 hdr[6] = pn[1]; 617 hdr[7] = pn[0]; 618 hdr[7] = pn[0]; 618 } 619 } 619 620 620 static inline void gcmp_hdr2pn(u8 *pn, const u 621 static inline void gcmp_hdr2pn(u8 *pn, const u8 *hdr) 621 { 622 { 622 pn[0] = hdr[7]; 623 pn[0] = hdr[7]; 623 pn[1] = hdr[6]; 624 pn[1] = hdr[6]; 624 pn[2] = hdr[5]; 625 pn[2] = hdr[5]; 625 pn[3] = hdr[4]; 626 pn[3] = hdr[4]; 626 pn[4] = hdr[1]; 627 pn[4] = hdr[1]; 627 pn[5] = hdr[0]; 628 pn[5] = hdr[0]; 628 } 629 } 629 630 630 static int gcmp_encrypt_skb(struct ieee80211_t 631 static int gcmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb) 631 { 632 { 632 struct ieee80211_hdr *hdr = (struct ie 633 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 633 struct ieee80211_key *key = tx->key; 634 struct ieee80211_key *key = tx->key; 634 struct ieee80211_tx_info *info = IEEE8 635 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 635 int hdrlen, len, tail; 636 int hdrlen, len, tail; 636 u8 *pos; 637 u8 *pos; 637 u8 pn[6]; 638 u8 pn[6]; 638 u64 pn64; 639 u64 pn64; 639 u8 aad[GCM_AAD_LEN]; 640 u8 aad[GCM_AAD_LEN]; 640 u8 j_0[AES_BLOCK_SIZE]; 641 u8 j_0[AES_BLOCK_SIZE]; 641 642 642 if (info->control.hw_key && 643 if (info->control.hw_key && 643 !(info->control.hw_key->flags & IE 644 !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) && 644 !(info->control.hw_key->flags & IE 645 !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) && 645 !((info->control.hw_key->flags & 646 !((info->control.hw_key->flags & 646 IEEE80211_KEY_FLAG_GENERATE_IV_ 647 IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) && 647 ieee80211_is_mgmt(hdr->frame_con 648 ieee80211_is_mgmt(hdr->frame_control))) { 648 /* hwaccel has no need for pre 649 /* hwaccel has no need for preallocated room for GCMP 649 * header or MIC fields 650 * header or MIC fields 650 */ 651 */ 651 return 0; 652 return 0; 652 } 653 } 653 654 654 hdrlen = ieee80211_hdrlen(hdr->frame_c 655 hdrlen = ieee80211_hdrlen(hdr->frame_control); 655 len = skb->len - hdrlen; 656 len = skb->len - hdrlen; 656 657 657 if (info->control.hw_key) 658 if (info->control.hw_key) 658 tail = 0; 659 tail = 0; 659 else 660 else 660 tail = IEEE80211_GCMP_MIC_LEN; 661 tail = IEEE80211_GCMP_MIC_LEN; 661 662 662 if (WARN_ON(skb_tailroom(skb) < tail | 663 if (WARN_ON(skb_tailroom(skb) < tail || 663 skb_headroom(skb) < IEEE80 664 skb_headroom(skb) < IEEE80211_GCMP_HDR_LEN)) 664 return -1; 665 return -1; 665 666 666 pos = skb_push(skb, IEEE80211_GCMP_HDR 667 pos = skb_push(skb, IEEE80211_GCMP_HDR_LEN); 667 memmove(pos, pos + IEEE80211_GCMP_HDR_ 668 memmove(pos, pos + IEEE80211_GCMP_HDR_LEN, hdrlen); 668 skb_set_network_header(skb, skb_networ 669 skb_set_network_header(skb, skb_network_offset(skb) + 669 IEEE80211_ 670 IEEE80211_GCMP_HDR_LEN); 670 671 671 /* the HW only needs room for the IV, 672 /* the HW only needs room for the IV, but not the actual IV */ 672 if (info->control.hw_key && 673 if (info->control.hw_key && 673 (info->control.hw_key->flags & IEE 674 (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) 674 return 0; 675 return 0; 675 676 >> 677 hdr = (struct ieee80211_hdr *)pos; 676 pos += hdrlen; 678 pos += hdrlen; 677 679 678 pn64 = atomic64_inc_return(&key->conf. 680 pn64 = atomic64_inc_return(&key->conf.tx_pn); 679 681 680 pn[5] = pn64; 682 pn[5] = pn64; 681 pn[4] = pn64 >> 8; 683 pn[4] = pn64 >> 8; 682 pn[3] = pn64 >> 16; 684 pn[3] = pn64 >> 16; 683 pn[2] = pn64 >> 24; 685 pn[2] = pn64 >> 24; 684 pn[1] = pn64 >> 32; 686 pn[1] = pn64 >> 32; 685 pn[0] = pn64 >> 40; 687 pn[0] = pn64 >> 40; 686 688 687 gcmp_pn2hdr(pos, pn, key->conf.keyidx) 689 gcmp_pn2hdr(pos, pn, key->conf.keyidx); 688 690 689 /* hwaccel - with software GCMP header 691 /* hwaccel - with software GCMP header */ 690 if (info->control.hw_key) 692 if (info->control.hw_key) 691 return 0; 693 return 0; 692 694 693 pos += IEEE80211_GCMP_HDR_LEN; 695 pos += IEEE80211_GCMP_HDR_LEN; 694 gcmp_special_blocks(skb, pn, j_0, aad, !! 696 gcmp_special_blocks(skb, pn, j_0, aad); 695 key->conf.flags & << 696 return ieee80211_aes_gcm_encrypt(key-> 697 return ieee80211_aes_gcm_encrypt(key->u.gcmp.tfm, j_0, aad, pos, len, 697 skb_p 698 skb_put(skb, IEEE80211_GCMP_MIC_LEN)); 698 } 699 } 699 700 700 ieee80211_tx_result 701 ieee80211_tx_result 701 ieee80211_crypto_gcmp_encrypt(struct ieee80211 702 ieee80211_crypto_gcmp_encrypt(struct ieee80211_tx_data *tx) 702 { 703 { 703 struct sk_buff *skb; 704 struct sk_buff *skb; 704 705 705 ieee80211_tx_set_protected(tx); 706 ieee80211_tx_set_protected(tx); 706 707 707 skb_queue_walk(&tx->skbs, skb) { 708 skb_queue_walk(&tx->skbs, skb) { 708 if (gcmp_encrypt_skb(tx, skb) 709 if (gcmp_encrypt_skb(tx, skb) < 0) 709 return TX_DROP; 710 return TX_DROP; 710 } 711 } 711 712 712 return TX_CONTINUE; 713 return TX_CONTINUE; 713 } 714 } 714 715 715 ieee80211_rx_result 716 ieee80211_rx_result 716 ieee80211_crypto_gcmp_decrypt(struct ieee80211 717 ieee80211_crypto_gcmp_decrypt(struct ieee80211_rx_data *rx) 717 { 718 { 718 struct ieee80211_hdr *hdr = (struct ie 719 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data; 719 int hdrlen; 720 int hdrlen; 720 struct ieee80211_key *key = rx->key; 721 struct ieee80211_key *key = rx->key; 721 struct sk_buff *skb = rx->skb; 722 struct sk_buff *skb = rx->skb; 722 struct ieee80211_rx_status *status = I 723 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 723 u8 pn[IEEE80211_GCMP_PN_LEN]; 724 u8 pn[IEEE80211_GCMP_PN_LEN]; 724 int data_len, queue, mic_len = IEEE802 725 int data_len, queue, mic_len = IEEE80211_GCMP_MIC_LEN; 725 726 726 hdrlen = ieee80211_hdrlen(hdr->frame_c 727 hdrlen = ieee80211_hdrlen(hdr->frame_control); 727 728 728 if (!ieee80211_is_data(hdr->frame_cont 729 if (!ieee80211_is_data(hdr->frame_control) && 729 !ieee80211_is_robust_mgmt_frame(sk 730 !ieee80211_is_robust_mgmt_frame(skb)) 730 return RX_CONTINUE; 731 return RX_CONTINUE; 731 732 732 if (status->flag & RX_FLAG_DECRYPTED) 733 if (status->flag & RX_FLAG_DECRYPTED) { 733 if (!pskb_may_pull(rx->skb, hd 734 if (!pskb_may_pull(rx->skb, hdrlen + IEEE80211_GCMP_HDR_LEN)) 734 return RX_DROP_U_SHORT !! 735 return RX_DROP_UNUSABLE; 735 if (status->flag & RX_FLAG_MIC 736 if (status->flag & RX_FLAG_MIC_STRIPPED) 736 mic_len = 0; 737 mic_len = 0; 737 } else { 738 } else { 738 if (skb_linearize(rx->skb)) 739 if (skb_linearize(rx->skb)) 739 return RX_DROP_U_OOM; !! 740 return RX_DROP_UNUSABLE; 740 } 741 } 741 742 742 /* reload hdr - skb might have been re << 743 hdr = (void *)rx->skb->data; << 744 << 745 data_len = skb->len - hdrlen - IEEE802 743 data_len = skb->len - hdrlen - IEEE80211_GCMP_HDR_LEN - mic_len; 746 if (!rx->sta || data_len < 0) 744 if (!rx->sta || data_len < 0) 747 return RX_DROP_U_SHORT_GCMP; !! 745 return RX_DROP_UNUSABLE; 748 746 749 if (!(status->flag & RX_FLAG_PN_VALIDA 747 if (!(status->flag & RX_FLAG_PN_VALIDATED)) { 750 int res; 748 int res; 751 749 752 gcmp_hdr2pn(pn, skb->data + hd 750 gcmp_hdr2pn(pn, skb->data + hdrlen); 753 751 754 queue = rx->security_idx; 752 queue = rx->security_idx; 755 753 756 res = memcmp(pn, key->u.gcmp.r 754 res = memcmp(pn, key->u.gcmp.rx_pn[queue], 757 IEEE80211_GCMP_PN 755 IEEE80211_GCMP_PN_LEN); 758 if (res < 0 || 756 if (res < 0 || 759 (!res && !(status->flag & 757 (!res && !(status->flag & RX_FLAG_ALLOW_SAME_PN))) { 760 key->u.gcmp.replays++; 758 key->u.gcmp.replays++; 761 return RX_DROP_U_REPLA !! 759 return RX_DROP_UNUSABLE; 762 } 760 } 763 761 764 if (!(status->flag & RX_FLAG_D 762 if (!(status->flag & RX_FLAG_DECRYPTED)) { 765 u8 aad[2 * AES_BLOCK_S 763 u8 aad[2 * AES_BLOCK_SIZE]; 766 u8 j_0[AES_BLOCK_SIZE] 764 u8 j_0[AES_BLOCK_SIZE]; 767 /* hardware didn't dec 765 /* hardware didn't decrypt/verify MIC */ 768 gcmp_special_blocks(sk !! 766 gcmp_special_blocks(skb, pn, j_0, aad); 769 ke << 770 767 771 if (ieee80211_aes_gcm_ 768 if (ieee80211_aes_gcm_decrypt( 772 key->u.gcm 769 key->u.gcmp.tfm, j_0, aad, 773 skb->data 770 skb->data + hdrlen + IEEE80211_GCMP_HDR_LEN, 774 data_len, 771 data_len, 775 skb->data 772 skb->data + skb->len - 776 IEEE80211_ 773 IEEE80211_GCMP_MIC_LEN)) 777 return RX_DROP !! 774 return RX_DROP_UNUSABLE; 778 } 775 } 779 776 780 memcpy(key->u.gcmp.rx_pn[queue 777 memcpy(key->u.gcmp.rx_pn[queue], pn, IEEE80211_GCMP_PN_LEN); 781 if (unlikely(ieee80211_is_frag << 782 memcpy(rx->ccm_gcm.pn, << 783 } 778 } 784 779 785 /* Remove GCMP header and MIC */ 780 /* Remove GCMP header and MIC */ 786 if (pskb_trim(skb, skb->len - mic_len) 781 if (pskb_trim(skb, skb->len - mic_len)) 787 return RX_DROP_U_SHORT_GCMP_MI !! 782 return RX_DROP_UNUSABLE; 788 memmove(skb->data + IEEE80211_GCMP_HDR 783 memmove(skb->data + IEEE80211_GCMP_HDR_LEN, skb->data, hdrlen); 789 skb_pull(skb, IEEE80211_GCMP_HDR_LEN); 784 skb_pull(skb, IEEE80211_GCMP_HDR_LEN); 790 785 791 return RX_CONTINUE; 786 return RX_CONTINUE; 792 } 787 } 793 788 >> 789 static ieee80211_tx_result >> 790 ieee80211_crypto_cs_encrypt(struct ieee80211_tx_data *tx, >> 791 struct sk_buff *skb) >> 792 { >> 793 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; >> 794 struct ieee80211_key *key = tx->key; >> 795 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); >> 796 int hdrlen; >> 797 u8 *pos, iv_len = key->conf.iv_len; >> 798 >> 799 if (info->control.hw_key && >> 800 !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) { >> 801 /* hwaccel has no need for preallocated head room */ >> 802 return TX_CONTINUE; >> 803 } >> 804 >> 805 if (unlikely(skb_headroom(skb) < iv_len && >> 806 pskb_expand_head(skb, iv_len, 0, GFP_ATOMIC))) >> 807 return TX_DROP; >> 808 >> 809 hdrlen = ieee80211_hdrlen(hdr->frame_control); >> 810 >> 811 pos = skb_push(skb, iv_len); >> 812 memmove(pos, pos + iv_len, hdrlen); >> 813 >> 814 return TX_CONTINUE; >> 815 } >> 816 >> 817 static inline int ieee80211_crypto_cs_pn_compare(u8 *pn1, u8 *pn2, int len) >> 818 { >> 819 int i; >> 820 >> 821 /* pn is little endian */ >> 822 for (i = len - 1; i >= 0; i--) { >> 823 if (pn1[i] < pn2[i]) >> 824 return -1; >> 825 else if (pn1[i] > pn2[i]) >> 826 return 1; >> 827 } >> 828 >> 829 return 0; >> 830 } >> 831 >> 832 static ieee80211_rx_result >> 833 ieee80211_crypto_cs_decrypt(struct ieee80211_rx_data *rx) >> 834 { >> 835 struct ieee80211_key *key = rx->key; >> 836 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data; >> 837 const struct ieee80211_cipher_scheme *cs = NULL; >> 838 int hdrlen = ieee80211_hdrlen(hdr->frame_control); >> 839 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb); >> 840 int data_len; >> 841 u8 *rx_pn; >> 842 u8 *skb_pn; >> 843 u8 qos_tid; >> 844 >> 845 if (!rx->sta || !rx->sta->cipher_scheme || >> 846 !(status->flag & RX_FLAG_DECRYPTED)) >> 847 return RX_DROP_UNUSABLE; >> 848 >> 849 if (!ieee80211_is_data(hdr->frame_control)) >> 850 return RX_CONTINUE; >> 851 >> 852 cs = rx->sta->cipher_scheme; >> 853 >> 854 data_len = rx->skb->len - hdrlen - cs->hdr_len; >> 855 >> 856 if (data_len < 0) >> 857 return RX_DROP_UNUSABLE; >> 858 >> 859 if (ieee80211_is_data_qos(hdr->frame_control)) >> 860 qos_tid = *ieee80211_get_qos_ctl(hdr) & >> 861 IEEE80211_QOS_CTL_TID_MASK; >> 862 else >> 863 qos_tid = 0; >> 864 >> 865 if (skb_linearize(rx->skb)) >> 866 return RX_DROP_UNUSABLE; >> 867 >> 868 hdr = (struct ieee80211_hdr *)rx->skb->data; >> 869 >> 870 rx_pn = key->u.gen.rx_pn[qos_tid]; >> 871 skb_pn = rx->skb->data + hdrlen + cs->pn_off; >> 872 >> 873 if (ieee80211_crypto_cs_pn_compare(skb_pn, rx_pn, cs->pn_len) <= 0) >> 874 return RX_DROP_UNUSABLE; >> 875 >> 876 memcpy(rx_pn, skb_pn, cs->pn_len); >> 877 >> 878 /* remove security header and MIC */ >> 879 if (pskb_trim(rx->skb, rx->skb->len - cs->mic_len)) >> 880 return RX_DROP_UNUSABLE; >> 881 >> 882 memmove(rx->skb->data + cs->hdr_len, rx->skb->data, hdrlen); >> 883 skb_pull(rx->skb, cs->hdr_len); >> 884 >> 885 return RX_CONTINUE; >> 886 } >> 887 794 static void bip_aad(struct sk_buff *skb, u8 *a 888 static void bip_aad(struct sk_buff *skb, u8 *aad) 795 { 889 { 796 __le16 mask_fc; 890 __le16 mask_fc; 797 struct ieee80211_hdr *hdr = (struct ie 891 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 798 892 799 /* BIP AAD: FC(masked) || A1 || A2 || 893 /* BIP AAD: FC(masked) || A1 || A2 || A3 */ 800 894 801 /* FC type/subtype */ 895 /* FC type/subtype */ 802 /* Mask FC Retry, PwrMgt, MoreData fla 896 /* Mask FC Retry, PwrMgt, MoreData flags to zero */ 803 mask_fc = hdr->frame_control; 897 mask_fc = hdr->frame_control; 804 mask_fc &= ~cpu_to_le16(IEEE80211_FCTL 898 mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY | IEEE80211_FCTL_PM | 805 IEEE80211_FCTL 899 IEEE80211_FCTL_MOREDATA); 806 put_unaligned(mask_fc, (__le16 *) &aad 900 put_unaligned(mask_fc, (__le16 *) &aad[0]); 807 /* A1 || A2 || A3 */ 901 /* A1 || A2 || A3 */ 808 memcpy(aad + 2, &hdr->addrs, 3 * ETH_A !! 902 memcpy(aad + 2, &hdr->addr1, 3 * ETH_ALEN); 809 } 903 } 810 904 811 905 812 static inline void bip_ipn_set64(u8 *d, u64 pn 906 static inline void bip_ipn_set64(u8 *d, u64 pn) 813 { 907 { 814 *d++ = pn; 908 *d++ = pn; 815 *d++ = pn >> 8; 909 *d++ = pn >> 8; 816 *d++ = pn >> 16; 910 *d++ = pn >> 16; 817 *d++ = pn >> 24; 911 *d++ = pn >> 24; 818 *d++ = pn >> 32; 912 *d++ = pn >> 32; 819 *d = pn >> 40; 913 *d = pn >> 40; 820 } 914 } 821 915 822 static inline void bip_ipn_swap(u8 *d, const u 916 static inline void bip_ipn_swap(u8 *d, const u8 *s) 823 { 917 { 824 *d++ = s[5]; 918 *d++ = s[5]; 825 *d++ = s[4]; 919 *d++ = s[4]; 826 *d++ = s[3]; 920 *d++ = s[3]; 827 *d++ = s[2]; 921 *d++ = s[2]; 828 *d++ = s[1]; 922 *d++ = s[1]; 829 *d = s[0]; 923 *d = s[0]; 830 } 924 } 831 925 832 926 833 ieee80211_tx_result 927 ieee80211_tx_result 834 ieee80211_crypto_aes_cmac_encrypt(struct ieee8 928 ieee80211_crypto_aes_cmac_encrypt(struct ieee80211_tx_data *tx) 835 { 929 { 836 struct sk_buff *skb; 930 struct sk_buff *skb; 837 struct ieee80211_tx_info *info; 931 struct ieee80211_tx_info *info; 838 struct ieee80211_key *key = tx->key; 932 struct ieee80211_key *key = tx->key; 839 struct ieee80211_mmie *mmie; 933 struct ieee80211_mmie *mmie; 840 u8 aad[20]; 934 u8 aad[20]; 841 u64 pn64; 935 u64 pn64; 842 936 843 if (WARN_ON(skb_queue_len(&tx->skbs) ! 937 if (WARN_ON(skb_queue_len(&tx->skbs) != 1)) 844 return TX_DROP; 938 return TX_DROP; 845 939 846 skb = skb_peek(&tx->skbs); 940 skb = skb_peek(&tx->skbs); 847 941 848 info = IEEE80211_SKB_CB(skb); 942 info = IEEE80211_SKB_CB(skb); 849 943 850 if (info->control.hw_key && !! 944 if (info->control.hw_key) 851 !(key->conf.flags & IEEE80211_KEY_ << 852 return TX_CONTINUE; 945 return TX_CONTINUE; 853 946 854 if (WARN_ON(skb_tailroom(skb) < sizeof 947 if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie))) 855 return TX_DROP; 948 return TX_DROP; 856 949 857 mmie = skb_put(skb, sizeof(*mmie)); !! 950 mmie = (struct ieee80211_mmie *) skb_put(skb, sizeof(*mmie)); 858 mmie->element_id = WLAN_EID_MMIE; 951 mmie->element_id = WLAN_EID_MMIE; 859 mmie->length = sizeof(*mmie) - 2; 952 mmie->length = sizeof(*mmie) - 2; 860 mmie->key_id = cpu_to_le16(key->conf.k 953 mmie->key_id = cpu_to_le16(key->conf.keyidx); 861 954 862 /* PN = PN + 1 */ 955 /* PN = PN + 1 */ 863 pn64 = atomic64_inc_return(&key->conf. 956 pn64 = atomic64_inc_return(&key->conf.tx_pn); 864 957 865 bip_ipn_set64(mmie->sequence_number, p 958 bip_ipn_set64(mmie->sequence_number, pn64); 866 959 867 if (info->control.hw_key) << 868 return TX_CONTINUE; << 869 << 870 bip_aad(skb, aad); 960 bip_aad(skb, aad); 871 961 872 /* 962 /* 873 * MIC = AES-128-CMAC(IGTK, AAD || Man 963 * MIC = AES-128-CMAC(IGTK, AAD || Management Frame Body || MMIE, 64) 874 */ 964 */ 875 ieee80211_aes_cmac(key->u.aes_cmac.tfm 965 ieee80211_aes_cmac(key->u.aes_cmac.tfm, aad, 876 skb->data + 24, skb 966 skb->data + 24, skb->len - 24, mmie->mic); 877 967 878 return TX_CONTINUE; 968 return TX_CONTINUE; 879 } 969 } 880 970 881 ieee80211_tx_result 971 ieee80211_tx_result 882 ieee80211_crypto_aes_cmac_256_encrypt(struct i 972 ieee80211_crypto_aes_cmac_256_encrypt(struct ieee80211_tx_data *tx) 883 { 973 { 884 struct sk_buff *skb; 974 struct sk_buff *skb; 885 struct ieee80211_tx_info *info; 975 struct ieee80211_tx_info *info; 886 struct ieee80211_key *key = tx->key; 976 struct ieee80211_key *key = tx->key; 887 struct ieee80211_mmie_16 *mmie; 977 struct ieee80211_mmie_16 *mmie; 888 u8 aad[20]; 978 u8 aad[20]; 889 u64 pn64; 979 u64 pn64; 890 980 891 if (WARN_ON(skb_queue_len(&tx->skbs) ! 981 if (WARN_ON(skb_queue_len(&tx->skbs) != 1)) 892 return TX_DROP; 982 return TX_DROP; 893 983 894 skb = skb_peek(&tx->skbs); 984 skb = skb_peek(&tx->skbs); 895 985 896 info = IEEE80211_SKB_CB(skb); 986 info = IEEE80211_SKB_CB(skb); 897 987 898 if (info->control.hw_key && !! 988 if (info->control.hw_key) 899 !(key->conf.flags & IEEE80211_KEY_ << 900 return TX_CONTINUE; 989 return TX_CONTINUE; 901 990 902 if (WARN_ON(skb_tailroom(skb) < sizeof 991 if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie))) 903 return TX_DROP; 992 return TX_DROP; 904 993 905 mmie = skb_put(skb, sizeof(*mmie)); !! 994 mmie = (struct ieee80211_mmie_16 *)skb_put(skb, sizeof(*mmie)); 906 mmie->element_id = WLAN_EID_MMIE; 995 mmie->element_id = WLAN_EID_MMIE; 907 mmie->length = sizeof(*mmie) - 2; 996 mmie->length = sizeof(*mmie) - 2; 908 mmie->key_id = cpu_to_le16(key->conf.k 997 mmie->key_id = cpu_to_le16(key->conf.keyidx); 909 998 910 /* PN = PN + 1 */ 999 /* PN = PN + 1 */ 911 pn64 = atomic64_inc_return(&key->conf. 1000 pn64 = atomic64_inc_return(&key->conf.tx_pn); 912 1001 913 bip_ipn_set64(mmie->sequence_number, p 1002 bip_ipn_set64(mmie->sequence_number, pn64); 914 1003 915 if (info->control.hw_key) << 916 return TX_CONTINUE; << 917 << 918 bip_aad(skb, aad); 1004 bip_aad(skb, aad); 919 1005 920 /* MIC = AES-256-CMAC(IGTK, AAD || Man 1006 /* MIC = AES-256-CMAC(IGTK, AAD || Management Frame Body || MMIE, 128) 921 */ 1007 */ 922 ieee80211_aes_cmac_256(key->u.aes_cmac 1008 ieee80211_aes_cmac_256(key->u.aes_cmac.tfm, aad, 923 skb->data + 24, 1009 skb->data + 24, skb->len - 24, mmie->mic); 924 1010 925 return TX_CONTINUE; 1011 return TX_CONTINUE; 926 } 1012 } 927 1013 928 ieee80211_rx_result 1014 ieee80211_rx_result 929 ieee80211_crypto_aes_cmac_decrypt(struct ieee8 1015 ieee80211_crypto_aes_cmac_decrypt(struct ieee80211_rx_data *rx) 930 { 1016 { 931 struct sk_buff *skb = rx->skb; 1017 struct sk_buff *skb = rx->skb; 932 struct ieee80211_rx_status *status = I 1018 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 933 struct ieee80211_key *key = rx->key; 1019 struct ieee80211_key *key = rx->key; 934 struct ieee80211_mmie *mmie; 1020 struct ieee80211_mmie *mmie; 935 u8 aad[20], mic[8], ipn[6]; 1021 u8 aad[20], mic[8], ipn[6]; 936 struct ieee80211_hdr *hdr = (struct ie 1022 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 937 1023 938 if (!ieee80211_is_mgmt(hdr->frame_cont 1024 if (!ieee80211_is_mgmt(hdr->frame_control)) 939 return RX_CONTINUE; 1025 return RX_CONTINUE; 940 1026 941 /* management frames are already linea 1027 /* management frames are already linear */ 942 1028 943 if (skb->len < 24 + sizeof(*mmie)) 1029 if (skb->len < 24 + sizeof(*mmie)) 944 return RX_DROP_U_SHORT_CMAC; !! 1030 return RX_DROP_UNUSABLE; 945 1031 946 mmie = (struct ieee80211_mmie *) 1032 mmie = (struct ieee80211_mmie *) 947 (skb->data + skb->len - sizeof 1033 (skb->data + skb->len - sizeof(*mmie)); 948 if (mmie->element_id != WLAN_EID_MMIE 1034 if (mmie->element_id != WLAN_EID_MMIE || 949 mmie->length != sizeof(*mmie) - 2) 1035 mmie->length != sizeof(*mmie) - 2) 950 return RX_DROP_U_BAD_MMIE; /* !! 1036 return RX_DROP_UNUSABLE; /* Invalid MMIE */ 951 1037 952 bip_ipn_swap(ipn, mmie->sequence_numbe 1038 bip_ipn_swap(ipn, mmie->sequence_number); 953 1039 954 if (memcmp(ipn, key->u.aes_cmac.rx_pn, 1040 if (memcmp(ipn, key->u.aes_cmac.rx_pn, 6) <= 0) { 955 key->u.aes_cmac.replays++; 1041 key->u.aes_cmac.replays++; 956 return RX_DROP_U_REPLAY; !! 1042 return RX_DROP_UNUSABLE; 957 } 1043 } 958 1044 959 if (!(status->flag & RX_FLAG_DECRYPTED 1045 if (!(status->flag & RX_FLAG_DECRYPTED)) { 960 /* hardware didn't decrypt/ver 1046 /* hardware didn't decrypt/verify MIC */ 961 bip_aad(skb, aad); 1047 bip_aad(skb, aad); 962 ieee80211_aes_cmac(key->u.aes_ 1048 ieee80211_aes_cmac(key->u.aes_cmac.tfm, aad, 963 skb->data + 1049 skb->data + 24, skb->len - 24, mic); 964 if (crypto_memneq(mic, mmie->m !! 1050 if (memcmp(mic, mmie->mic, sizeof(mmie->mic)) != 0) { 965 key->u.aes_cmac.icverr 1051 key->u.aes_cmac.icverrors++; 966 return RX_DROP_U_MIC_F !! 1052 return RX_DROP_UNUSABLE; 967 } 1053 } 968 } 1054 } 969 1055 970 memcpy(key->u.aes_cmac.rx_pn, ipn, 6); 1056 memcpy(key->u.aes_cmac.rx_pn, ipn, 6); 971 1057 972 /* Remove MMIE */ 1058 /* Remove MMIE */ 973 skb_trim(skb, skb->len - sizeof(*mmie) 1059 skb_trim(skb, skb->len - sizeof(*mmie)); 974 1060 975 return RX_CONTINUE; 1061 return RX_CONTINUE; 976 } 1062 } 977 1063 978 ieee80211_rx_result 1064 ieee80211_rx_result 979 ieee80211_crypto_aes_cmac_256_decrypt(struct i 1065 ieee80211_crypto_aes_cmac_256_decrypt(struct ieee80211_rx_data *rx) 980 { 1066 { 981 struct sk_buff *skb = rx->skb; 1067 struct sk_buff *skb = rx->skb; 982 struct ieee80211_rx_status *status = I 1068 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 983 struct ieee80211_key *key = rx->key; 1069 struct ieee80211_key *key = rx->key; 984 struct ieee80211_mmie_16 *mmie; 1070 struct ieee80211_mmie_16 *mmie; 985 u8 aad[20], mic[16], ipn[6]; 1071 u8 aad[20], mic[16], ipn[6]; 986 struct ieee80211_hdr *hdr = (struct ie 1072 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 987 1073 988 if (!ieee80211_is_mgmt(hdr->frame_cont 1074 if (!ieee80211_is_mgmt(hdr->frame_control)) 989 return RX_CONTINUE; 1075 return RX_CONTINUE; 990 1076 991 /* management frames are already linea 1077 /* management frames are already linear */ 992 1078 993 if (skb->len < 24 + sizeof(*mmie)) 1079 if (skb->len < 24 + sizeof(*mmie)) 994 return RX_DROP_U_SHORT_CMAC256 !! 1080 return RX_DROP_UNUSABLE; 995 1081 996 mmie = (struct ieee80211_mmie_16 *) 1082 mmie = (struct ieee80211_mmie_16 *) 997 (skb->data + skb->len - sizeof 1083 (skb->data + skb->len - sizeof(*mmie)); 998 if (mmie->element_id != WLAN_EID_MMIE 1084 if (mmie->element_id != WLAN_EID_MMIE || 999 mmie->length != sizeof(*mmie) - 2) 1085 mmie->length != sizeof(*mmie) - 2) 1000 return RX_DROP_U_BAD_MMIE; /* !! 1086 return RX_DROP_UNUSABLE; /* Invalid MMIE */ 1001 1087 1002 bip_ipn_swap(ipn, mmie->sequence_numb 1088 bip_ipn_swap(ipn, mmie->sequence_number); 1003 1089 1004 if (memcmp(ipn, key->u.aes_cmac.rx_pn 1090 if (memcmp(ipn, key->u.aes_cmac.rx_pn, 6) <= 0) { 1005 key->u.aes_cmac.replays++; 1091 key->u.aes_cmac.replays++; 1006 return RX_DROP_U_REPLAY; !! 1092 return RX_DROP_UNUSABLE; 1007 } 1093 } 1008 1094 1009 if (!(status->flag & RX_FLAG_DECRYPTE 1095 if (!(status->flag & RX_FLAG_DECRYPTED)) { 1010 /* hardware didn't decrypt/ve 1096 /* hardware didn't decrypt/verify MIC */ 1011 bip_aad(skb, aad); 1097 bip_aad(skb, aad); 1012 ieee80211_aes_cmac_256(key->u 1098 ieee80211_aes_cmac_256(key->u.aes_cmac.tfm, aad, 1013 skb->d 1099 skb->data + 24, skb->len - 24, mic); 1014 if (crypto_memneq(mic, mmie-> !! 1100 if (memcmp(mic, mmie->mic, sizeof(mmie->mic)) != 0) { 1015 key->u.aes_cmac.icver 1101 key->u.aes_cmac.icverrors++; 1016 return RX_DROP_U_MIC_ !! 1102 return RX_DROP_UNUSABLE; 1017 } 1103 } 1018 } 1104 } 1019 1105 1020 memcpy(key->u.aes_cmac.rx_pn, ipn, 6) 1106 memcpy(key->u.aes_cmac.rx_pn, ipn, 6); 1021 1107 1022 /* Remove MMIE */ 1108 /* Remove MMIE */ 1023 skb_trim(skb, skb->len - sizeof(*mmie 1109 skb_trim(skb, skb->len - sizeof(*mmie)); 1024 1110 1025 return RX_CONTINUE; 1111 return RX_CONTINUE; 1026 } 1112 } 1027 1113 1028 ieee80211_tx_result 1114 ieee80211_tx_result 1029 ieee80211_crypto_aes_gmac_encrypt(struct ieee 1115 ieee80211_crypto_aes_gmac_encrypt(struct ieee80211_tx_data *tx) 1030 { 1116 { 1031 struct sk_buff *skb; 1117 struct sk_buff *skb; 1032 struct ieee80211_tx_info *info; 1118 struct ieee80211_tx_info *info; 1033 struct ieee80211_key *key = tx->key; 1119 struct ieee80211_key *key = tx->key; 1034 struct ieee80211_mmie_16 *mmie; 1120 struct ieee80211_mmie_16 *mmie; 1035 struct ieee80211_hdr *hdr; 1121 struct ieee80211_hdr *hdr; 1036 u8 aad[GMAC_AAD_LEN]; 1122 u8 aad[GMAC_AAD_LEN]; 1037 u64 pn64; 1123 u64 pn64; 1038 u8 nonce[GMAC_NONCE_LEN]; 1124 u8 nonce[GMAC_NONCE_LEN]; 1039 1125 1040 if (WARN_ON(skb_queue_len(&tx->skbs) 1126 if (WARN_ON(skb_queue_len(&tx->skbs) != 1)) 1041 return TX_DROP; 1127 return TX_DROP; 1042 1128 1043 skb = skb_peek(&tx->skbs); 1129 skb = skb_peek(&tx->skbs); 1044 1130 1045 info = IEEE80211_SKB_CB(skb); 1131 info = IEEE80211_SKB_CB(skb); 1046 1132 1047 if (info->control.hw_key && !! 1133 if (info->control.hw_key) 1048 !(key->conf.flags & IEEE80211_KEY << 1049 return TX_CONTINUE; 1134 return TX_CONTINUE; 1050 1135 1051 if (WARN_ON(skb_tailroom(skb) < sizeo 1136 if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie))) 1052 return TX_DROP; 1137 return TX_DROP; 1053 1138 1054 mmie = skb_put(skb, sizeof(*mmie)); !! 1139 mmie = (struct ieee80211_mmie_16 *)skb_put(skb, sizeof(*mmie)); 1055 mmie->element_id = WLAN_EID_MMIE; 1140 mmie->element_id = WLAN_EID_MMIE; 1056 mmie->length = sizeof(*mmie) - 2; 1141 mmie->length = sizeof(*mmie) - 2; 1057 mmie->key_id = cpu_to_le16(key->conf. 1142 mmie->key_id = cpu_to_le16(key->conf.keyidx); 1058 1143 1059 /* PN = PN + 1 */ 1144 /* PN = PN + 1 */ 1060 pn64 = atomic64_inc_return(&key->conf 1145 pn64 = atomic64_inc_return(&key->conf.tx_pn); 1061 1146 1062 bip_ipn_set64(mmie->sequence_number, 1147 bip_ipn_set64(mmie->sequence_number, pn64); 1063 1148 1064 if (info->control.hw_key) << 1065 return TX_CONTINUE; << 1066 << 1067 bip_aad(skb, aad); 1149 bip_aad(skb, aad); 1068 1150 1069 hdr = (struct ieee80211_hdr *)skb->da 1151 hdr = (struct ieee80211_hdr *)skb->data; 1070 memcpy(nonce, hdr->addr2, ETH_ALEN); 1152 memcpy(nonce, hdr->addr2, ETH_ALEN); 1071 bip_ipn_swap(nonce + ETH_ALEN, mmie-> 1153 bip_ipn_swap(nonce + ETH_ALEN, mmie->sequence_number); 1072 1154 1073 /* MIC = AES-GMAC(IGTK, AAD || Manage 1155 /* MIC = AES-GMAC(IGTK, AAD || Management Frame Body || MMIE, 128) */ 1074 if (ieee80211_aes_gmac(key->u.aes_gma 1156 if (ieee80211_aes_gmac(key->u.aes_gmac.tfm, aad, nonce, 1075 skb->data + 24 1157 skb->data + 24, skb->len - 24, mmie->mic) < 0) 1076 return TX_DROP; 1158 return TX_DROP; 1077 1159 1078 return TX_CONTINUE; 1160 return TX_CONTINUE; 1079 } 1161 } 1080 1162 1081 ieee80211_rx_result 1163 ieee80211_rx_result 1082 ieee80211_crypto_aes_gmac_decrypt(struct ieee 1164 ieee80211_crypto_aes_gmac_decrypt(struct ieee80211_rx_data *rx) 1083 { 1165 { 1084 struct sk_buff *skb = rx->skb; 1166 struct sk_buff *skb = rx->skb; 1085 struct ieee80211_rx_status *status = 1167 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb); 1086 struct ieee80211_key *key = rx->key; 1168 struct ieee80211_key *key = rx->key; 1087 struct ieee80211_mmie_16 *mmie; 1169 struct ieee80211_mmie_16 *mmie; 1088 u8 aad[GMAC_AAD_LEN], *mic, ipn[6], n !! 1170 u8 aad[GMAC_AAD_LEN], mic[GMAC_MIC_LEN], ipn[6], nonce[GMAC_NONCE_LEN]; 1089 struct ieee80211_hdr *hdr = (struct i 1171 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 1090 1172 1091 if (!ieee80211_is_mgmt(hdr->frame_con 1173 if (!ieee80211_is_mgmt(hdr->frame_control)) 1092 return RX_CONTINUE; 1174 return RX_CONTINUE; 1093 1175 1094 /* management frames are already line 1176 /* management frames are already linear */ 1095 1177 1096 if (skb->len < 24 + sizeof(*mmie)) 1178 if (skb->len < 24 + sizeof(*mmie)) 1097 return RX_DROP_U_SHORT_GMAC; !! 1179 return RX_DROP_UNUSABLE; 1098 1180 1099 mmie = (struct ieee80211_mmie_16 *) 1181 mmie = (struct ieee80211_mmie_16 *) 1100 (skb->data + skb->len - sizeo 1182 (skb->data + skb->len - sizeof(*mmie)); 1101 if (mmie->element_id != WLAN_EID_MMIE 1183 if (mmie->element_id != WLAN_EID_MMIE || 1102 mmie->length != sizeof(*mmie) - 2 1184 mmie->length != sizeof(*mmie) - 2) 1103 return RX_DROP_U_BAD_MMIE; /* !! 1185 return RX_DROP_UNUSABLE; /* Invalid MMIE */ 1104 1186 1105 bip_ipn_swap(ipn, mmie->sequence_numb 1187 bip_ipn_swap(ipn, mmie->sequence_number); 1106 1188 1107 if (memcmp(ipn, key->u.aes_gmac.rx_pn 1189 if (memcmp(ipn, key->u.aes_gmac.rx_pn, 6) <= 0) { 1108 key->u.aes_gmac.replays++; 1190 key->u.aes_gmac.replays++; 1109 return RX_DROP_U_REPLAY; !! 1191 return RX_DROP_UNUSABLE; 1110 } 1192 } 1111 1193 1112 if (!(status->flag & RX_FLAG_DECRYPTE 1194 if (!(status->flag & RX_FLAG_DECRYPTED)) { 1113 /* hardware didn't decrypt/ve 1195 /* hardware didn't decrypt/verify MIC */ 1114 bip_aad(skb, aad); 1196 bip_aad(skb, aad); 1115 1197 1116 memcpy(nonce, hdr->addr2, ETH 1198 memcpy(nonce, hdr->addr2, ETH_ALEN); 1117 memcpy(nonce + ETH_ALEN, ipn, 1199 memcpy(nonce + ETH_ALEN, ipn, 6); 1118 1200 1119 mic = kmalloc(GMAC_MIC_LEN, G << 1120 if (!mic) << 1121 return RX_DROP_U_OOM; << 1122 if (ieee80211_aes_gmac(key->u 1201 if (ieee80211_aes_gmac(key->u.aes_gmac.tfm, aad, nonce, 1123 skb->d 1202 skb->data + 24, skb->len - 24, 1124 mic) < 1203 mic) < 0 || 1125 crypto_memneq(mic, mmie-> !! 1204 memcmp(mic, mmie->mic, sizeof(mmie->mic)) != 0) { 1126 key->u.aes_gmac.icver 1205 key->u.aes_gmac.icverrors++; 1127 kfree(mic); !! 1206 return RX_DROP_UNUSABLE; 1128 return RX_DROP_U_MIC_ << 1129 } 1207 } 1130 kfree(mic); << 1131 } 1208 } 1132 1209 1133 memcpy(key->u.aes_gmac.rx_pn, ipn, 6) 1210 memcpy(key->u.aes_gmac.rx_pn, ipn, 6); 1134 1211 1135 /* Remove MMIE */ 1212 /* Remove MMIE */ 1136 skb_trim(skb, skb->len - sizeof(*mmie 1213 skb_trim(skb, skb->len - sizeof(*mmie)); 1137 1214 1138 return RX_CONTINUE; 1215 return RX_CONTINUE; >> 1216 } >> 1217 >> 1218 ieee80211_tx_result >> 1219 ieee80211_crypto_hw_encrypt(struct ieee80211_tx_data *tx) >> 1220 { >> 1221 struct sk_buff *skb; >> 1222 struct ieee80211_tx_info *info = NULL; >> 1223 ieee80211_tx_result res; >> 1224 >> 1225 skb_queue_walk(&tx->skbs, skb) { >> 1226 info = IEEE80211_SKB_CB(skb); >> 1227 >> 1228 /* handle hw-only algorithm */ >> 1229 if (!info->control.hw_key) >> 1230 return TX_DROP; >> 1231 >> 1232 if (tx->key->flags & KEY_FLAG_CIPHER_SCHEME) { >> 1233 res = ieee80211_crypto_cs_encrypt(tx, skb); >> 1234 if (res != TX_CONTINUE) >> 1235 return res; >> 1236 } >> 1237 } >> 1238 >> 1239 ieee80211_tx_set_protected(tx); >> 1240 >> 1241 return TX_CONTINUE; >> 1242 } >> 1243 >> 1244 ieee80211_rx_result >> 1245 ieee80211_crypto_hw_decrypt(struct ieee80211_rx_data *rx) >> 1246 { >> 1247 if (rx->sta && rx->sta->cipher_scheme) >> 1248 return ieee80211_crypto_cs_decrypt(rx); >> 1249 >> 1250 return RX_DROP_UNUSABLE; 1139 } 1251 } 1140 1252
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.