key.c 13 KB

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