key.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 <net/mac80211.h>
  15. #include "ieee80211_i.h"
  16. #include "debugfs_key.h"
  17. #include "aes_ccm.h"
  18. /*
  19. * Key handling basics
  20. *
  21. * Key handling in mac80211 is done based on per-interface (sub_if_data)
  22. * keys and per-station keys. Since each station belongs to an interface,
  23. * each station key also belongs to that interface.
  24. *
  25. * Hardware acceleration is done on a best-effort basis, for each key
  26. * that is eligible the hardware is asked to enable that key but if
  27. * it cannot do that they key is simply kept for software encryption.
  28. * There is currently no way of knowing this except by looking into
  29. * debugfs.
  30. *
  31. * All operations here are called under RTNL so no extra locking is
  32. * required.
  33. */
  34. static const u8 bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  35. static const u8 zero_addr[ETH_ALEN];
  36. static const u8 *get_mac_for_key(struct ieee80211_key *key)
  37. {
  38. const u8 *addr = bcast_addr;
  39. /*
  40. * If we're an AP we won't ever receive frames with a non-WEP
  41. * group key so we tell the driver that by using the zero MAC
  42. * address to indicate a transmit-only key.
  43. */
  44. if (key->conf.alg != ALG_WEP &&
  45. (key->sdata->type == IEEE80211_IF_TYPE_AP ||
  46. key->sdata->type == IEEE80211_IF_TYPE_VLAN))
  47. addr = zero_addr;
  48. if (key->sta)
  49. addr = key->sta->addr;
  50. return addr;
  51. }
  52. static void ieee80211_key_enable_hw_accel(struct ieee80211_key *key)
  53. {
  54. const u8 *addr;
  55. int ret;
  56. if (!key->local->ops->set_key)
  57. return;
  58. addr = get_mac_for_key(key);
  59. ret = key->local->ops->set_key(local_to_hw(key->local), SET_KEY,
  60. key->sdata->dev->dev_addr, addr,
  61. &key->conf);
  62. WARN_ON(!ret && (key->conf.hw_key_idx == HW_KEY_IDX_INVALID));
  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. key->conf.hw_key_idx = HW_KEY_IDX_INVALID;
  90. }
  91. struct ieee80211_key *ieee80211_key_alloc(struct ieee80211_sub_if_data *sdata,
  92. struct sta_info *sta,
  93. ieee80211_key_alg alg,
  94. int idx,
  95. size_t key_len,
  96. const u8 *key_data)
  97. {
  98. struct ieee80211_key *key;
  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.hw_key_idx = HW_KEY_IDX_INVALID;
  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. if (sta) {
  130. ieee80211_debugfs_key_sta_link(key, sta);
  131. sta->key = key;
  132. /*
  133. * some hardware cannot handle TKIP with QoS, so
  134. * we indicate whether QoS could be in use.
  135. */
  136. if (sta->flags & WLAN_STA_WME)
  137. key->conf.flags |= IEEE80211_KEY_FLAG_WMM_STA;
  138. } else {
  139. if (sdata->type == IEEE80211_IF_TYPE_STA) {
  140. struct sta_info *ap;
  141. /* same here, the AP could be using QoS */
  142. ap = sta_info_get(key->local, key->sdata->u.sta.bssid);
  143. if (ap) {
  144. if (ap->flags & WLAN_STA_WME)
  145. key->conf.flags |=
  146. IEEE80211_KEY_FLAG_WMM_STA;
  147. sta_info_put(ap);
  148. }
  149. }
  150. if (idx >= 0 && idx < NUM_DEFAULT_KEYS) {
  151. if (!sdata->keys[idx])
  152. sdata->keys[idx] = key;
  153. else
  154. WARN_ON(1);
  155. } else
  156. WARN_ON(1);
  157. }
  158. list_add(&key->list, &sdata->key_list);
  159. if (netif_running(key->sdata->dev))
  160. ieee80211_key_enable_hw_accel(key);
  161. return key;
  162. }
  163. void ieee80211_key_free(struct ieee80211_key *key)
  164. {
  165. if (!key)
  166. return;
  167. ieee80211_key_disable_hw_accel(key);
  168. if (key->sta) {
  169. 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. key->sdata->keys[key->conf.keyidx] = NULL;
  176. else
  177. WARN_ON(1);
  178. }
  179. if (key->conf.alg == ALG_CCMP)
  180. ieee80211_aes_key_free(key->u.ccmp.tfm);
  181. ieee80211_debugfs_key_remove(key);
  182. list_del(&key->list);
  183. kfree(key);
  184. }
  185. void ieee80211_set_default_key(struct ieee80211_sub_if_data *sdata, int idx)
  186. {
  187. struct ieee80211_key *key = NULL;
  188. if (idx >= 0 && idx < NUM_DEFAULT_KEYS)
  189. key = sdata->keys[idx];
  190. if (sdata->default_key != key) {
  191. ieee80211_debugfs_key_remove_default(sdata);
  192. sdata->default_key = key;
  193. if (sdata->default_key)
  194. ieee80211_debugfs_key_add_default(sdata);
  195. if (sdata->local->ops->set_key_idx)
  196. sdata->local->ops->set_key_idx(
  197. local_to_hw(sdata->local), idx);
  198. }
  199. }
  200. void ieee80211_free_keys(struct ieee80211_sub_if_data *sdata)
  201. {
  202. struct ieee80211_key *key, *tmp;
  203. list_for_each_entry_safe(key, tmp, &sdata->key_list, list)
  204. ieee80211_key_free(key);
  205. }
  206. void ieee80211_enable_keys(struct ieee80211_sub_if_data *sdata)
  207. {
  208. struct ieee80211_key *key;
  209. WARN_ON(!netif_running(sdata->dev));
  210. if (!netif_running(sdata->dev))
  211. return;
  212. list_for_each_entry(key, &sdata->key_list, list)
  213. ieee80211_key_enable_hw_accel(key);
  214. }
  215. void ieee80211_disable_keys(struct ieee80211_sub_if_data *sdata)
  216. {
  217. struct ieee80211_key *key;
  218. list_for_each_entry(key, &sdata->key_list, list)
  219. ieee80211_key_disable_hw_accel(key);
  220. }