key.c 13 KB

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