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