key.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. static const u8 zero_addr[ETH_ALEN];
  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. /* only add if not already added */
  69. if (list_empty(&key->todo))
  70. list_add(&key->todo, &todo_list);
  71. schedule_work(&todo_work);
  72. spin_unlock(&todo_lock);
  73. }
  74. /**
  75. * ieee80211_key_lock - lock the mac80211 key operation lock
  76. *
  77. * This locks the (global) mac80211 key operation lock, all
  78. * key operations must be done under this lock.
  79. */
  80. static void ieee80211_key_lock(void)
  81. {
  82. mutex_lock(&key_mutex);
  83. }
  84. /**
  85. * ieee80211_key_unlock - unlock the mac80211 key operation lock
  86. */
  87. static void ieee80211_key_unlock(void)
  88. {
  89. mutex_unlock(&key_mutex);
  90. }
  91. static void assert_key_lock(void)
  92. {
  93. WARN_ON(!mutex_is_locked(&key_mutex));
  94. }
  95. static const u8 *get_mac_for_key(struct ieee80211_key *key)
  96. {
  97. const u8 *addr = bcast_addr;
  98. /*
  99. * If we're an AP we won't ever receive frames with a non-WEP
  100. * group key so we tell the driver that by using the zero MAC
  101. * address to indicate a transmit-only key.
  102. */
  103. if (key->conf.alg != ALG_WEP &&
  104. (key->sdata->vif.type == IEEE80211_IF_TYPE_AP ||
  105. key->sdata->vif.type == IEEE80211_IF_TYPE_VLAN))
  106. addr = zero_addr;
  107. if (key->sta)
  108. addr = key->sta->addr;
  109. return addr;
  110. }
  111. static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
  112. {
  113. const u8 *addr;
  114. int ret;
  115. DECLARE_MAC_BUF(mac);
  116. assert_key_lock();
  117. might_sleep();
  118. if (!key->local->ops->set_key)
  119. return;
  120. addr = get_mac_for_key(key);
  121. ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY,
  122. key->sdata->dev->dev_addr, addr,
  123. &key->conf);
  124. if (!ret) {
  125. spin_lock(&todo_lock);
  126. key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
  127. spin_unlock(&todo_lock);
  128. }
  129. if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
  130. printk(KERN_ERR "mac80211-%s: failed to set key "
  131. "(%d, %s) to hardware (%d)\n",
  132. wiphy_name(key->local->hw.wiphy),
  133. key->conf.keyidx, print_mac(mac, addr), ret);
  134. }
  135. static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
  136. {
  137. const u8 *addr;
  138. int ret;
  139. DECLARE_MAC_BUF(mac);
  140. assert_key_lock();
  141. might_sleep();
  142. if (!key || !key->local->ops->set_key)
  143. return;
  144. spin_lock(&todo_lock);
  145. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) {
  146. spin_unlock(&todo_lock);
  147. return;
  148. }
  149. spin_unlock(&todo_lock);
  150. addr = get_mac_for_key(key);
  151. ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY,
  152. key->sdata->dev->dev_addr, addr,
  153. &key->conf);
  154. if (ret)
  155. printk(KERN_ERR "mac80211-%s: failed to remove key "
  156. "(%d, %s) from hardware (%d)\n",
  157. wiphy_name(key->local->hw.wiphy),
  158. key->conf.keyidx, print_mac(mac, 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->sta_lock, flags);
  177. __ieee80211_set_default_key(sdata, idx);
  178. spin_unlock_irqrestore(&sdata->local->sta_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. memcpy(key->conf.key, key_data, key_len);
  231. INIT_LIST_HEAD(&key->list);
  232. INIT_LIST_HEAD(&key->todo);
  233. if (alg == ALG_CCMP) {
  234. /*
  235. * Initialize AES key state here as an optimization so that
  236. * it does not need to be initialized for every packet.
  237. */
  238. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
  239. if (!key->u.ccmp.tfm) {
  240. kfree(key);
  241. return NULL;
  242. }
  243. }
  244. return key;
  245. }
  246. void ieee80211_key_link(struct ieee80211_key *key,
  247. struct ieee80211_sub_if_data *sdata,
  248. struct sta_info *sta)
  249. {
  250. struct ieee80211_key *old_key;
  251. unsigned long flags;
  252. int idx;
  253. BUG_ON(!sdata);
  254. BUG_ON(!key);
  255. idx = key->conf.keyidx;
  256. key->local = sdata->local;
  257. key->sdata = sdata;
  258. key->sta = sta;
  259. if (sta) {
  260. /*
  261. * some hardware cannot handle TKIP with QoS, so
  262. * we indicate whether QoS could be in use.
  263. */
  264. if (sta->flags & WLAN_STA_WME)
  265. key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
  266. } else {
  267. if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
  268. struct sta_info *ap;
  269. /*
  270. * We're getting a sta pointer in,
  271. * so must be under RCU read lock.
  272. */
  273. /* same here, the AP could be using QoS */
  274. ap = sta_info_get(key->local, key->sdata->u.sta.bssid);
  275. if (ap) {
  276. if (ap->flags & WLAN_STA_WME)
  277. key->conf.flags |=
  278. IEEE80211_KEY_FLAG_WMM_STA;
  279. }
  280. }
  281. }
  282. spin_lock_irqsave(&sdata->local->sta_lock, flags);
  283. if (sta)
  284. old_key = sta->key;
  285. else
  286. old_key = sdata->keys[idx];
  287. __ieee80211_key_replace(sdata, sta, old_key, key);
  288. spin_unlock_irqrestore(&sdata->local->sta_lock, flags);
  289. /* free old key later */
  290. add_todo(old_key, KEY_FLAG_TODO_DELETE);
  291. add_todo(key, KEY_FLAG_TODO_ADD_DEBUGFS);
  292. if (netif_running(sdata->dev))
  293. add_todo(key, KEY_FLAG_TODO_HWACCEL_ADD);
  294. }
  295. static void __ieee80211_key_free(struct ieee80211_key *key)
  296. {
  297. /*
  298. * Replace key with nothingness if it was ever used.
  299. */
  300. if (key->sdata)
  301. __ieee80211_key_replace(key->sdata, key->sta,
  302. key, NULL);
  303. add_todo(key, KEY_FLAG_TODO_DELETE);
  304. }
  305. void ieee80211_key_free(struct ieee80211_key *key)
  306. {
  307. unsigned long flags;
  308. if (!key)
  309. return;
  310. spin_lock_irqsave(&key->sdata->local->sta_lock, flags);
  311. __ieee80211_key_free(key);
  312. spin_unlock_irqrestore(&key->sdata->local->sta_lock, flags);
  313. }
  314. /*
  315. * To be safe against concurrent manipulations of the list (which shouldn't
  316. * actually happen) we need to hold the spinlock. But under the spinlock we
  317. * can't actually do much, so we defer processing to the todo list. Then run
  318. * the todo list to be sure the operation and possibly previously pending
  319. * operations are completed.
  320. */
  321. static void ieee80211_todo_for_each_key(struct ieee80211_sub_if_data *sdata,
  322. u32 todo_flags)
  323. {
  324. struct ieee80211_key *key;
  325. unsigned long flags;
  326. might_sleep();
  327. spin_lock_irqsave(&sdata->local->sta_lock, flags);
  328. list_for_each_entry(key, &sdata->key_list, list)
  329. add_todo(key, todo_flags);
  330. spin_unlock_irqrestore(&sdata->local->sta_lock, flags);
  331. ieee80211_key_todo();
  332. }
  333. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  334. {
  335. ASSERT_RTNL();
  336. if (WARN_ON(!netif_running(sdata->dev)))
  337. return;
  338. ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_ADD);
  339. }
  340. void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
  341. {
  342. ASSERT_RTNL();
  343. ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_REMOVE);
  344. }
  345. static void __ieee80211_key_destroy(struct ieee80211_key *key)
  346. {
  347. if (!key)
  348. return;
  349. ieee80211_key_disable_hw_accel(key);
  350. if (key->conf.alg == ALG_CCMP)
  351. ieee80211_aes_key_free(key->u.ccmp.tfm);
  352. ieee80211_debugfs_key_remove(key);
  353. kfree(key);
  354. }
  355. static void __ieee80211_key_todo(void)
  356. {
  357. struct ieee80211_key *key;
  358. bool work_done;
  359. u32 todoflags;
  360. /*
  361. * NB: sta_info_destroy relies on this!
  362. */
  363. synchronize_rcu();
  364. spin_lock(&todo_lock);
  365. while (!list_empty(&todo_list)) {
  366. key = list_first_entry(&todo_list, struct ieee80211_key, todo);
  367. list_del_init(&key->todo);
  368. todoflags = key->flags & (KEY_FLAG_TODO_ADD_DEBUGFS |
  369. KEY_FLAG_TODO_DEFKEY |
  370. KEY_FLAG_TODO_HWACCEL_ADD |
  371. KEY_FLAG_TODO_HWACCEL_REMOVE |
  372. KEY_FLAG_TODO_DELETE);
  373. key->flags &= ~todoflags;
  374. spin_unlock(&todo_lock);
  375. work_done = false;
  376. if (todoflags & KEY_FLAG_TODO_ADD_DEBUGFS) {
  377. ieee80211_debugfs_key_add(key);
  378. work_done = true;
  379. }
  380. if (todoflags & KEY_FLAG_TODO_DEFKEY) {
  381. ieee80211_debugfs_key_remove_default(key->sdata);
  382. ieee80211_debugfs_key_add_default(key->sdata);
  383. work_done = true;
  384. }
  385. if (todoflags & KEY_FLAG_TODO_HWACCEL_ADD) {
  386. ieee80211_key_enable_hw_accel(key);
  387. work_done = true;
  388. }
  389. if (todoflags & KEY_FLAG_TODO_HWACCEL_REMOVE) {
  390. ieee80211_key_disable_hw_accel(key);
  391. work_done = true;
  392. }
  393. if (todoflags & KEY_FLAG_TODO_DELETE) {
  394. __ieee80211_key_destroy(key);
  395. work_done = true;
  396. }
  397. WARN_ON(!work_done);
  398. spin_lock(&todo_lock);
  399. }
  400. spin_unlock(&todo_lock);
  401. }
  402. void ieee80211_key_todo(void)
  403. {
  404. ieee80211_key_lock();
  405. __ieee80211_key_todo();
  406. ieee80211_key_unlock();
  407. }
  408. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
  409. {
  410. struct ieee80211_key *key, *tmp;
  411. unsigned long flags;
  412. ieee80211_key_lock();
  413. ieee80211_debugfs_key_remove_default(sdata);
  414. spin_lock_irqsave(&sdata->local->sta_lock, flags);
  415. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  416. __ieee80211_key_free(key);
  417. spin_unlock_irqrestore(&sdata->local->sta_lock, flags);
  418. __ieee80211_key_todo();
  419. ieee80211_key_unlock();
  420. }