1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2002-2005, Instant802 Networks, Inc. 4 * Copyright 2005-2006, Devicescape Software, Inc. 5 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> 6 * Copyright 2007-2008 Johannes Berg <johannes@sipsolutions.net> 7 * Copyright 2013-2014 Intel Mobile Communications GmbH 8 * Copyright 2015-2017 Intel Deutschland GmbH 9 * Copyright 2018-2019 Intel Corporation 10 */ 11 12 #include <linux/if_ether.h> 13 #include <linux/etherdevice.h> 14 #include <linux/list.h> 15 #include <linux/rcupdate.h> 16 #include <linux/rtnetlink.h> 17 #include <linux/slab.h> 18 #include <linux/export.h> 19 #include <net/mac80211.h> 20 #include <crypto/algapi.h> 21 #include <asm/unaligned.h> 22 #include "ieee80211_i.h" 23 #include "driver-ops.h" 24 #include "debugfs_key.h" 25 #include "aes_ccm.h" 26 #include "aes_cmac.h" 27 #include "aes_gmac.h" 28 #include "aes_gcm.h" 29 30 31 /** 32 * DOC: Key handling basics 33 * 34 * Key handling in mac80211 is done based on per-interface (sub_if_data) 35 * keys and per-station keys. Since each station belongs to an interface, 36 * each station key also belongs to that interface. 37 * 38 * Hardware acceleration is done on a best-effort basis for algorithms 39 * that are implemented in software, for each key the hardware is asked 40 * to enable that key for offloading but if it cannot do that the key is 41 * simply kept for software encryption (unless it is for an algorithm 42 * that isn't implemented in software). 43 * There is currently no way of knowing whether a key is handled in SW 44 * or HW except by looking into debugfs. 45 * 46 * All key management is internally protected by a mutex. Within all 47 * other parts of mac80211, key references are, just as STA structure 48 * references, protected by RCU. Note, however, that some things are 49 * unprotected, namely the key->sta dereferences within the hardware 50 * acceleration functions. This means that sta_info_destroy() must 51 * remove the key which waits for an RCU grace period. 52 */ 53 54 static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 55 56 static void assert_key_lock(struct ieee80211_local *local) 57 { 58 lockdep_assert_held(&local->key_mtx); 59 } 60 61 static void 62 update_vlan_tailroom_need_count(struct ieee80211_sub_if_data *sdata, int delta) 63 { 64 struct ieee80211_sub_if_data *vlan; 65 66 if (sdata->vif.type != NL80211_IFTYPE_AP) 67 return; 68 69 /* crypto_tx_tailroom_needed_cnt is protected by this */ 70 assert_key_lock(sdata->local); 71 72 rcu_read_lock(); 73 74 list_for_each_entry_rcu(vlan, &sdata->u.ap.vlans, u.vlan.list) 75 vlan->crypto_tx_tailroom_needed_cnt += delta; 76 77 rcu_read_unlock(); 78 } 79 80 static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata) 81 { 82 /* 83 * When this count is zero, SKB resizing for allocating tailroom 84 * for IV or MMIC is skipped. But, this check has created two race 85 * cases in xmit path while transiting from zero count to one: 86 * 87 * 1. SKB resize was skipped because no key was added but just before 88 * the xmit key is added and SW encryption kicks off. 89 * 90 * 2. SKB resize was skipped because all the keys were hw planted but 91 * just before xmit one of the key is deleted and SW encryption kicks 92 * off. 93 * 94 * In both the above case SW encryption will find not enough space for 95 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c) 96 * 97 * Solution has been explained at 98 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net 99 */ 100 101 assert_key_lock(sdata->local); 102 103 update_vlan_tailroom_need_count(sdata, 1); 104 105 if (!sdata->crypto_tx_tailroom_needed_cnt++) { 106 /* 107 * Flush all XMIT packets currently using HW encryption or no 108 * encryption at all if the count transition is from 0 -> 1. 109 */ 110 synchronize_net(); 111 } 112 } 113 114 static void decrease_tailroom_need_count(struct ieee80211_sub_if_data *sdata, 115 int delta) 116 { 117 assert_key_lock(sdata->local); 118 119 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt < delta); 120 121 update_vlan_tailroom_need_count(sdata, -delta); 122 sdata->crypto_tx_tailroom_needed_cnt -= delta; 123 } 124 125 static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key) 126 { 127 struct ieee80211_sub_if_data *sdata = key->sdata; 128 struct sta_info *sta; 129 int ret = -EOPNOTSUPP; 130 131 might_sleep(); 132 133 if (key->flags & KEY_FLAG_TAINTED) { 134 /* If we get here, it's during resume and the key is 135 * tainted so shouldn't be used/programmed any more. 136 * However, its flags may still indicate that it was 137 * programmed into the device (since we're in resume) 138 * so clear that flag now to avoid trying to remove 139 * it again later. 140 */ 141 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE && 142 !(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC | 143 IEEE80211_KEY_FLAG_PUT_MIC_SPACE | 144 IEEE80211_KEY_FLAG_RESERVE_TAILROOM))) 145 increment_tailroom_need_count(sdata); 146 147 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE; 148 return -EINVAL; 149 } 150 151 if (!key->local->ops->set_key) 152 goto out_unsupported; 153 154 assert_key_lock(key->local); 155 156 sta = key->sta; 157 158 /* 159 * If this is a per-STA GTK, check if it 160 * is supported; if not, return. 161 */ 162 if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) && 163 !ieee80211_hw_check(&key->local->hw, SUPPORTS_PER_STA_GTK)) 164 goto out_unsupported; 165 166 if (sta && !sta->uploaded) 167 goto out_unsupported; 168 169 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 170 /* 171 * The driver doesn't know anything about VLAN interfaces. 172 * Hence, don't send GTKs for VLAN interfaces to the driver. 173 */ 174 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) { 175 ret = 1; 176 goto out_unsupported; 177 } 178 } 179 180 /* TKIP countermeasures don't work in encap offload mode */ 181 if (key->conf.cipher == WLAN_CIPHER_SUITE_TKIP && 182 sdata->hw_80211_encap) { 183 sdata_dbg(sdata, "TKIP is not allowed in hw 80211 encap mode\n"); 184 return -EINVAL; 185 } 186 187 ret = drv_set_key(key->local, SET_KEY, sdata, 188 sta ? &sta->sta : NULL, &key->conf); 189 190 if (!ret) { 191 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE; 192 193 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC | 194 IEEE80211_KEY_FLAG_PUT_MIC_SPACE | 195 IEEE80211_KEY_FLAG_RESERVE_TAILROOM))) 196 decrease_tailroom_need_count(sdata, 1); 197 198 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) && 199 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)); 200 201 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) && 202 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)); 203 204 return 0; 205 } 206 207 if (ret != -ENOSPC && ret != -EOPNOTSUPP && ret != 1) 208 sdata_err(sdata, 209 "failed to set key (%d, %pM) to hardware (%d)\n", 210 key->conf.keyidx, 211 sta ? sta->sta.addr : bcast_addr, ret); 212 213 out_unsupported: 214 switch (key->conf.cipher) { 215 case WLAN_CIPHER_SUITE_WEP40: 216 case WLAN_CIPHER_SUITE_WEP104: 217 case WLAN_CIPHER_SUITE_TKIP: 218 case WLAN_CIPHER_SUITE_CCMP: 219 case WLAN_CIPHER_SUITE_CCMP_256: 220 case WLAN_CIPHER_SUITE_GCMP: 221 case WLAN_CIPHER_SUITE_GCMP_256: 222 /* We cannot do software crypto of data frames with 223 * encapsulation offload enabled. However for 802.11w to 224 * function properly we need cmac/gmac keys. 225 */ 226 if (sdata->hw_80211_encap) 227 return -EINVAL; 228 /* Fall through */ 229 230 case WLAN_CIPHER_SUITE_AES_CMAC: 231 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 232 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 233 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 234 /* all of these we can do in software - if driver can */ 235 if (ret == 1) 236 return 0; 237 if (ieee80211_hw_check(&key->local->hw, SW_CRYPTO_CONTROL)) 238 return -EINVAL; 239 return 0; 240 default: 241 return -EINVAL; 242 } 243 } 244 245 static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key) 246 { 247 struct ieee80211_sub_if_data *sdata; 248 struct sta_info *sta; 249 int ret; 250 251 might_sleep(); 252 253 if (!key || !key->local->ops->set_key) 254 return; 255 256 assert_key_lock(key->local); 257 258 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) 259 return; 260 261 sta = key->sta; 262 sdata = key->sdata; 263 264 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC | 265 IEEE80211_KEY_FLAG_PUT_MIC_SPACE | 266 IEEE80211_KEY_FLAG_RESERVE_TAILROOM))) 267 increment_tailroom_need_count(sdata); 268 269 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE; 270 ret = drv_set_key(key->local, DISABLE_KEY, sdata, 271 sta ? &sta->sta : NULL, &key->conf); 272 273 if (ret) 274 sdata_err(sdata, 275 "failed to remove key (%d, %pM) from hardware (%d)\n", 276 key->conf.keyidx, 277 sta ? sta->sta.addr : bcast_addr, ret); 278 } 279 280 int ieee80211_set_tx_key(struct ieee80211_key *key) 281 { 282 struct sta_info *sta = key->sta; 283 struct ieee80211_local *local = key->local; 284 285 assert_key_lock(local); 286 287 sta->ptk_idx = key->conf.keyidx; 288 289 if (!ieee80211_hw_check(&local->hw, AMPDU_KEYBORDER_SUPPORT)) 290 clear_sta_flag(sta, WLAN_STA_BLOCK_BA); 291 ieee80211_check_fast_xmit(sta); 292 293 return 0; 294 } 295 296 static void ieee80211_pairwise_rekey(struct ieee80211_key *old, 297 struct ieee80211_key *new) 298 { 299 struct ieee80211_local *local = new->local; 300 struct sta_info *sta = new->sta; 301 int i; 302 303 assert_key_lock(local); 304 305 if (new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX) { 306 /* Extended Key ID key install, initial one or rekey */ 307 308 if (sta->ptk_idx != INVALID_PTK_KEYIDX && 309 !ieee80211_hw_check(&local->hw, AMPDU_KEYBORDER_SUPPORT)) { 310 /* Aggregation Sessions with Extended Key ID must not 311 * mix MPDUs with different keyIDs within one A-MPDU. 312 * Tear down running Tx aggregation sessions and block 313 * new Rx/Tx aggregation requests during rekey to 314 * ensure there are no A-MPDUs when the driver is not 315 * supporting A-MPDU key borders. (Blocking Tx only 316 * would be sufficient but WLAN_STA_BLOCK_BA gets the 317 * job done for the few ms we need it.) 318 */ 319 set_sta_flag(sta, WLAN_STA_BLOCK_BA); 320 mutex_lock(&sta->ampdu_mlme.mtx); 321 for (i = 0; i < IEEE80211_NUM_TIDS; i++) 322 ___ieee80211_stop_tx_ba_session(sta, i, 323 AGG_STOP_LOCAL_REQUEST); 324 mutex_unlock(&sta->ampdu_mlme.mtx); 325 } 326 } else if (old) { 327 /* Rekey without Extended Key ID. 328 * Aggregation sessions are OK when running on SW crypto. 329 * A broken remote STA may cause issues not observed with HW 330 * crypto, though. 331 */ 332 if (!(old->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) 333 return; 334 335 /* Stop Tx till we are on the new key */ 336 old->flags |= KEY_FLAG_TAINTED; 337 ieee80211_clear_fast_xmit(sta); 338 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) { 339 set_sta_flag(sta, WLAN_STA_BLOCK_BA); 340 ieee80211_sta_tear_down_BA_sessions(sta, 341 AGG_STOP_LOCAL_REQUEST); 342 } 343 if (!wiphy_ext_feature_isset(local->hw.wiphy, 344 NL80211_EXT_FEATURE_CAN_REPLACE_PTK0)) { 345 pr_warn_ratelimited("Rekeying PTK for STA %pM but driver can't safely do that.", 346 sta->sta.addr); 347 /* Flushing the driver queues *may* help prevent 348 * the clear text leaks and freezes. 349 */ 350 ieee80211_flush_queues(local, old->sdata, false); 351 } 352 } 353 } 354 355 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, 356 int idx, bool uni, bool multi) 357 { 358 struct ieee80211_key *key = NULL; 359 360 assert_key_lock(sdata->local); 361 362 if (idx >= 0 && idx < NUM_DEFAULT_KEYS) 363 key = key_mtx_dereference(sdata->local, sdata->keys[idx]); 364 365 if (uni) { 366 rcu_assign_pointer(sdata->default_unicast_key, key); 367 ieee80211_check_fast_xmit_iface(sdata); 368 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN) 369 drv_set_default_unicast_key(sdata->local, sdata, idx); 370 } 371 372 if (multi) 373 rcu_assign_pointer(sdata->default_multicast_key, key); 374 375 ieee80211_debugfs_key_update_default(sdata); 376 } 377 378 void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx, 379 bool uni, bool multi) 380 { 381 mutex_lock(&sdata->local->key_mtx); 382 __ieee80211_set_default_key(sdata, idx, uni, multi); 383 mutex_unlock(&sdata->local->key_mtx); 384 } 385 386 static void 387 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx) 388 { 389 struct ieee80211_key *key = NULL; 390 391 assert_key_lock(sdata->local); 392 393 if (idx >= NUM_DEFAULT_KEYS && 394 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS) 395 key = key_mtx_dereference(sdata->local, sdata->keys[idx]); 396 397 rcu_assign_pointer(sdata->default_mgmt_key, key); 398 399 ieee80211_debugfs_key_update_default(sdata); 400 } 401 402 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, 403 int idx) 404 { 405 mutex_lock(&sdata->local->key_mtx); 406 __ieee80211_set_default_mgmt_key(sdata, idx); 407 mutex_unlock(&sdata->local->key_mtx); 408 } 409 410 static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata, 411 struct sta_info *sta, 412 bool pairwise, 413 struct ieee80211_key *old, 414 struct ieee80211_key *new) 415 { 416 int idx; 417 int ret = 0; 418 bool defunikey, defmultikey, defmgmtkey; 419 420 /* caller must provide at least one old/new */ 421 if (WARN_ON(!new && !old)) 422 return 0; 423 424 if (new) 425 list_add_tail_rcu(&new->list, &sdata->key_list); 426 427 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx); 428 429 if (new && sta && pairwise) { 430 /* Unicast rekey needs special handling. With Extended Key ID 431 * old is still NULL for the first rekey. 432 */ 433 ieee80211_pairwise_rekey(old, new); 434 } 435 436 if (old) { 437 idx = old->conf.keyidx; 438 439 if (old->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) { 440 ieee80211_key_disable_hw_accel(old); 441 442 if (new) 443 ret = ieee80211_key_enable_hw_accel(new); 444 } 445 } else { 446 /* new must be provided in case old is not */ 447 idx = new->conf.keyidx; 448 if (!new->local->wowlan) 449 ret = ieee80211_key_enable_hw_accel(new); 450 } 451 452 if (ret) 453 return ret; 454 455 if (sta) { 456 if (pairwise) { 457 rcu_assign_pointer(sta->ptk[idx], new); 458 if (new && 459 !(new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX)) { 460 sta->ptk_idx = idx; 461 clear_sta_flag(sta, WLAN_STA_BLOCK_BA); 462 ieee80211_check_fast_xmit(sta); 463 } 464 } else { 465 rcu_assign_pointer(sta->gtk[idx], new); 466 } 467 /* Only needed for transition from no key -> key. 468 * Still triggers unnecessary when using Extended Key ID 469 * and installing the second key ID the first time. 470 */ 471 if (new && !old) 472 ieee80211_check_fast_rx(sta); 473 } else { 474 defunikey = old && 475 old == key_mtx_dereference(sdata->local, 476 sdata->default_unicast_key); 477 defmultikey = old && 478 old == key_mtx_dereference(sdata->local, 479 sdata->default_multicast_key); 480 defmgmtkey = old && 481 old == key_mtx_dereference(sdata->local, 482 sdata->default_mgmt_key); 483 484 if (defunikey && !new) 485 __ieee80211_set_default_key(sdata, -1, true, false); 486 if (defmultikey && !new) 487 __ieee80211_set_default_key(sdata, -1, false, true); 488 if (defmgmtkey && !new) 489 __ieee80211_set_default_mgmt_key(sdata, -1); 490 491 rcu_assign_pointer(sdata->keys[idx], new); 492 if (defunikey && new) 493 __ieee80211_set_default_key(sdata, new->conf.keyidx, 494 true, false); 495 if (defmultikey && new) 496 __ieee80211_set_default_key(sdata, new->conf.keyidx, 497 false, true); 498 if (defmgmtkey && new) 499 __ieee80211_set_default_mgmt_key(sdata, 500 new->conf.keyidx); 501 } 502 503 if (old) 504 list_del_rcu(&old->list); 505 506 return 0; 507 } 508 509 struct ieee80211_key * 510 ieee80211_key_alloc(u32 cipher, int idx, size_t key_len, 511 const u8 *key_data, 512 size_t seq_len, const u8 *seq, 513 const struct ieee80211_cipher_scheme *cs) 514 { 515 struct ieee80211_key *key; 516 int i, j, err; 517 518 if (WARN_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)) 519 return ERR_PTR(-EINVAL); 520 521 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL); 522 if (!key) 523 return ERR_PTR(-ENOMEM); 524 525 /* 526 * Default to software encryption; we'll later upload the 527 * key to the hardware if possible. 528 */ 529 key->conf.flags = 0; 530 key->flags = 0; 531 532 key->conf.cipher = cipher; 533 key->conf.keyidx = idx; 534 key->conf.keylen = key_len; 535 switch (cipher) { 536 case WLAN_CIPHER_SUITE_WEP40: 537 case WLAN_CIPHER_SUITE_WEP104: 538 key->conf.iv_len = IEEE80211_WEP_IV_LEN; 539 key->conf.icv_len = IEEE80211_WEP_ICV_LEN; 540 break; 541 case WLAN_CIPHER_SUITE_TKIP: 542 key->conf.iv_len = IEEE80211_TKIP_IV_LEN; 543 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN; 544 if (seq) { 545 for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 546 key->u.tkip.rx[i].iv32 = 547 get_unaligned_le32(&seq[2]); 548 key->u.tkip.rx[i].iv16 = 549 get_unaligned_le16(seq); 550 } 551 } 552 spin_lock_init(&key->u.tkip.txlock); 553 break; 554 case WLAN_CIPHER_SUITE_CCMP: 555 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN; 556 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN; 557 if (seq) { 558 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) 559 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++) 560 key->u.ccmp.rx_pn[i][j] = 561 seq[IEEE80211_CCMP_PN_LEN - j - 1]; 562 } 563 /* 564 * Initialize AES key state here as an optimization so that 565 * it does not need to be initialized for every packet. 566 */ 567 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt( 568 key_data, key_len, IEEE80211_CCMP_MIC_LEN); 569 if (IS_ERR(key->u.ccmp.tfm)) { 570 err = PTR_ERR(key->u.ccmp.tfm); 571 kfree(key); 572 return ERR_PTR(err); 573 } 574 break; 575 case WLAN_CIPHER_SUITE_CCMP_256: 576 key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN; 577 key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN; 578 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++) 579 for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++) 580 key->u.ccmp.rx_pn[i][j] = 581 seq[IEEE80211_CCMP_256_PN_LEN - j - 1]; 582 /* Initialize AES key state here as an optimization so that 583 * it does not need to be initialized for every packet. 584 */ 585 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt( 586 key_data, key_len, IEEE80211_CCMP_256_MIC_LEN); 587 if (IS_ERR(key->u.ccmp.tfm)) { 588 err = PTR_ERR(key->u.ccmp.tfm); 589 kfree(key); 590 return ERR_PTR(err); 591 } 592 break; 593 case WLAN_CIPHER_SUITE_AES_CMAC: 594 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 595 key->conf.iv_len = 0; 596 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC) 597 key->conf.icv_len = sizeof(struct ieee80211_mmie); 598 else 599 key->conf.icv_len = sizeof(struct ieee80211_mmie_16); 600 if (seq) 601 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++) 602 key->u.aes_cmac.rx_pn[j] = 603 seq[IEEE80211_CMAC_PN_LEN - j - 1]; 604 /* 605 * Initialize AES key state here as an optimization so that 606 * it does not need to be initialized for every packet. 607 */ 608 key->u.aes_cmac.tfm = 609 ieee80211_aes_cmac_key_setup(key_data, key_len); 610 if (IS_ERR(key->u.aes_cmac.tfm)) { 611 err = PTR_ERR(key->u.aes_cmac.tfm); 612 kfree(key); 613 return ERR_PTR(err); 614 } 615 break; 616 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 617 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 618 key->conf.iv_len = 0; 619 key->conf.icv_len = sizeof(struct ieee80211_mmie_16); 620 if (seq) 621 for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++) 622 key->u.aes_gmac.rx_pn[j] = 623 seq[IEEE80211_GMAC_PN_LEN - j - 1]; 624 /* Initialize AES key state here as an optimization so that 625 * it does not need to be initialized for every packet. 626 */ 627 key->u.aes_gmac.tfm = 628 ieee80211_aes_gmac_key_setup(key_data, key_len); 629 if (IS_ERR(key->u.aes_gmac.tfm)) { 630 err = PTR_ERR(key->u.aes_gmac.tfm); 631 kfree(key); 632 return ERR_PTR(err); 633 } 634 break; 635 case WLAN_CIPHER_SUITE_GCMP: 636 case WLAN_CIPHER_SUITE_GCMP_256: 637 key->conf.iv_len = IEEE80211_GCMP_HDR_LEN; 638 key->conf.icv_len = IEEE80211_GCMP_MIC_LEN; 639 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++) 640 for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++) 641 key->u.gcmp.rx_pn[i][j] = 642 seq[IEEE80211_GCMP_PN_LEN - j - 1]; 643 /* Initialize AES key state here as an optimization so that 644 * it does not need to be initialized for every packet. 645 */ 646 key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data, 647 key_len); 648 if (IS_ERR(key->u.gcmp.tfm)) { 649 err = PTR_ERR(key->u.gcmp.tfm); 650 kfree(key); 651 return ERR_PTR(err); 652 } 653 break; 654 default: 655 if (cs) { 656 if (seq_len && seq_len != cs->pn_len) { 657 kfree(key); 658 return ERR_PTR(-EINVAL); 659 } 660 661 key->conf.iv_len = cs->hdr_len; 662 key->conf.icv_len = cs->mic_len; 663 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) 664 for (j = 0; j < seq_len; j++) 665 key->u.gen.rx_pn[i][j] = 666 seq[seq_len - j - 1]; 667 key->flags |= KEY_FLAG_CIPHER_SCHEME; 668 } 669 } 670 memcpy(key->conf.key, key_data, key_len); 671 INIT_LIST_HEAD(&key->list); 672 673 return key; 674 } 675 676 static void ieee80211_key_free_common(struct ieee80211_key *key) 677 { 678 switch (key->conf.cipher) { 679 case WLAN_CIPHER_SUITE_CCMP: 680 case WLAN_CIPHER_SUITE_CCMP_256: 681 ieee80211_aes_key_free(key->u.ccmp.tfm); 682 break; 683 case WLAN_CIPHER_SUITE_AES_CMAC: 684 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 685 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm); 686 break; 687 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 688 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 689 ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm); 690 break; 691 case WLAN_CIPHER_SUITE_GCMP: 692 case WLAN_CIPHER_SUITE_GCMP_256: 693 ieee80211_aes_gcm_key_free(key->u.gcmp.tfm); 694 break; 695 } 696 kzfree(key); 697 } 698 699 static void __ieee80211_key_destroy(struct ieee80211_key *key, 700 bool delay_tailroom) 701 { 702 if (key->local) { 703 struct ieee80211_sub_if_data *sdata = key->sdata; 704 705 ieee80211_debugfs_key_remove(key); 706 707 if (delay_tailroom) { 708 /* see ieee80211_delayed_tailroom_dec */ 709 sdata->crypto_tx_tailroom_pending_dec++; 710 schedule_delayed_work(&sdata->dec_tailroom_needed_wk, 711 HZ/2); 712 } else { 713 decrease_tailroom_need_count(sdata, 1); 714 } 715 } 716 717 ieee80211_key_free_common(key); 718 } 719 720 static void ieee80211_key_destroy(struct ieee80211_key *key, 721 bool delay_tailroom) 722 { 723 if (!key) 724 return; 725 726 /* 727 * Synchronize so the TX path and rcu key iterators 728 * can no longer be using this key before we free/remove it. 729 */ 730 synchronize_net(); 731 732 __ieee80211_key_destroy(key, delay_tailroom); 733 } 734 735 void ieee80211_key_free_unused(struct ieee80211_key *key) 736 { 737 WARN_ON(key->sdata || key->local); 738 ieee80211_key_free_common(key); 739 } 740 741 static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata, 742 struct ieee80211_key *old, 743 struct ieee80211_key *new) 744 { 745 u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP]; 746 u8 *tk_old, *tk_new; 747 748 if (!old || new->conf.keylen != old->conf.keylen) 749 return false; 750 751 tk_old = old->conf.key; 752 tk_new = new->conf.key; 753 754 /* 755 * In station mode, don't compare the TX MIC key, as it's never used 756 * and offloaded rekeying may not care to send it to the host. This 757 * is the case in iwlwifi, for example. 758 */ 759 if (sdata->vif.type == NL80211_IFTYPE_STATION && 760 new->conf.cipher == WLAN_CIPHER_SUITE_TKIP && 761 new->conf.keylen == WLAN_KEY_LEN_TKIP && 762 !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) { 763 memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP); 764 memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP); 765 memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8); 766 memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8); 767 tk_old = tkip_old; 768 tk_new = tkip_new; 769 } 770 771 return !crypto_memneq(tk_old, tk_new, new->conf.keylen); 772 } 773 774 int ieee80211_key_link(struct ieee80211_key *key, 775 struct ieee80211_sub_if_data *sdata, 776 struct sta_info *sta) 777 { 778 struct ieee80211_key *old_key; 779 int idx = key->conf.keyidx; 780 bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE; 781 /* 782 * We want to delay tailroom updates only for station - in that 783 * case it helps roaming speed, but in other cases it hurts and 784 * can cause warnings to appear. 785 */ 786 bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION; 787 int ret = -EOPNOTSUPP; 788 789 mutex_lock(&sdata->local->key_mtx); 790 791 if (sta && pairwise) { 792 struct ieee80211_key *alt_key; 793 794 old_key = key_mtx_dereference(sdata->local, sta->ptk[idx]); 795 alt_key = key_mtx_dereference(sdata->local, sta->ptk[idx ^ 1]); 796 797 /* The rekey code assumes that the old and new key are using 798 * the same cipher. Enforce the assumption for pairwise keys. 799 */ 800 if ((alt_key && alt_key->conf.cipher != key->conf.cipher) || 801 (old_key && old_key->conf.cipher != key->conf.cipher)) 802 goto out; 803 } else if (sta) { 804 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]); 805 } else { 806 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]); 807 } 808 809 /* Non-pairwise keys must also not switch the cipher on rekey */ 810 if (!pairwise) { 811 if (old_key && old_key->conf.cipher != key->conf.cipher) 812 goto out; 813 } 814 815 /* 816 * Silently accept key re-installation without really installing the 817 * new version of the key to avoid nonce reuse or replay issues. 818 */ 819 if (ieee80211_key_identical(sdata, old_key, key)) { 820 ieee80211_key_free_unused(key); 821 ret = 0; 822 goto out; 823 } 824 825 key->local = sdata->local; 826 key->sdata = sdata; 827 key->sta = sta; 828 829 increment_tailroom_need_count(sdata); 830 831 ret = ieee80211_key_replace(sdata, sta, pairwise, old_key, key); 832 833 if (!ret) { 834 ieee80211_debugfs_key_add(key); 835 ieee80211_key_destroy(old_key, delay_tailroom); 836 } else { 837 ieee80211_key_free(key, delay_tailroom); 838 } 839 840 out: 841 mutex_unlock(&sdata->local->key_mtx); 842 843 return ret; 844 } 845 846 void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom) 847 { 848 if (!key) 849 return; 850 851 /* 852 * Replace key with nothingness if it was ever used. 853 */ 854 if (key->sdata) 855 ieee80211_key_replace(key->sdata, key->sta, 856 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE, 857 key, NULL); 858 ieee80211_key_destroy(key, delay_tailroom); 859 } 860 861 void ieee80211_reenable_keys(struct ieee80211_sub_if_data *sdata) 862 { 863 struct ieee80211_key *key; 864 struct ieee80211_sub_if_data *vlan; 865 866 ASSERT_RTNL(); 867 868 mutex_lock(&sdata->local->key_mtx); 869 870 sdata->crypto_tx_tailroom_needed_cnt = 0; 871 sdata->crypto_tx_tailroom_pending_dec = 0; 872 873 if (sdata->vif.type == NL80211_IFTYPE_AP) { 874 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) { 875 vlan->crypto_tx_tailroom_needed_cnt = 0; 876 vlan->crypto_tx_tailroom_pending_dec = 0; 877 } 878 } 879 880 if (ieee80211_sdata_running(sdata)) { 881 list_for_each_entry(key, &sdata->key_list, list) { 882 increment_tailroom_need_count(sdata); 883 ieee80211_key_enable_hw_accel(key); 884 } 885 } 886 887 mutex_unlock(&sdata->local->key_mtx); 888 } 889 890 void ieee80211_iter_keys(struct ieee80211_hw *hw, 891 struct ieee80211_vif *vif, 892 void (*iter)(struct ieee80211_hw *hw, 893 struct ieee80211_vif *vif, 894 struct ieee80211_sta *sta, 895 struct ieee80211_key_conf *key, 896 void *data), 897 void *iter_data) 898 { 899 struct ieee80211_local *local = hw_to_local(hw); 900 struct ieee80211_key *key, *tmp; 901 struct ieee80211_sub_if_data *sdata; 902 903 ASSERT_RTNL(); 904 905 mutex_lock(&local->key_mtx); 906 if (vif) { 907 sdata = vif_to_sdata(vif); 908 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) 909 iter(hw, &sdata->vif, 910 key->sta ? &key->sta->sta : NULL, 911 &key->conf, iter_data); 912 } else { 913 list_for_each_entry(sdata, &local->interfaces, list) 914 list_for_each_entry_safe(key, tmp, 915 &sdata->key_list, list) 916 iter(hw, &sdata->vif, 917 key->sta ? &key->sta->sta : NULL, 918 &key->conf, iter_data); 919 } 920 mutex_unlock(&local->key_mtx); 921 } 922 EXPORT_SYMBOL(ieee80211_iter_keys); 923 924 static void 925 _ieee80211_iter_keys_rcu(struct ieee80211_hw *hw, 926 struct ieee80211_sub_if_data *sdata, 927 void (*iter)(struct ieee80211_hw *hw, 928 struct ieee80211_vif *vif, 929 struct ieee80211_sta *sta, 930 struct ieee80211_key_conf *key, 931 void *data), 932 void *iter_data) 933 { 934 struct ieee80211_key *key; 935 936 list_for_each_entry_rcu(key, &sdata->key_list, list) { 937 /* skip keys of station in removal process */ 938 if (key->sta && key->sta->removed) 939 continue; 940 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) 941 continue; 942 943 iter(hw, &sdata->vif, 944 key->sta ? &key->sta->sta : NULL, 945 &key->conf, iter_data); 946 } 947 } 948 949 void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw, 950 struct ieee80211_vif *vif, 951 void (*iter)(struct ieee80211_hw *hw, 952 struct ieee80211_vif *vif, 953 struct ieee80211_sta *sta, 954 struct ieee80211_key_conf *key, 955 void *data), 956 void *iter_data) 957 { 958 struct ieee80211_local *local = hw_to_local(hw); 959 struct ieee80211_sub_if_data *sdata; 960 961 if (vif) { 962 sdata = vif_to_sdata(vif); 963 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data); 964 } else { 965 list_for_each_entry_rcu(sdata, &local->interfaces, list) 966 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data); 967 } 968 } 969 EXPORT_SYMBOL(ieee80211_iter_keys_rcu); 970 971 static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata, 972 struct list_head *keys) 973 { 974 struct ieee80211_key *key, *tmp; 975 976 decrease_tailroom_need_count(sdata, 977 sdata->crypto_tx_tailroom_pending_dec); 978 sdata->crypto_tx_tailroom_pending_dec = 0; 979 980 ieee80211_debugfs_key_remove_mgmt_default(sdata); 981 982 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) { 983 ieee80211_key_replace(key->sdata, key->sta, 984 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE, 985 key, NULL); 986 list_add_tail(&key->list, keys); 987 } 988 989 ieee80211_debugfs_key_update_default(sdata); 990 } 991 992 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata, 993 bool force_synchronize) 994 { 995 struct ieee80211_local *local = sdata->local; 996 struct ieee80211_sub_if_data *vlan; 997 struct ieee80211_sub_if_data *master; 998 struct ieee80211_key *key, *tmp; 999 LIST_HEAD(keys); 1000 1001 cancel_delayed_work_sync(&sdata->dec_tailroom_needed_wk); 1002 1003 mutex_lock(&local->key_mtx); 1004 1005 ieee80211_free_keys_iface(sdata, &keys); 1006 1007 if (sdata->vif.type == NL80211_IFTYPE_AP) { 1008 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) 1009 ieee80211_free_keys_iface(vlan, &keys); 1010 } 1011 1012 if (!list_empty(&keys) || force_synchronize) 1013 synchronize_net(); 1014 list_for_each_entry_safe(key, tmp, &keys, list) 1015 __ieee80211_key_destroy(key, false); 1016 1017 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 1018 if (sdata->bss) { 1019 master = container_of(sdata->bss, 1020 struct ieee80211_sub_if_data, 1021 u.ap); 1022 1023 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt != 1024 master->crypto_tx_tailroom_needed_cnt); 1025 } 1026 } else { 1027 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt || 1028 sdata->crypto_tx_tailroom_pending_dec); 1029 } 1030 1031 if (sdata->vif.type == NL80211_IFTYPE_AP) { 1032 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) 1033 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt || 1034 vlan->crypto_tx_tailroom_pending_dec); 1035 } 1036 1037 mutex_unlock(&local->key_mtx); 1038 } 1039 1040 void ieee80211_free_sta_keys(struct ieee80211_local *local, 1041 struct sta_info *sta) 1042 { 1043 struct ieee80211_key *key; 1044 int i; 1045 1046 mutex_lock(&local->key_mtx); 1047 for (i = 0; i < ARRAY_SIZE(sta->gtk); i++) { 1048 key = key_mtx_dereference(local, sta->gtk[i]); 1049 if (!key) 1050 continue; 1051 ieee80211_key_replace(key->sdata, key->sta, 1052 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE, 1053 key, NULL); 1054 __ieee80211_key_destroy(key, key->sdata->vif.type == 1055 NL80211_IFTYPE_STATION); 1056 } 1057 1058 for (i = 0; i < NUM_DEFAULT_KEYS; i++) { 1059 key = key_mtx_dereference(local, sta->ptk[i]); 1060 if (!key) 1061 continue; 1062 ieee80211_key_replace(key->sdata, key->sta, 1063 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE, 1064 key, NULL); 1065 __ieee80211_key_destroy(key, key->sdata->vif.type == 1066 NL80211_IFTYPE_STATION); 1067 } 1068 1069 mutex_unlock(&local->key_mtx); 1070 } 1071 1072 void ieee80211_delayed_tailroom_dec(struct work_struct *wk) 1073 { 1074 struct ieee80211_sub_if_data *sdata; 1075 1076 sdata = container_of(wk, struct ieee80211_sub_if_data, 1077 dec_tailroom_needed_wk.work); 1078 1079 /* 1080 * The reason for the delayed tailroom needed decrementing is to 1081 * make roaming faster: during roaming, all keys are first deleted 1082 * and then new keys are installed. The first new key causes the 1083 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes 1084 * the cost of synchronize_net() (which can be slow). Avoid this 1085 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on 1086 * key removal for a while, so if we roam the value is larger than 1087 * zero and no 0->1 transition happens. 1088 * 1089 * The cost is that if the AP switching was from an AP with keys 1090 * to one without, we still allocate tailroom while it would no 1091 * longer be needed. However, in the typical (fast) roaming case 1092 * within an ESS this usually won't happen. 1093 */ 1094 1095 mutex_lock(&sdata->local->key_mtx); 1096 decrease_tailroom_need_count(sdata, 1097 sdata->crypto_tx_tailroom_pending_dec); 1098 sdata->crypto_tx_tailroom_pending_dec = 0; 1099 mutex_unlock(&sdata->local->key_mtx); 1100 } 1101 1102 void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid, 1103 const u8 *replay_ctr, gfp_t gfp) 1104 { 1105 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 1106 1107 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr); 1108 1109 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp); 1110 } 1111 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify); 1112 1113 void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf, 1114 int tid, struct ieee80211_key_seq *seq) 1115 { 1116 struct ieee80211_key *key; 1117 const u8 *pn; 1118 1119 key = container_of(keyconf, struct ieee80211_key, conf); 1120 1121 switch (key->conf.cipher) { 1122 case WLAN_CIPHER_SUITE_TKIP: 1123 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS)) 1124 return; 1125 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32; 1126 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16; 1127 break; 1128 case WLAN_CIPHER_SUITE_CCMP: 1129 case WLAN_CIPHER_SUITE_CCMP_256: 1130 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS)) 1131 return; 1132 if (tid < 0) 1133 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS]; 1134 else 1135 pn = key->u.ccmp.rx_pn[tid]; 1136 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN); 1137 break; 1138 case WLAN_CIPHER_SUITE_AES_CMAC: 1139 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 1140 if (WARN_ON(tid != 0)) 1141 return; 1142 pn = key->u.aes_cmac.rx_pn; 1143 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN); 1144 break; 1145 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 1146 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 1147 if (WARN_ON(tid != 0)) 1148 return; 1149 pn = key->u.aes_gmac.rx_pn; 1150 memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN); 1151 break; 1152 case WLAN_CIPHER_SUITE_GCMP: 1153 case WLAN_CIPHER_SUITE_GCMP_256: 1154 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS)) 1155 return; 1156 if (tid < 0) 1157 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS]; 1158 else 1159 pn = key->u.gcmp.rx_pn[tid]; 1160 memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN); 1161 break; 1162 } 1163 } 1164 EXPORT_SYMBOL(ieee80211_get_key_rx_seq); 1165 1166 void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf, 1167 int tid, struct ieee80211_key_seq *seq) 1168 { 1169 struct ieee80211_key *key; 1170 u8 *pn; 1171 1172 key = container_of(keyconf, struct ieee80211_key, conf); 1173 1174 switch (key->conf.cipher) { 1175 case WLAN_CIPHER_SUITE_TKIP: 1176 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS)) 1177 return; 1178 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32; 1179 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16; 1180 break; 1181 case WLAN_CIPHER_SUITE_CCMP: 1182 case WLAN_CIPHER_SUITE_CCMP_256: 1183 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS)) 1184 return; 1185 if (tid < 0) 1186 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS]; 1187 else 1188 pn = key->u.ccmp.rx_pn[tid]; 1189 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN); 1190 break; 1191 case WLAN_CIPHER_SUITE_AES_CMAC: 1192 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 1193 if (WARN_ON(tid != 0)) 1194 return; 1195 pn = key->u.aes_cmac.rx_pn; 1196 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN); 1197 break; 1198 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 1199 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 1200 if (WARN_ON(tid != 0)) 1201 return; 1202 pn = key->u.aes_gmac.rx_pn; 1203 memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN); 1204 break; 1205 case WLAN_CIPHER_SUITE_GCMP: 1206 case WLAN_CIPHER_SUITE_GCMP_256: 1207 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS)) 1208 return; 1209 if (tid < 0) 1210 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS]; 1211 else 1212 pn = key->u.gcmp.rx_pn[tid]; 1213 memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN); 1214 break; 1215 default: 1216 WARN_ON(1); 1217 break; 1218 } 1219 } 1220 EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq); 1221 1222 void ieee80211_remove_key(struct ieee80211_key_conf *keyconf) 1223 { 1224 struct ieee80211_key *key; 1225 1226 key = container_of(keyconf, struct ieee80211_key, conf); 1227 1228 assert_key_lock(key->local); 1229 1230 /* 1231 * if key was uploaded, we assume the driver will/has remove(d) 1232 * it, so adjust bookkeeping accordingly 1233 */ 1234 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) { 1235 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE; 1236 1237 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC | 1238 IEEE80211_KEY_FLAG_PUT_MIC_SPACE | 1239 IEEE80211_KEY_FLAG_RESERVE_TAILROOM))) 1240 increment_tailroom_need_count(key->sdata); 1241 } 1242 1243 ieee80211_key_free(key, false); 1244 } 1245 EXPORT_SYMBOL_GPL(ieee80211_remove_key); 1246 1247 struct ieee80211_key_conf * 1248 ieee80211_gtk_rekey_add(struct ieee80211_vif *vif, 1249 struct ieee80211_key_conf *keyconf) 1250 { 1251 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 1252 struct ieee80211_local *local = sdata->local; 1253 struct ieee80211_key *key; 1254 int err; 1255 1256 if (WARN_ON(!local->wowlan)) 1257 return ERR_PTR(-EINVAL); 1258 1259 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION)) 1260 return ERR_PTR(-EINVAL); 1261 1262 key = ieee80211_key_alloc(keyconf->cipher, keyconf->keyidx, 1263 keyconf->keylen, keyconf->key, 1264 0, NULL, NULL); 1265 if (IS_ERR(key)) 1266 return ERR_CAST(key); 1267 1268 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED) 1269 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT; 1270 1271 err = ieee80211_key_link(key, sdata, NULL); 1272 if (err) 1273 return ERR_PTR(err); 1274 1275 return &key->conf; 1276 } 1277 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add); 1278