key.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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)
  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. rcu_assign_pointer(sdata->default_key, key);
  154. if (key) {
  155. ieee80211_debugfs_key_remove_default(key->sdata);
  156. ieee80211_debugfs_key_add_default(key->sdata);
  157. }
  158. }
  159. void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
  160. {
  161. mutex_lock(&sdata->local->key_mtx);
  162. __ieee80211_set_default_key(sdata, idx);
  163. mutex_unlock(&sdata->local->key_mtx);
  164. }
  165. static void
  166. __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
  167. {
  168. struct ieee80211_key *key = NULL;
  169. assert_key_lock(sdata->local);
  170. if (idx >= NUM_DEFAULT_KEYS &&
  171. idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  172. key = sdata->keys[idx];
  173. rcu_assign_pointer(sdata->default_mgmt_key, key);
  174. if (key) {
  175. ieee80211_debugfs_key_remove_mgmt_default(key->sdata);
  176. ieee80211_debugfs_key_add_mgmt_default(key->sdata);
  177. }
  178. }
  179. void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
  180. int idx)
  181. {
  182. mutex_lock(&sdata->local->key_mtx);
  183. __ieee80211_set_default_mgmt_key(sdata, idx);
  184. mutex_unlock(&sdata->local->key_mtx);
  185. }
  186. static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
  187. struct sta_info *sta,
  188. bool pairwise,
  189. struct ieee80211_key *old,
  190. struct ieee80211_key *new)
  191. {
  192. int idx, defkey, defmgmtkey;
  193. if (new)
  194. list_add(&new->list, &sdata->key_list);
  195. if (sta && pairwise) {
  196. rcu_assign_pointer(sta->ptk, new);
  197. } else if (sta) {
  198. if (old)
  199. idx = old->conf.keyidx;
  200. else
  201. idx = new->conf.keyidx;
  202. rcu_assign_pointer(sta->gtk[idx], new);
  203. } else {
  204. WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
  205. if (old)
  206. idx = old->conf.keyidx;
  207. else
  208. idx = new->conf.keyidx;
  209. defkey = old && sdata->default_key == old;
  210. defmgmtkey = old && sdata->default_mgmt_key == old;
  211. if (defkey && !new)
  212. __ieee80211_set_default_key(sdata, -1);
  213. if (defmgmtkey && !new)
  214. __ieee80211_set_default_mgmt_key(sdata, -1);
  215. rcu_assign_pointer(sdata->keys[idx], new);
  216. if (defkey && new)
  217. __ieee80211_set_default_key(sdata, new->conf.keyidx);
  218. if (defmgmtkey && new)
  219. __ieee80211_set_default_mgmt_key(sdata,
  220. new->conf.keyidx);
  221. }
  222. if (old) {
  223. /*
  224. * We'll use an empty list to indicate that the key
  225. * has already been removed.
  226. */
  227. list_del_init(&old->list);
  228. }
  229. }
  230. struct ieee80211_key *ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
  231. const u8 *key_data,
  232. size_t seq_len, const u8 *seq)
  233. {
  234. struct ieee80211_key *key;
  235. int i, j, err;
  236. BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS);
  237. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  238. if (!key)
  239. return ERR_PTR(-ENOMEM);
  240. /*
  241. * Default to software encryption; we'll later upload the
  242. * key to the hardware if possible.
  243. */
  244. key->conf.flags = 0;
  245. key->flags = 0;
  246. key->conf.cipher = cipher;
  247. key->conf.keyidx = idx;
  248. key->conf.keylen = key_len;
  249. switch (cipher) {
  250. case WLAN_CIPHER_SUITE_WEP40:
  251. case WLAN_CIPHER_SUITE_WEP104:
  252. key->conf.iv_len = WEP_IV_LEN;
  253. key->conf.icv_len = WEP_ICV_LEN;
  254. break;
  255. case WLAN_CIPHER_SUITE_TKIP:
  256. key->conf.iv_len = TKIP_IV_LEN;
  257. key->conf.icv_len = TKIP_ICV_LEN;
  258. if (seq) {
  259. for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
  260. key->u.tkip.rx[i].iv32 =
  261. get_unaligned_le32(&seq[2]);
  262. key->u.tkip.rx[i].iv16 =
  263. get_unaligned_le16(seq);
  264. }
  265. }
  266. break;
  267. case WLAN_CIPHER_SUITE_CCMP:
  268. key->conf.iv_len = CCMP_HDR_LEN;
  269. key->conf.icv_len = CCMP_MIC_LEN;
  270. if (seq) {
  271. for (i = 0; i < NUM_RX_DATA_QUEUES + 1; i++)
  272. for (j = 0; j < CCMP_PN_LEN; j++)
  273. key->u.ccmp.rx_pn[i][j] =
  274. seq[CCMP_PN_LEN - j - 1];
  275. }
  276. /*
  277. * Initialize AES key state here as an optimization so that
  278. * it does not need to be initialized for every packet.
  279. */
  280. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
  281. if (IS_ERR(key->u.ccmp.tfm)) {
  282. err = PTR_ERR(key->u.ccmp.tfm);
  283. kfree(key);
  284. key = ERR_PTR(err);
  285. }
  286. break;
  287. case WLAN_CIPHER_SUITE_AES_CMAC:
  288. key->conf.iv_len = 0;
  289. key->conf.icv_len = sizeof(struct ieee80211_mmie);
  290. if (seq)
  291. for (j = 0; j < 6; j++)
  292. key->u.aes_cmac.rx_pn[j] = seq[6 - j - 1];
  293. /*
  294. * Initialize AES key state here as an optimization so that
  295. * it does not need to be initialized for every packet.
  296. */
  297. key->u.aes_cmac.tfm =
  298. ieee80211_aes_cmac_key_setup(key_data);
  299. if (IS_ERR(key->u.aes_cmac.tfm)) {
  300. err = PTR_ERR(key->u.aes_cmac.tfm);
  301. kfree(key);
  302. key = ERR_PTR(err);
  303. }
  304. break;
  305. }
  306. memcpy(key->conf.key, key_data, key_len);
  307. INIT_LIST_HEAD(&key->list);
  308. return key;
  309. }
  310. static void __ieee80211_key_destroy(struct ieee80211_key *key)
  311. {
  312. if (!key)
  313. return;
  314. if (key->local)
  315. ieee80211_key_disable_hw_accel(key);
  316. if (key->conf.cipher == WLAN_CIPHER_SUITE_CCMP)
  317. ieee80211_aes_key_free(key->u.ccmp.tfm);
  318. if (key->conf.cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  319. ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
  320. if (key->local)
  321. ieee80211_debugfs_key_remove(key);
  322. kfree(key);
  323. }
  324. int ieee80211_key_link(struct ieee80211_key *key,
  325. struct ieee80211_sub_if_data *sdata,
  326. struct sta_info *sta)
  327. {
  328. struct ieee80211_key *old_key;
  329. int idx, ret;
  330. bool pairwise = key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE;
  331. BUG_ON(!sdata);
  332. BUG_ON(!key);
  333. idx = key->conf.keyidx;
  334. key->local = sdata->local;
  335. key->sdata = sdata;
  336. key->sta = sta;
  337. if (sta) {
  338. /*
  339. * some hardware cannot handle TKIP with QoS, so
  340. * we indicate whether QoS could be in use.
  341. */
  342. if (test_sta_flags(sta, WLAN_STA_WME))
  343. key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
  344. } else {
  345. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  346. struct sta_info *ap;
  347. /*
  348. * We're getting a sta pointer in,
  349. * so must be under RCU read lock.
  350. */
  351. /* same here, the AP could be using QoS */
  352. ap = sta_info_get(key->sdata, key->sdata->u.mgd.bssid);
  353. if (ap) {
  354. if (test_sta_flags(ap, WLAN_STA_WME))
  355. key->conf.flags |=
  356. IEEE80211_KEY_FLAG_WMM_STA;
  357. }
  358. }
  359. }
  360. mutex_lock(&sdata->local->key_mtx);
  361. if (sta && pairwise)
  362. old_key = sta->ptk;
  363. else if (sta)
  364. old_key = sta->gtk[idx];
  365. else
  366. old_key = sdata->keys[idx];
  367. __ieee80211_key_replace(sdata, sta, pairwise, old_key, key);
  368. __ieee80211_key_destroy(old_key);
  369. ieee80211_debugfs_key_add(key);
  370. ret = ieee80211_key_enable_hw_accel(key);
  371. mutex_unlock(&sdata->local->key_mtx);
  372. return ret;
  373. }
  374. static void __ieee80211_key_free(struct ieee80211_key *key)
  375. {
  376. /*
  377. * Replace key with nothingness if it was ever used.
  378. */
  379. if (key->sdata)
  380. __ieee80211_key_replace(key->sdata, key->sta,
  381. key->conf.flags & IEEE80211_KEY_FLAG_PAIRWISE,
  382. key, NULL);
  383. __ieee80211_key_destroy(key);
  384. }
  385. void ieee80211_key_free(struct ieee80211_local *local,
  386. struct ieee80211_key *key)
  387. {
  388. if (!key)
  389. return;
  390. mutex_lock(&local->key_mtx);
  391. __ieee80211_key_free(key);
  392. mutex_unlock(&local->key_mtx);
  393. }
  394. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  395. {
  396. struct ieee80211_key *key;
  397. ASSERT_RTNL();
  398. if (WARN_ON(!ieee80211_sdata_running(sdata)))
  399. return;
  400. mutex_lock(&sdata->local->key_mtx);
  401. list_for_each_entry(key, &sdata->key_list, list)
  402. ieee80211_key_enable_hw_accel(key);
  403. mutex_unlock(&sdata->local->key_mtx);
  404. }
  405. void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
  406. {
  407. struct ieee80211_key *key;
  408. ASSERT_RTNL();
  409. mutex_lock(&sdata->local->key_mtx);
  410. list_for_each_entry(key, &sdata->key_list, list)
  411. ieee80211_key_disable_hw_accel(key);
  412. mutex_unlock(&sdata->local->key_mtx);
  413. }
  414. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
  415. {
  416. struct ieee80211_key *key, *tmp;
  417. mutex_lock(&sdata->local->key_mtx);
  418. ieee80211_debugfs_key_remove_default(sdata);
  419. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  420. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  421. __ieee80211_key_free(key);
  422. mutex_unlock(&sdata->local->key_mtx);
  423. }