ieee80211_crypt.c 6.0 KB

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