key.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. 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. INIT_LIST_HEAD(&key->list);
  127. if (alg == ALG_CCMP) {
  128. /*
  129. * Initialize AES key state here as an optimization so that
  130. * it does not need to be initialized for every packet.
  131. */
  132. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
  133. if (!key->u.ccmp.tfm) {
  134. ieee80211_key_free(key);
  135. return NULL;
  136. }
  137. }
  138. return key;
  139. }
  140. static void __ieee80211_key_replace(struct ieee80211_sub_if_data *sdata,
  141. struct sta_info *sta,
  142. struct ieee80211_key *key,
  143. struct ieee80211_key *new)
  144. {
  145. int idx, defkey;
  146. if (new)
  147. list_add(&new->list, &sdata->key_list);
  148. if (sta) {
  149. rcu_assign_pointer(sta->key, new);
  150. } else {
  151. WARN_ON(new && key && new->conf.keyidx != key->conf.keyidx);
  152. if (key)
  153. idx = key->conf.keyidx;
  154. else
  155. idx = new->conf.keyidx;
  156. defkey = key && sdata->default_key == key;
  157. if (defkey && !new)
  158. ieee80211_set_default_key(sdata, -1);
  159. rcu_assign_pointer(sdata->keys[idx], new);
  160. if (defkey && new)
  161. ieee80211_set_default_key(sdata, new->conf.keyidx);
  162. }
  163. if (key) {
  164. ieee80211_key_mark_hw_accel_off(key);
  165. /*
  166. * We'll use an empty list to indicate that the key
  167. * has already been removed.
  168. */
  169. list_del_init(&key->list);
  170. }
  171. }
  172. void ieee80211_key_link(struct ieee80211_key *key,
  173. struct ieee80211_sub_if_data *sdata,
  174. struct sta_info *sta)
  175. {
  176. struct ieee80211_key *old_key;
  177. int idx;
  178. ASSERT_RTNL();
  179. might_sleep();
  180. BUG_ON(!sdata);
  181. BUG_ON(!key);
  182. idx = key->conf.keyidx;
  183. key->local = sdata->local;
  184. key->sdata = sdata;
  185. key->sta = sta;
  186. ieee80211_debugfs_key_add(key->local, key);
  187. if (sta) {
  188. ieee80211_debugfs_key_sta_link(key, sta);
  189. /*
  190. * some hardware cannot handle TKIP with QoS, so
  191. * we indicate whether QoS could be in use.
  192. */
  193. if (sta->flags & WLAN_STA_WME)
  194. key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
  195. } else {
  196. if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
  197. struct sta_info *ap;
  198. rcu_read_lock();
  199. /* same here, the AP could be using QoS */
  200. ap = sta_info_get(key->local, key->sdata->u.sta.bssid);
  201. if (ap) {
  202. if (ap->flags & WLAN_STA_WME)
  203. key->conf.flags |=
  204. IEEE80211_KEY_FLAG_WMM_STA;
  205. }
  206. rcu_read_unlock();
  207. }
  208. }
  209. if (sta)
  210. old_key = sta->key;
  211. else
  212. old_key = sdata->keys[idx];
  213. __ieee80211_key_replace(sdata, sta, old_key, key);
  214. if (old_key) {
  215. synchronize_rcu();
  216. ieee80211_key_free(old_key);
  217. }
  218. if (netif_running(sdata->dev))
  219. ieee80211_key_enable_hw_accel(key);
  220. }
  221. void ieee80211_key_free(struct ieee80211_key *key)
  222. {
  223. ASSERT_RTNL();
  224. might_sleep();
  225. if (!key)
  226. return;
  227. if (key->sdata) {
  228. /*
  229. * Replace key with nothingness.
  230. *
  231. * Because other code may have key reference (RCU protected)
  232. * right now, we then wait for a grace period before freeing
  233. * it.
  234. * An empty list indicates it was never added to the key list
  235. * or has been removed already. It may, however, still be in
  236. * hardware for acceleration.
  237. */
  238. if (!list_empty(&key->list))
  239. __ieee80211_key_replace(key->sdata, key->sta,
  240. key, NULL);
  241. /*
  242. * Do NOT remove this without looking at sta_info_destroy()
  243. */
  244. synchronize_rcu();
  245. /*
  246. * Remove from hwaccel if appropriate, this will
  247. * only happen when the key is actually unlinked,
  248. * it will already be done when the key was replaced.
  249. */
  250. ieee80211_key_disable_hw_accel(key);
  251. }
  252. if (key->conf.alg == ALG_CCMP)
  253. ieee80211_aes_key_free(key->u.ccmp.tfm);
  254. ieee80211_debugfs_key_remove(key);
  255. kfree(key);
  256. }
  257. void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
  258. {
  259. struct ieee80211_key *key = NULL;
  260. if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
  261. key = sdata->keys[idx];
  262. if (sdata->default_key != key) {
  263. ieee80211_debugfs_key_remove_default(sdata);
  264. rcu_assign_pointer(sdata->default_key, key);
  265. if (sdata->default_key)
  266. ieee80211_debugfs_key_add_default(sdata);
  267. }
  268. }
  269. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
  270. {
  271. struct ieee80211_key *key, *tmp;
  272. LIST_HEAD(tmp_list);
  273. ASSERT_RTNL();
  274. might_sleep();
  275. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  276. ieee80211_key_free(key);
  277. }
  278. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  279. {
  280. struct ieee80211_key *key;
  281. ASSERT_RTNL();
  282. might_sleep();
  283. if (WARN_ON(!netif_running(sdata->dev)))
  284. return;
  285. list_for_each_entry(key, &sdata->key_list, list)
  286. ieee80211_key_enable_hw_accel(key);
  287. }
  288. void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
  289. {
  290. struct ieee80211_key *key;
  291. ASSERT_RTNL();
  292. might_sleep();
  293. list_for_each_entry(key, &sdata->key_list, list)
  294. ieee80211_key_disable_hw_accel(key);
  295. }