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