key.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. /*
  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 const u8 *get_mac_for_key(struct ieee80211_key *key)
  99. {
  100. const u8 *addr = bcast_addr;
  101. /*
  102. * If we're an AP we won't ever receive frames with a non-WEP
  103. * group key so we tell the driver that by using the zero MAC
  104. * address to indicate a transmit-only key.
  105. */
  106. if (key->conf.alg != ALG_WEP &&
  107. (key->sdata->vif.type == NL80211_IFTYPE_AP ||
  108. key->sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
  109. addr = zero_addr;
  110. if (key->sta)
  111. addr = key->sta->sta.addr;
  112. return addr;
  113. }
  114. static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
  115. {
  116. const u8 *addr;
  117. int ret;
  118. DECLARE_MAC_BUF(mac);
  119. assert_key_lock();
  120. might_sleep();
  121. if (!key->local->ops->set_key)
  122. return;
  123. addr = get_mac_for_key(key);
  124. ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY,
  125. key->sdata->dev->dev_addr, addr,
  126. &key->conf);
  127. if (!ret) {
  128. spin_lock(&todo_lock);
  129. key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
  130. spin_unlock(&todo_lock);
  131. }
  132. if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
  133. printk(KERN_ERR "mac80211-%s: failed to set key "
  134. "(%d, %s) to hardware (%d)\n",
  135. wiphy_name(key->local->hw.wiphy),
  136. key->conf.keyidx, print_mac(mac, addr), ret);
  137. }
  138. static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
  139. {
  140. const u8 *addr;
  141. int ret;
  142. DECLARE_MAC_BUF(mac);
  143. assert_key_lock();
  144. might_sleep();
  145. if (!key || !key->local->ops->set_key)
  146. return;
  147. spin_lock(&todo_lock);
  148. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) {
  149. spin_unlock(&todo_lock);
  150. return;
  151. }
  152. spin_unlock(&todo_lock);
  153. addr = get_mac_for_key(key);
  154. ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY,
  155. key->sdata->dev->dev_addr, addr,
  156. &key->conf);
  157. if (ret)
  158. printk(KERN_ERR "mac80211-%s: failed to remove key "
  159. "(%d, %s) from hardware (%d)\n",
  160. wiphy_name(key->local->hw.wiphy),
  161. key->conf.keyidx, print_mac(mac, addr), ret);
  162. spin_lock(&todo_lock);
  163. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  164. spin_unlock(&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 __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
  184. struct sta_info *sta,
  185. struct ieee80211_key *old,
  186. struct ieee80211_key *new)
  187. {
  188. int idx, defkey;
  189. if (new)
  190. list_add(&new->list, &sdata->key_list);
  191. if (sta) {
  192. rcu_assign_pointer(sta->key, new);
  193. } else {
  194. WARN_ON(new && old && new->conf.keyidx != old->conf.keyidx);
  195. if (old)
  196. idx = old->conf.keyidx;
  197. else
  198. idx = new->conf.keyidx;
  199. defkey = old && sdata->default_key == old;
  200. if (defkey && !new)
  201. __ieee80211_set_default_key(sdata, -1);
  202. rcu_assign_pointer(sdata->keys[idx], new);
  203. if (defkey && new)
  204. __ieee80211_set_default_key(sdata, new->conf.keyidx);
  205. }
  206. if (old) {
  207. /*
  208. * We'll use an empty list to indicate that the key
  209. * has already been removed.
  210. */
  211. list_del_init(&old->list);
  212. }
  213. }
  214. struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg,
  215. int idx,
  216. size_t key_len,
  217. const u8 *key_data)
  218. {
  219. struct ieee80211_key *key;
  220. BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS);
  221. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  222. if (!key)
  223. return NULL;
  224. /*
  225. * Default to software encryption; we'll later upload the
  226. * key to the hardware if possible.
  227. */
  228. key->conf.flags = 0;
  229. key->flags = 0;
  230. key->conf.alg = alg;
  231. key->conf.keyidx = idx;
  232. key->conf.keylen = key_len;
  233. switch (alg) {
  234. case ALG_WEP:
  235. key->conf.iv_len = WEP_IV_LEN;
  236. key->conf.icv_len = WEP_ICV_LEN;
  237. break;
  238. case ALG_TKIP:
  239. key->conf.iv_len = TKIP_IV_LEN;
  240. key->conf.icv_len = TKIP_ICV_LEN;
  241. break;
  242. case ALG_CCMP:
  243. key->conf.iv_len = CCMP_HDR_LEN;
  244. key->conf.icv_len = CCMP_MIC_LEN;
  245. break;
  246. }
  247. memcpy(key->conf.key, key_data, key_len);
  248. INIT_LIST_HEAD(&key->list);
  249. INIT_LIST_HEAD(&key->todo);
  250. if (alg == ALG_CCMP) {
  251. /*
  252. * Initialize AES key state here as an optimization so that
  253. * it does not need to be initialized for every packet.
  254. */
  255. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
  256. if (!key->u.ccmp.tfm) {
  257. kfree(key);
  258. return NULL;
  259. }
  260. }
  261. return key;
  262. }
  263. void ieee80211_key_link(struct ieee80211_key *key,
  264. struct ieee80211_sub_if_data *sdata,
  265. struct sta_info *sta)
  266. {
  267. struct ieee80211_key *old_key;
  268. unsigned long flags;
  269. int idx;
  270. BUG_ON(!sdata);
  271. BUG_ON(!key);
  272. idx = key->conf.keyidx;
  273. key->local = sdata->local;
  274. key->sdata = sdata;
  275. key->sta = sta;
  276. if (sta) {
  277. /*
  278. * some hardware cannot handle TKIP with QoS, so
  279. * we indicate whether QoS could be in use.
  280. */
  281. if (test_sta_flags(sta, WLAN_STA_WME))
  282. key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
  283. /*
  284. * This key is for a specific sta interface,
  285. * inform the driver that it should try to store
  286. * this key as pairwise key.
  287. */
  288. key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;
  289. } else {
  290. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  291. struct sta_info *ap;
  292. /*
  293. * We're getting a sta pointer in,
  294. * so must be under RCU read lock.
  295. */
  296. /* same here, the AP could be using QoS */
  297. ap = sta_info_get(key->local, key->sdata->u.sta.bssid);
  298. if (ap) {
  299. if (test_sta_flags(ap, WLAN_STA_WME))
  300. key->conf.flags |=
  301. IEEE80211_KEY_FLAG_WMM_STA;
  302. }
  303. }
  304. }
  305. spin_lock_irqsave(&sdata->local->key_lock, flags);
  306. if (sta)
  307. old_key = sta->key;
  308. else
  309. old_key = sdata->keys[idx];
  310. __ieee80211_key_replace(sdata, sta, old_key, key);
  311. spin_unlock_irqrestore(&sdata->local->key_lock, flags);
  312. /* free old key later */
  313. add_todo(old_key, KEY_FLAG_TODO_DELETE);
  314. add_todo(key, KEY_FLAG_TODO_ADD_DEBUGFS);
  315. if (netif_running(sdata->dev))
  316. add_todo(key, KEY_FLAG_TODO_HWACCEL_ADD);
  317. }
  318. static void __ieee80211_key_free(struct ieee80211_key *key)
  319. {
  320. /*
  321. * Replace key with nothingness if it was ever used.
  322. */
  323. if (key->sdata)
  324. __ieee80211_key_replace(key->sdata, key->sta,
  325. key, NULL);
  326. add_todo(key, KEY_FLAG_TODO_DELETE);
  327. }
  328. void ieee80211_key_free(struct ieee80211_key *key)
  329. {
  330. unsigned long flags;
  331. if (!key)
  332. return;
  333. if (!key->sdata) {
  334. /* The key has not been linked yet, simply free it
  335. * and don't Oops */
  336. if (key->conf.alg == ALG_CCMP)
  337. ieee80211_aes_key_free(key->u.ccmp.tfm);
  338. kfree(key);
  339. return;
  340. }
  341. spin_lock_irqsave(&key->sdata->local->key_lock, flags);
  342. __ieee80211_key_free(key);
  343. spin_unlock_irqrestore(&key->sdata->local->key_lock, flags);
  344. }
  345. /*
  346. * To be safe against concurrent manipulations of the list (which shouldn't
  347. * actually happen) we need to hold the spinlock. But under the spinlock we
  348. * can't actually do much, so we defer processing to the todo list. Then run
  349. * the todo list to be sure the operation and possibly previously pending
  350. * operations are completed.
  351. */
  352. static void ieee80211_todo_for_each_key(struct ieee80211_sub_if_data *sdata,
  353. u32 todo_flags)
  354. {
  355. struct ieee80211_key *key;
  356. unsigned long flags;
  357. might_sleep();
  358. spin_lock_irqsave(&sdata->local->key_lock, flags);
  359. list_for_each_entry(key, &sdata->key_list, list)
  360. add_todo(key, todo_flags);
  361. spin_unlock_irqrestore(&sdata->local->key_lock, flags);
  362. ieee80211_key_todo();
  363. }
  364. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  365. {
  366. ASSERT_RTNL();
  367. if (WARN_ON(!netif_running(sdata->dev)))
  368. return;
  369. ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_ADD);
  370. }
  371. void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
  372. {
  373. ASSERT_RTNL();
  374. ieee80211_todo_for_each_key(sdata, KEY_FLAG_TODO_HWACCEL_REMOVE);
  375. }
  376. static void __ieee80211_key_destroy(struct ieee80211_key *key)
  377. {
  378. if (!key)
  379. return;
  380. ieee80211_key_disable_hw_accel(key);
  381. if (key->conf.alg == ALG_CCMP)
  382. ieee80211_aes_key_free(key->u.ccmp.tfm);
  383. ieee80211_debugfs_key_remove(key);
  384. kfree(key);
  385. }
  386. static void __ieee80211_key_todo(void)
  387. {
  388. struct ieee80211_key *key;
  389. bool work_done;
  390. u32 todoflags;
  391. /*
  392. * NB: sta_info_destroy relies on this!
  393. */
  394. synchronize_rcu();
  395. spin_lock(&todo_lock);
  396. while (!list_empty(&todo_list)) {
  397. key = list_first_entry(&todo_list, struct ieee80211_key, todo);
  398. list_del_init(&key->todo);
  399. todoflags = key->flags & (KEY_FLAG_TODO_ADD_DEBUGFS |
  400. KEY_FLAG_TODO_DEFKEY |
  401. KEY_FLAG_TODO_HWACCEL_ADD |
  402. KEY_FLAG_TODO_HWACCEL_REMOVE |
  403. KEY_FLAG_TODO_DELETE);
  404. key->flags &= ~todoflags;
  405. spin_unlock(&todo_lock);
  406. work_done = false;
  407. if (todoflags & KEY_FLAG_TODO_ADD_DEBUGFS) {
  408. ieee80211_debugfs_key_add(key);
  409. work_done = true;
  410. }
  411. if (todoflags & KEY_FLAG_TODO_DEFKEY) {
  412. ieee80211_debugfs_key_remove_default(key->sdata);
  413. ieee80211_debugfs_key_add_default(key->sdata);
  414. work_done = true;
  415. }
  416. if (todoflags & KEY_FLAG_TODO_HWACCEL_ADD) {
  417. ieee80211_key_enable_hw_accel(key);
  418. work_done = true;
  419. }
  420. if (todoflags & KEY_FLAG_TODO_HWACCEL_REMOVE) {
  421. ieee80211_key_disable_hw_accel(key);
  422. work_done = true;
  423. }
  424. if (todoflags & KEY_FLAG_TODO_DELETE) {
  425. __ieee80211_key_destroy(key);
  426. work_done = true;
  427. }
  428. WARN_ON(!work_done);
  429. spin_lock(&todo_lock);
  430. }
  431. spin_unlock(&todo_lock);
  432. }
  433. void ieee80211_key_todo(void)
  434. {
  435. ieee80211_key_lock();
  436. __ieee80211_key_todo();
  437. ieee80211_key_unlock();
  438. }
  439. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
  440. {
  441. struct ieee80211_key *key, *tmp;
  442. unsigned long flags;
  443. ieee80211_key_lock();
  444. ieee80211_debugfs_key_remove_default(sdata);
  445. spin_lock_irqsave(&sdata->local->key_lock, flags);
  446. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  447. __ieee80211_key_free(key);
  448. spin_unlock_irqrestore(&sdata->local->key_lock, flags);
  449. __ieee80211_key_todo();
  450. ieee80211_key_unlock();
  451. }