key.c 14 KB

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