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