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-2020, 2022-2026 Intel Corporation 10 */ 11 12 #include <crypto/utils.h> 13 #include <linux/if_ether.h> 14 #include <linux/etherdevice.h> 15 #include <linux/list.h> 16 #include <linux/rcupdate.h> 17 #include <linux/rtnetlink.h> 18 #include <linux/slab.h> 19 #include <linux/export.h> 20 #include <net/mac80211.h> 21 #include <linux/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 57 update_vlan_tailroom_need_count(struct ieee80211_sub_if_data *sdata, int delta) 58 { 59 struct ieee80211_sub_if_data *vlan; 60 61 if (sdata->vif.type != NL80211_IFTYPE_AP) 62 return; 63 64 /* crypto_tx_tailroom_needed_cnt is protected by this */ 65 lockdep_assert_wiphy(sdata->local->hw.wiphy); 66 67 rcu_read_lock(); 68 69 list_for_each_entry_rcu(vlan, &sdata->u.ap.vlans, u.vlan.list) 70 vlan->crypto_tx_tailroom_needed_cnt += delta; 71 72 rcu_read_unlock(); 73 } 74 75 static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata) 76 { 77 /* 78 * When this count is zero, SKB resizing for allocating tailroom 79 * for IV or MMIC is skipped. But, this check has created two race 80 * cases in xmit path while transiting from zero count to one: 81 * 82 * 1. SKB resize was skipped because no key was added but just before 83 * the xmit key is added and SW encryption kicks off. 84 * 85 * 2. SKB resize was skipped because all the keys were hw planted but 86 * just before xmit one of the key is deleted and SW encryption kicks 87 * off. 88 * 89 * In both the above case SW encryption will find not enough space for 90 * tailroom and exits with WARN_ON. (See WARN_ONs at wpa.c) 91 * 92 * Solution has been explained at 93 * http://mid.gmane.org/1308590980.4322.19.camel@jlt3.sipsolutions.net 94 */ 95 96 lockdep_assert_wiphy(sdata->local->hw.wiphy); 97 98 update_vlan_tailroom_need_count(sdata, 1); 99 100 if (!sdata->crypto_tx_tailroom_needed_cnt++) { 101 /* 102 * Flush all XMIT packets currently using HW encryption or no 103 * encryption at all if the count transition is from 0 -> 1. 104 */ 105 synchronize_net(); 106 } 107 } 108 109 static void decrease_tailroom_need_count(struct ieee80211_sub_if_data *sdata, 110 int delta) 111 { 112 lockdep_assert_wiphy(sdata->local->hw.wiphy); 113 114 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt < delta); 115 116 update_vlan_tailroom_need_count(sdata, -delta); 117 sdata->crypto_tx_tailroom_needed_cnt -= delta; 118 } 119 120 static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key) 121 { 122 struct ieee80211_sub_if_data *sdata = key->sdata; 123 struct sta_info *sta; 124 int ret = -EOPNOTSUPP; 125 126 might_sleep(); 127 lockdep_assert_wiphy(key->local->hw.wiphy); 128 129 if (key->flags & KEY_FLAG_TAINTED) { 130 /* If we get here, it's during resume and the key is 131 * tainted so shouldn't be used/programmed any more. 132 * However, its flags may still indicate that it was 133 * programmed into the device (since we're in resume) 134 * so clear that flag now to avoid trying to remove 135 * it again later. 136 */ 137 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE && 138 !(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC | 139 IEEE80211_KEY_FLAG_PUT_MIC_SPACE | 140 IEEE80211_KEY_FLAG_RESERVE_TAILROOM))) 141 increment_tailroom_need_count(sdata); 142 143 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE; 144 return -EINVAL; 145 } 146 147 if (!key->local->ops->set_key) 148 goto out_unsupported; 149 150 sta = key->sta; 151 152 /* 153 * Allow installation of a per-STA GTK if per-STA GTK is supported 154 * by the driver or the interface is a NAN Data interface (as 155 * per-station GTKs are required to be supported if secure NAN is 156 * supported). 157 */ 158 if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) && 159 !(ieee80211_hw_check(&key->local->hw, SUPPORTS_PER_STA_GTK) || 160 sdata->vif.type == NL80211_IFTYPE_NAN_DATA)) 161 goto out_unsupported; 162 163 if (sta && !sta->uploaded) 164 goto out_unsupported; 165 166 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 167 /* 168 * The driver doesn't know anything about VLAN interfaces. 169 * Hence, don't send GTKs for VLAN interfaces to the driver. 170 */ 171 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) { 172 ret = 1; 173 goto out_unsupported; 174 } 175 } 176 177 if (key->conf.link_id >= 0 && sdata->vif.active_links && 178 !(sdata->vif.active_links & BIT(key->conf.link_id))) 179 return 0; 180 181 ret = drv_set_key(key->local, SET_KEY, sdata, 182 sta ? &sta->sta : NULL, &key->conf); 183 184 if (!ret) { 185 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE; 186 187 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC | 188 IEEE80211_KEY_FLAG_PUT_MIC_SPACE | 189 IEEE80211_KEY_FLAG_RESERVE_TAILROOM))) 190 decrease_tailroom_need_count(sdata, 1); 191 192 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) && 193 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)); 194 195 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) && 196 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)); 197 198 return 0; 199 } 200 201 if (ret != -ENOSPC && ret != -EOPNOTSUPP && ret != 1) 202 sdata_err(sdata, 203 "failed to set key (%d, %pM) to hardware (%d)\n", 204 key->conf.keyidx, 205 sta ? sta->sta.addr : bcast_addr, ret); 206 207 out_unsupported: 208 switch (key->conf.cipher) { 209 case WLAN_CIPHER_SUITE_WEP40: 210 case WLAN_CIPHER_SUITE_WEP104: 211 case WLAN_CIPHER_SUITE_TKIP: 212 case WLAN_CIPHER_SUITE_CCMP: 213 case WLAN_CIPHER_SUITE_CCMP_256: 214 case WLAN_CIPHER_SUITE_GCMP: 215 case WLAN_CIPHER_SUITE_GCMP_256: 216 case WLAN_CIPHER_SUITE_AES_CMAC: 217 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 218 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 219 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 220 /* all of these we can do in software - if driver can */ 221 if (ret == 1) 222 return 0; 223 if (ieee80211_hw_check(&key->local->hw, SW_CRYPTO_CONTROL)) 224 return -EINVAL; 225 return 0; 226 default: 227 return -EINVAL; 228 } 229 } 230 231 static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key) 232 { 233 struct ieee80211_sub_if_data *sdata; 234 struct sta_info *sta; 235 int ret; 236 237 might_sleep(); 238 239 if (!key || !key->local->ops->set_key) 240 return; 241 242 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) 243 return; 244 245 sta = key->sta; 246 sdata = key->sdata; 247 248 lockdep_assert_wiphy(key->local->hw.wiphy); 249 250 if (key->conf.link_id >= 0 && sdata->vif.active_links && 251 !(sdata->vif.active_links & BIT(key->conf.link_id))) 252 return; 253 254 if (!(key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC | 255 IEEE80211_KEY_FLAG_PUT_MIC_SPACE | 256 IEEE80211_KEY_FLAG_RESERVE_TAILROOM))) 257 increment_tailroom_need_count(sdata); 258 259 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE; 260 ret = drv_set_key(key->local, DISABLE_KEY, sdata, 261 sta ? &sta->sta : NULL, &key->conf); 262 263 if (ret) 264 sdata_err(sdata, 265 "failed to remove key (%d, %pM) from hardware (%d)\n", 266 key->conf.keyidx, 267 sta ? sta->sta.addr : bcast_addr, ret); 268 } 269 270 static int _ieee80211_set_tx_key(struct ieee80211_key *key, bool force) 271 { 272 struct sta_info *sta = key->sta; 273 struct ieee80211_local *local = key->local; 274 275 lockdep_assert_wiphy(local->hw.wiphy); 276 277 set_sta_flag(sta, WLAN_STA_USES_ENCRYPTION); 278 279 sta->ptk_idx = key->conf.keyidx; 280 281 if (force || !ieee80211_hw_check(&local->hw, AMPDU_KEYBORDER_SUPPORT)) 282 clear_sta_flag(sta, WLAN_STA_BLOCK_BA); 283 ieee80211_check_fast_xmit(sta); 284 285 return 0; 286 } 287 288 int ieee80211_set_tx_key(struct ieee80211_key *key) 289 { 290 return _ieee80211_set_tx_key(key, false); 291 } 292 293 static void ieee80211_pairwise_rekey(struct ieee80211_key *old, 294 struct ieee80211_key *new) 295 { 296 struct ieee80211_local *local = new->local; 297 struct sta_info *sta = new->sta; 298 int i; 299 300 lockdep_assert_wiphy(local->hw.wiphy); 301 302 if (new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX) { 303 /* Extended Key ID key install, initial one or rekey */ 304 305 if (sta->ptk_idx != INVALID_PTK_KEYIDX && 306 !ieee80211_hw_check(&local->hw, AMPDU_KEYBORDER_SUPPORT)) { 307 /* Aggregation Sessions with Extended Key ID must not 308 * mix MPDUs with different keyIDs within one A-MPDU. 309 * Tear down running Tx aggregation sessions and block 310 * new Rx/Tx aggregation requests during rekey to 311 * ensure there are no A-MPDUs when the driver is not 312 * supporting A-MPDU key borders. (Blocking Tx only 313 * would be sufficient but WLAN_STA_BLOCK_BA gets the 314 * job done for the few ms we need it.) 315 */ 316 set_sta_flag(sta, WLAN_STA_BLOCK_BA); 317 for (i = 0; i < IEEE80211_NUM_TIDS; i++) 318 __ieee80211_stop_tx_ba_session(sta, i, 319 AGG_STOP_LOCAL_REQUEST); 320 } 321 } else if (old) { 322 /* Rekey without Extended Key ID. 323 * Aggregation sessions are OK when running on SW crypto. 324 * A broken remote STA may cause issues not observed with HW 325 * crypto, though. 326 */ 327 if (!(old->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) 328 return; 329 330 /* Stop Tx till we are on the new key */ 331 old->flags |= KEY_FLAG_TAINTED; 332 ieee80211_clear_fast_xmit(sta); 333 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) { 334 set_sta_flag(sta, WLAN_STA_BLOCK_BA); 335 ieee80211_sta_tear_down_BA_sessions(sta, 336 AGG_STOP_LOCAL_REQUEST); 337 } 338 if (!wiphy_ext_feature_isset(local->hw.wiphy, 339 NL80211_EXT_FEATURE_CAN_REPLACE_PTK0)) { 340 pr_warn_ratelimited("Rekeying PTK for STA %pM but driver can't safely do that.", 341 sta->sta.addr); 342 /* Flushing the driver queues *may* help prevent 343 * the clear text leaks and freezes. 344 */ 345 ieee80211_flush_queues(local, old->sdata, false); 346 } 347 } 348 } 349 350 static void __ieee80211_set_default_key(struct ieee80211_link_data *link, 351 int idx, bool uni, bool multi) 352 { 353 struct ieee80211_sub_if_data *sdata = link->sdata; 354 struct ieee80211_key *key = NULL; 355 356 lockdep_assert_wiphy(sdata->local->hw.wiphy); 357 358 if (idx >= 0 && idx < NUM_DEFAULT_KEYS) { 359 key = wiphy_dereference(sdata->local->hw.wiphy, 360 sdata->keys[idx]); 361 if (!key) 362 key = wiphy_dereference(sdata->local->hw.wiphy, 363 link->gtk[idx]); 364 } 365 366 if (uni) { 367 rcu_assign_pointer(sdata->default_unicast_key, key); 368 ieee80211_check_fast_xmit_iface(sdata); 369 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN) 370 drv_set_default_unicast_key(sdata->local, sdata, idx); 371 } 372 373 if (multi) 374 rcu_assign_pointer(link->default_multicast_key, key); 375 376 ieee80211_debugfs_key_update_default(sdata); 377 } 378 379 void ieee80211_set_default_key(struct ieee80211_link_data *link, int idx, 380 bool uni, bool multi) 381 { 382 lockdep_assert_wiphy(link->sdata->local->hw.wiphy); 383 384 __ieee80211_set_default_key(link, idx, uni, multi); 385 } 386 387 static void 388 __ieee80211_set_default_mgmt_key(struct ieee80211_link_data *link, int idx) 389 { 390 struct ieee80211_sub_if_data *sdata = link->sdata; 391 struct ieee80211_key *key = NULL; 392 393 lockdep_assert_wiphy(sdata->local->hw.wiphy); 394 395 if (idx >= NUM_DEFAULT_KEYS && 396 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS) 397 key = wiphy_dereference(sdata->local->hw.wiphy, 398 link->gtk[idx]); 399 400 rcu_assign_pointer(link->default_mgmt_key, key); 401 402 ieee80211_debugfs_key_update_default(sdata); 403 } 404 405 void ieee80211_set_default_mgmt_key(struct ieee80211_link_data *link, 406 int idx) 407 { 408 lockdep_assert_wiphy(link->sdata->local->hw.wiphy); 409 410 __ieee80211_set_default_mgmt_key(link, idx); 411 } 412 413 static void 414 __ieee80211_set_default_beacon_key(struct ieee80211_link_data *link, int idx) 415 { 416 struct ieee80211_sub_if_data *sdata = link->sdata; 417 struct ieee80211_key *key = NULL; 418 419 lockdep_assert_wiphy(sdata->local->hw.wiphy); 420 421 if (idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS && 422 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS + 423 NUM_DEFAULT_BEACON_KEYS) 424 key = wiphy_dereference(sdata->local->hw.wiphy, 425 link->gtk[idx]); 426 427 rcu_assign_pointer(link->default_beacon_key, key); 428 429 ieee80211_debugfs_key_update_default(sdata); 430 } 431 432 void ieee80211_set_default_beacon_key(struct ieee80211_link_data *link, 433 int idx) 434 { 435 lockdep_assert_wiphy(link->sdata->local->hw.wiphy); 436 437 __ieee80211_set_default_beacon_key(link, idx); 438 } 439 440 static int ieee80211_key_replace(struct ieee80211_sub_if_data *sdata, 441 struct ieee80211_link_data *link, 442 struct sta_info *sta, 443 bool pairwise, 444 struct ieee80211_key *old, 445 struct ieee80211_key *new) 446 { 447 struct link_sta_info *link_sta = sta ? &sta->deflink : NULL; 448 int link_id; 449 int idx; 450 int ret = 0; 451 bool defunikey, defmultikey, defmgmtkey, defbeaconkey; 452 bool is_wep; 453 454 lockdep_assert_wiphy(sdata->local->hw.wiphy); 455 456 /* caller must provide at least one old/new */ 457 if (WARN_ON(!new && !old)) 458 return 0; 459 460 if (new) { 461 idx = new->conf.keyidx; 462 is_wep = new->conf.cipher == WLAN_CIPHER_SUITE_WEP40 || 463 new->conf.cipher == WLAN_CIPHER_SUITE_WEP104; 464 link_id = new->conf.link_id; 465 } else { 466 idx = old->conf.keyidx; 467 is_wep = old->conf.cipher == WLAN_CIPHER_SUITE_WEP40 || 468 old->conf.cipher == WLAN_CIPHER_SUITE_WEP104; 469 link_id = old->conf.link_id; 470 } 471 472 if (WARN(old && old->conf.link_id != link_id, 473 "old link ID %d doesn't match new link ID %d\n", 474 old->conf.link_id, link_id)) 475 return -EINVAL; 476 477 if (link_id >= 0) { 478 if (!link) { 479 link = sdata_dereference(sdata->link[link_id], sdata); 480 if (!link) 481 return -ENOLINK; 482 } 483 484 if (sta) { 485 link_sta = rcu_dereference_protected(sta->link[link_id], 486 lockdep_is_held(&sta->local->hw.wiphy->mtx)); 487 if (!link_sta) 488 return -ENOLINK; 489 } 490 } else { 491 link = &sdata->deflink; 492 } 493 494 if ((is_wep || pairwise) && idx >= NUM_DEFAULT_KEYS) 495 return -EINVAL; 496 497 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx); 498 499 if (new && sta && pairwise) { 500 /* Unicast rekey needs special handling. With Extended Key ID 501 * old is still NULL for the first rekey. 502 */ 503 ieee80211_pairwise_rekey(old, new); 504 } 505 506 if (old) { 507 if (old->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) { 508 ieee80211_key_disable_hw_accel(old); 509 510 if (new) 511 ret = ieee80211_key_enable_hw_accel(new); 512 } 513 } else { 514 if (!new->local->wowlan) { 515 ret = ieee80211_key_enable_hw_accel(new); 516 } else if (link_id < 0 || !sdata->vif.active_links || 517 BIT(link_id) & sdata->vif.active_links) { 518 new->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE; 519 if (!(new->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC | 520 IEEE80211_KEY_FLAG_PUT_MIC_SPACE | 521 IEEE80211_KEY_FLAG_RESERVE_TAILROOM))) 522 decrease_tailroom_need_count(sdata, 1); 523 } 524 } 525 526 if (ret) 527 return ret; 528 529 if (new) 530 list_add_tail_rcu(&new->list, &sdata->key_list); 531 532 if (sta) { 533 if (pairwise) { 534 rcu_assign_pointer(sta->ptk[idx], new); 535 if (new && 536 !(new->conf.flags & IEEE80211_KEY_FLAG_NO_AUTO_TX)) 537 _ieee80211_set_tx_key(new, true); 538 } else { 539 rcu_assign_pointer(link_sta->gtk[idx], new); 540 } 541 /* Only needed for transition from no key -> key. 542 * Still triggers unnecessary when using Extended Key ID 543 * and installing the second key ID the first time. 544 */ 545 if (new && !old) 546 ieee80211_check_fast_rx(sta); 547 } else { 548 defunikey = old && 549 old == wiphy_dereference(sdata->local->hw.wiphy, 550 sdata->default_unicast_key); 551 defmultikey = old && 552 old == wiphy_dereference(sdata->local->hw.wiphy, 553 link->default_multicast_key); 554 defmgmtkey = old && 555 old == wiphy_dereference(sdata->local->hw.wiphy, 556 link->default_mgmt_key); 557 defbeaconkey = old && 558 old == wiphy_dereference(sdata->local->hw.wiphy, 559 link->default_beacon_key); 560 561 if (defunikey && !new) 562 __ieee80211_set_default_key(link, -1, true, false); 563 if (defmultikey && !new) 564 __ieee80211_set_default_key(link, -1, false, true); 565 if (defmgmtkey && !new) 566 __ieee80211_set_default_mgmt_key(link, -1); 567 if (defbeaconkey && !new) 568 __ieee80211_set_default_beacon_key(link, -1); 569 570 if (is_wep || pairwise) 571 rcu_assign_pointer(sdata->keys[idx], new); 572 else 573 rcu_assign_pointer(link->gtk[idx], new); 574 575 if (defunikey && new) 576 __ieee80211_set_default_key(link, new->conf.keyidx, 577 true, false); 578 if (defmultikey && new) 579 __ieee80211_set_default_key(link, new->conf.keyidx, 580 false, true); 581 if (defmgmtkey && new) 582 __ieee80211_set_default_mgmt_key(link, 583 new->conf.keyidx); 584 if (defbeaconkey && new) 585 __ieee80211_set_default_beacon_key(link, 586 new->conf.keyidx); 587 } 588 589 if (old) 590 list_del_rcu(&old->list); 591 592 return 0; 593 } 594 595 struct ieee80211_key * 596 ieee80211_key_alloc(u32 cipher, int idx, size_t key_len, 597 const u8 *key_data, 598 size_t seq_len, const u8 *seq) 599 { 600 struct ieee80211_key *key; 601 int i, j, err; 602 603 if (WARN_ON(idx < 0 || 604 idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS + 605 NUM_DEFAULT_BEACON_KEYS)) 606 return ERR_PTR(-EINVAL); 607 608 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL); 609 if (!key) 610 return ERR_PTR(-ENOMEM); 611 612 /* 613 * Default to software encryption; we'll later upload the 614 * key to the hardware if possible. 615 */ 616 key->conf.flags = 0; 617 key->flags = 0; 618 619 key->conf.link_id = -1; 620 key->conf.cipher = cipher; 621 key->conf.keyidx = idx; 622 key->conf.keylen = key_len; 623 switch (cipher) { 624 case WLAN_CIPHER_SUITE_WEP40: 625 case WLAN_CIPHER_SUITE_WEP104: 626 key->conf.iv_len = IEEE80211_WEP_IV_LEN; 627 key->conf.icv_len = IEEE80211_WEP_ICV_LEN; 628 break; 629 case WLAN_CIPHER_SUITE_TKIP: 630 key->conf.iv_len = IEEE80211_TKIP_IV_LEN; 631 key->conf.icv_len = IEEE80211_TKIP_ICV_LEN; 632 if (seq) { 633 for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 634 key->u.tkip.rx[i].iv32 = 635 get_unaligned_le32(&seq[2]); 636 key->u.tkip.rx[i].iv16 = 637 get_unaligned_le16(seq); 638 } 639 } 640 spin_lock_init(&key->u.tkip.txlock); 641 break; 642 case WLAN_CIPHER_SUITE_CCMP: 643 key->conf.iv_len = IEEE80211_CCMP_HDR_LEN; 644 key->conf.icv_len = IEEE80211_CCMP_MIC_LEN; 645 if (seq) { 646 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) 647 for (j = 0; j < IEEE80211_CCMP_PN_LEN; j++) 648 key->u.ccmp.rx_pn[i][j] = 649 seq[IEEE80211_CCMP_PN_LEN - j - 1]; 650 } 651 /* 652 * Initialize AES key state here as an optimization so that 653 * it does not need to be initialized for every packet. 654 */ 655 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt( 656 key_data, key_len, IEEE80211_CCMP_MIC_LEN); 657 if (IS_ERR(key->u.ccmp.tfm)) { 658 err = PTR_ERR(key->u.ccmp.tfm); 659 kfree(key); 660 return ERR_PTR(err); 661 } 662 break; 663 case WLAN_CIPHER_SUITE_CCMP_256: 664 key->conf.iv_len = IEEE80211_CCMP_256_HDR_LEN; 665 key->conf.icv_len = IEEE80211_CCMP_256_MIC_LEN; 666 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++) 667 for (j = 0; j < IEEE80211_CCMP_256_PN_LEN; j++) 668 key->u.ccmp.rx_pn[i][j] = 669 seq[IEEE80211_CCMP_256_PN_LEN - j - 1]; 670 /* Initialize AES key state here as an optimization so that 671 * it does not need to be initialized for every packet. 672 */ 673 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt( 674 key_data, key_len, IEEE80211_CCMP_256_MIC_LEN); 675 if (IS_ERR(key->u.ccmp.tfm)) { 676 err = PTR_ERR(key->u.ccmp.tfm); 677 kfree(key); 678 return ERR_PTR(err); 679 } 680 break; 681 case WLAN_CIPHER_SUITE_AES_CMAC: 682 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 683 key->conf.iv_len = 0; 684 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC) 685 key->conf.icv_len = sizeof(struct ieee80211_mmie); 686 else 687 key->conf.icv_len = sizeof(struct ieee80211_mmie_16); 688 if (seq) 689 for (j = 0; j < IEEE80211_CMAC_PN_LEN; j++) 690 key->u.aes_cmac.rx_pn[j] = 691 seq[IEEE80211_CMAC_PN_LEN - j - 1]; 692 /* 693 * Initialize AES key state here as an optimization so that 694 * it does not need to be initialized for every packet. 695 */ 696 err = aes_cmac_preparekey(&key->u.aes_cmac.key, key_data, 697 key_len); 698 if (err) { 699 kfree(key); 700 return ERR_PTR(err); 701 } 702 break; 703 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 704 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 705 key->conf.iv_len = 0; 706 key->conf.icv_len = sizeof(struct ieee80211_mmie_16); 707 if (seq) 708 for (j = 0; j < IEEE80211_GMAC_PN_LEN; j++) 709 key->u.aes_gmac.rx_pn[j] = 710 seq[IEEE80211_GMAC_PN_LEN - j - 1]; 711 /* Initialize AES key state here as an optimization so that 712 * it does not need to be initialized for every packet. 713 */ 714 key->u.aes_gmac.tfm = 715 ieee80211_aes_gmac_key_setup(key_data, key_len); 716 if (IS_ERR(key->u.aes_gmac.tfm)) { 717 err = PTR_ERR(key->u.aes_gmac.tfm); 718 kfree(key); 719 return ERR_PTR(err); 720 } 721 break; 722 case WLAN_CIPHER_SUITE_GCMP: 723 case WLAN_CIPHER_SUITE_GCMP_256: 724 key->conf.iv_len = IEEE80211_GCMP_HDR_LEN; 725 key->conf.icv_len = IEEE80211_GCMP_MIC_LEN; 726 for (i = 0; seq && i < IEEE80211_NUM_TIDS + 1; i++) 727 for (j = 0; j < IEEE80211_GCMP_PN_LEN; j++) 728 key->u.gcmp.rx_pn[i][j] = 729 seq[IEEE80211_GCMP_PN_LEN - j - 1]; 730 /* Initialize AES key state here as an optimization so that 731 * it does not need to be initialized for every packet. 732 */ 733 key->u.gcmp.tfm = ieee80211_aes_gcm_key_setup_encrypt(key_data, 734 key_len); 735 if (IS_ERR(key->u.gcmp.tfm)) { 736 err = PTR_ERR(key->u.gcmp.tfm); 737 kfree(key); 738 return ERR_PTR(err); 739 } 740 break; 741 } 742 memcpy(key->conf.key, key_data, key_len); 743 INIT_LIST_HEAD(&key->list); 744 745 return key; 746 } 747 748 static void ieee80211_key_free_common(struct ieee80211_key *key) 749 { 750 switch (key->conf.cipher) { 751 case WLAN_CIPHER_SUITE_CCMP: 752 case WLAN_CIPHER_SUITE_CCMP_256: 753 ieee80211_aes_key_free(key->u.ccmp.tfm); 754 break; 755 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 756 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 757 ieee80211_aes_gmac_key_free(key->u.aes_gmac.tfm); 758 break; 759 case WLAN_CIPHER_SUITE_GCMP: 760 case WLAN_CIPHER_SUITE_GCMP_256: 761 ieee80211_aes_gcm_key_free(key->u.gcmp.tfm); 762 break; 763 } 764 kfree_sensitive(key); 765 } 766 767 static void __ieee80211_key_destroy(struct ieee80211_key *key, 768 bool delay_tailroom) 769 { 770 if (key->local) { 771 struct ieee80211_sub_if_data *sdata = key->sdata; 772 773 ieee80211_debugfs_key_remove(key); 774 775 if (delay_tailroom) { 776 /* see ieee80211_delayed_tailroom_dec */ 777 sdata->crypto_tx_tailroom_pending_dec++; 778 wiphy_delayed_work_queue(sdata->local->hw.wiphy, 779 &sdata->dec_tailroom_needed_wk, 780 HZ / 2); 781 } else { 782 decrease_tailroom_need_count(sdata, 1); 783 } 784 } 785 786 ieee80211_key_free_common(key); 787 } 788 789 static void ieee80211_key_destroy(struct ieee80211_key *key, 790 bool delay_tailroom) 791 { 792 if (!key) 793 return; 794 795 /* 796 * Synchronize so the TX path and rcu key iterators 797 * can no longer be using this key before we free/remove it. 798 */ 799 synchronize_net(); 800 801 __ieee80211_key_destroy(key, delay_tailroom); 802 } 803 804 void ieee80211_key_free_unused(struct ieee80211_key *key) 805 { 806 if (!key) 807 return; 808 809 WARN_ON(key->sdata || key->local); 810 ieee80211_key_free_common(key); 811 } 812 813 static bool ieee80211_key_identical(struct ieee80211_sub_if_data *sdata, 814 struct ieee80211_key *old, 815 struct ieee80211_key *new) 816 { 817 u8 tkip_old[WLAN_KEY_LEN_TKIP], tkip_new[WLAN_KEY_LEN_TKIP]; 818 u8 *tk_old, *tk_new; 819 820 if (!old || new->conf.keylen != old->conf.keylen) 821 return false; 822 823 tk_old = old->conf.key; 824 tk_new = new->conf.key; 825 826 /* 827 * In station mode, don't compare the TX MIC key, as it's never used 828 * and offloaded rekeying may not care to send it to the host. This 829 * is the case in iwlwifi, for example. 830 */ 831 if (sdata->vif.type == NL80211_IFTYPE_STATION && 832 new->conf.cipher == WLAN_CIPHER_SUITE_TKIP && 833 new->conf.keylen == WLAN_KEY_LEN_TKIP && 834 !(new->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE)) { 835 memcpy(tkip_old, tk_old, WLAN_KEY_LEN_TKIP); 836 memcpy(tkip_new, tk_new, WLAN_KEY_LEN_TKIP); 837 memset(tkip_old + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8); 838 memset(tkip_new + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY, 0, 8); 839 tk_old = tkip_old; 840 tk_new = tkip_new; 841 } 842 843 return !crypto_memneq(tk_old, tk_new, new->conf.keylen); 844 } 845 846 int ieee80211_key_link(struct ieee80211_key *key, 847 struct ieee80211_link_data *link, 848 struct sta_info *sta) 849 { 850 struct ieee80211_sub_if_data *sdata = link->sdata; 851 static atomic_t key_color = ATOMIC_INIT(0); 852 struct ieee80211_key *old_key = NULL; 853 int idx = key->conf.keyidx; 854 bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE; 855 /* 856 * We want to delay tailroom updates only for station - in that 857 * case it helps roaming speed, but in other cases it hurts and 858 * can cause warnings to appear. 859 */ 860 bool delay_tailroom = sdata->vif.type == NL80211_IFTYPE_STATION; 861 int ret; 862 863 lockdep_assert_wiphy(sdata->local->hw.wiphy); 864 865 if (sta && pairwise) { 866 struct ieee80211_key *alt_key; 867 868 old_key = wiphy_dereference(sdata->local->hw.wiphy, 869 sta->ptk[idx]); 870 alt_key = wiphy_dereference(sdata->local->hw.wiphy, 871 sta->ptk[idx ^ 1]); 872 873 /* 874 * The rekey code assumes that the old and new key are using 875 * the same cipher. Enforce the assumption for pairwise keys. 876 * NAN Data interfaces are exempt: Wi-Fi Aware v4.0 section 7.4 877 * requires upgrading the ND-TKSA when a new NDP negotiates a 878 * stronger cipher suite. 879 */ 880 if (sdata->vif.type != NL80211_IFTYPE_NAN_DATA && 881 ((alt_key && alt_key->conf.cipher != key->conf.cipher) || 882 (old_key && old_key->conf.cipher != key->conf.cipher))) { 883 ret = -EOPNOTSUPP; 884 goto out; 885 } 886 } else if (sta) { 887 struct link_sta_info *link_sta = &sta->deflink; 888 int link_id = key->conf.link_id; 889 890 if (link_id >= 0) { 891 link_sta = rcu_dereference_protected(sta->link[link_id], 892 lockdep_is_held(&sta->local->hw.wiphy->mtx)); 893 if (!link_sta) { 894 ret = -ENOLINK; 895 goto out; 896 } 897 } 898 899 old_key = wiphy_dereference(sdata->local->hw.wiphy, 900 link_sta->gtk[idx]); 901 } else { 902 if (idx < NUM_DEFAULT_KEYS) 903 old_key = wiphy_dereference(sdata->local->hw.wiphy, 904 sdata->keys[idx]); 905 if (!old_key) 906 old_key = wiphy_dereference(sdata->local->hw.wiphy, 907 link->gtk[idx]); 908 } 909 910 /* Non-pairwise keys must also not switch the cipher on rekey */ 911 if (!pairwise) { 912 if (old_key && old_key->conf.cipher != key->conf.cipher) { 913 ret = -EOPNOTSUPP; 914 goto out; 915 } 916 } 917 918 /* 919 * Silently accept key re-installation without really installing the 920 * new version of the key to avoid nonce reuse or replay issues. 921 */ 922 if (ieee80211_key_identical(sdata, old_key, key)) { 923 ret = -EALREADY; 924 goto out; 925 } 926 927 key->local = sdata->local; 928 key->sdata = sdata; 929 key->sta = sta; 930 931 /* 932 * Assign a unique ID to every key so we can easily prevent mixed 933 * key and fragment cache attacks. 934 */ 935 key->color = atomic_inc_return(&key_color); 936 937 /* keep this flag for easier access later */ 938 if (sta && sta->sta.spp_amsdu) 939 key->conf.flags |= IEEE80211_KEY_FLAG_SPP_AMSDU; 940 941 increment_tailroom_need_count(sdata); 942 943 ret = ieee80211_key_replace(sdata, link, sta, pairwise, old_key, key); 944 945 if (!ret) { 946 ieee80211_debugfs_key_add(key); 947 ieee80211_key_destroy(old_key, delay_tailroom); 948 } else { 949 ieee80211_key_free(key, delay_tailroom); 950 } 951 952 key = NULL; 953 954 out: 955 ieee80211_key_free_unused(key); 956 return ret; 957 } 958 959 void ieee80211_key_free(struct ieee80211_key *key, bool delay_tailroom) 960 { 961 if (!key) 962 return; 963 964 /* 965 * Replace key with nothingness if it was ever used. 966 */ 967 if (key->sdata) 968 ieee80211_key_replace(key->sdata, NULL, key->sta, 969 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE, 970 key, NULL); 971 ieee80211_key_destroy(key, delay_tailroom); 972 } 973 974 void ieee80211_reenable_keys(struct ieee80211_sub_if_data *sdata) 975 { 976 struct ieee80211_key *key; 977 struct ieee80211_sub_if_data *vlan; 978 979 lockdep_assert_wiphy(sdata->local->hw.wiphy); 980 981 sdata->crypto_tx_tailroom_needed_cnt = 0; 982 sdata->crypto_tx_tailroom_pending_dec = 0; 983 984 if (sdata->vif.type == NL80211_IFTYPE_AP) { 985 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) { 986 vlan->crypto_tx_tailroom_needed_cnt = 0; 987 vlan->crypto_tx_tailroom_pending_dec = 0; 988 } 989 } 990 991 if (ieee80211_sdata_running(sdata)) { 992 list_for_each_entry(key, &sdata->key_list, list) { 993 if (!(key->flags & KEY_FLAG_TAINTED)) 994 increment_tailroom_need_count(sdata); 995 ieee80211_key_enable_hw_accel(key); 996 } 997 } 998 } 999 1000 static void 1001 ieee80211_key_iter(struct ieee80211_hw *hw, 1002 struct ieee80211_vif *vif, 1003 struct ieee80211_key *key, 1004 void (*iter)(struct ieee80211_hw *hw, 1005 struct ieee80211_vif *vif, 1006 struct ieee80211_sta *sta, 1007 struct ieee80211_key_conf *key, 1008 void *data), 1009 void *iter_data) 1010 { 1011 /* skip keys of station in removal process */ 1012 if (key->sta && key->sta->removed) 1013 return; 1014 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) 1015 return; 1016 iter(hw, vif, key->sta ? &key->sta->sta : NULL, 1017 &key->conf, iter_data); 1018 } 1019 1020 void ieee80211_iter_keys(struct ieee80211_hw *hw, 1021 struct ieee80211_vif *vif, 1022 void (*iter)(struct ieee80211_hw *hw, 1023 struct ieee80211_vif *vif, 1024 struct ieee80211_sta *sta, 1025 struct ieee80211_key_conf *key, 1026 void *data), 1027 void *iter_data) 1028 { 1029 struct ieee80211_local *local = hw_to_local(hw); 1030 struct ieee80211_key *key, *tmp; 1031 struct ieee80211_sub_if_data *sdata; 1032 1033 lockdep_assert_wiphy(hw->wiphy); 1034 1035 if (vif) { 1036 sdata = vif_to_sdata(vif); 1037 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) 1038 ieee80211_key_iter(hw, vif, key, iter, iter_data); 1039 } else { 1040 list_for_each_entry(sdata, &local->interfaces, list) 1041 list_for_each_entry_safe(key, tmp, 1042 &sdata->key_list, list) 1043 ieee80211_key_iter(hw, &sdata->vif, key, 1044 iter, iter_data); 1045 } 1046 } 1047 EXPORT_SYMBOL(ieee80211_iter_keys); 1048 1049 static void 1050 _ieee80211_iter_keys_rcu(struct ieee80211_hw *hw, 1051 struct ieee80211_sub_if_data *sdata, 1052 void (*iter)(struct ieee80211_hw *hw, 1053 struct ieee80211_vif *vif, 1054 struct ieee80211_sta *sta, 1055 struct ieee80211_key_conf *key, 1056 void *data), 1057 void *iter_data) 1058 { 1059 struct ieee80211_key *key; 1060 1061 list_for_each_entry_rcu(key, &sdata->key_list, list) 1062 ieee80211_key_iter(hw, &sdata->vif, key, iter, iter_data); 1063 } 1064 1065 void ieee80211_iter_keys_rcu(struct ieee80211_hw *hw, 1066 struct ieee80211_vif *vif, 1067 void (*iter)(struct ieee80211_hw *hw, 1068 struct ieee80211_vif *vif, 1069 struct ieee80211_sta *sta, 1070 struct ieee80211_key_conf *key, 1071 void *data), 1072 void *iter_data) 1073 { 1074 struct ieee80211_local *local = hw_to_local(hw); 1075 struct ieee80211_sub_if_data *sdata; 1076 1077 if (vif) { 1078 sdata = vif_to_sdata(vif); 1079 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data); 1080 } else { 1081 list_for_each_entry_rcu(sdata, &local->interfaces, list) 1082 _ieee80211_iter_keys_rcu(hw, sdata, iter, iter_data); 1083 } 1084 } 1085 EXPORT_SYMBOL(ieee80211_iter_keys_rcu); 1086 1087 static void ieee80211_free_keys_iface(struct ieee80211_sub_if_data *sdata, 1088 struct list_head *keys) 1089 { 1090 struct ieee80211_key *key, *tmp; 1091 1092 decrease_tailroom_need_count(sdata, 1093 sdata->crypto_tx_tailroom_pending_dec); 1094 sdata->crypto_tx_tailroom_pending_dec = 0; 1095 1096 ieee80211_debugfs_key_remove_mgmt_default(sdata); 1097 ieee80211_debugfs_key_remove_beacon_default(sdata); 1098 1099 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) { 1100 ieee80211_key_replace(key->sdata, NULL, key->sta, 1101 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE, 1102 key, NULL); 1103 list_add_tail(&key->list, keys); 1104 } 1105 1106 ieee80211_debugfs_key_update_default(sdata); 1107 } 1108 1109 void ieee80211_remove_link_keys(struct ieee80211_link_data *link, 1110 struct list_head *keys) 1111 { 1112 struct ieee80211_sub_if_data *sdata = link->sdata; 1113 struct ieee80211_local *local = sdata->local; 1114 struct ieee80211_key *key, *tmp; 1115 1116 lockdep_assert_wiphy(local->hw.wiphy); 1117 1118 list_for_each_entry_safe(key, tmp, &sdata->key_list, list) { 1119 if (key->conf.link_id != link->link_id) 1120 continue; 1121 ieee80211_key_replace(key->sdata, link, key->sta, 1122 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE, 1123 key, NULL); 1124 list_add_tail(&key->list, keys); 1125 } 1126 } 1127 1128 void ieee80211_free_key_list(struct ieee80211_local *local, 1129 struct list_head *keys) 1130 { 1131 struct ieee80211_key *key, *tmp; 1132 1133 lockdep_assert_wiphy(local->hw.wiphy); 1134 1135 list_for_each_entry_safe(key, tmp, keys, list) 1136 __ieee80211_key_destroy(key, false); 1137 } 1138 1139 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata, 1140 bool force_synchronize) 1141 { 1142 struct ieee80211_local *local = sdata->local; 1143 struct ieee80211_sub_if_data *vlan; 1144 struct ieee80211_sub_if_data *master; 1145 struct ieee80211_key *key, *tmp; 1146 LIST_HEAD(keys); 1147 1148 wiphy_delayed_work_cancel(local->hw.wiphy, 1149 &sdata->dec_tailroom_needed_wk); 1150 1151 lockdep_assert_wiphy(local->hw.wiphy); 1152 1153 ieee80211_free_keys_iface(sdata, &keys); 1154 1155 if (sdata->vif.type == NL80211_IFTYPE_AP) { 1156 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) 1157 ieee80211_free_keys_iface(vlan, &keys); 1158 } 1159 1160 if (!list_empty(&keys) || force_synchronize) 1161 synchronize_net(); 1162 list_for_each_entry_safe(key, tmp, &keys, list) 1163 __ieee80211_key_destroy(key, false); 1164 1165 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 1166 if (sdata->bss) { 1167 master = container_of(sdata->bss, 1168 struct ieee80211_sub_if_data, 1169 u.ap); 1170 1171 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt != 1172 master->crypto_tx_tailroom_needed_cnt); 1173 } 1174 } else { 1175 WARN_ON_ONCE(sdata->crypto_tx_tailroom_needed_cnt || 1176 sdata->crypto_tx_tailroom_pending_dec); 1177 } 1178 1179 if (sdata->vif.type == NL80211_IFTYPE_AP) { 1180 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) 1181 WARN_ON_ONCE(vlan->crypto_tx_tailroom_needed_cnt || 1182 vlan->crypto_tx_tailroom_pending_dec); 1183 } 1184 } 1185 1186 void ieee80211_free_sta_keys(struct ieee80211_local *local, 1187 struct sta_info *sta) 1188 { 1189 struct ieee80211_key *key; 1190 int i; 1191 1192 lockdep_assert_wiphy(local->hw.wiphy); 1193 1194 for (i = 0; i < ARRAY_SIZE(sta->deflink.gtk); i++) { 1195 key = wiphy_dereference(local->hw.wiphy, sta->deflink.gtk[i]); 1196 if (!key) 1197 continue; 1198 ieee80211_key_replace(key->sdata, NULL, key->sta, 1199 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE, 1200 key, NULL); 1201 __ieee80211_key_destroy(key, key->sdata->vif.type == 1202 NL80211_IFTYPE_STATION); 1203 } 1204 1205 for (i = 0; i < NUM_DEFAULT_KEYS; i++) { 1206 key = wiphy_dereference(local->hw.wiphy, sta->ptk[i]); 1207 if (!key) 1208 continue; 1209 ieee80211_key_replace(key->sdata, NULL, key->sta, 1210 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE, 1211 key, NULL); 1212 __ieee80211_key_destroy(key, key->sdata->vif.type == 1213 NL80211_IFTYPE_STATION); 1214 } 1215 } 1216 1217 void ieee80211_delayed_tailroom_dec(struct wiphy *wiphy, 1218 struct wiphy_work *wk) 1219 { 1220 struct ieee80211_sub_if_data *sdata; 1221 1222 sdata = container_of(wk, struct ieee80211_sub_if_data, 1223 dec_tailroom_needed_wk.work); 1224 1225 /* 1226 * The reason for the delayed tailroom needed decrementing is to 1227 * make roaming faster: during roaming, all keys are first deleted 1228 * and then new keys are installed. The first new key causes the 1229 * crypto_tx_tailroom_needed_cnt to go from 0 to 1, which invokes 1230 * the cost of synchronize_net() (which can be slow). Avoid this 1231 * by deferring the crypto_tx_tailroom_needed_cnt decrementing on 1232 * key removal for a while, so if we roam the value is larger than 1233 * zero and no 0->1 transition happens. 1234 * 1235 * The cost is that if the AP switching was from an AP with keys 1236 * to one without, we still allocate tailroom while it would no 1237 * longer be needed. However, in the typical (fast) roaming case 1238 * within an ESS this usually won't happen. 1239 */ 1240 1241 decrease_tailroom_need_count(sdata, 1242 sdata->crypto_tx_tailroom_pending_dec); 1243 sdata->crypto_tx_tailroom_pending_dec = 0; 1244 } 1245 1246 void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid, 1247 const u8 *replay_ctr, gfp_t gfp) 1248 { 1249 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 1250 1251 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr); 1252 1253 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp); 1254 } 1255 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify); 1256 1257 void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf, 1258 int tid, struct ieee80211_key_seq *seq) 1259 { 1260 struct ieee80211_key *key; 1261 const u8 *pn; 1262 1263 key = container_of(keyconf, struct ieee80211_key, conf); 1264 1265 switch (key->conf.cipher) { 1266 case WLAN_CIPHER_SUITE_TKIP: 1267 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS)) 1268 return; 1269 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32; 1270 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16; 1271 break; 1272 case WLAN_CIPHER_SUITE_CCMP: 1273 case WLAN_CIPHER_SUITE_CCMP_256: 1274 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS)) 1275 return; 1276 if (tid < 0) 1277 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS]; 1278 else 1279 pn = key->u.ccmp.rx_pn[tid]; 1280 memcpy(seq->ccmp.pn, pn, IEEE80211_CCMP_PN_LEN); 1281 break; 1282 case WLAN_CIPHER_SUITE_AES_CMAC: 1283 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 1284 if (WARN_ON(tid != 0)) 1285 return; 1286 pn = key->u.aes_cmac.rx_pn; 1287 memcpy(seq->aes_cmac.pn, pn, IEEE80211_CMAC_PN_LEN); 1288 break; 1289 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 1290 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 1291 if (WARN_ON(tid != 0)) 1292 return; 1293 pn = key->u.aes_gmac.rx_pn; 1294 memcpy(seq->aes_gmac.pn, pn, IEEE80211_GMAC_PN_LEN); 1295 break; 1296 case WLAN_CIPHER_SUITE_GCMP: 1297 case WLAN_CIPHER_SUITE_GCMP_256: 1298 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS)) 1299 return; 1300 if (tid < 0) 1301 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS]; 1302 else 1303 pn = key->u.gcmp.rx_pn[tid]; 1304 memcpy(seq->gcmp.pn, pn, IEEE80211_GCMP_PN_LEN); 1305 break; 1306 } 1307 } 1308 EXPORT_SYMBOL(ieee80211_get_key_rx_seq); 1309 1310 void ieee80211_set_key_rx_seq(struct ieee80211_key_conf *keyconf, 1311 int tid, struct ieee80211_key_seq *seq) 1312 { 1313 struct ieee80211_key *key; 1314 u8 *pn; 1315 1316 key = container_of(keyconf, struct ieee80211_key, conf); 1317 1318 switch (key->conf.cipher) { 1319 case WLAN_CIPHER_SUITE_TKIP: 1320 if (WARN_ON(tid < 0 || tid >= IEEE80211_NUM_TIDS)) 1321 return; 1322 key->u.tkip.rx[tid].iv32 = seq->tkip.iv32; 1323 key->u.tkip.rx[tid].iv16 = seq->tkip.iv16; 1324 break; 1325 case WLAN_CIPHER_SUITE_CCMP: 1326 case WLAN_CIPHER_SUITE_CCMP_256: 1327 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS)) 1328 return; 1329 if (tid < 0) 1330 pn = key->u.ccmp.rx_pn[IEEE80211_NUM_TIDS]; 1331 else 1332 pn = key->u.ccmp.rx_pn[tid]; 1333 memcpy(pn, seq->ccmp.pn, IEEE80211_CCMP_PN_LEN); 1334 break; 1335 case WLAN_CIPHER_SUITE_AES_CMAC: 1336 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 1337 if (WARN_ON(tid != 0)) 1338 return; 1339 pn = key->u.aes_cmac.rx_pn; 1340 memcpy(pn, seq->aes_cmac.pn, IEEE80211_CMAC_PN_LEN); 1341 break; 1342 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 1343 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 1344 if (WARN_ON(tid != 0)) 1345 return; 1346 pn = key->u.aes_gmac.rx_pn; 1347 memcpy(pn, seq->aes_gmac.pn, IEEE80211_GMAC_PN_LEN); 1348 break; 1349 case WLAN_CIPHER_SUITE_GCMP: 1350 case WLAN_CIPHER_SUITE_GCMP_256: 1351 if (WARN_ON(tid < -1 || tid >= IEEE80211_NUM_TIDS)) 1352 return; 1353 if (tid < 0) 1354 pn = key->u.gcmp.rx_pn[IEEE80211_NUM_TIDS]; 1355 else 1356 pn = key->u.gcmp.rx_pn[tid]; 1357 memcpy(pn, seq->gcmp.pn, IEEE80211_GCMP_PN_LEN); 1358 break; 1359 default: 1360 WARN_ON(1); 1361 break; 1362 } 1363 } 1364 EXPORT_SYMBOL_GPL(ieee80211_set_key_rx_seq); 1365 1366 struct ieee80211_key_conf * 1367 ieee80211_gtk_rekey_add(struct ieee80211_vif *vif, 1368 u8 idx, u8 *key_data, u8 key_len, 1369 int link_id) 1370 { 1371 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 1372 struct ieee80211_local *local = sdata->local; 1373 struct ieee80211_key *prev_key; 1374 struct ieee80211_key *key; 1375 int err; 1376 struct ieee80211_link_data *link_data = 1377 link_id < 0 ? &sdata->deflink : 1378 sdata_dereference(sdata->link[link_id], sdata); 1379 1380 if (WARN_ON(!link_data)) 1381 return ERR_PTR(-EINVAL); 1382 1383 if (WARN_ON(!local->wowlan)) 1384 return ERR_PTR(-EINVAL); 1385 1386 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION)) 1387 return ERR_PTR(-EINVAL); 1388 1389 if (WARN_ON(idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS + 1390 NUM_DEFAULT_BEACON_KEYS)) 1391 return ERR_PTR(-EINVAL); 1392 1393 prev_key = wiphy_dereference(local->hw.wiphy, 1394 link_data->gtk[idx]); 1395 if (!prev_key) { 1396 if (idx < NUM_DEFAULT_KEYS) { 1397 for (int i = 0; i < NUM_DEFAULT_KEYS; i++) { 1398 if (i == idx) 1399 continue; 1400 prev_key = wiphy_dereference(local->hw.wiphy, 1401 link_data->gtk[i]); 1402 if (prev_key) 1403 break; 1404 } 1405 } else { 1406 /* For IGTK we have 4 and 5 and for BIGTK - 6 and 7 */ 1407 prev_key = wiphy_dereference(local->hw.wiphy, 1408 link_data->gtk[idx ^ 1]); 1409 } 1410 } 1411 1412 if (WARN_ON(!prev_key)) 1413 return ERR_PTR(-EINVAL); 1414 1415 if (WARN_ON(key_len < prev_key->conf.keylen)) 1416 return ERR_PTR(-EINVAL); 1417 1418 key = ieee80211_key_alloc(prev_key->conf.cipher, idx, 1419 prev_key->conf.keylen, key_data, 1420 0, NULL); 1421 if (IS_ERR(key)) 1422 return ERR_CAST(key); 1423 1424 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED) 1425 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT; 1426 1427 key->conf.link_id = link_data->link_id; 1428 1429 err = ieee80211_key_link(key, link_data, NULL); 1430 if (err) 1431 return ERR_PTR(err); 1432 1433 return &key->conf; 1434 } 1435 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_add); 1436 1437 void ieee80211_key_mic_failure(struct ieee80211_key_conf *keyconf) 1438 { 1439 struct ieee80211_key *key; 1440 1441 key = container_of(keyconf, struct ieee80211_key, conf); 1442 1443 switch (key->conf.cipher) { 1444 case WLAN_CIPHER_SUITE_AES_CMAC: 1445 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 1446 key->u.aes_cmac.icverrors++; 1447 break; 1448 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 1449 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 1450 key->u.aes_gmac.icverrors++; 1451 break; 1452 default: 1453 /* ignore the others for now, we don't keep counters now */ 1454 break; 1455 } 1456 } 1457 EXPORT_SYMBOL_GPL(ieee80211_key_mic_failure); 1458 1459 void ieee80211_key_replay(struct ieee80211_key_conf *keyconf) 1460 { 1461 struct ieee80211_key *key; 1462 1463 key = container_of(keyconf, struct ieee80211_key, conf); 1464 1465 switch (key->conf.cipher) { 1466 case WLAN_CIPHER_SUITE_CCMP: 1467 case WLAN_CIPHER_SUITE_CCMP_256: 1468 key->u.ccmp.replays++; 1469 break; 1470 case WLAN_CIPHER_SUITE_AES_CMAC: 1471 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 1472 key->u.aes_cmac.replays++; 1473 break; 1474 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 1475 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 1476 key->u.aes_gmac.replays++; 1477 break; 1478 case WLAN_CIPHER_SUITE_GCMP: 1479 case WLAN_CIPHER_SUITE_GCMP_256: 1480 key->u.gcmp.replays++; 1481 break; 1482 } 1483 } 1484 EXPORT_SYMBOL_GPL(ieee80211_key_replay); 1485 1486 int ieee80211_key_switch_links(struct ieee80211_sub_if_data *sdata, 1487 unsigned long del_links_mask, 1488 unsigned long add_links_mask) 1489 { 1490 struct ieee80211_key *key; 1491 int ret; 1492 1493 list_for_each_entry(key, &sdata->key_list, list) { 1494 if (key->conf.link_id < 0 || 1495 !(del_links_mask & BIT(key->conf.link_id))) 1496 continue; 1497 1498 /* shouldn't happen for per-link keys */ 1499 WARN_ON(key->sta); 1500 1501 ieee80211_key_disable_hw_accel(key); 1502 } 1503 1504 list_for_each_entry(key, &sdata->key_list, list) { 1505 if (key->conf.link_id < 0 || 1506 !(add_links_mask & BIT(key->conf.link_id))) 1507 continue; 1508 1509 /* shouldn't happen for per-link keys */ 1510 WARN_ON(key->sta); 1511 1512 ret = ieee80211_key_enable_hw_accel(key); 1513 if (ret) 1514 return ret; 1515 } 1516 1517 return 0; 1518 } 1519