key.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. * 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. addr = get_mac_for_key(key);
  66. ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY,
  67. key->sdata->dev->dev_addr, addr,
  68. &key->conf);
  69. if (!ret)
  70. key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
  71. if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
  72. printk(KERN_ERR "mac80211-%s: failed to set key "
  73. "(%d, %s) to hardware (%d)\n",
  74. wiphy_name(key->local->hw.wiphy),
  75. key->conf.keyidx, print_mac(mac, addr), ret);
  76. }
  77. static void ieee80211_key_mark_hw_accel_off(struct ieee80211_key *key)
  78. {
  79. if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
  80. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  81. key->flags |= KEY_FLAG_REMOVE_FROM_HARDWARE;
  82. }
  83. }
  84. static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
  85. {
  86. const u8 *addr;
  87. int ret;
  88. DECLARE_MAC_BUF(mac);
  89. if (!key || !key->local->ops->set_key)
  90. return;
  91. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
  92. !(key->flags & KEY_FLAG_REMOVE_FROM_HARDWARE))
  93. return;
  94. addr = get_mac_for_key(key);
  95. ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY,
  96. key->sdata->dev->dev_addr, addr,
  97. &key->conf);
  98. if (ret)
  99. printk(KERN_ERR "mac80211-%s: failed to remove key "
  100. "(%d, %s) from hardware (%d)\n",
  101. wiphy_name(key->local->hw.wiphy),
  102. key->conf.keyidx, print_mac(mac, addr), ret);
  103. key->flags &= ~(KEY_FLAG_UPLOADED_TO_HARDWARE |
  104. KEY_FLAG_REMOVE_FROM_HARDWARE);
  105. }
  106. struct ieee80211_key *ieee80211_key_alloc(enum ieee80211_key_alg alg,
  107. int idx,
  108. size_t key_len,
  109. const u8 *key_data)
  110. {
  111. struct ieee80211_key *key;
  112. BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS);
  113. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  114. if (!key)
  115. return NULL;
  116. /*
  117. * Default to software encryption; we'll later upload the
  118. * key to the hardware if possible.
  119. */
  120. key->conf.flags = 0;
  121. key->flags = 0;
  122. key->conf.alg = alg;
  123. key->conf.keyidx = idx;
  124. key->conf.keylen = key_len;
  125. memcpy(key->conf.key, key_data, key_len);
  126. if (alg == ALG_CCMP) {
  127. /*
  128. * Initialize AES key state here as an optimization so that
  129. * it does not need to be initialized for every packet.
  130. */
  131. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
  132. if (!key->u.ccmp.tfm) {
  133. ieee80211_key_free(key);
  134. return NULL;
  135. }
  136. }
  137. return key;
  138. }
  139. static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
  140. struct sta_info *sta,
  141. struct ieee80211_key *key,
  142. struct ieee80211_key *new)
  143. {
  144. int idx, defkey;
  145. if (sta) {
  146. rcu_assign_pointer(sta->key, new);
  147. } else {
  148. WARN_ON(new && key && new->conf.keyidx != key->conf.keyidx);
  149. if (key)
  150. idx = key->conf.keyidx;
  151. else
  152. idx = new->conf.keyidx;
  153. defkey = key && sdata->default_key == key;
  154. if (defkey && !new)
  155. ieee80211_set_default_key(sdata, -1);
  156. rcu_assign_pointer(sdata->keys[idx], new);
  157. if (defkey && new)
  158. ieee80211_set_default_key(sdata, new->conf.keyidx);
  159. }
  160. if (key) {
  161. ieee80211_key_mark_hw_accel_off(key);
  162. list_del(&key->list);
  163. }
  164. }
  165. void ieee80211_key_link(struct ieee80211_key *key,
  166. struct ieee80211_sub_if_data *sdata,
  167. struct sta_info *sta)
  168. {
  169. struct ieee80211_key *old_key;
  170. int idx;
  171. ASSERT_RTNL();
  172. might_sleep();
  173. BUG_ON(!sdata);
  174. BUG_ON(!key);
  175. idx = key->conf.keyidx;
  176. key->local = sdata->local;
  177. key->sdata = sdata;
  178. key->sta = sta;
  179. ieee80211_debugfs_key_add(key->local, key);
  180. if (sta) {
  181. ieee80211_debugfs_key_sta_link(key, sta);
  182. /*
  183. * some hardware cannot handle TKIP with QoS, so
  184. * we indicate whether QoS could be in use.
  185. */
  186. if (sta->flags & WLAN_STA_WME)
  187. key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
  188. } else {
  189. if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
  190. struct sta_info *ap;
  191. /* same here, the AP could be using QoS */
  192. ap = sta_info_get(key->local, key->sdata->u.sta.bssid);
  193. if (ap) {
  194. if (ap->flags & WLAN_STA_WME)
  195. key->conf.flags |=
  196. IEEE80211_KEY_FLAG_WMM_STA;
  197. sta_info_put(ap);
  198. }
  199. }
  200. }
  201. if (sta)
  202. old_key = sta->key;
  203. else
  204. old_key = sdata->keys[idx];
  205. __ieee80211_key_replace(sdata, sta, old_key, key);
  206. list_add(&key->list, &sdata->key_list);
  207. synchronize_rcu();
  208. ieee80211_key_free(old_key);
  209. ieee80211_key_enable_hw_accel(key);
  210. }
  211. void ieee80211_key_free(struct ieee80211_key *key)
  212. {
  213. ASSERT_RTNL();
  214. might_sleep();
  215. if (!key)
  216. return;
  217. if (key->sdata) {
  218. /*
  219. * Replace key with nothingness.
  220. *
  221. * Because other code may have key reference (RCU protected)
  222. * right now, we then wait for a grace period before freeing
  223. * it.
  224. */
  225. __ieee80211_key_replace(key->sdata, key->sta, key, NULL);
  226. synchronize_rcu();
  227. /*
  228. * Remove from hwaccel if appropriate, this will
  229. * only happen when the key is actually unlinked,
  230. * it will already be done when the key was replaced.
  231. */
  232. ieee80211_key_disable_hw_accel(key);
  233. }
  234. if (key->conf.alg == ALG_CCMP)
  235. ieee80211_aes_key_free(key->u.ccmp.tfm);
  236. ieee80211_debugfs_key_remove(key);
  237. kfree(key);
  238. }
  239. void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
  240. {
  241. struct ieee80211_key *key = NULL;
  242. if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
  243. key = sdata->keys[idx];
  244. if (sdata->default_key != key) {
  245. ieee80211_debugfs_key_remove_default(sdata);
  246. rcu_assign_pointer(sdata->default_key, key);
  247. if (sdata->default_key)
  248. ieee80211_debugfs_key_add_default(sdata);
  249. }
  250. }
  251. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
  252. {
  253. struct ieee80211_key *key, *tmp;
  254. LIST_HEAD(tmp_list);
  255. ASSERT_RTNL();
  256. might_sleep();
  257. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  258. ieee80211_key_free(key);
  259. }
  260. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  261. {
  262. struct ieee80211_key *key;
  263. ASSERT_RTNL();
  264. might_sleep();
  265. if (WARN_ON(!netif_running(sdata->dev)))
  266. return;
  267. list_for_each_entry(key, &sdata->key_list, list)
  268. ieee80211_key_enable_hw_accel(key);
  269. }
  270. void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
  271. {
  272. struct ieee80211_key *key;
  273. ASSERT_RTNL();
  274. might_sleep();
  275. list_for_each_entry(key, &sdata->key_list, list)
  276. ieee80211_key_disable_hw_accel(key);
  277. }