key.c 15 KB

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