ieee80211_crypt.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Host AP crypto routines
  3. *
  4. * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
  5. * Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com>
  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. See README and COPYING for
  10. * more details.
  11. *
  12. */
  13. #include <linux/config.h>
  14. #include <linux/version.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <asm/string.h>
  19. #include <asm/errno.h>
  20. #include <net/ieee80211.h>
  21. MODULE_AUTHOR("Jouni Malinen");
  22. MODULE_DESCRIPTION("HostAP crypto");
  23. MODULE_LICENSE("GPL");
  24. struct ieee80211_crypto_alg {
  25. struct list_head list;
  26. struct ieee80211_crypto_ops *ops;
  27. };
  28. struct ieee80211_crypto {
  29. struct list_head algs;
  30. spinlock_t lock;
  31. };
  32. static struct ieee80211_crypto *hcrypt;
  33. void ieee80211_crypt_deinit_entries(struct ieee80211_device *ieee, int force)
  34. {
  35. struct list_head *ptr, *n;
  36. struct ieee80211_crypt_data *entry;
  37. unsigned long flags;
  38. spin_lock_irqsave(&ieee->lock, flags);
  39. if (list_empty(&ieee->crypt_deinit_list))
  40. goto unlock;
  41. for (ptr = ieee->crypt_deinit_list.next, n = ptr->next;
  42. ptr != &ieee->crypt_deinit_list; ptr = n, n = ptr->next) {
  43. entry = list_entry(ptr, struct ieee80211_crypt_data, list);
  44. if (atomic_read(&entry->refcnt) != 0 && !force)
  45. continue;
  46. list_del(ptr);
  47. if (entry->ops) {
  48. entry->ops->deinit(entry->priv);
  49. module_put(entry->ops->owner);
  50. }
  51. kfree(entry);
  52. }
  53. unlock:
  54. spin_unlock_irqrestore(&ieee->lock, flags);
  55. }
  56. /* After this, crypt_deinit_list won't accept new members */
  57. void ieee80211_crypt_quiescing(struct ieee80211_device *ieee)
  58. {
  59. unsigned long flags;
  60. spin_lock_irqsave(&ieee->lock, flags);
  61. ieee->crypt_quiesced = 1;
  62. spin_unlock_irqrestore(&ieee->lock, flags);
  63. }
  64. void ieee80211_crypt_deinit_handler(unsigned long data)
  65. {
  66. struct ieee80211_device *ieee = (struct ieee80211_device *)data;
  67. unsigned long flags;
  68. ieee80211_crypt_deinit_entries(ieee, 0);
  69. spin_lock_irqsave(&ieee->lock, flags);
  70. if (!list_empty(&ieee->crypt_deinit_list) && !ieee->crypt_quiesced) {
  71. printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
  72. "deletion list\n", ieee->dev->name);
  73. ieee->crypt_deinit_timer.expires = jiffies + HZ;
  74. add_timer(&ieee->crypt_deinit_timer);
  75. }
  76. spin_unlock_irqrestore(&ieee->lock, flags);
  77. }
  78. void ieee80211_crypt_delayed_deinit(struct ieee80211_device *ieee,
  79. struct ieee80211_crypt_data **crypt)
  80. {
  81. struct ieee80211_crypt_data *tmp;
  82. unsigned long flags;
  83. if (*crypt == NULL)
  84. return;
  85. tmp = *crypt;
  86. *crypt = NULL;
  87. /* must not run ops->deinit() while there may be pending encrypt or
  88. * decrypt operations. Use a list of delayed deinits to avoid needing
  89. * locking. */
  90. spin_lock_irqsave(&ieee->lock, flags);
  91. if (!ieee->crypt_quiesced) {
  92. list_add(&tmp->list, &ieee->crypt_deinit_list);
  93. if (!timer_pending(&ieee->crypt_deinit_timer)) {
  94. ieee->crypt_deinit_timer.expires = jiffies + HZ;
  95. add_timer(&ieee->crypt_deinit_timer);
  96. }
  97. }
  98. spin_unlock_irqrestore(&ieee->lock, flags);
  99. }
  100. int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops *ops)
  101. {
  102. unsigned long flags;
  103. struct ieee80211_crypto_alg *alg;
  104. if (hcrypt == NULL)
  105. return -1;
  106. alg = kmalloc(sizeof(*alg), GFP_KERNEL);
  107. if (alg == NULL)
  108. return -ENOMEM;
  109. memset(alg, 0, sizeof(*alg));
  110. alg->ops = ops;
  111. spin_lock_irqsave(&hcrypt->lock, flags);
  112. list_add(&alg->list, &hcrypt->algs);
  113. spin_unlock_irqrestore(&hcrypt->lock, flags);
  114. printk(KERN_DEBUG "ieee80211_crypt: registered algorithm '%s'\n",
  115. ops->name);
  116. return 0;
  117. }
  118. int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops *ops)
  119. {
  120. unsigned long flags;
  121. struct list_head *ptr;
  122. struct ieee80211_crypto_alg *del_alg = NULL;
  123. if (hcrypt == NULL)
  124. return -1;
  125. spin_lock_irqsave(&hcrypt->lock, flags);
  126. for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
  127. struct ieee80211_crypto_alg *alg =
  128. (struct ieee80211_crypto_alg *)ptr;
  129. if (alg->ops == ops) {
  130. list_del(&alg->list);
  131. del_alg = alg;
  132. break;
  133. }
  134. }
  135. spin_unlock_irqrestore(&hcrypt->lock, flags);
  136. if (del_alg) {
  137. printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
  138. "'%s'\n", ops->name);
  139. kfree(del_alg);
  140. }
  141. return del_alg ? 0 : -1;
  142. }
  143. struct ieee80211_crypto_ops *ieee80211_get_crypto_ops(const char *name)
  144. {
  145. unsigned long flags;
  146. struct list_head *ptr;
  147. struct ieee80211_crypto_alg *found_alg = NULL;
  148. if (hcrypt == NULL)
  149. return NULL;
  150. spin_lock_irqsave(&hcrypt->lock, flags);
  151. for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
  152. struct ieee80211_crypto_alg *alg =
  153. (struct ieee80211_crypto_alg *)ptr;
  154. if (strcmp(alg->ops->name, name) == 0) {
  155. found_alg = alg;
  156. break;
  157. }
  158. }
  159. spin_unlock_irqrestore(&hcrypt->lock, flags);
  160. if (found_alg)
  161. return found_alg->ops;
  162. else
  163. return NULL;
  164. }
  165. static void *ieee80211_crypt_null_init(int keyidx)
  166. {
  167. return (void *)1;
  168. }
  169. static void ieee80211_crypt_null_deinit(void *priv)
  170. {
  171. }
  172. static struct ieee80211_crypto_ops ieee80211_crypt_null = {
  173. .name = "NULL",
  174. .init = ieee80211_crypt_null_init,
  175. .deinit = ieee80211_crypt_null_deinit,
  176. .encrypt_mpdu = NULL,
  177. .decrypt_mpdu = NULL,
  178. .encrypt_msdu = NULL,
  179. .decrypt_msdu = NULL,
  180. .set_key = NULL,
  181. .get_key = NULL,
  182. .extra_mpdu_prefix_len = 0,
  183. .extra_mpdu_postfix_len = 0,
  184. .owner = THIS_MODULE,
  185. };
  186. static int __init ieee80211_crypto_init(void)
  187. {
  188. int ret = -ENOMEM;
  189. hcrypt = kmalloc(sizeof(*hcrypt), GFP_KERNEL);
  190. if (!hcrypt)
  191. goto out;
  192. memset(hcrypt, 0, sizeof(*hcrypt));
  193. INIT_LIST_HEAD(&hcrypt->algs);
  194. spin_lock_init(&hcrypt->lock);
  195. ret = ieee80211_register_crypto_ops(&ieee80211_crypt_null);
  196. if (ret < 0) {
  197. kfree(hcrypt);
  198. hcrypt = NULL;
  199. }
  200. out:
  201. return ret;
  202. }
  203. static void __exit ieee80211_crypto_deinit(void)
  204. {
  205. struct list_head *ptr, *n;
  206. if (hcrypt == NULL)
  207. return;
  208. for (ptr = hcrypt->algs.next, n = ptr->next; ptr != &hcrypt->algs;
  209. ptr = n, n = ptr->next) {
  210. struct ieee80211_crypto_alg *alg =
  211. (struct ieee80211_crypto_alg *)ptr;
  212. list_del(ptr);
  213. printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
  214. "'%s' (deinit)\n", alg->ops->name);
  215. kfree(alg);
  216. }
  217. kfree(hcrypt);
  218. }
  219. EXPORT_SYMBOL(ieee80211_crypt_deinit_entries);
  220. EXPORT_SYMBOL(ieee80211_crypt_deinit_handler);
  221. EXPORT_SYMBOL(ieee80211_crypt_delayed_deinit);
  222. EXPORT_SYMBOL(ieee80211_crypt_quiescing);
  223. EXPORT_SYMBOL(ieee80211_register_crypto_ops);
  224. EXPORT_SYMBOL(ieee80211_unregister_crypto_ops);
  225. EXPORT_SYMBOL(ieee80211_get_crypto_ops);
  226. module_init(ieee80211_crypto_init);
  227. module_exit(ieee80211_crypto_deinit);