key.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. * Copyright 2007-2008 Johannes Berg <johannes@sipsolutions.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/if_ether.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/list.h>
  14. #include <linux/rcupdate.h>
  15. #include <linux/rtnetlink.h>
  16. #include <linux/slab.h>
  17. #include <net/mac80211.h>
  18. #include "ieee80211_i.h"
  19. #include "driver-ops.h"
  20. #include "debugfs_key.h"
  21. #include "aes_ccm.h"
  22. #include "aes_cmac.h"
  23. /**
  24. * DOC: Key handling basics
  25. *
  26. * Key handling in mac80211 is done based on per-interface (sub_if_data)
  27. * keys and per-station keys. Since each station belongs to an interface,
  28. * each station key also belongs to that interface.
  29. *
  30. * Hardware acceleration is done on a best-effort basis for algorithms
  31. * that are implemented in software, for each key the hardware is asked
  32. * to enable that key for offloading but if it cannot do that the key is
  33. * simply kept for software encryption (unless it is for an algorithm
  34. * that isn't implemented in software).
  35. * There is currently no way of knowing whether a key is handled in SW
  36. * or HW except by looking into debugfs.
  37. *
  38. * All key management is internally protected by a mutex. Within all
  39. * other parts of mac80211, key references are, just as STA structure
  40. * references, protected by RCU. Note, however, that some things are
  41. * unprotected, namely the key->sta dereferences within the hardware
  42. * acceleration functions. This means that sta_info_destroy() must
  43. * remove the key which waits for an RCU grace period.
  44. */
  45. static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  46. static void assert_key_lock(struct ieee80211_local *local)
  47. {
  48. lockdep_assert_held(&local->key_mtx);
  49. }
  50. static struct ieee80211_sta *get_sta_for_key(struct ieee80211_key *key)
  51. {
  52. if (key->sta)
  53. return &key->sta->sta;
  54. return NULL;
  55. }
  56. static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
  57. {
  58. struct ieee80211_sub_if_data *sdata;
  59. struct ieee80211_sta *sta;
  60. int ret;
  61. might_sleep();
  62. if (!key->local->ops->set_key)
  63. goto out_unsupported;
  64. assert_key_lock(key->local);
  65. sta = get_sta_for_key(key);
  66. /*
  67. * If this is a per-STA GTK, check if it
  68. * is supported; if not, return.
  69. */
  70. if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
  71. !(key->local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK))
  72. goto out_unsupported;
  73. sdata = key->sdata;
  74. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  75. /*
  76. * The driver doesn't know anything about VLAN interfaces.
  77. * Hence, don't send GTKs for VLAN interfaces to the driver.
  78. */
  79. if (!(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE))
  80. goto out_unsupported;
  81. sdata = container_of(sdata->bss,
  82. struct ieee80211_sub_if_data,
  83. u.ap);
  84. }
  85. ret = drv_set_key(key->local, SET_KEY, sdata, sta, &key->conf);
  86. if (!ret) {
  87. key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
  88. if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
  89. (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
  90. key->local->crypto_tx_tailroom_needed_cnt--;
  91. return 0;
  92. }
  93. if (ret != -ENOSPC && ret != -EOPNOTSUPP)
  94. wiphy_err(key->local->hw.wiphy,
  95. "failed to set key (%d, %pM) to hardware (%d)\n",
  96. key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
  97. out_unsupported:
  98. switch (key->conf.cipher) {
  99. case WLAN_CIPHER_SUITE_WEP40:
  100. case WLAN_CIPHER_SUITE_WEP104:
  101. case WLAN_CIPHER_SUITE_TKIP:
  102. case WLAN_CIPHER_SUITE_CCMP:
  103. case WLAN_CIPHER_SUITE_AES_CMAC:
  104. /* all of these we can do in software */
  105. return 0;
  106. default:
  107. return -EINVAL;
  108. }
  109. }
  110. static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
  111. {
  112. struct ieee80211_sub_if_data *sdata;
  113. struct ieee80211_sta *sta;
  114. int ret;
  115. might_sleep();
  116. if (!key || !key->local->ops->set_key)
  117. return;
  118. assert_key_lock(key->local);
  119. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  120. return;
  121. sta = get_sta_for_key(key);
  122. sdata = key->sdata;
  123. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  124. sdata = container_of(sdata->bss,
  125. struct ieee80211_sub_if_data,
  126. u.ap);
  127. ret = drv_set_key(key->local, DISABLE_KEY, sdata,
  128. sta, &key->conf);
  129. if (ret)
  130. wiphy_err(key->local->hw.wiphy,
  131. "failed to remove key (%d, %pM) from hardware (%d)\n",
  132. key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
  133. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  134. if (!((key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) ||
  135. (key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)))
  136. key->local->crypto_tx_tailroom_needed_cnt++;
  137. }
  138. void ieee80211_key_removed(struct ieee80211_key_conf *key_conf)
  139. {
  140. struct ieee80211_key *key;
  141. key = container_of(key_conf, struct ieee80211_key, conf);
  142. might_sleep();
  143. assert_key_lock(key->local);
  144. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  145. /*
  146. * Flush TX path to avoid attempts to use this key
  147. * after this function returns. Until then, drivers
  148. * must be prepared to handle the key.
  149. */
  150. synchronize_rcu();
  151. }
  152. EXPORT_SYMBOL_GPL(ieee80211_key_removed);
  153. static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
  154. int idx, bool uni, bool multi)
  155. {
  156. struct ieee80211_key *key = NULL;
  157. assert_key_lock(sdata->local);
  158. if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
  159. key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  160. if (uni)
  161. rcu_assign_pointer(sdata->default_unicast_key, key);
  162. if (multi)
  163. rcu_assign_pointer(sdata->default_multicast_key, key);
  164. ieee80211_debugfs_key_update_default(sdata);
  165. }
  166. void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
  167. bool uni, bool multi)
  168. {
  169. mutex_lock(&sdata->local->key_mtx);
  170. __ieee80211_set_default_key(sdata, idx, uni, multi);
  171. mutex_unlock(&sdata->local->key_mtx);
  172. }
  173. static void
  174. __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
  175. {
  176. struct ieee80211_key *key = NULL;
  177. assert_key_lock(sdata->local);
  178. if (idx >= NUM_DEFAULT_KEYS &&
  179. idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  180. key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  181. rcu_assign_pointer(sdata->default_mgmt_key, key);
  182. ieee80211_debugfs_key_update_default(sdata);
  183. }
  184. void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
  185. int idx)
  186. {
  187. mutex_lock(&sdata->local->key_mtx);
  188. __ieee80211_set_default_mgmt_key(sdata, idx);
  189. mutex_unlock(&sdata->local->key_mtx);
  190. }
  191. static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
  192. struct sta_info *sta,
  193. bool pairwise,
  194. struct ieee80211_key *old,
  195. struct ieee80211_key *new)
  196. {
  197. int idx;
  198. bool defunikey, defmultikey, defmgmtkey;
  199. if (new)
  200. list_add(&new->list, &sdata->key_list);
  201. if (sta && pairwise) {
  202. rcu_assign_pointer(sta->ptk, new);
  203. } else if (sta) {
  204. if (old)
  205. idx = old->conf.keyidx;
  206. else
  207. idx = new->conf.keyidx;
  208. rcu_assign_pointer(sta->gtk[idx], new);
  209. } else {
  210. WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
  211. if (old)
  212. idx = old->conf.keyidx;
  213. else
  214. idx = new->conf.keyidx;
  215. defunikey = old &&
  216. old == key_mtx_dereference(sdata->local,
  217. sdata->default_unicast_key);
  218. defmultikey = old &&
  219. old == key_mtx_dereference(sdata->local,
  220. sdata->default_multicast_key);
  221. defmgmtkey = old &&
  222. old == key_mtx_dereference(sdata->local,
  223. sdata->default_mgmt_key);
  224. if (defunikey && !new)
  225. __ieee80211_set_default_key(sdata, -1, true, false);
  226. if (defmultikey && !new)
  227. __ieee80211_set_default_key(sdata, -1, false, true);
  228. if (defmgmtkey && !new)
  229. __ieee80211_set_default_mgmt_key(sdata, -1);
  230. rcu_assign_pointer(sdata->keys[idx], new);
  231. if (defunikey && new)
  232. __ieee80211_set_default_key(sdata, new->conf.keyidx,
  233. true, false);
  234. if (defmultikey && new)
  235. __ieee80211_set_default_key(sdata, new->conf.keyidx,
  236. false, true);
  237. if (defmgmtkey && new)
  238. __ieee80211_set_default_mgmt_key(sdata,
  239. new->conf.keyidx);
  240. }
  241. if (old)
  242. list_del(&old->list);
  243. }
  244. struct ieee80211_key *ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
  245. const u8 *key_data,
  246. size_t seq_len, const u8 *seq)
  247. {
  248. struct ieee80211_key *key;
  249. int i, j, err;
  250. BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS);
  251. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  252. if (!key)
  253. return ERR_PTR(-ENOMEM);
  254. /*
  255. * Default to software encryption; we'll later upload the
  256. * key to the hardware if possible.
  257. */
  258. key->conf.flags = 0;
  259. key->flags = 0;
  260. key->conf.cipher = cipher;
  261. key->conf.keyidx = idx;
  262. key->conf.keylen = key_len;
  263. switch (cipher) {
  264. case WLAN_CIPHER_SUITE_WEP40:
  265. case WLAN_CIPHER_SUITE_WEP104:
  266. key->conf.iv_len = WEP_IV_LEN;
  267. key->conf.icv_len = WEP_ICV_LEN;
  268. break;
  269. case WLAN_CIPHER_SUITE_TKIP:
  270. key->conf.iv_len = TKIP_IV_LEN;
  271. key->conf.icv_len = TKIP_ICV_LEN;
  272. if (seq) {
  273. for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
  274. key->u.tkip.rx[i].iv32 =
  275. get_unaligned_le32(&seq[2]);
  276. key->u.tkip.rx[i].iv16 =
  277. get_unaligned_le16(seq);
  278. }
  279. }
  280. break;
  281. case WLAN_CIPHER_SUITE_CCMP:
  282. key->conf.iv_len = CCMP_HDR_LEN;
  283. key->conf.icv_len = CCMP_MIC_LEN;
  284. if (seq) {
  285. for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++)
  286. for (j = 0; j < CCMP_PN_LEN; j++)
  287. key->u.ccmp.rx_pn[i][j] =
  288. seq[CCMP_PN_LEN - j - 1];
  289. }
  290. /*
  291. * Initialize AES key state here as an optimization so that
  292. * it does not need to be initialized for every packet.
  293. */
  294. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
  295. if (IS_ERR(key->u.ccmp.tfm)) {
  296. err = PTR_ERR(key->u.ccmp.tfm);
  297. kfree(key);
  298. return ERR_PTR(err);
  299. }
  300. break;
  301. case WLAN_CIPHER_SUITE_AES_CMAC:
  302. key->conf.iv_len = 0;
  303. key->conf.icv_len = sizeof(struct ieee80211_mmie);
  304. if (seq)
  305. for (j = 0; j < 6; j++)
  306. key->u.aes_cmac.rx_pn[j] = seq[6 - j - 1];
  307. /*
  308. * Initialize AES key state here as an optimization so that
  309. * it does not need to be initialized for every packet.
  310. */
  311. key->u.aes_cmac.tfm =
  312. ieee80211_aes_cmac_key_setup(key_data);
  313. if (IS_ERR(key->u.aes_cmac.tfm)) {
  314. err = PTR_ERR(key->u.aes_cmac.tfm);
  315. kfree(key);
  316. return ERR_PTR(err);
  317. }
  318. break;
  319. }
  320. memcpy(key->conf.key, key_data, key_len);
  321. INIT_LIST_HEAD(&key->list);
  322. return key;
  323. }
  324. static void __ieee80211_key_destroy(struct ieee80211_key *key)
  325. {
  326. if (!key)
  327. return;
  328. /*
  329. * Synchronize so the TX path can no longer be using
  330. * this key before we free/remove it.
  331. */
  332. synchronize_rcu();
  333. if (key->local)
  334. ieee80211_key_disable_hw_accel(key);
  335. if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
  336. ieee80211_aes_key_free(key->u.ccmp.tfm);
  337. if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  338. ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
  339. if (key->local) {
  340. ieee80211_debugfs_key_remove(key);
  341. key->local->crypto_tx_tailroom_needed_cnt--;
  342. }
  343. kfree(key);
  344. }
  345. int ieee80211_key_link(struct ieee80211_key *key,
  346. struct ieee80211_sub_if_data *sdata,
  347. struct sta_info *sta)
  348. {
  349. struct ieee80211_key *old_key;
  350. int idx, ret;
  351. bool pairwise;
  352. BUG_ON(!sdata);
  353. BUG_ON(!key);
  354. pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
  355. idx = key->conf.keyidx;
  356. key->local = sdata->local;
  357. key->sdata = sdata;
  358. key->sta = sta;
  359. if (sta) {
  360. /*
  361. * some hardware cannot handle TKIP with QoS, so
  362. * we indicate whether QoS could be in use.
  363. */
  364. if (test_sta_flags(sta, WLAN_STA_WME))
  365. key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
  366. } else {
  367. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  368. struct sta_info *ap;
  369. /*
  370. * We're getting a sta pointer in, so must be under
  371. * appropriate locking for sta_info_get().
  372. */
  373. /* same here, the AP could be using QoS */
  374. ap = sta_info_get(key->sdata, key->sdata->u.mgd.bssid);
  375. if (ap) {
  376. if (test_sta_flags(ap, WLAN_STA_WME))
  377. key->conf.flags |=
  378. IEEE80211_KEY_FLAG_WMM_STA;
  379. }
  380. }
  381. }
  382. mutex_lock(&sdata->local->key_mtx);
  383. if (sta && pairwise)
  384. old_key = key_mtx_dereference(sdata->local, sta->ptk);
  385. else if (sta)
  386. old_key = key_mtx_dereference(sdata->local, sta->gtk[idx]);
  387. else
  388. old_key = key_mtx_dereference(sdata->local, sdata->keys[idx]);
  389. __ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
  390. __ieee80211_key_destroy(old_key);
  391. ieee80211_debugfs_key_add(key);
  392. key->local->crypto_tx_tailroom_needed_cnt++;
  393. ret = ieee80211_key_enable_hw_accel(key);
  394. mutex_unlock(&sdata->local->key_mtx);
  395. return ret;
  396. }
  397. void __ieee80211_key_free(struct ieee80211_key *key)
  398. {
  399. if (!key)
  400. return;
  401. /*
  402. * Replace key with nothingness if it was ever used.
  403. */
  404. if (key->sdata)
  405. __ieee80211_key_replace(key->sdata, key->sta,
  406. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  407. key, NULL);
  408. __ieee80211_key_destroy(key);
  409. }
  410. void ieee80211_key_free(struct ieee80211_local *local,
  411. struct ieee80211_key *key)
  412. {
  413. mutex_lock(&local->key_mtx);
  414. __ieee80211_key_free(key);
  415. mutex_unlock(&local->key_mtx);
  416. }
  417. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  418. {
  419. struct ieee80211_key *key;
  420. ASSERT_RTNL();
  421. if (WARN_ON(!ieee80211_sdata_running(sdata)))
  422. return;
  423. mutex_lock(&sdata->local->key_mtx);
  424. sdata->local->crypto_tx_tailroom_needed_cnt = 0;
  425. list_for_each_entry(key, &sdata->key_list, list) {
  426. sdata->local->crypto_tx_tailroom_needed_cnt++;
  427. ieee80211_key_enable_hw_accel(key);
  428. }
  429. mutex_unlock(&sdata->local->key_mtx);
  430. }
  431. void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
  432. {
  433. struct ieee80211_key *key;
  434. ASSERT_RTNL();
  435. mutex_lock(&sdata->local->key_mtx);
  436. list_for_each_entry(key, &sdata->key_list, list)
  437. ieee80211_key_disable_hw_accel(key);
  438. mutex_unlock(&sdata->local->key_mtx);
  439. }
  440. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
  441. {
  442. struct ieee80211_key *key, *tmp;
  443. mutex_lock(&sdata->local->key_mtx);
  444. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  445. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  446. __ieee80211_key_free(key);
  447. ieee80211_debugfs_key_update_default(sdata);
  448. mutex_unlock(&sdata->local->key_mtx);
  449. }