key.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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 so you can call them at
  37. * any time.
  38. *
  39. * Within mac80211, key references are, just as STA structure references,
  40. * protected by RCU. Note, however, that some things are unprotected,
  41. * namely the key->sta dereferences within the hardware acceleration
  42. * functions. This means that sta_info_destroy() must flush the key todo
  43. * list.
  44. *
  45. * All the direct key list manipulation functions must not sleep because
  46. * they can operate on STA info structs that are protected by RCU.
  47. */
  48. static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  49. /* key mutex: used to synchronise todo runners */
  50. static DEFINE_MUTEX(key_mutex);
  51. static DEFINE_SPINLOCK(todo_lock);
  52. static LIST_HEAD(todo_list);
  53. static void key_todo(struct work_struct *work)
  54. {
  55. ieee80211_key_todo();
  56. }
  57. static DECLARE_WORK(todo_work, key_todo);
  58. /**
  59. * add_todo - add todo item for a key
  60. *
  61. * @key: key to add to do item for
  62. * @flag: todo flag(s)
  63. *
  64. * Must be called with IRQs or softirqs disabled.
  65. */
  66. static void add_todo(struct ieee80211_key *key, u32 flag)
  67. {
  68. if (!key)
  69. return;
  70. spin_lock(&todo_lock);
  71. key->flags |= flag;
  72. /*
  73. * Remove again if already on the list so that we move it to the end.
  74. */
  75. if (!list_empty(&key->todo))
  76. list_del(&key->todo);
  77. list_add_tail(&key->todo, &todo_list);
  78. schedule_work(&todo_work);
  79. spin_unlock(&todo_lock);
  80. }
  81. /**
  82. * ieee80211_key_lock - lock the mac80211 key operation lock
  83. *
  84. * This locks the (global) mac80211 key operation lock, all
  85. * key operations must be done under this lock.
  86. */
  87. static void ieee80211_key_lock(void)
  88. {
  89. mutex_lock(&key_mutex);
  90. }
  91. /**
  92. * ieee80211_key_unlock - unlock the mac80211 key operation lock
  93. */
  94. static void ieee80211_key_unlock(void)
  95. {
  96. mutex_unlock(&key_mutex);
  97. }
  98. static void assert_key_lock(void)
  99. {
  100. WARN_ON(!mutex_is_locked(&key_mutex));
  101. }
  102. static struct ieee80211_sta *get_sta_for_key(struct ieee80211_key *key)
  103. {
  104. if (key->sta)
  105. return &key->sta->sta;
  106. return NULL;
  107. }
  108. static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
  109. {
  110. struct ieee80211_sub_if_data *sdata;
  111. struct ieee80211_sta *sta;
  112. int ret;
  113. assert_key_lock();
  114. might_sleep();
  115. if (!key->local->ops->set_key)
  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, SET_KEY, sdata, sta, &key->conf);
  124. if (!ret) {
  125. spin_lock_bh(&todo_lock);
  126. key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
  127. spin_unlock_bh(&todo_lock);
  128. }
  129. if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
  130. printk(KERN_ERR "mac80211-%s: failed to set key "
  131. "(%d, %pM) to hardware (%d)\n",
  132. wiphy_name(key->local->hw.wiphy),
  133. key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
  134. }
  135. static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
  136. {
  137. struct ieee80211_sub_if_data *sdata;
  138. struct ieee80211_sta *sta;
  139. int ret;
  140. assert_key_lock();
  141. might_sleep();
  142. if (!key || !key->local->ops->set_key)
  143. return;
  144. spin_lock_bh(&todo_lock);
  145. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) {
  146. spin_unlock_bh(&todo_lock);
  147. return;
  148. }
  149. spin_unlock_bh(&todo_lock);
  150. sta = get_sta_for_key(key);
  151. sdata = key->sdata;
  152. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  153. sdata = container_of(sdata->bss,
  154. struct ieee80211_sub_if_data,
  155. u.ap);
  156. ret = drv_set_key(key->local, DISABLE_KEY, sdata,
  157. sta, &key->conf);
  158. if (ret)
  159. printk(KERN_ERR "mac80211-%s: failed to remove key "
  160. "(%d, %pM) from hardware (%d)\n",
  161. wiphy_name(key->local->hw.wiphy),
  162. key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
  163. spin_lock_bh(&todo_lock);
  164. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  165. spin_unlock_bh(&todo_lock);
  166. }
  167. static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
  168. int idx)
  169. {
  170. struct ieee80211_key *key = NULL;
  171. if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
  172. key = sdata->keys[idx];
  173. rcu_assign_pointer(sdata->default_key, key);
  174. if (key)
  175. add_todo(key, KEY_FLAG_TODO_DEFKEY);
  176. }
  177. void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
  178. {
  179. unsigned long flags;
  180. spin_lock_irqsave(&sdata->local->key_lock, flags);
  181. __ieee80211_set_default_key(sdata, idx);
  182. spin_unlock_irqrestore(&sdata->local->key_lock, flags);
  183. }
  184. static void
  185. __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
  186. {
  187. struct ieee80211_key *key = NULL;
  188. if (idx >= NUM_DEFAULT_KEYS &&
  189. idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  190. key = sdata->keys[idx];
  191. rcu_assign_pointer(sdata->default_mgmt_key, key);
  192. if (key)
  193. add_todo(key, KEY_FLAG_TODO_DEFMGMTKEY);
  194. }
  195. void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
  196. int idx)
  197. {
  198. unsigned long flags;
  199. spin_lock_irqsave(&sdata->local->key_lock, flags);
  200. __ieee80211_set_default_mgmt_key(sdata, idx);
  201. spin_unlock_irqrestore(&sdata->local->key_lock, flags);
  202. }
  203. static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
  204. struct sta_info *sta,
  205. struct ieee80211_key *old,
  206. struct ieee80211_key *new)
  207. {
  208. int idx, defkey, defmgmtkey;
  209. if (new)
  210. list_add(&new->list, &sdata->key_list);
  211. if (sta) {
  212. rcu_assign_pointer(sta->key, new);
  213. } else {
  214. WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
  215. if (old)
  216. idx = old->conf.keyidx;
  217. else
  218. idx = new->conf.keyidx;
  219. defkey = old && sdata->default_key == old;
  220. defmgmtkey = old && sdata->default_mgmt_key == old;
  221. if (defkey && !new)
  222. __ieee80211_set_default_key(sdata, -1);
  223. if (defmgmtkey && !new)
  224. __ieee80211_set_default_mgmt_key(sdata, -1);
  225. rcu_assign_pointer(sdata->keys[idx], new);
  226. if (defkey && new)
  227. __ieee80211_set_default_key(sdata, new->conf.keyidx);
  228. if (defmgmtkey && new)
  229. __ieee80211_set_default_mgmt_key(sdata,
  230. new->conf.keyidx);
  231. }
  232. if (old) {
  233. /*
  234. * We'll use an empty list to indicate that the key
  235. * has already been removed.
  236. */
  237. list_del_init(&old->list);
  238. }
  239. }
  240. struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg,
  241. int idx,
  242. size_t key_len,
  243. const u8 *key_data,
  244. size_t seq_len, const u8 *seq)
  245. {
  246. struct ieee80211_key *key;
  247. int i, j;
  248. BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS);
  249. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  250. if (!key)
  251. return NULL;
  252. /*
  253. * Default to software encryption; we'll later upload the
  254. * key to the hardware if possible.
  255. */
  256. key->conf.flags = 0;
  257. key->flags = 0;
  258. key->conf.alg = alg;
  259. key->conf.keyidx = idx;
  260. key->conf.keylen = key_len;
  261. switch (alg) {
  262. case ALG_WEP:
  263. key->conf.iv_len = WEP_IV_LEN;
  264. key->conf.icv_len = WEP_ICV_LEN;
  265. break;
  266. case ALG_TKIP:
  267. key->conf.iv_len = TKIP_IV_LEN;
  268. key->conf.icv_len = TKIP_ICV_LEN;
  269. if (seq) {
  270. for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
  271. key->u.tkip.rx[i].iv32 =
  272. get_unaligned_le32(&seq[2]);
  273. key->u.tkip.rx[i].iv16 =
  274. get_unaligned_le16(seq);
  275. }
  276. }
  277. break;
  278. case ALG_CCMP:
  279. key->conf.iv_len = CCMP_HDR_LEN;
  280. key->conf.icv_len = CCMP_MIC_LEN;
  281. if (seq) {
  282. for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
  283. for (j = 0; j < CCMP_PN_LEN; j++)
  284. key->u.ccmp.rx_pn[i][j] =
  285. seq[CCMP_PN_LEN - j - 1];
  286. }
  287. break;
  288. case ALG_AES_CMAC:
  289. key->conf.iv_len = 0;
  290. key->conf.icv_len = sizeof(struct ieee80211_mmie);
  291. if (seq)
  292. for (j = 0; j < 6; j++)
  293. key->u.aes_cmac.rx_pn[j] = seq[6 - j - 1];
  294. break;
  295. }
  296. memcpy(key->conf.key, key_data, key_len);
  297. INIT_LIST_HEAD(&key->list);
  298. INIT_LIST_HEAD(&key->todo);
  299. if (alg == ALG_CCMP) {
  300. /*
  301. * Initialize AES key state here as an optimization so that
  302. * it does not need to be initialized for every packet.
  303. */
  304. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
  305. if (!key->u.ccmp.tfm) {
  306. kfree(key);
  307. return NULL;
  308. }
  309. }
  310. if (alg == ALG_AES_CMAC) {
  311. /*
  312. * Initialize AES key state here as an optimization so that
  313. * it does not need to be initialized for every packet.
  314. */
  315. key->u.aes_cmac.tfm =
  316. ieee80211_aes_cmac_key_setup(key_data);
  317. if (!key->u.aes_cmac.tfm) {
  318. kfree(key);
  319. return NULL;
  320. }
  321. }
  322. return key;
  323. }
  324. void 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. unsigned long flags;
  330. int idx;
  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. /*
  345. * This key is for a specific sta interface,
  346. * inform the driver that it should try to store
  347. * this key as pairwise key.
  348. */
  349. key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;
  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. spin_lock_irqsave(&sdata->local->key_lock, flags);
  367. if (sta)
  368. old_key = sta->key;
  369. else
  370. old_key = sdata->keys[idx];
  371. __ieee80211_key_replace(sdata, sta, old_key, key);
  372. /* free old key later */
  373. add_todo(old_key, KEY_FLAG_TODO_DELETE);
  374. add_todo(key, KEY_FLAG_TODO_ADD_DEBUGFS);
  375. if (ieee80211_sdata_running(sdata))
  376. add_todo(key, KEY_FLAG_TODO_HWACCEL_ADD);
  377. spin_unlock_irqrestore(&sdata->local->key_lock, flags);
  378. }
  379. static void __ieee80211_key_free(struct ieee80211_key *key)
  380. {
  381. /*
  382. * Replace key with nothingness if it was ever used.
  383. */
  384. if (key->sdata)
  385. __ieee80211_key_replace(key->sdata, key->sta,
  386. key, NULL);
  387. add_todo(key, KEY_FLAG_TODO_DELETE);
  388. }
  389. void ieee80211_key_free(struct ieee80211_key *key)
  390. {
  391. unsigned long flags;
  392. if (!key)
  393. return;
  394. if (!key->sdata) {
  395. /* The key has not been linked yet, simply free it
  396. * and don't Oops */
  397. if (key->conf.alg == ALG_CCMP)
  398. ieee80211_aes_key_free(key->u.ccmp.tfm);
  399. kfree(key);
  400. return;
  401. }
  402. spin_lock_irqsave(&key->sdata->local->key_lock, flags);
  403. __ieee80211_key_free(key);
  404. spin_unlock_irqrestore(&key->sdata->local->key_lock, flags);
  405. }
  406. /*
  407. * To be safe against concurrent manipulations of the list (which shouldn't
  408. * actually happen) we need to hold the spinlock. But under the spinlock we
  409. * can't actually do much, so we defer processing to the todo list. Then run
  410. * the todo list to be sure the operation and possibly previously pending
  411. * operations are completed.
  412. */
  413. static void ieee80211_todo_for_each_key(struct ieee80211_sub_if_data *sdata,
  414. u32 todo_flags)
  415. {
  416. struct ieee80211_key *key;
  417. unsigned long flags;
  418. might_sleep();
  419. spin_lock_irqsave(&sdata->local->key_lock, flags);
  420. list_for_each_entry(key, &sdata->key_list, list)
  421. add_todo(key, todo_flags);
  422. spin_unlock_irqrestore(&sdata->local->key_lock, flags);
  423. ieee80211_key_todo();
  424. }
  425. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  426. {
  427. ASSERT_RTNL();
  428. if (WARN_ON(!ieee80211_sdata_running(sdata)))
  429. return;
  430. ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_ADD);
  431. }
  432. void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
  433. {
  434. ASSERT_RTNL();
  435. ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_REMOVE);
  436. }
  437. static void __ieee80211_key_destroy(struct ieee80211_key *key)
  438. {
  439. if (!key)
  440. return;
  441. ieee80211_key_disable_hw_accel(key);
  442. if (key->conf.alg == ALG_CCMP)
  443. ieee80211_aes_key_free(key->u.ccmp.tfm);
  444. if (key->conf.alg == ALG_AES_CMAC)
  445. ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
  446. ieee80211_debugfs_key_remove(key);
  447. kfree(key);
  448. }
  449. static void __ieee80211_key_todo(void)
  450. {
  451. struct ieee80211_key *key;
  452. bool work_done;
  453. u32 todoflags;
  454. /*
  455. * NB: sta_info_destroy relies on this!
  456. */
  457. synchronize_rcu();
  458. spin_lock_bh(&todo_lock);
  459. while (!list_empty(&todo_list)) {
  460. key = list_first_entry(&todo_list, struct ieee80211_key, todo);
  461. list_del_init(&key->todo);
  462. todoflags = key->flags & (KEY_FLAG_TODO_ADD_DEBUGFS |
  463. KEY_FLAG_TODO_DEFKEY |
  464. KEY_FLAG_TODO_DEFMGMTKEY |
  465. KEY_FLAG_TODO_HWACCEL_ADD |
  466. KEY_FLAG_TODO_HWACCEL_REMOVE |
  467. KEY_FLAG_TODO_DELETE);
  468. key->flags &= ~todoflags;
  469. spin_unlock_bh(&todo_lock);
  470. work_done = false;
  471. if (todoflags & KEY_FLAG_TODO_ADD_DEBUGFS) {
  472. ieee80211_debugfs_key_add(key);
  473. work_done = true;
  474. }
  475. if (todoflags & KEY_FLAG_TODO_DEFKEY) {
  476. ieee80211_debugfs_key_remove_default(key->sdata);
  477. ieee80211_debugfs_key_add_default(key->sdata);
  478. work_done = true;
  479. }
  480. if (todoflags & KEY_FLAG_TODO_DEFMGMTKEY) {
  481. ieee80211_debugfs_key_remove_mgmt_default(key->sdata);
  482. ieee80211_debugfs_key_add_mgmt_default(key->sdata);
  483. work_done = true;
  484. }
  485. if (todoflags & KEY_FLAG_TODO_HWACCEL_ADD) {
  486. ieee80211_key_enable_hw_accel(key);
  487. work_done = true;
  488. }
  489. if (todoflags & KEY_FLAG_TODO_HWACCEL_REMOVE) {
  490. ieee80211_key_disable_hw_accel(key);
  491. work_done = true;
  492. }
  493. if (todoflags & KEY_FLAG_TODO_DELETE) {
  494. __ieee80211_key_destroy(key);
  495. work_done = true;
  496. }
  497. WARN_ON(!work_done);
  498. spin_lock_bh(&todo_lock);
  499. }
  500. spin_unlock_bh(&todo_lock);
  501. }
  502. void ieee80211_key_todo(void)
  503. {
  504. ieee80211_key_lock();
  505. __ieee80211_key_todo();
  506. ieee80211_key_unlock();
  507. }
  508. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
  509. {
  510. struct ieee80211_key *key, *tmp;
  511. unsigned long flags;
  512. ieee80211_key_lock();
  513. ieee80211_debugfs_key_remove_default(sdata);
  514. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  515. spin_lock_irqsave(&sdata->local->key_lock, flags);
  516. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  517. __ieee80211_key_free(key);
  518. spin_unlock_irqrestore(&sdata->local->key_lock, flags);
  519. __ieee80211_key_todo();
  520. ieee80211_key_unlock();
  521. }