00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <linux/if_ether.h>
00013 #include <linux/etherdevice.h>
00014 #include <linux/list.h>
00015 #include <linux/rcupdate.h>
00016 #include <linux/rtnetlink.h>
00017 #include <linux/slab.h>
00018 #include <linux/export.h>
00019 #include <net/mac80211.h>
00020 #include <asm/unaligned.h>
00021 #include "ieee80211_i.h"
00022 #include "driver-ops.h"
00023 #include "debugfs_key.h"
00024 #include "aes_ccm.h"
00025 #include "aes_cmac.h"
00026
00027
00051 static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
00052
00053 static void assert_key_lock(struct ieee80211_local *local)
00054 {
00055 lockdep_assert_held(&local->key_mtx);
00056 }
00057
00058 static void increment_tailroom_need_count(struct ieee80211_sub_if_data *sdata)
00059 {
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 if (!sdata->crypto_tx_tailroom_needed_cnt++) {
00080
00081
00082
00083
00084 synchronize_net();
00085 }
00086 }
00087
00088 static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
00089 {
00090 struct ieee80211_sub_if_data *sdata;
00091 struct sta_info *sta;
00092 int ret;
00093
00094 might_sleep();
00095
00096 if (!key->local->ops->set_key)
00097 goto out_unsupported;
00098
00099 assert_key_lock(key->local);
00100
00101 sta = key->sta;
00102
00103
00104
00105
00106
00107 if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
00108 !(key->local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK))
00109 goto out_unsupported;
00110
00111 if (sta && !sta->uploaded)
00112 goto out_unsupported;
00113
00114 sdata = key->sdata;
00115 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
00116
00117
00118
00119
00120 if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
00121 goto out_unsupported;
00122 }
00123
00124 ret = drv_set_key(key->local, SET_KEY, sdata,
00125 sta ? &sta->sta : NULL, &key->conf);
00126
00127 if (!ret) {
00128 key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
00129
00130 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
00131 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) ||
00132 (key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)))
00133 sdata->crypto_tx_tailroom_needed_cnt--;
00134
00135 WARN_ON((key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
00136 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV));
00137
00138 return 0;
00139 }
00140
00141 if (ret != -ENOSPC && ret != -EOPNOTSUPP)
00142 wiphy_err(key->local->hw.wiphy,
00143 "failed to set key (%d, %pM) to hardware (%d)\n",
00144 key->conf.keyidx,
00145 sta ? sta->sta.addr : bcast_addr, ret);
00146
00147 out_unsupported:
00148 switch (key->conf.cipher) {
00149 case WLAN_CIPHER_SUITE_WEP40:
00150 case WLAN_CIPHER_SUITE_WEP104:
00151 case WLAN_CIPHER_SUITE_TKIP:
00152 case WLAN_CIPHER_SUITE_CCMP:
00153 case WLAN_CIPHER_SUITE_AES_CMAC:
00154
00155 return 0;
00156 default:
00157 return -EINVAL;
00158 }
00159 }
00160
00161 static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
00162 {
00163 struct ieee80211_sub_if_data *sdata;
00164 struct sta_info *sta;
00165 int ret;
00166
00167 might_sleep();
00168
00169 if (!key || !key->local->ops->set_key)
00170 return;
00171
00172 assert_key_lock(key->local);
00173
00174 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
00175 return;
00176
00177 sta = key->sta;
00178 sdata = key->sdata;
00179
00180 if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
00181 (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) ||
00182 (key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)))
00183 increment_tailroom_need_count(sdata);
00184
00185 ret = drv_set_key(key->local, DISABLE_KEY, sdata,
00186 sta ? &sta->sta : NULL, &key->conf);
00187
00188 if (ret)
00189 wiphy_err(key->local->hw.wiphy,
00190 "failed to remove key (%d, %pM) from hardware (%d)\n",
00191 key->conf.keyidx,
00192 sta ? sta->sta.addr : bcast_addr, ret);
00193
00194 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
00195 }
00196
00197 void ieee80211_key_removed(struct ieee80211_key_conf *key_conf)
00198 {
00199 struct ieee80211_key *key;
00200
00201 key = container_of(key_conf, struct ieee80211_key, conf);
00202
00203 might_sleep();
00204 assert_key_lock(key->local);
00205
00206 key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
00207
00208
00209
00210
00211
00212
00213 synchronize_rcu();
00214 }
00215 EXPORT_SYMBOL_GPL(ieee80211_key_removed);
00216
00217 static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
00218 int idx, bool uni, bool multi)
00219 {
00220 struct ieee80211_key *key = NULL;
00221
00222 assert_key_lock(sdata->local);
00223
00224 if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
00225 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
00226
00227 if (uni)
00228 rcu_assign_pointer(sdata->default_unicast_key, key);
00229 if (multi)
00230 rcu_assign_pointer(sdata->default_multicast_key, key);
00231
00232 ieee80211_debugfs_key_update_default(sdata);
00233 }
00234
00235 void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
00236 bool uni, bool multi)
00237 {
00238 mutex_lock(&sdata->local->key_mtx);
00239 __ieee80211_set_default_key(sdata, idx, uni, multi);
00240 mutex_unlock(&sdata->local->key_mtx);
00241 }
00242
00243 static void
00244 __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
00245 {
00246 struct ieee80211_key *key = NULL;
00247
00248 assert_key_lock(sdata->local);
00249
00250 if (idx >= NUM_DEFAULT_KEYS &&
00251 idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
00252 key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
00253
00254 rcu_assign_pointer(sdata->default_mgmt_key, key);
00255
00256 ieee80211_debugfs_key_update_default(sdata);
00257 }
00258
00259 void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
00260 int idx)
00261 {
00262 mutex_lock(&sdata->local->key_mtx);
00263 __ieee80211_set_default_mgmt_key(sdata, idx);
00264 mutex_unlock(&sdata->local->key_mtx);
00265 }
00266
00267
00268 static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
00269 struct sta_info *sta,
00270 bool pairwise,
00271 struct ieee80211_key *old,
00272 struct ieee80211_key *new)
00273 {
00274 int idx;
00275 bool defunikey, defmultikey, defmgmtkey;
00276
00277 if (new)
00278 list_add_tail(&new->list, &sdata->key_list);
00279
00280 if (sta && pairwise) {
00281 rcu_assign_pointer(sta->ptk, new);
00282 } else if (sta) {
00283 if (old)
00284 idx = old->conf.keyidx;
00285 else
00286 idx = new->conf.keyidx;
00287 rcu_assign_pointer(sta->gtk[idx], new);
00288 } else {
00289 WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
00290
00291 if (old)
00292 idx = old->conf.keyidx;
00293 else
00294 idx = new->conf.keyidx;
00295
00296 defunikey = old &&
00297 old == key_mtx_dereference(sdata->local,
00298 sdata->default_unicast_key);
00299 defmultikey = old &&
00300 old == key_mtx_dereference(sdata->local,
00301 sdata->default_multicast_key);
00302 defmgmtkey = old &&
00303 old == key_mtx_dereference(sdata->local,
00304 sdata->default_mgmt_key);
00305
00306 if (defunikey && !new)
00307 __ieee80211_set_default_key(sdata, -1, true, false);
00308 if (defmultikey && !new)
00309 __ieee80211_set_default_key(sdata, -1, false, true);
00310 if (defmgmtkey && !new)
00311 __ieee80211_set_default_mgmt_key(sdata, -1);
00312
00313 rcu_assign_pointer(sdata->keys[idx], new);
00314 if (defunikey && new)
00315 __ieee80211_set_default_key(sdata, new->conf.keyidx,
00316 true, false);
00317 if (defmultikey && new)
00318 __ieee80211_set_default_key(sdata, new->conf.keyidx,
00319 false, true);
00320 if (defmgmtkey && new)
00321 __ieee80211_set_default_mgmt_key(sdata,
00322 new->conf.keyidx);
00323 }
00324
00325 if (old)
00326 list_del(&old->list);
00327 }
00328
00329 struct ieee80211_key *ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
00330 const u8 *key_data,
00331 size_t seq_len, const u8 *seq)
00332 {
00333 struct ieee80211_key *key;
00334 int i, j, err;
00335
00336 BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS);
00337
00338 key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
00339 if (!key)
00340 return ERR_PTR(-ENOMEM);
00341
00342
00343
00344
00345
00346 key->conf.flags = 0;
00347 key->flags = 0;
00348
00349 key->conf.cipher = cipher;
00350 key->conf.keyidx = idx;
00351 key->conf.keylen = key_len;
00352 switch (cipher) {
00353 case WLAN_CIPHER_SUITE_WEP40:
00354 case WLAN_CIPHER_SUITE_WEP104:
00355 key->conf.iv_len = WEP_IV_LEN;
00356 key->conf.icv_len = WEP_ICV_LEN;
00357 break;
00358 case WLAN_CIPHER_SUITE_TKIP:
00359 key->conf.iv_len = TKIP_IV_LEN;
00360 key->conf.icv_len = TKIP_ICV_LEN;
00361 if (seq) {
00362 for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
00363 key->u.tkip.rx[i].iv32 =
00364 get_unaligned_le32(&seq[2]);
00365 key->u.tkip.rx[i].iv16 =
00366 get_unaligned_le16(seq);
00367 }
00368 }
00369 spin_lock_init(&key->u.tkip.txlock);
00370 break;
00371 case WLAN_CIPHER_SUITE_CCMP:
00372 key->conf.iv_len = CCMP_HDR_LEN;
00373 key->conf.icv_len = CCMP_MIC_LEN;
00374 if (seq) {
00375 for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++)
00376 for (j = 0; j < CCMP_PN_LEN; j++)
00377 key->u.ccmp.rx_pn[i][j] =
00378 seq[CCMP_PN_LEN - j - 1];
00379 }
00380
00381
00382
00383
00384 key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
00385 if (IS_ERR(key->u.ccmp.tfm)) {
00386 err = PTR_ERR(key->u.ccmp.tfm);
00387 kfree(key);
00388 return ERR_PTR(err);
00389 }
00390 break;
00391 case WLAN_CIPHER_SUITE_AES_CMAC:
00392 key->conf.iv_len = 0;
00393 key->conf.icv_len = sizeof(struct ieee80211_mmie);
00394 if (seq)
00395 for (j = 0; j < 6; j++)
00396 key->u.aes_cmac.rx_pn[j] = seq[6 - j - 1];
00397
00398
00399
00400
00401 key->u.aes_cmac.tfm =
00402 ieee80211_aes_cmac_key_setup(key_data);
00403 if (IS_ERR(key->u.aes_cmac.tfm)) {
00404 err = PTR_ERR(key->u.aes_cmac.tfm);
00405 kfree(key);
00406 return ERR_PTR(err);
00407 }
00408 break;
00409 }
00410 memcpy(key->conf.key, key_data, key_len);
00411 INIT_LIST_HEAD(&key->list);
00412
00413 return key;
00414 }
00415
00416 static void __ieee80211_key_destroy(struct ieee80211_key *key)
00417 {
00418 if (!key)
00419 return;
00420
00421
00422
00423
00424
00425 synchronize_rcu();
00426
00427 if (key->local)
00428 ieee80211_key_disable_hw_accel(key);
00429
00430 if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
00431 ieee80211_aes_key_free(key->u.ccmp.tfm);
00432 if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
00433 ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
00434 if (key->local) {
00435 ieee80211_debugfs_key_remove(key);
00436 key->sdata->crypto_tx_tailroom_needed_cnt--;
00437 }
00438
00439 kfree(key);
00440 }
00441
00442 int ieee80211_key_link(struct ieee80211_key *key,
00443 struct ieee80211_sub_if_data *sdata,
00444 struct sta_info *sta)
00445 {
00446 struct ieee80211_key *old_key;
00447 int idx, ret;
00448 bool pairwise;
00449
00450 BUG_ON(!sdata);
00451 BUG_ON(!key);
00452
00453 pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
00454 idx = key->conf.keyidx;
00455 key->local = sdata->local;
00456 key->sdata = sdata;
00457 key->sta = sta;
00458
00459 if (sta) {
00460
00461
00462
00463
00464 if (test_sta_flag(sta, WLAN_STA_WME))
00465 key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
00466 } else {
00467 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
00468 struct sta_info *ap;
00469
00470
00471
00472
00473
00474
00475
00476 ap = sta_info_get(key->sdata, key->sdata->u.mgd.bssid);
00477 if (ap) {
00478 if (test_sta_flag(ap, WLAN_STA_WME))
00479 key->conf.flags |=
00480 IEEE80211_KEY_FLAG_WMM_STA;
00481 }
00482 }
00483 }
00484
00485 mutex_lock(&sdata->local->key_mtx);
00486
00487 if (sta && pairwise)
00488 old_key = key_mtx_dereference(sdata->local, sta->ptk);
00489 else if (sta)
00490 old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
00491 else
00492 old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
00493
00494 increment_tailroom_need_count(sdata);
00495
00496 __ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
00497 __ieee80211_key_destroy(old_key);
00498
00499 ieee80211_debugfs_key_add(key);
00500
00501 ret = ieee80211_key_enable_hw_accel(key);
00502
00503 mutex_unlock(&sdata->local->key_mtx);
00504
00505 return ret;
00506 }
00507
00508 void __ieee80211_key_free(struct ieee80211_key *key)
00509 {
00510 if (!key)
00511 return;
00512
00513
00514
00515
00516 if (key->sdata)
00517 __ieee80211_key_replace(key->sdata, key->sta,
00518 key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
00519 key, NULL);
00520 __ieee80211_key_destroy(key);
00521 }
00522
00523 void ieee80211_key_free(struct ieee80211_local *local,
00524 struct ieee80211_key *key)
00525 {
00526 mutex_lock(&local->key_mtx);
00527 __ieee80211_key_free(key);
00528 mutex_unlock(&local->key_mtx);
00529 }
00530
00531 void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
00532 {
00533 struct ieee80211_key *key;
00534
00535 ASSERT_RTNL();
00536
00537 if (WARN_ON(!ieee80211_sdata_running(sdata)))
00538 return;
00539
00540 mutex_lock(&sdata->local->key_mtx);
00541
00542 sdata->crypto_tx_tailroom_needed_cnt = 0;
00543
00544 list_for_each_entry(key, &sdata->key_list, list) {
00545 increment_tailroom_need_count(sdata);
00546 ieee80211_key_enable_hw_accel(key);
00547 }
00548
00549 mutex_unlock(&sdata->local->key_mtx);
00550 }
00551
00552 void ieee80211_iter_keys(struct ieee80211_hw *hw,
00553 struct ieee80211_vif *vif,
00554 void (*iter)(struct ieee80211_hw *hw,
00555 struct ieee80211_vif *vif,
00556 struct ieee80211_sta *sta,
00557 struct ieee80211_key_conf *key,
00558 void *data),
00559 void *iter_data)
00560 {
00561 struct ieee80211_local *local = hw_to_local(hw);
00562 struct ieee80211_key *key;
00563 struct ieee80211_sub_if_data *sdata;
00564
00565 ASSERT_RTNL();
00566
00567 mutex_lock(&local->key_mtx);
00568 if (vif) {
00569 sdata = vif_to_sdata(vif);
00570 list_for_each_entry(key, &sdata->key_list, list)
00571 iter(hw, &sdata->vif,
00572 key->sta ? &key->sta->sta : NULL,
00573 &key->conf, iter_data);
00574 } else {
00575 list_for_each_entry(sdata, &local->interfaces, list)
00576 list_for_each_entry(key, &sdata->key_list, list)
00577 iter(hw, &sdata->vif,
00578 key->sta ? &key->sta->sta : NULL,
00579 &key->conf, iter_data);
00580 }
00581 mutex_unlock(&local->key_mtx);
00582 }
00583 EXPORT_SYMBOL(ieee80211_iter_keys);
00584
00585 void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
00586 {
00587 struct ieee80211_key *key;
00588
00589 ASSERT_RTNL();
00590
00591 mutex_lock(&sdata->local->key_mtx);
00592
00593 list_for_each_entry(key, &sdata->key_list, list)
00594 ieee80211_key_disable_hw_accel(key);
00595
00596 mutex_unlock(&sdata->local->key_mtx);
00597 }
00598
00599 void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
00600 {
00601 struct ieee80211_key *key, *tmp;
00602
00603 mutex_lock(&sdata->local->key_mtx);
00604
00605 ieee80211_debugfs_key_remove_mgmt_default(sdata);
00606
00607 list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
00608 __ieee80211_key_free(key);
00609
00610 ieee80211_debugfs_key_update_default(sdata);
00611
00612 mutex_unlock(&sdata->local->key_mtx);
00613 }
00614
00615
00616 void ieee80211_gtk_rekey_notify(struct ieee80211_vif *vif, const u8 *bssid,
00617 const u8 *replay_ctr, gfp_t gfp)
00618 {
00619 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
00620
00621 trace_api_gtk_rekey_notify(sdata, bssid, replay_ctr);
00622
00623 cfg80211_gtk_rekey_notify(sdata->dev, bssid, replay_ctr, gfp);
00624 }
00625 EXPORT_SYMBOL_GPL(ieee80211_gtk_rekey_notify);
00626
00627 void ieee80211_get_key_tx_seq(struct ieee80211_key_conf *keyconf,
00628 struct ieee80211_key_seq *seq)
00629 {
00630 struct ieee80211_key *key;
00631 u64 pn64;
00632
00633 if (WARN_ON(!(keyconf->flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
00634 return;
00635
00636 key = container_of(keyconf, struct ieee80211_key, conf);
00637
00638 switch (key->conf.cipher) {
00639 case WLAN_CIPHER_SUITE_TKIP:
00640 seq->tkip.iv32 = key->u.tkip.tx.iv32;
00641 seq->tkip.iv16 = key->u.tkip.tx.iv16;
00642 break;
00643 case WLAN_CIPHER_SUITE_CCMP:
00644 pn64 = atomic64_read(&key->u.ccmp.tx_pn);
00645 seq->ccmp.pn[5] = pn64;
00646 seq->ccmp.pn[4] = pn64 >> 8;
00647 seq->ccmp.pn[3] = pn64 >> 16;
00648 seq->ccmp.pn[2] = pn64 >> 24;
00649 seq->ccmp.pn[1] = pn64 >> 32;
00650 seq->ccmp.pn[0] = pn64 >> 40;
00651 break;
00652 case WLAN_CIPHER_SUITE_AES_CMAC:
00653 pn64 = atomic64_read(&key->u.aes_cmac.tx_pn);
00654 seq->ccmp.pn[5] = pn64;
00655 seq->ccmp.pn[4] = pn64 >> 8;
00656 seq->ccmp.pn[3] = pn64 >> 16;
00657 seq->ccmp.pn[2] = pn64 >> 24;
00658 seq->ccmp.pn[1] = pn64 >> 32;
00659 seq->ccmp.pn[0] = pn64 >> 40;
00660 break;
00661 default:
00662 WARN_ON(1);
00663 }
00664 }
00665 EXPORT_SYMBOL(ieee80211_get_key_tx_seq);
00666
00667 void ieee80211_get_key_rx_seq(struct ieee80211_key_conf *keyconf,
00668 int tid, struct ieee80211_key_seq *seq)
00669 {
00670 struct ieee80211_key *key;
00671 const u8 *pn;
00672
00673 key = container_of(keyconf, struct ieee80211_key, conf);
00674
00675 switch (key->conf.cipher) {
00676 case WLAN_CIPHER_SUITE_TKIP:
00677 if (WARN_ON(tid < 0 || tid >= NUM_RX_DATA_QUEUES))
00678 return;
00679 seq->tkip.iv32 = key->u.tkip.rx[tid].iv32;
00680 seq->tkip.iv16 = key->u.tkip.rx[tid].iv16;
00681 break;
00682 case WLAN_CIPHER_SUITE_CCMP:
00683 if (WARN_ON(tid < -1 || tid >= NUM_RX_DATA_QUEUES))
00684 return;
00685 if (tid < 0)
00686 pn = key->u.ccmp.rx_pn[NUM_RX_DATA_QUEUES];
00687 else
00688 pn = key->u.ccmp.rx_pn[tid];
00689 memcpy(seq->ccmp.pn, pn, CCMP_PN_LEN);
00690 break;
00691 case WLAN_CIPHER_SUITE_AES_CMAC:
00692 if (WARN_ON(tid != 0))
00693 return;
00694 pn = key->u.aes_cmac.rx_pn;
00695 memcpy(seq->aes_cmac.pn, pn, CMAC_PN_LEN);
00696 break;
00697 }
00698 }
00699 EXPORT_SYMBOL(ieee80211_get_key_rx_seq);