key.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 each key
  31. * that is eligible the hardware is asked to enable that key but if
  32. * it cannot do that they key is simply kept for software encryption.
  33. * There is currently no way of knowing this except by looking into
  34. * debugfs.
  35. *
  36. * All key operations are protected internally.
  37. *
  38. * Within mac80211, key references are, just as STA structure references,
  39. * protected by RCU. Note, however, that some things are unprotected,
  40. * namely the key->sta dereferences within the hardware acceleration
  41. * functions. This means that sta_info_destroy() must remove the key
  42. * which waits for an RCU grace period.
  43. */
  44. static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  45. static void assert_key_lock(struct ieee80211_local *local)
  46. {
  47. lockdep_assert_held(&local->key_mtx);
  48. }
  49. static struct ieee80211_sta *get_sta_for_key(struct ieee80211_key *key)
  50. {
  51. if (key->sta)
  52. return &key->sta->sta;
  53. return NULL;
  54. }
  55. static int ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
  56. {
  57. struct ieee80211_sub_if_data *sdata;
  58. struct ieee80211_sta *sta;
  59. int ret;
  60. might_sleep();
  61. if (!key->local->ops->set_key)
  62. goto out_unsupported;
  63. assert_key_lock(key->local);
  64. sta = get_sta_for_key(key);
  65. /*
  66. * If this is a per-STA GTK, check if it
  67. * is supported; if not, return.
  68. */
  69. if (sta && !(key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE) &&
  70. !(key->local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK))
  71. goto out_unsupported;
  72. sdata = key->sdata;
  73. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  74. sdata = container_of(sdata->bss,
  75. struct ieee80211_sub_if_data,
  76. u.ap);
  77. ret = drv_set_key(key->local, SET_KEY, sdata, sta, &key->conf);
  78. if (!ret) {
  79. key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
  80. return 0;
  81. }
  82. if (ret != -ENOSPC && ret != -EOPNOTSUPP)
  83. wiphy_err(key->local->hw.wiphy,
  84. "failed to set key (%d, %pM) to hardware (%d)\n",
  85. key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
  86. out_unsupported:
  87. switch (key->conf.cipher) {
  88. case WLAN_CIPHER_SUITE_WEP40:
  89. case WLAN_CIPHER_SUITE_WEP104:
  90. case WLAN_CIPHER_SUITE_TKIP:
  91. case WLAN_CIPHER_SUITE_CCMP:
  92. case WLAN_CIPHER_SUITE_AES_CMAC:
  93. /* all of these we can do in software */
  94. return 0;
  95. default:
  96. return -EINVAL;
  97. }
  98. }
  99. static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
  100. {
  101. struct ieee80211_sub_if_data *sdata;
  102. struct ieee80211_sta *sta;
  103. int ret;
  104. might_sleep();
  105. if (!key || !key->local->ops->set_key)
  106. return;
  107. assert_key_lock(key->local);
  108. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  109. return;
  110. sta = get_sta_for_key(key);
  111. sdata = key->sdata;
  112. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  113. sdata = container_of(sdata->bss,
  114. struct ieee80211_sub_if_data,
  115. u.ap);
  116. ret = drv_set_key(key->local, DISABLE_KEY, sdata,
  117. sta, &key->conf);
  118. if (ret)
  119. wiphy_err(key->local->hw.wiphy,
  120. "failed to remove key (%d, %pM) from hardware (%d)\n",
  121. key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
  122. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  123. }
  124. void ieee80211_key_removed(struct ieee80211_key_conf *key_conf)
  125. {
  126. struct ieee80211_key *key;
  127. key = container_of(key_conf, struct ieee80211_key, conf);
  128. might_sleep();
  129. assert_key_lock(key->local);
  130. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  131. /*
  132. * Flush TX path to avoid attempts to use this key
  133. * after this function returns. Until then, drivers
  134. * must be prepared to handle the key.
  135. */
  136. synchronize_rcu();
  137. }
  138. EXPORT_SYMBOL_GPL(ieee80211_key_removed);
  139. static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
  140. int idx)
  141. {
  142. struct ieee80211_key *key = NULL;
  143. assert_key_lock(sdata->local);
  144. if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
  145. key = sdata->keys[idx];
  146. rcu_assign_pointer(sdata->default_key, key);
  147. if (key) {
  148. ieee80211_debugfs_key_remove_default(key->sdata);
  149. ieee80211_debugfs_key_add_default(key->sdata);
  150. }
  151. }
  152. void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
  153. {
  154. mutex_lock(&sdata->local->key_mtx);
  155. __ieee80211_set_default_key(sdata, idx);
  156. mutex_unlock(&sdata->local->key_mtx);
  157. }
  158. static void
  159. __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
  160. {
  161. struct ieee80211_key *key = NULL;
  162. assert_key_lock(sdata->local);
  163. if (idx >= NUM_DEFAULT_KEYS &&
  164. idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  165. key = sdata->keys[idx];
  166. rcu_assign_pointer(sdata->default_mgmt_key, key);
  167. if (key) {
  168. ieee80211_debugfs_key_remove_mgmt_default(key->sdata);
  169. ieee80211_debugfs_key_add_mgmt_default(key->sdata);
  170. }
  171. }
  172. void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
  173. int idx)
  174. {
  175. mutex_lock(&sdata->local->key_mtx);
  176. __ieee80211_set_default_mgmt_key(sdata, idx);
  177. mutex_unlock(&sdata->local->key_mtx);
  178. }
  179. static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
  180. struct sta_info *sta,
  181. bool pairwise,
  182. struct ieee80211_key *old,
  183. struct ieee80211_key *new)
  184. {
  185. int idx, defkey, defmgmtkey;
  186. if (new)
  187. list_add(&new->list, &sdata->key_list);
  188. if (sta && pairwise) {
  189. rcu_assign_pointer(sta->ptk, new);
  190. } else if (sta) {
  191. if (old)
  192. idx = old->conf.keyidx;
  193. else
  194. idx = new->conf.keyidx;
  195. rcu_assign_pointer(sta->gtk[idx], new);
  196. } else {
  197. WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
  198. if (old)
  199. idx = old->conf.keyidx;
  200. else
  201. idx = new->conf.keyidx;
  202. defkey = old && sdata->default_key == old;
  203. defmgmtkey = old && sdata->default_mgmt_key == old;
  204. if (defkey && !new)
  205. __ieee80211_set_default_key(sdata, -1);
  206. if (defmgmtkey && !new)
  207. __ieee80211_set_default_mgmt_key(sdata, -1);
  208. rcu_assign_pointer(sdata->keys[idx], new);
  209. if (defkey && new)
  210. __ieee80211_set_default_key(sdata, new->conf.keyidx);
  211. if (defmgmtkey && new)
  212. __ieee80211_set_default_mgmt_key(sdata,
  213. new->conf.keyidx);
  214. }
  215. if (old) {
  216. /*
  217. * We'll use an empty list to indicate that the key
  218. * has already been removed.
  219. */
  220. list_del_init(&old->list);
  221. }
  222. }
  223. struct ieee80211_key *ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
  224. const u8 *key_data,
  225. size_t seq_len, const u8 *seq)
  226. {
  227. struct ieee80211_key *key;
  228. int i, j, err;
  229. BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS);
  230. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  231. if (!key)
  232. return ERR_PTR(-ENOMEM);
  233. /*
  234. * Default to software encryption; we'll later upload the
  235. * key to the hardware if possible.
  236. */
  237. key->conf.flags = 0;
  238. key->flags = 0;
  239. key->conf.cipher = cipher;
  240. key->conf.keyidx = idx;
  241. key->conf.keylen = key_len;
  242. switch (cipher) {
  243. case WLAN_CIPHER_SUITE_WEP40:
  244. case WLAN_CIPHER_SUITE_WEP104:
  245. key->conf.iv_len = WEP_IV_LEN;
  246. key->conf.icv_len = WEP_ICV_LEN;
  247. break;
  248. case WLAN_CIPHER_SUITE_TKIP:
  249. key->conf.iv_len = TKIP_IV_LEN;
  250. key->conf.icv_len = TKIP_ICV_LEN;
  251. if (seq) {
  252. for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
  253. key->u.tkip.rx[i].iv32 =
  254. get_unaligned_le32(&seq[2]);
  255. key->u.tkip.rx[i].iv16 =
  256. get_unaligned_le16(seq);
  257. }
  258. }
  259. break;
  260. case WLAN_CIPHER_SUITE_CCMP:
  261. key->conf.iv_len = CCMP_HDR_LEN;
  262. key->conf.icv_len = CCMP_MIC_LEN;
  263. if (seq) {
  264. for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++)
  265. for (j = 0; j < CCMP_PN_LEN; j++)
  266. key->u.ccmp.rx_pn[i][j] =
  267. seq[CCMP_PN_LEN - j - 1];
  268. }
  269. /*
  270. * Initialize AES key state here as an optimization so that
  271. * it does not need to be initialized for every packet.
  272. */
  273. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
  274. if (IS_ERR(key->u.ccmp.tfm)) {
  275. err = PTR_ERR(key->u.ccmp.tfm);
  276. kfree(key);
  277. key = ERR_PTR(err);
  278. }
  279. break;
  280. case WLAN_CIPHER_SUITE_AES_CMAC:
  281. key->conf.iv_len = 0;
  282. key->conf.icv_len = sizeof(struct ieee80211_mmie);
  283. if (seq)
  284. for (j = 0; j < 6; j++)
  285. key->u.aes_cmac.rx_pn[j] = seq[6 - j - 1];
  286. /*
  287. * Initialize AES key state here as an optimization so that
  288. * it does not need to be initialized for every packet.
  289. */
  290. key->u.aes_cmac.tfm =
  291. ieee80211_aes_cmac_key_setup(key_data);
  292. if (IS_ERR(key->u.aes_cmac.tfm)) {
  293. err = PTR_ERR(key->u.aes_cmac.tfm);
  294. kfree(key);
  295. key = ERR_PTR(err);
  296. }
  297. break;
  298. }
  299. memcpy(key->conf.key, key_data, key_len);
  300. INIT_LIST_HEAD(&key->list);
  301. return key;
  302. }
  303. static void __ieee80211_key_destroy(struct ieee80211_key *key)
  304. {
  305. if (!key)
  306. return;
  307. if (key->local)
  308. ieee80211_key_disable_hw_accel(key);
  309. if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
  310. ieee80211_aes_key_free(key->u.ccmp.tfm);
  311. if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  312. ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
  313. if (key->local)
  314. ieee80211_debugfs_key_remove(key);
  315. kfree(key);
  316. }
  317. int ieee80211_key_link(struct ieee80211_key *key,
  318. struct ieee80211_sub_if_data *sdata,
  319. struct sta_info *sta)
  320. {
  321. struct ieee80211_key *old_key;
  322. int idx, ret;
  323. bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
  324. BUG_ON(!sdata);
  325. BUG_ON(!key);
  326. idx = key->conf.keyidx;
  327. key->local = sdata->local;
  328. key->sdata = sdata;
  329. key->sta = sta;
  330. if (sta) {
  331. /*
  332. * some hardware cannot handle TKIP with QoS, so
  333. * we indicate whether QoS could be in use.
  334. */
  335. if (test_sta_flags(sta, WLAN_STA_WME))
  336. key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
  337. } else {
  338. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  339. struct sta_info *ap;
  340. /*
  341. * We're getting a sta pointer in,
  342. * so must be under RCU read lock.
  343. */
  344. /* same here, the AP could be using QoS */
  345. ap = sta_info_get(key->sdata, key->sdata->u.mgd.bssid);
  346. if (ap) {
  347. if (test_sta_flags(ap, WLAN_STA_WME))
  348. key->conf.flags |=
  349. IEEE80211_KEY_FLAG_WMM_STA;
  350. }
  351. }
  352. }
  353. mutex_lock(&sdata->local->key_mtx);
  354. if (sta && pairwise)
  355. old_key = sta->ptk;
  356. else if (sta)
  357. old_key = sta->gtk[idx];
  358. else
  359. old_key = sdata->keys[idx];
  360. __ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
  361. __ieee80211_key_destroy(old_key);
  362. ieee80211_debugfs_key_add(key);
  363. ret = ieee80211_key_enable_hw_accel(key);
  364. mutex_unlock(&sdata->local->key_mtx);
  365. return ret;
  366. }
  367. static void __ieee80211_key_free(struct ieee80211_key *key)
  368. {
  369. /*
  370. * Replace key with nothingness if it was ever used.
  371. */
  372. if (key->sdata)
  373. __ieee80211_key_replace(key->sdata, key->sta,
  374. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  375. key, NULL);
  376. __ieee80211_key_destroy(key);
  377. }
  378. void ieee80211_key_free(struct ieee80211_local *local,
  379. struct ieee80211_key *key)
  380. {
  381. if (!key)
  382. return;
  383. mutex_lock(&local->key_mtx);
  384. __ieee80211_key_free(key);
  385. mutex_unlock(&local->key_mtx);
  386. }
  387. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  388. {
  389. struct ieee80211_key *key;
  390. ASSERT_RTNL();
  391. if (WARN_ON(!ieee80211_sdata_running(sdata)))
  392. return;
  393. mutex_lock(&sdata->local->key_mtx);
  394. list_for_each_entry(key, &sdata->key_list, list)
  395. ieee80211_key_enable_hw_accel(key);
  396. mutex_unlock(&sdata->local->key_mtx);
  397. }
  398. void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
  399. {
  400. struct ieee80211_key *key;
  401. ASSERT_RTNL();
  402. mutex_lock(&sdata->local->key_mtx);
  403. list_for_each_entry(key, &sdata->key_list, list)
  404. ieee80211_key_disable_hw_accel(key);
  405. mutex_unlock(&sdata->local->key_mtx);
  406. }
  407. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
  408. {
  409. struct ieee80211_key *key, *tmp;
  410. mutex_lock(&sdata->local->key_mtx);
  411. ieee80211_debugfs_key_remove_default(sdata);
  412. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  413. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  414. __ieee80211_key_free(key);
  415. mutex_unlock(&sdata->local->key_mtx);
  416. }