key.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. key->conf.ap_addr = sdata->dev->dev_addr;
  124. ret = drv_set_key(key->local, SET_KEY, sdata, sta, &key->conf);
  125. if (!ret) {
  126. spin_lock_bh(&todo_lock);
  127. key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
  128. spin_unlock_bh(&todo_lock);
  129. }
  130. if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
  131. printk(KERN_ERR "mac80211-%s: failed to set key "
  132. "(%d, %pM) to hardware (%d)\n",
  133. wiphy_name(key->local->hw.wiphy),
  134. key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
  135. }
  136. static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
  137. {
  138. struct ieee80211_sub_if_data *sdata;
  139. struct ieee80211_sta *sta;
  140. int ret;
  141. assert_key_lock();
  142. might_sleep();
  143. if (!key || !key->local->ops->set_key)
  144. return;
  145. spin_lock_bh(&todo_lock);
  146. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) {
  147. spin_unlock_bh(&todo_lock);
  148. return;
  149. }
  150. spin_unlock_bh(&todo_lock);
  151. sta = get_sta_for_key(key);
  152. sdata = key->sdata;
  153. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  154. sdata = container_of(sdata->bss,
  155. struct ieee80211_sub_if_data,
  156. u.ap);
  157. ret = drv_set_key(key->local, DISABLE_KEY, sdata,
  158. sta, &key->conf);
  159. if (ret)
  160. printk(KERN_ERR "mac80211-%s: failed to remove key "
  161. "(%d, %pM) from hardware (%d)\n",
  162. wiphy_name(key->local->hw.wiphy),
  163. key->conf.keyidx, sta ? sta->addr : bcast_addr, ret);
  164. spin_lock_bh(&todo_lock);
  165. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  166. spin_unlock_bh(&todo_lock);
  167. }
  168. static void __ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata,
  169. int idx)
  170. {
  171. struct ieee80211_key *key = NULL;
  172. if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
  173. key = sdata->keys[idx];
  174. rcu_assign_pointer(sdata->default_key, key);
  175. if (key)
  176. add_todo(key, KEY_FLAG_TODO_DEFKEY);
  177. }
  178. void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
  179. {
  180. unsigned long flags;
  181. spin_lock_irqsave(&sdata->local->key_lock, flags);
  182. __ieee80211_set_default_key(sdata, idx);
  183. spin_unlock_irqrestore(&sdata->local->key_lock, flags);
  184. }
  185. static void
  186. __ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata, int idx)
  187. {
  188. struct ieee80211_key *key = NULL;
  189. if (idx >= NUM_DEFAULT_KEYS &&
  190. idx < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  191. key = sdata->keys[idx];
  192. rcu_assign_pointer(sdata->default_mgmt_key, key);
  193. if (key)
  194. add_todo(key, KEY_FLAG_TODO_DEFMGMTKEY);
  195. }
  196. void ieee80211_set_default_mgmt_key(struct ieee80211_sub_if_data *sdata,
  197. int idx)
  198. {
  199. unsigned long flags;
  200. spin_lock_irqsave(&sdata->local->key_lock, flags);
  201. __ieee80211_set_default_mgmt_key(sdata, idx);
  202. spin_unlock_irqrestore(&sdata->local->key_lock, flags);
  203. }
  204. static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
  205. struct sta_info *sta,
  206. struct ieee80211_key *old,
  207. struct ieee80211_key *new)
  208. {
  209. int idx, defkey, defmgmtkey;
  210. if (new)
  211. list_add(&new->list, &sdata->key_list);
  212. if (sta) {
  213. rcu_assign_pointer(sta->key, new);
  214. } else {
  215. WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
  216. if (old)
  217. idx = old->conf.keyidx;
  218. else
  219. idx = new->conf.keyidx;
  220. defkey = old && sdata->default_key == old;
  221. defmgmtkey = old && sdata->default_mgmt_key == old;
  222. if (defkey && !new)
  223. __ieee80211_set_default_key(sdata, -1);
  224. if (defmgmtkey && !new)
  225. __ieee80211_set_default_mgmt_key(sdata, -1);
  226. rcu_assign_pointer(sdata->keys[idx], new);
  227. if (defkey && new)
  228. __ieee80211_set_default_key(sdata, new->conf.keyidx);
  229. if (defmgmtkey && new)
  230. __ieee80211_set_default_mgmt_key(sdata,
  231. new->conf.keyidx);
  232. }
  233. if (old) {
  234. /*
  235. * We'll use an empty list to indicate that the key
  236. * has already been removed.
  237. */
  238. list_del_init(&old->list);
  239. }
  240. }
  241. struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg,
  242. int idx,
  243. size_t key_len,
  244. const u8 *key_data,
  245. size_t seq_len, const u8 *seq)
  246. {
  247. struct ieee80211_key *key;
  248. int i, j;
  249. BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS);
  250. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  251. if (!key)
  252. return NULL;
  253. /*
  254. * Default to software encryption; we'll later upload the
  255. * key to the hardware if possible.
  256. */
  257. key->conf.flags = 0;
  258. key->flags = 0;
  259. key->conf.alg = alg;
  260. key->conf.keyidx = idx;
  261. key->conf.keylen = key_len;
  262. switch (alg) {
  263. case ALG_WEP:
  264. key->conf.iv_len = WEP_IV_LEN;
  265. key->conf.icv_len = WEP_ICV_LEN;
  266. break;
  267. case ALG_TKIP:
  268. key->conf.iv_len = TKIP_IV_LEN;
  269. key->conf.icv_len = TKIP_ICV_LEN;
  270. if (seq) {
  271. for (i = 0; i < NUM_RX_DATA_QUEUES; i++) {
  272. key->u.tkip.rx[i].iv32 =
  273. get_unaligned_le32(&seq[2]);
  274. key->u.tkip.rx[i].iv16 =
  275. get_unaligned_le16(seq);
  276. }
  277. }
  278. break;
  279. case ALG_CCMP:
  280. key->conf.iv_len = CCMP_HDR_LEN;
  281. key->conf.icv_len = CCMP_MIC_LEN;
  282. if (seq) {
  283. for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
  284. for (j = 0; j < CCMP_PN_LEN; j++)
  285. key->u.ccmp.rx_pn[i][j] =
  286. seq[CCMP_PN_LEN - j - 1];
  287. }
  288. break;
  289. case ALG_AES_CMAC:
  290. key->conf.iv_len = 0;
  291. key->conf.icv_len = sizeof(struct ieee80211_mmie);
  292. if (seq)
  293. for (j = 0; j < 6; j++)
  294. key->u.aes_cmac.rx_pn[j] = seq[6 - j - 1];
  295. break;
  296. }
  297. memcpy(key->conf.key, key_data, key_len);
  298. INIT_LIST_HEAD(&key->list);
  299. INIT_LIST_HEAD(&key->todo);
  300. if (alg == ALG_CCMP) {
  301. /*
  302. * Initialize AES key state here as an optimization so that
  303. * it does not need to be initialized for every packet.
  304. */
  305. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
  306. if (!key->u.ccmp.tfm) {
  307. kfree(key);
  308. return NULL;
  309. }
  310. }
  311. if (alg == ALG_AES_CMAC) {
  312. /*
  313. * Initialize AES key state here as an optimization so that
  314. * it does not need to be initialized for every packet.
  315. */
  316. key->u.aes_cmac.tfm =
  317. ieee80211_aes_cmac_key_setup(key_data);
  318. if (!key->u.aes_cmac.tfm) {
  319. kfree(key);
  320. return NULL;
  321. }
  322. }
  323. return key;
  324. }
  325. void ieee80211_key_link(struct ieee80211_key *key,
  326. struct ieee80211_sub_if_data *sdata,
  327. struct sta_info *sta)
  328. {
  329. struct ieee80211_key *old_key;
  330. unsigned long flags;
  331. int idx;
  332. BUG_ON(!sdata);
  333. BUG_ON(!key);
  334. idx = key->conf.keyidx;
  335. key->local = sdata->local;
  336. key->sdata = sdata;
  337. key->sta = sta;
  338. if (sta) {
  339. /*
  340. * some hardware cannot handle TKIP with QoS, so
  341. * we indicate whether QoS could be in use.
  342. */
  343. if (test_sta_flags(sta, WLAN_STA_WME))
  344. key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
  345. /*
  346. * This key is for a specific sta interface,
  347. * inform the driver that it should try to store
  348. * this key as pairwise key.
  349. */
  350. key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;
  351. } else {
  352. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  353. struct sta_info *ap;
  354. /*
  355. * We're getting a sta pointer in,
  356. * so must be under RCU read lock.
  357. */
  358. /* same here, the AP could be using QoS */
  359. ap = sta_info_get(key->sdata, key->sdata->u.mgd.bssid);
  360. if (ap) {
  361. if (test_sta_flags(ap, WLAN_STA_WME))
  362. key->conf.flags |=
  363. IEEE80211_KEY_FLAG_WMM_STA;
  364. }
  365. }
  366. }
  367. spin_lock_irqsave(&sdata->local->key_lock, flags);
  368. if (sta)
  369. old_key = sta->key;
  370. else
  371. old_key = sdata->keys[idx];
  372. __ieee80211_key_replace(sdata, sta, old_key, key);
  373. /* free old key later */
  374. add_todo(old_key, KEY_FLAG_TODO_DELETE);
  375. add_todo(key, KEY_FLAG_TODO_ADD_DEBUGFS);
  376. if (ieee80211_sdata_running(sdata))
  377. add_todo(key, KEY_FLAG_TODO_HWACCEL_ADD);
  378. spin_unlock_irqrestore(&sdata->local->key_lock, flags);
  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, NULL);
  388. add_todo(key, KEY_FLAG_TODO_DELETE);
  389. }
  390. void ieee80211_key_free(struct ieee80211_key *key)
  391. {
  392. unsigned long flags;
  393. if (!key)
  394. return;
  395. if (!key->sdata) {
  396. /* The key has not been linked yet, simply free it
  397. * and don't Oops */
  398. if (key->conf.alg == ALG_CCMP)
  399. ieee80211_aes_key_free(key->u.ccmp.tfm);
  400. kfree(key);
  401. return;
  402. }
  403. spin_lock_irqsave(&key->sdata->local->key_lock, flags);
  404. __ieee80211_key_free(key);
  405. spin_unlock_irqrestore(&key->sdata->local->key_lock, flags);
  406. }
  407. /*
  408. * To be safe against concurrent manipulations of the list (which shouldn't
  409. * actually happen) we need to hold the spinlock. But under the spinlock we
  410. * can't actually do much, so we defer processing to the todo list. Then run
  411. * the todo list to be sure the operation and possibly previously pending
  412. * operations are completed.
  413. */
  414. static void ieee80211_todo_for_each_key(struct ieee80211_sub_if_data *sdata,
  415. u32 todo_flags)
  416. {
  417. struct ieee80211_key *key;
  418. unsigned long flags;
  419. might_sleep();
  420. spin_lock_irqsave(&sdata->local->key_lock, flags);
  421. list_for_each_entry(key, &sdata->key_list, list)
  422. add_todo(key, todo_flags);
  423. spin_unlock_irqrestore(&sdata->local->key_lock, flags);
  424. ieee80211_key_todo();
  425. }
  426. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  427. {
  428. ASSERT_RTNL();
  429. if (WARN_ON(!ieee80211_sdata_running(sdata)))
  430. return;
  431. ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_ADD);
  432. }
  433. void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
  434. {
  435. ASSERT_RTNL();
  436. ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_REMOVE);
  437. }
  438. static void __ieee80211_key_destroy(struct ieee80211_key *key)
  439. {
  440. if (!key)
  441. return;
  442. ieee80211_key_disable_hw_accel(key);
  443. if (key->conf.alg == ALG_CCMP)
  444. ieee80211_aes_key_free(key->u.ccmp.tfm);
  445. if (key->conf.alg == ALG_AES_CMAC)
  446. ieee80211_aes_cmac_key_free(key->u.aes_cmac.tfm);
  447. ieee80211_debugfs_key_remove(key);
  448. kfree(key);
  449. }
  450. static void __ieee80211_key_todo(void)
  451. {
  452. struct ieee80211_key *key;
  453. bool work_done;
  454. u32 todoflags;
  455. /*
  456. * NB: sta_info_destroy relies on this!
  457. */
  458. synchronize_rcu();
  459. spin_lock_bh(&todo_lock);
  460. while (!list_empty(&todo_list)) {
  461. key = list_first_entry(&todo_list, struct ieee80211_key, todo);
  462. list_del_init(&key->todo);
  463. todoflags = key->flags & (KEY_FLAG_TODO_ADD_DEBUGFS |
  464. KEY_FLAG_TODO_DEFKEY |
  465. KEY_FLAG_TODO_DEFMGMTKEY |
  466. KEY_FLAG_TODO_HWACCEL_ADD |
  467. KEY_FLAG_TODO_HWACCEL_REMOVE |
  468. KEY_FLAG_TODO_DELETE);
  469. key->flags &= ~todoflags;
  470. spin_unlock_bh(&todo_lock);
  471. work_done = false;
  472. if (todoflags & KEY_FLAG_TODO_ADD_DEBUGFS) {
  473. ieee80211_debugfs_key_add(key);
  474. work_done = true;
  475. }
  476. if (todoflags & KEY_FLAG_TODO_DEFKEY) {
  477. ieee80211_debugfs_key_remove_default(key->sdata);
  478. ieee80211_debugfs_key_add_default(key->sdata);
  479. work_done = true;
  480. }
  481. if (todoflags & KEY_FLAG_TODO_DEFMGMTKEY) {
  482. ieee80211_debugfs_key_remove_mgmt_default(key->sdata);
  483. ieee80211_debugfs_key_add_mgmt_default(key->sdata);
  484. work_done = true;
  485. }
  486. if (todoflags & KEY_FLAG_TODO_HWACCEL_ADD) {
  487. ieee80211_key_enable_hw_accel(key);
  488. work_done = true;
  489. }
  490. if (todoflags & KEY_FLAG_TODO_HWACCEL_REMOVE) {
  491. ieee80211_key_disable_hw_accel(key);
  492. work_done = true;
  493. }
  494. if (todoflags & KEY_FLAG_TODO_DELETE) {
  495. __ieee80211_key_destroy(key);
  496. work_done = true;
  497. }
  498. WARN_ON(!work_done);
  499. spin_lock_bh(&todo_lock);
  500. }
  501. spin_unlock_bh(&todo_lock);
  502. }
  503. void ieee80211_key_todo(void)
  504. {
  505. ieee80211_key_lock();
  506. __ieee80211_key_todo();
  507. ieee80211_key_unlock();
  508. }
  509. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
  510. {
  511. struct ieee80211_key *key, *tmp;
  512. unsigned long flags;
  513. ieee80211_key_lock();
  514. ieee80211_debugfs_key_remove_default(sdata);
  515. ieee80211_debugfs_key_remove_mgmt_default(sdata);
  516. spin_lock_irqsave(&sdata->local->key_lock, flags);
  517. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  518. __ieee80211_key_free(key);
  519. spin_unlock_irqrestore(&sdata->local->key_lock, flags);
  520. __ieee80211_key_todo();
  521. ieee80211_key_unlock();
  522. }