key.c 11 KB

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