key.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. DECLARE_MAC_BUF(mac);
  58. if (!key->local->ops->set_key)
  59. return;
  60. addr = get_mac_for_key(key);
  61. ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY,
  62. key->sdata->dev->dev_addr, addr,
  63. &key->conf);
  64. if (!ret)
  65. key->flags |= KEY_FLAG_UPLOADED_TO_HARDWARE;
  66. if (ret && ret != -ENOSPC && ret != -EOPNOTSUPP)
  67. printk(KERN_ERR "mac80211-%s: failed to set key "
  68. "(%d, %s) to hardware (%d)\n",
  69. wiphy_name(key->local->hw.wiphy),
  70. key->conf.keyidx, print_mac(mac, addr), ret);
  71. }
  72. static void ieee80211_key_disable_hw_accel(struct ieee80211_key *key)
  73. {
  74. const u8 *addr;
  75. int ret;
  76. DECLARE_MAC_BUF(mac);
  77. if (!key->local->ops->set_key)
  78. return;
  79. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  80. return;
  81. addr = get_mac_for_key(key);
  82. ret = key->local->ops->set_key(local_to_hw(key->local), DISABLE_KEY,
  83. key->sdata->dev->dev_addr, addr,
  84. &key->conf);
  85. if (ret)
  86. printk(KERN_ERR "mac80211-%s: failed to remove key "
  87. "(%d, %s) from hardware (%d)\n",
  88. wiphy_name(key->local->hw.wiphy),
  89. key->conf.keyidx, print_mac(mac, addr), ret);
  90. key->flags &= ~KEY_FLAG_UPLOADED_TO_HARDWARE;
  91. }
  92. struct ieee80211_key *ieee80211_key_alloc(struct ieee80211_sub_if_data *sdata,
  93. struct sta_info *sta,
  94. enum ieee80211_key_alg alg,
  95. int idx,
  96. size_t key_len,
  97. const u8 *key_data)
  98. {
  99. struct ieee80211_key *key;
  100. BUG_ON(idx < 0 || idx >= NUM_DEFAULT_KEYS);
  101. key = kzalloc(sizeof(struct ieee80211_key) + key_len, GFP_KERNEL);
  102. if (!key)
  103. return NULL;
  104. /*
  105. * Default to software encryption; we'll later upload the
  106. * key to the hardware if possible.
  107. */
  108. key->conf.flags = 0;
  109. key->flags = 0;
  110. key->conf.alg = alg;
  111. key->conf.keyidx = idx;
  112. key->conf.keylen = key_len;
  113. memcpy(key->conf.key, key_data, key_len);
  114. key->local = sdata->local;
  115. key->sdata = sdata;
  116. key->sta = sta;
  117. if (alg == ALG_CCMP) {
  118. /*
  119. * Initialize AES key state here as an optimization so that
  120. * it does not need to be initialized for every packet.
  121. */
  122. key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(key_data);
  123. if (!key->u.ccmp.tfm) {
  124. ieee80211_key_free(key);
  125. return NULL;
  126. }
  127. }
  128. ieee80211_debugfs_key_add(key->local, key);
  129. /* remove key first */
  130. if (sta)
  131. ieee80211_key_free(sta->key);
  132. else
  133. ieee80211_key_free(sdata->keys[idx]);
  134. if (sta) {
  135. ieee80211_debugfs_key_sta_link(key, sta);
  136. /*
  137. * some hardware cannot handle TKIP with QoS, so
  138. * we indicate whether QoS could be in use.
  139. */
  140. if (sta->flags & WLAN_STA_WME)
  141. key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
  142. } else {
  143. if (sdata->type == IEEE80211_IF_TYPE_STA) {
  144. struct sta_info *ap;
  145. /* same here, the AP could be using QoS */
  146. ap = sta_info_get(key->local, key->sdata->u.sta.bssid);
  147. if (ap) {
  148. if (ap->flags & WLAN_STA_WME)
  149. key->conf.flags |=
  150. IEEE80211_KEY_FLAG_WMM_STA;
  151. sta_info_put(ap);
  152. }
  153. }
  154. }
  155. /* enable hwaccel if appropriate */
  156. if (netif_running(key->sdata->dev))
  157. ieee80211_key_enable_hw_accel(key);
  158. if (sta)
  159. rcu_assign_pointer(sta->key, key);
  160. else
  161. rcu_assign_pointer(sdata->keys[idx], key);
  162. list_add(&key->list, &sdata->key_list);
  163. return key;
  164. }
  165. void ieee80211_key_free(struct ieee80211_key *key)
  166. {
  167. if (!key)
  168. return;
  169. if (key->sta) {
  170. rcu_assign_pointer(key->sta->key, NULL);
  171. } else {
  172. if (key->sdata->default_key == key)
  173. ieee80211_set_default_key(key->sdata, -1);
  174. if (key->conf.keyidx >= 0 &&
  175. key->conf.keyidx < NUM_DEFAULT_KEYS)
  176. rcu_assign_pointer(key->sdata->keys[key->conf.keyidx],
  177. NULL);
  178. else
  179. WARN_ON(1);
  180. }
  181. /* wait for all key users to complete */
  182. synchronize_rcu();
  183. /* remove from hwaccel if appropriate */
  184. ieee80211_key_disable_hw_accel(key);
  185. if (key->conf.alg == ALG_CCMP)
  186. ieee80211_aes_key_free(key->u.ccmp.tfm);
  187. ieee80211_debugfs_key_remove(key);
  188. list_del(&key->list);
  189. kfree(key);
  190. }
  191. void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
  192. {
  193. struct ieee80211_key *key = NULL;
  194. if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
  195. key = sdata->keys[idx];
  196. if (sdata->default_key != key) {
  197. ieee80211_debugfs_key_remove_default(sdata);
  198. rcu_assign_pointer(sdata->default_key, key);
  199. if (sdata->default_key)
  200. ieee80211_debugfs_key_add_default(sdata);
  201. }
  202. }
  203. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
  204. {
  205. struct ieee80211_key *key, *tmp;
  206. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  207. ieee80211_key_free(key);
  208. }
  209. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  210. {
  211. struct ieee80211_key *key;
  212. WARN_ON(!netif_running(sdata->dev));
  213. if (!netif_running(sdata->dev))
  214. return;
  215. list_for_each_entry(key, &sdata->key_list, list)
  216. ieee80211_key_enable_hw_accel(key);
  217. }
  218. void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
  219. {
  220. struct ieee80211_key *key;
  221. list_for_each_entry(key, &sdata->key_list, list)
  222. ieee80211_key_disable_hw_accel(key);
  223. }