key.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 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 operations here are called under RTNL so no extra locking is
  34. * required.
  35. *
  36. * NOTE: This code requires that sta info *destruction* is done under
  37. * RTNL, otherwise it can try to access already freed STA structs
  38. * when a STA key is being freed.
  39. */
  40. static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  41. static const u8 zero_addr[ETH_ALEN];
  42. static const u8 *get_mac_for_key(struct ieee80211_key *key)
  43. {
  44. const u8 *addr = bcast_addr;
  45. /*
  46. * If we're an AP we won't ever receive frames with a non-WEP
  47. * group key so we tell the driver that by using the zero MAC
  48. * address to indicate a transmit-only key.
  49. */
  50. if (key->conf.alg != ALG_WEP &&
  51. (key->sdata->vif.type == IEEE80211_IF_TYPE_AP ||
  52. key->sdata->vif.type == IEEE80211_IF_TYPE_VLAN))
  53. addr = zero_addr;
  54. if (key->sta)
  55. addr = key->sta->addr;
  56. return addr;
  57. }
  58. static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
  59. {
  60. const u8 *addr;
  61. int ret;
  62. DECLARE_MAC_BUF(mac);
  63. if (!key->local->ops->set_key)
  64. return;
  65. /*
  66. * This makes sure that all pending flushes have
  67. * actually completed prior to uploading new key
  68. * material to the hardware. That is necessary to
  69. * avoid races between flushing STAs and adding
  70. * new keys for them.
  71. */
  72. __ieee80211_run_pending_flush(key->local);
  73. addr = get_mac_for_key(key);
  74. ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY,
  75. key->sdata->dev->dev_addr, addr,
  76. &key->conf);
  77. if (!ret)
  78. key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
  79. if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
  80. printk(KERN_ERR "mac80211-%s: failed to set key "
  81. "(%d, %s) to hardware (%d)\n",
  82. wiphy_name(key->local->hw.wiphy),
  83. key->conf.keyidx, print_mac(mac, addr), ret);
  84. }
  85. static void ieee80211_key_mark_hw_accel_off(struct ieee80211_key *key)
  86. {
  87. if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
  88. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  89. key->flags |= KEY_FLAG_REMOVE_FROM_HARDWARE;
  90. }
  91. }
  92. static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
  93. {
  94. const u8 *addr;
  95. int ret;
  96. DECLARE_MAC_BUF(mac);
  97. if (!key || !key->local->ops->set_key)
  98. return;
  99. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
  100. !(key->flags & KEY_FLAG_REMOVE_FROM_HARDWARE))
  101. return;
  102. addr = get_mac_for_key(key);
  103. ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY,
  104. key->sdata->dev->dev_addr, addr,
  105. &key->conf);
  106. if (ret)
  107. printk(KERN_ERR "mac80211-%s: failed to remove key "
  108. "(%d, %s) from hardware (%d)\n",
  109. wiphy_name(key->local->hw.wiphy),
  110. key->conf.keyidx, print_mac(mac, addr), ret);
  111. key->flags &= ~(KEY_FLAG_UPLOADED_TO_HARDWARE |
  112. KEY_FLAG_REMOVE_FROM_HARDWARE);
  113. }
  114. struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg,
  115. int idx,
  116. size_t key_len,
  117. const u8 *key_data)
  118. {
  119. struct ieee80211_key *key;
  120. BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS);
  121. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  122. if (!key)
  123. return NULL;
  124. /*
  125. * Default to software encryption; we'll later upload the
  126. * key to the hardware if possible.
  127. */
  128. key->conf.flags = 0;
  129. key->flags = 0;
  130. key->conf.alg = alg;
  131. key->conf.keyidx = idx;
  132. key->conf.keylen = key_len;
  133. memcpy(key->conf.key, key_data, key_len);
  134. INIT_LIST_HEAD(&key->list);
  135. if (alg == ALG_CCMP) {
  136. /*
  137. * Initialize AES key state here as an optimization so that
  138. * it does not need to be initialized for every packet.
  139. */
  140. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
  141. if (!key->u.ccmp.tfm) {
  142. ieee80211_key_free(key);
  143. return NULL;
  144. }
  145. }
  146. return key;
  147. }
  148. static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
  149. struct sta_info *sta,
  150. struct ieee80211_key *key,
  151. struct ieee80211_key *new)
  152. {
  153. int idx, defkey;
  154. if (new)
  155. list_add(&new->list, &sdata->key_list);
  156. if (sta) {
  157. rcu_assign_pointer(sta->key, new);
  158. } else {
  159. WARN_ON(new && key && new->conf.keyidx != key->conf.keyidx);
  160. if (key)
  161. idx = key->conf.keyidx;
  162. else
  163. idx = new->conf.keyidx;
  164. defkey = key && sdata->default_key == key;
  165. if (defkey && !new)
  166. ieee80211_set_default_key(sdata, -1);
  167. rcu_assign_pointer(sdata->keys[idx], new);
  168. if (defkey && new)
  169. ieee80211_set_default_key(sdata, new->conf.keyidx);
  170. }
  171. if (key) {
  172. ieee80211_key_mark_hw_accel_off(key);
  173. /*
  174. * We'll use an empty list to indicate that the key
  175. * has already been removed.
  176. */
  177. list_del_init(&key->list);
  178. }
  179. }
  180. void ieee80211_key_link(struct ieee80211_key *key,
  181. struct ieee80211_sub_if_data *sdata,
  182. struct sta_info *sta)
  183. {
  184. struct ieee80211_key *old_key;
  185. int idx;
  186. ASSERT_RTNL();
  187. might_sleep();
  188. BUG_ON(!sdata);
  189. BUG_ON(!key);
  190. idx = key->conf.keyidx;
  191. key->local = sdata->local;
  192. key->sdata = sdata;
  193. key->sta = sta;
  194. ieee80211_debugfs_key_add(key->local, key);
  195. if (sta) {
  196. ieee80211_debugfs_key_sta_link(key, sta);
  197. /*
  198. * some hardware cannot handle TKIP with QoS, so
  199. * we indicate whether QoS could be in use.
  200. */
  201. if (sta->flags & WLAN_STA_WME)
  202. key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
  203. } else {
  204. if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
  205. struct sta_info *ap;
  206. rcu_read_lock();
  207. /* same here, the AP could be using QoS */
  208. ap = sta_info_get(key->local, key->sdata->u.sta.bssid);
  209. if (ap) {
  210. if (ap->flags & WLAN_STA_WME)
  211. key->conf.flags |=
  212. IEEE80211_KEY_FLAG_WMM_STA;
  213. }
  214. rcu_read_unlock();
  215. }
  216. }
  217. if (sta)
  218. old_key = sta->key;
  219. else
  220. old_key = sdata->keys[idx];
  221. __ieee80211_key_replace(sdata, sta, old_key, key);
  222. if (old_key) {
  223. synchronize_rcu();
  224. ieee80211_key_free(old_key);
  225. }
  226. if (netif_running(sdata->dev))
  227. ieee80211_key_enable_hw_accel(key);
  228. }
  229. void ieee80211_key_free(struct ieee80211_key *key)
  230. {
  231. ASSERT_RTNL();
  232. might_sleep();
  233. if (!key)
  234. return;
  235. if (key->sdata) {
  236. /*
  237. * Replace key with nothingness.
  238. *
  239. * Because other code may have key reference (RCU protected)
  240. * right now, we then wait for a grace period before freeing
  241. * it.
  242. * An empty list indicates it was never added to the key list
  243. * or has been removed already. It may, however, still be in
  244. * hardware for acceleration.
  245. */
  246. if (!list_empty(&key->list))
  247. __ieee80211_key_replace(key->sdata, key->sta,
  248. key, NULL);
  249. /*
  250. * Do NOT remove this without looking at sta_info_destroy()
  251. */
  252. synchronize_rcu();
  253. /*
  254. * Remove from hwaccel if appropriate, this will
  255. * only happen when the key is actually unlinked,
  256. * it will already be done when the key was replaced.
  257. */
  258. ieee80211_key_disable_hw_accel(key);
  259. }
  260. if (key->conf.alg == ALG_CCMP)
  261. ieee80211_aes_key_free(key->u.ccmp.tfm);
  262. ieee80211_debugfs_key_remove(key);
  263. kfree(key);
  264. }
  265. void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
  266. {
  267. struct ieee80211_key *key = NULL;
  268. if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
  269. key = sdata->keys[idx];
  270. if (sdata->default_key != key) {
  271. ieee80211_debugfs_key_remove_default(sdata);
  272. rcu_assign_pointer(sdata->default_key, key);
  273. if (sdata->default_key)
  274. ieee80211_debugfs_key_add_default(sdata);
  275. }
  276. }
  277. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
  278. {
  279. struct ieee80211_key *key, *tmp;
  280. LIST_HEAD(tmp_list);
  281. ASSERT_RTNL();
  282. might_sleep();
  283. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  284. ieee80211_key_free(key);
  285. }
  286. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  287. {
  288. struct ieee80211_key *key;
  289. ASSERT_RTNL();
  290. might_sleep();
  291. if (WARN_ON(!netif_running(sdata->dev)))
  292. return;
  293. list_for_each_entry(key, &sdata->key_list, list)
  294. ieee80211_key_enable_hw_accel(key);
  295. }
  296. void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
  297. {
  298. struct ieee80211_key *key;
  299. ASSERT_RTNL();
  300. might_sleep();
  301. list_for_each_entry(key, &sdata->key_list, list)
  302. ieee80211_key_disable_hw_accel(key);
  303. }