key.c 11 KB

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