key.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. return 0;
  89. }
  90. if (ret != -ENOSPC && ret != -EOPNOTSUPP)
  91. wiphy_err(key->local->hw.wiphy,
  92. "failed to set key (%d, %pM) to hardware (%d)\n",
  93. key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
  94. out_unsupported:
  95. switch (key->conf.cipher) {
  96. case WLAN_CIPHER_SUITE_WEP40:
  97. case WLAN_CIPHER_SUITE_WEP104:
  98. case WLAN_CIPHER_SUITE_TKIP:
  99. case WLAN_CIPHER_SUITE_CCMP:
  100. case WLAN_CIPHER_SUITE_AES_CMAC:
  101. /* all of these we can do in software */
  102. return 0;
  103. default:
  104. return -EINVAL;
  105. }
  106. }
  107. static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
  108. {
  109. struct ieee80211_sub_if_data *sdata;
  110. struct ieee80211_sta *sta;
  111. int ret;
  112. might_sleep();
  113. if (!key || !key->local->ops->set_key)
  114. return;
  115. assert_key_lock(key->local);
  116. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  117. return;
  118. sta = get_sta_for_key(key);
  119. sdata = key->sdata;
  120. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  121. sdata = container_of(sdata->bss,
  122. struct ieee80211_sub_if_data,
  123. u.ap);
  124. ret = drv_set_key(key->local, DISABLE_KEY, sdata,
  125. sta, &key->conf);
  126. if (ret)
  127. wiphy_err(key->local->hw.wiphy,
  128. "failed to remove key (%d, %pM) from hardware (%d)\n",
  129. key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
  130. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  131. }
  132. void ieee80211_key_removed(struct ieee80211_key_conf *key_conf)
  133. {
  134. struct ieee80211_key *key;
  135. key = container_of(key_conf, struct ieee80211_key, conf);
  136. might_sleep();
  137. assert_key_lock(key->local);
  138. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  139. /*
  140. * Flush TX path to avoid attempts to use this key
  141. * after this function returns. Until then, drivers
  142. * must be prepared to handle the key.
  143. */
  144. synchronize_rcu();
  145. }
  146. EXPORT_SYMBOL_GPL(ieee80211_key_removed);
  147. static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
  148. int idx, bool uni, bool multi)
  149. {
  150. struct ieee80211_key *key = NULL;
  151. assert_key_lock(sdata->local);
  152. if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
  153. key = sdata->keys[idx];
  154. if (uni)
  155. rcu_assign_pointer(sdata->default_unicast_key, key);
  156. if (multi)
  157. rcu_assign_pointer(sdata->default_multicast_key, key);
  158. ieee80211_debugfs_key_update_default(sdata);
  159. }
  160. void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx,
  161. bool uni, bool multi)
  162. {
  163. mutex_lock(&sdata->local->key_mtx);
  164. __ieee80211_set_default_key(sdata, idx, uni, multi);
  165. mutex_unlock(&sdata->local->key_mtx);
  166. }
  167. static void
  168. __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
  169. {
  170. struct ieee80211_key *key = NULL;
  171. assert_key_lock(sdata->local);
  172. if (idx >= NUM_DEFAULT_KEYS &&
  173. idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  174. key = sdata->keys[idx];
  175. rcu_assign_pointer(sdata->default_mgmt_key, key);
  176. ieee80211_debugfs_key_update_default(sdata);
  177. }
  178. void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
  179. int idx)
  180. {
  181. mutex_lock(&sdata->local->key_mtx);
  182. __ieee80211_set_default_mgmt_key(sdata, idx);
  183. mutex_unlock(&sdata->local->key_mtx);
  184. }
  185. static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
  186. struct sta_info *sta,
  187. bool pairwise,
  188. struct ieee80211_key *old,
  189. struct ieee80211_key *new)
  190. {
  191. int idx;
  192. bool defunikey, defmultikey, defmgmtkey;
  193. if (new)
  194. list_add(&new->list, &sdata->key_list);
  195. if (sta && pairwise) {
  196. rcu_assign_pointer(sta->ptk, new);
  197. } else if (sta) {
  198. if (old)
  199. idx = old->conf.keyidx;
  200. else
  201. idx = new->conf.keyidx;
  202. rcu_assign_pointer(sta->gtk[idx], new);
  203. } else {
  204. WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
  205. if (old)
  206. idx = old->conf.keyidx;
  207. else
  208. idx = new->conf.keyidx;
  209. defunikey = old && sdata->default_unicast_key == old;
  210. defmultikey = old && sdata->default_multicast_key == old;
  211. defmgmtkey = old && sdata->default_mgmt_key == old;
  212. if (defunikey && !new)
  213. __ieee80211_set_default_key(sdata, -1, true, false);
  214. if (defmultikey && !new)
  215. __ieee80211_set_default_key(sdata, -1, false, true);
  216. if (defmgmtkey && !new)
  217. __ieee80211_set_default_mgmt_key(sdata, -1);
  218. rcu_assign_pointer(sdata->keys[idx], new);
  219. if (defunikey && new)
  220. __ieee80211_set_default_key(sdata, new->conf.keyidx,
  221. true, false);
  222. if (defmultikey && new)
  223. __ieee80211_set_default_key(sdata, new->conf.keyidx,
  224. false, true);
  225. if (defmgmtkey && new)
  226. __ieee80211_set_default_mgmt_key(sdata,
  227. new->conf.keyidx);
  228. }
  229. if (old)
  230. list_del(&old->list);
  231. }
  232. struct ieee80211_key *ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
  233. const u8 *key_data,
  234. size_t seq_len, const u8 *seq)
  235. {
  236. struct ieee80211_key *key;
  237. int i, j, err;
  238. BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS);
  239. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  240. if (!key)
  241. return ERR_PTR(-ENOMEM);
  242. /*
  243. * Default to software encryption; we'll later upload the
  244. * key to the hardware if possible.
  245. */
  246. key->conf.flags = 0;
  247. key->flags = 0;
  248. key->conf.cipher = cipher;
  249. key->conf.keyidx = idx;
  250. key->conf.keylen = key_len;
  251. switch (cipher) {
  252. case WLAN_CIPHER_SUITE_WEP40:
  253. case WLAN_CIPHER_SUITE_WEP104:
  254. key->conf.iv_len = WEP_IV_LEN;
  255. key->conf.icv_len = WEP_ICV_LEN;
  256. break;
  257. case WLAN_CIPHER_SUITE_TKIP:
  258. key->conf.iv_len = TKIP_IV_LEN;
  259. key->conf.icv_len = TKIP_ICV_LEN;
  260. if (seq) {
  261. for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
  262. key->u.tkip.rx[i].iv32 =
  263. get_unaligned_le32(&seq[2]);
  264. key->u.tkip.rx[i].iv16 =
  265. get_unaligned_le16(seq);
  266. }
  267. }
  268. break;
  269. case WLAN_CIPHER_SUITE_CCMP:
  270. key->conf.iv_len = CCMP_HDR_LEN;
  271. key->conf.icv_len = CCMP_MIC_LEN;
  272. if (seq) {
  273. for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++)
  274. for (j = 0; j < CCMP_PN_LEN; j++)
  275. key->u.ccmp.rx_pn[i][j] =
  276. seq[CCMP_PN_LEN - j - 1];
  277. }
  278. /*
  279. * Initialize AES key state here as an optimization so that
  280. * it does not need to be initialized for every packet.
  281. */
  282. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
  283. if (IS_ERR(key->u.ccmp.tfm)) {
  284. err = PTR_ERR(key->u.ccmp.tfm);
  285. kfree(key);
  286. key = ERR_PTR(err);
  287. }
  288. break;
  289. case WLAN_CIPHER_SUITE_AES_CMAC:
  290. key->conf.iv_len = 0;
  291. key->conf.icv_len = sizeof(struct ieee80211_mmie);
  292. if (seq)
  293. for (j = 0; j < 6; j++)
  294. key->u.aes_cmac.rx_pn[j] = seq[6 - j - 1];
  295. /*
  296. * Initialize AES key state here as an optimization so that
  297. * it does not need to be initialized for every packet.
  298. */
  299. key->u.aes_cmac.tfm =
  300. ieee80211_aes_cmac_key_setup(key_data);
  301. if (IS_ERR(key->u.aes_cmac.tfm)) {
  302. err = PTR_ERR(key->u.aes_cmac.tfm);
  303. kfree(key);
  304. key = ERR_PTR(err);
  305. }
  306. break;
  307. }
  308. memcpy(key->conf.key, key_data, key_len);
  309. INIT_LIST_HEAD(&key->list);
  310. return key;
  311. }
  312. static void __ieee80211_key_destroy(struct ieee80211_key *key)
  313. {
  314. if (!key)
  315. return;
  316. /*
  317. * Synchronize so the TX path can no longer be using
  318. * this key before we free/remove it.
  319. */
  320. synchronize_rcu();
  321. if (key->local)
  322. ieee80211_key_disable_hw_accel(key);
  323. if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
  324. ieee80211_aes_key_free(key->u.ccmp.tfm);
  325. if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  326. ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
  327. if (key->local)
  328. ieee80211_debugfs_key_remove(key);
  329. kfree(key);
  330. }
  331. int ieee80211_key_link(struct ieee80211_key *key,
  332. struct ieee80211_sub_if_data *sdata,
  333. struct sta_info *sta)
  334. {
  335. struct ieee80211_key *old_key;
  336. int idx, ret;
  337. bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
  338. BUG_ON(!sdata);
  339. BUG_ON(!key);
  340. idx = key->conf.keyidx;
  341. key->local = sdata->local;
  342. key->sdata = sdata;
  343. key->sta = sta;
  344. if (sta) {
  345. /*
  346. * some hardware cannot handle TKIP with QoS, so
  347. * we indicate whether QoS could be in use.
  348. */
  349. if (test_sta_flags(sta, WLAN_STA_WME))
  350. key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
  351. } else {
  352. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  353. struct sta_info *ap;
  354. /*
  355. * We're getting a sta pointer in, so must be under
  356. * appropriate locking for sta_info_get().
  357. */
  358. /* same here, the AP could be using QoS */
  359. ap = sta_info_get(key->sdata, key->sdata->u.mgd.bssid);
  360. if (ap) {
  361. if (test_sta_flags(ap, WLAN_STA_WME))
  362. key->conf.flags |=
  363. IEEE80211_KEY_FLAG_WMM_STA;
  364. }
  365. }
  366. }
  367. mutex_lock(&sdata->local->key_mtx);
  368. if (sta && pairwise)
  369. old_key = sta->ptk;
  370. else if (sta)
  371. old_key = sta->gtk[idx];
  372. else
  373. old_key = sdata->keys[idx];
  374. __ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
  375. __ieee80211_key_destroy(old_key);
  376. ieee80211_debugfs_key_add(key);
  377. ret = ieee80211_key_enable_hw_accel(key);
  378. mutex_unlock(&sdata->local->key_mtx);
  379. return ret;
  380. }
  381. static void __ieee80211_key_free(struct ieee80211_key *key)
  382. {
  383. /*
  384. * Replace key with nothingness if it was ever used.
  385. */
  386. if (key->sdata)
  387. __ieee80211_key_replace(key->sdata, key->sta,
  388. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  389. key, NULL);
  390. __ieee80211_key_destroy(key);
  391. }
  392. void ieee80211_key_free(struct ieee80211_local *local,
  393. struct ieee80211_key *key)
  394. {
  395. if (!key)
  396. return;
  397. mutex_lock(&local->key_mtx);
  398. __ieee80211_key_free(key);
  399. mutex_unlock(&local->key_mtx);
  400. }
  401. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  402. {
  403. struct ieee80211_key *key;
  404. ASSERT_RTNL();
  405. if (WARN_ON(!ieee80211_sdata_running(sdata)))
  406. return;
  407. mutex_lock(&sdata->local->key_mtx);
  408. list_for_each_entry(key, &sdata->key_list, list)
  409. ieee80211_key_enable_hw_accel(key);
  410. mutex_unlock(&sdata->local->key_mtx);
  411. }
  412. void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
  413. {
  414. struct ieee80211_key *key;
  415. ASSERT_RTNL();
  416. mutex_lock(&sdata->local->key_mtx);
  417. list_for_each_entry(key, &sdata->key_list, list)
  418. ieee80211_key_disable_hw_accel(key);
  419. mutex_unlock(&sdata->local->key_mtx);
  420. }
  421. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
  422. {
  423. struct ieee80211_key *key, *tmp;
  424. mutex_lock(&sdata->local->key_mtx);
  425. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  426. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  427. __ieee80211_key_free(key);
  428. ieee80211_debugfs_key_update_default(sdata);
  429. mutex_unlock(&sdata->local->key_mtx);
  430. }