ieee80211_crypt.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. for (ptr = ieee->crypt_deinit_list.next, n = ptr->next;
  38. ptr != &ieee->crypt_deinit_list; ptr = n, n = ptr->next) {
  39. entry = list_entry(ptr, struct ieee80211_crypt_data, list);
  40. if (atomic_read(&entry->refcnt) != 0 && !force)
  41. continue;
  42. list_del(ptr);
  43. if (entry->ops) {
  44. entry->ops->deinit(entry->priv);
  45. module_put(entry->ops->owner);
  46. }
  47. kfree(entry);
  48. }
  49. }
  50. void ieee80211_crypt_deinit_handler(unsigned long data)
  51. {
  52. struct ieee80211_device *ieee = (struct ieee80211_device *)data;
  53. unsigned long flags;
  54. spin_lock_irqsave(&ieee->lock, flags);
  55. ieee80211_crypt_deinit_entries(ieee, 0);
  56. if (!list_empty(&ieee->crypt_deinit_list)) {
  57. printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
  58. "deletion list\n", ieee->dev->name);
  59. ieee->crypt_deinit_timer.expires = jiffies + HZ;
  60. add_timer(&ieee->crypt_deinit_timer);
  61. }
  62. spin_unlock_irqrestore(&ieee->lock, flags);
  63. }
  64. void ieee80211_crypt_delayed_deinit(struct ieee80211_device *ieee,
  65. struct ieee80211_crypt_data **crypt)
  66. {
  67. struct ieee80211_crypt_data *tmp;
  68. unsigned long flags;
  69. if (*crypt == NULL)
  70. return;
  71. tmp = *crypt;
  72. *crypt = NULL;
  73. /* must not run ops->deinit() while there may be pending encrypt or
  74. * decrypt operations. Use a list of delayed deinits to avoid needing
  75. * locking. */
  76. spin_lock_irqsave(&ieee->lock, flags);
  77. list_add(&tmp->list, &ieee->crypt_deinit_list);
  78. if (!timer_pending(&ieee->crypt_deinit_timer)) {
  79. ieee->crypt_deinit_timer.expires = jiffies + HZ;
  80. add_timer(&ieee->crypt_deinit_timer);
  81. }
  82. spin_unlock_irqrestore(&ieee->lock, flags);
  83. }
  84. int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops *ops)
  85. {
  86. unsigned long flags;
  87. struct ieee80211_crypto_alg *alg;
  88. if (hcrypt == NULL)
  89. return -1;
  90. alg = kmalloc(sizeof(*alg), GFP_KERNEL);
  91. if (alg == NULL)
  92. return -ENOMEM;
  93. memset(alg, 0, sizeof(*alg));
  94. alg->ops = ops;
  95. spin_lock_irqsave(&hcrypt->lock, flags);
  96. list_add(&alg->list, &hcrypt->algs);
  97. spin_unlock_irqrestore(&hcrypt->lock, flags);
  98. printk(KERN_DEBUG "ieee80211_crypt: registered algorithm '%s'\n",
  99. ops->name);
  100. return 0;
  101. }
  102. int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops *ops)
  103. {
  104. unsigned long flags;
  105. struct list_head *ptr;
  106. struct ieee80211_crypto_alg *del_alg = NULL;
  107. if (hcrypt == NULL)
  108. return -1;
  109. spin_lock_irqsave(&hcrypt->lock, flags);
  110. for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
  111. struct ieee80211_crypto_alg *alg =
  112. (struct ieee80211_crypto_alg *)ptr;
  113. if (alg->ops == ops) {
  114. list_del(&alg->list);
  115. del_alg = alg;
  116. break;
  117. }
  118. }
  119. spin_unlock_irqrestore(&hcrypt->lock, flags);
  120. if (del_alg) {
  121. printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
  122. "'%s'\n", ops->name);
  123. kfree(del_alg);
  124. }
  125. return del_alg ? 0 : -1;
  126. }
  127. struct ieee80211_crypto_ops *ieee80211_get_crypto_ops(const char *name)
  128. {
  129. unsigned long flags;
  130. struct list_head *ptr;
  131. struct ieee80211_crypto_alg *found_alg = NULL;
  132. if (hcrypt == NULL)
  133. return NULL;
  134. spin_lock_irqsave(&hcrypt->lock, flags);
  135. for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
  136. struct ieee80211_crypto_alg *alg =
  137. (struct ieee80211_crypto_alg *)ptr;
  138. if (strcmp(alg->ops->name, name) == 0) {
  139. found_alg = alg;
  140. break;
  141. }
  142. }
  143. spin_unlock_irqrestore(&hcrypt->lock, flags);
  144. if (found_alg)
  145. return found_alg->ops;
  146. else
  147. return NULL;
  148. }
  149. static void *ieee80211_crypt_null_init(int keyidx)
  150. {
  151. return (void *)1;
  152. }
  153. static void ieee80211_crypt_null_deinit(void *priv)
  154. {
  155. }
  156. static struct ieee80211_crypto_ops ieee80211_crypt_null = {
  157. .name = "NULL",
  158. .init = ieee80211_crypt_null_init,
  159. .deinit = ieee80211_crypt_null_deinit,
  160. .encrypt_mpdu = NULL,
  161. .decrypt_mpdu = NULL,
  162. .encrypt_msdu = NULL,
  163. .decrypt_msdu = NULL,
  164. .set_key = NULL,
  165. .get_key = NULL,
  166. .extra_prefix_len = 0,
  167. .extra_postfix_len = 0,
  168. .owner = THIS_MODULE,
  169. };
  170. static int __init ieee80211_crypto_init(void)
  171. {
  172. int ret = -ENOMEM;
  173. hcrypt = kmalloc(sizeof(*hcrypt), GFP_KERNEL);
  174. if (!hcrypt)
  175. goto out;
  176. memset(hcrypt, 0, sizeof(*hcrypt));
  177. INIT_LIST_HEAD(&hcrypt->algs);
  178. spin_lock_init(&hcrypt->lock);
  179. ret = ieee80211_register_crypto_ops(&ieee80211_crypt_null);
  180. if (ret < 0) {
  181. kfree(hcrypt);
  182. hcrypt = NULL;
  183. }
  184. out:
  185. return ret;
  186. }
  187. static void __exit ieee80211_crypto_deinit(void)
  188. {
  189. struct list_head *ptr, *n;
  190. if (hcrypt == NULL)
  191. return;
  192. for (ptr = hcrypt->algs.next, n = ptr->next; ptr != &hcrypt->algs;
  193. ptr = n, n = ptr->next) {
  194. struct ieee80211_crypto_alg *alg =
  195. (struct ieee80211_crypto_alg *)ptr;
  196. list_del(ptr);
  197. printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
  198. "'%s' (deinit)\n", alg->ops->name);
  199. kfree(alg);
  200. }
  201. kfree(hcrypt);
  202. }
  203. EXPORT_SYMBOL(ieee80211_crypt_deinit_entries);
  204. EXPORT_SYMBOL(ieee80211_crypt_deinit_handler);
  205. EXPORT_SYMBOL(ieee80211_crypt_delayed_deinit);
  206. EXPORT_SYMBOL(ieee80211_register_crypto_ops);
  207. EXPORT_SYMBOL(ieee80211_unregister_crypto_ops);
  208. EXPORT_SYMBOL(ieee80211_get_crypto_ops);
  209. module_init(ieee80211_crypto_init);
  210. module_exit(ieee80211_crypto_deinit);