key.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 <net/mac80211.h>
  16. #include "ieee80211_i.h"
  17. #include "debugfs_key.h"
  18. #include "aes_ccm.h"
  19. /*
  20. * Key handling basics
  21. *
  22. * Key handling in mac80211 is done based on per-interface (sub_if_data)
  23. * keys and per-station keys. Since each station belongs to an interface,
  24. * each station key also belongs to that interface.
  25. *
  26. * Hardware acceleration is done on a best-effort basis, for each key
  27. * that is eligible the hardware is asked to enable that key but if
  28. * it cannot do that they key is simply kept for software encryption.
  29. * There is currently no way of knowing this except by looking into
  30. * debugfs.
  31. *
  32. * All operations here are called under RTNL so no extra locking is
  33. * required.
  34. */
  35. static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  36. static const u8 zero_addr[ETH_ALEN];
  37. static const u8 *get_mac_for_key(struct ieee80211_key *key)
  38. {
  39. const u8 *addr = bcast_addr;
  40. /*
  41. * If we're an AP we won't ever receive frames with a non-WEP
  42. * group key so we tell the driver that by using the zero MAC
  43. * address to indicate a transmit-only key.
  44. */
  45. if (key->conf.alg != ALG_WEP &&
  46. (key->sdata->type == IEEE80211_IF_TYPE_AP ||
  47. key->sdata->type == IEEE80211_IF_TYPE_VLAN))
  48. addr = zero_addr;
  49. if (key->sta)
  50. addr = key->sta->addr;
  51. return addr;
  52. }
  53. static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
  54. {
  55. const u8 *addr;
  56. int ret;
  57. if (!key->local->ops->set_key)
  58. return;
  59. addr = get_mac_for_key(key);
  60. ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY,
  61. key->sdata->dev->dev_addr, addr,
  62. &key->conf);
  63. if (!ret)
  64. key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
  65. if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
  66. printk(KERN_ERR "mac80211-%s: failed to set key "
  67. "(%d, " MAC_FMT ") to hardware (%d)\n",
  68. wiphy_name(key->local->hw.wiphy),
  69. key->conf.keyidx, MAC_ARG(addr), ret);
  70. }
  71. static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
  72. {
  73. const u8 *addr;
  74. int ret;
  75. if (!key->local->ops->set_key)
  76. return;
  77. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  78. return;
  79. addr = get_mac_for_key(key);
  80. ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY,
  81. key->sdata->dev->dev_addr, addr,
  82. &key->conf);
  83. if (ret)
  84. printk(KERN_ERR "mac80211-%s: failed to remove key "
  85. "(%d, " MAC_FMT ") from hardware (%d)\n",
  86. wiphy_name(key->local->hw.wiphy),
  87. key->conf.keyidx, MAC_ARG(addr), ret);
  88. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  89. }
  90. struct ieee80211_key *ieee80211_key_alloc(struct ieee80211_sub_if_data *sdata,
  91. struct sta_info *sta,
  92. ieee80211_key_alg alg,
  93. int idx,
  94. size_t key_len,
  95. const u8 *key_data)
  96. {
  97. struct ieee80211_key *key;
  98. BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS);
  99. BUG_ON(alg == ALG_NONE);
  100. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  101. if (!key)
  102. return NULL;
  103. /*
  104. * Default to software encryption; we'll later upload the
  105. * key to the hardware if possible.
  106. */
  107. key->conf.flags = 0;
  108. key->flags = 0;
  109. key->conf.alg = alg;
  110. key->conf.keyidx = idx;
  111. key->conf.keylen = key_len;
  112. memcpy(key->conf.key, key_data, key_len);
  113. key->local = sdata->local;
  114. key->sdata = sdata;
  115. key->sta = sta;
  116. if (alg == ALG_CCMP) {
  117. /*
  118. * Initialize AES key state here as an optimization so that
  119. * it does not need to be initialized for every packet.
  120. */
  121. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
  122. if (!key->u.ccmp.tfm) {
  123. ieee80211_key_free(key);
  124. return NULL;
  125. }
  126. }
  127. ieee80211_debugfs_key_add(key->local, key);
  128. /* remove key first */
  129. if (sta)
  130. ieee80211_key_free(sta->key);
  131. else
  132. ieee80211_key_free(sdata->keys[idx]);
  133. if (sta) {
  134. ieee80211_debugfs_key_sta_link(key, sta);
  135. /*
  136. * some hardware cannot handle TKIP with QoS, so
  137. * we indicate whether QoS could be in use.
  138. */
  139. if (sta->flags & WLAN_STA_WME)
  140. key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
  141. } else {
  142. if (sdata->type == IEEE80211_IF_TYPE_STA) {
  143. struct sta_info *ap;
  144. /* same here, the AP could be using QoS */
  145. ap = sta_info_get(key->local, key->sdata->u.sta.bssid);
  146. if (ap) {
  147. if (ap->flags & WLAN_STA_WME)
  148. key->conf.flags |=
  149. IEEE80211_KEY_FLAG_WMM_STA;
  150. sta_info_put(ap);
  151. }
  152. }
  153. }
  154. /* enable hwaccel if appropriate */
  155. if (netif_running(key->sdata->dev))
  156. ieee80211_key_enable_hw_accel(key);
  157. if (sta)
  158. rcu_assign_pointer(sta->key, key);
  159. else
  160. rcu_assign_pointer(sdata->keys[idx], key);
  161. list_add(&key->list, &sdata->key_list);
  162. return key;
  163. }
  164. void ieee80211_key_free(struct ieee80211_key *key)
  165. {
  166. if (!key)
  167. return;
  168. if (key->sta) {
  169. rcu_assign_pointer(key->sta->key, NULL);
  170. } else {
  171. if (key->sdata->default_key == key)
  172. ieee80211_set_default_key(key->sdata, -1);
  173. if (key->conf.keyidx >= 0 &&
  174. key->conf.keyidx < NUM_DEFAULT_KEYS)
  175. rcu_assign_pointer(key->sdata->keys[key->conf.keyidx],
  176. NULL);
  177. else
  178. WARN_ON(1);
  179. }
  180. /* wait for all key users to complete */
  181. synchronize_rcu();
  182. /* remove from hwaccel if appropriate */
  183. ieee80211_key_disable_hw_accel(key);
  184. if (key->conf.alg == ALG_CCMP)
  185. ieee80211_aes_key_free(key->u.ccmp.tfm);
  186. ieee80211_debugfs_key_remove(key);
  187. list_del(&key->list);
  188. kfree(key);
  189. }
  190. void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
  191. {
  192. struct ieee80211_key *key = NULL;
  193. if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
  194. key = sdata->keys[idx];
  195. if (sdata->default_key != key) {
  196. ieee80211_debugfs_key_remove_default(sdata);
  197. rcu_assign_pointer(sdata->default_key, key);
  198. if (sdata->default_key)
  199. ieee80211_debugfs_key_add_default(sdata);
  200. }
  201. }
  202. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
  203. {
  204. struct ieee80211_key *key, *tmp;
  205. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  206. ieee80211_key_free(key);
  207. }
  208. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  209. {
  210. struct ieee80211_key *key;
  211. WARN_ON(!netif_running(sdata->dev));
  212. if (!netif_running(sdata->dev))
  213. return;
  214. list_for_each_entry(key, &sdata->key_list, list)
  215. ieee80211_key_enable_hw_accel(key);
  216. }
  217. void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
  218. {
  219. struct ieee80211_key *key;
  220. list_for_each_entry(key, &sdata->key_list, list)
  221. ieee80211_key_disable_hw_accel(key);
  222. }