key.c 11 KB

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