ieee80211_crypt.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/errno.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <net/ieee80211.h>
  19. MODULE_AUTHOR("Jouni Malinen");
  20. MODULE_DESCRIPTION("HostAP crypto");
  21. MODULE_LICENSE("GPL");
  22. struct ieee80211_crypto_alg {
  23. struct list_head list;
  24. struct ieee80211_crypto_ops *ops;
  25. };
  26. static LIST_HEAD(ieee80211_crypto_algs);
  27. static DEFINE_SPINLOCK(ieee80211_crypto_lock);
  28. void ieee80211_crypt_deinit_entries(struct ieee80211_device *ieee, int force)
  29. {
  30. struct ieee80211_crypt_data *entry, *next;
  31. unsigned long flags;
  32. spin_lock_irqsave(&ieee->lock, flags);
  33. list_for_each_entry_safe(entry, next, &ieee->crypt_deinit_list, list) {
  34. if (atomic_read(&entry->refcnt) != 0 && !force)
  35. continue;
  36. list_del(&entry->list);
  37. if (entry->ops) {
  38. entry->ops->deinit(entry->priv);
  39. module_put(entry->ops->owner);
  40. }
  41. kfree(entry);
  42. }
  43. spin_unlock_irqrestore(&ieee->lock, flags);
  44. }
  45. /* After this, crypt_deinit_list won't accept new members */
  46. void ieee80211_crypt_quiescing(struct ieee80211_device *ieee)
  47. {
  48. unsigned long flags;
  49. spin_lock_irqsave(&ieee->lock, flags);
  50. ieee->crypt_quiesced = 1;
  51. spin_unlock_irqrestore(&ieee->lock, flags);
  52. }
  53. void ieee80211_crypt_deinit_handler(unsigned long data)
  54. {
  55. struct ieee80211_device *ieee = (struct ieee80211_device *)data;
  56. unsigned long flags;
  57. ieee80211_crypt_deinit_entries(ieee, 0);
  58. spin_lock_irqsave(&ieee->lock, flags);
  59. if (!list_empty(&ieee->crypt_deinit_list) && !ieee->crypt_quiesced) {
  60. printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
  61. "deletion list\n", ieee->dev->name);
  62. ieee->crypt_deinit_timer.expires = jiffies + HZ;
  63. add_timer(&ieee->crypt_deinit_timer);
  64. }
  65. spin_unlock_irqrestore(&ieee->lock, flags);
  66. }
  67. void ieee80211_crypt_delayed_deinit(struct ieee80211_device *ieee,
  68. struct ieee80211_crypt_data **crypt)
  69. {
  70. struct ieee80211_crypt_data *tmp;
  71. unsigned long flags;
  72. if (*crypt == NULL)
  73. return;
  74. tmp = *crypt;
  75. *crypt = NULL;
  76. /* must not run ops->deinit() while there may be pending encrypt or
  77. * decrypt operations. Use a list of delayed deinits to avoid needing
  78. * locking. */
  79. spin_lock_irqsave(&ieee->lock, flags);
  80. if (!ieee->crypt_quiesced) {
  81. list_add(&tmp->list, &ieee->crypt_deinit_list);
  82. if (!timer_pending(&ieee->crypt_deinit_timer)) {
  83. ieee->crypt_deinit_timer.expires = jiffies + HZ;
  84. add_timer(&ieee->crypt_deinit_timer);
  85. }
  86. }
  87. spin_unlock_irqrestore(&ieee->lock, flags);
  88. }
  89. int ieee80211_register_crypto_ops(struct ieee80211_crypto_ops *ops)
  90. {
  91. unsigned long flags;
  92. struct ieee80211_crypto_alg *alg;
  93. alg = kmalloc(sizeof(*alg), GFP_KERNEL);
  94. if (alg == NULL)
  95. return -ENOMEM;
  96. memset(alg, 0, sizeof(*alg));
  97. alg->ops = ops;
  98. spin_lock_irqsave(&ieee80211_crypto_lock, flags);
  99. list_add(&alg->list, &ieee80211_crypto_algs);
  100. spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
  101. printk(KERN_DEBUG "ieee80211_crypt: registered algorithm '%s'\n",
  102. ops->name);
  103. return 0;
  104. }
  105. int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops *ops)
  106. {
  107. struct ieee80211_crypto_alg *alg;
  108. unsigned long flags;
  109. spin_lock_irqsave(&ieee80211_crypto_lock, flags);
  110. list_for_each_entry(alg, &ieee80211_crypto_algs, list) {
  111. if (alg->ops == ops)
  112. goto found;
  113. }
  114. spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
  115. return -EINVAL;
  116. found:
  117. printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
  118. "'%s'\n", ops->name);
  119. list_del(&alg->list);
  120. spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
  121. kfree(alg);
  122. return 0;
  123. }
  124. struct ieee80211_crypto_ops *ieee80211_get_crypto_ops(const char *name)
  125. {
  126. struct ieee80211_crypto_alg *alg;
  127. unsigned long flags;
  128. spin_lock_irqsave(&ieee80211_crypto_lock, flags);
  129. list_for_each_entry(alg, &ieee80211_crypto_algs, list) {
  130. if (strcmp(alg->ops->name, name) == 0)
  131. goto found;
  132. }
  133. spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
  134. return NULL;
  135. found:
  136. spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
  137. return alg->ops;
  138. }
  139. static void *ieee80211_crypt_null_init(int keyidx)
  140. {
  141. return (void *)1;
  142. }
  143. static void ieee80211_crypt_null_deinit(void *priv)
  144. {
  145. }
  146. static struct ieee80211_crypto_ops ieee80211_crypt_null = {
  147. .name = "NULL",
  148. .init = ieee80211_crypt_null_init,
  149. .deinit = ieee80211_crypt_null_deinit,
  150. .owner = THIS_MODULE,
  151. };
  152. static int __init ieee80211_crypto_init(void)
  153. {
  154. return ieee80211_register_crypto_ops(&ieee80211_crypt_null);
  155. }
  156. static void __exit ieee80211_crypto_deinit(void)
  157. {
  158. ieee80211_unregister_crypto_ops(&ieee80211_crypt_null);
  159. BUG_ON(!list_empty(&ieee80211_crypto_algs));
  160. }
  161. EXPORT_SYMBOL(ieee80211_crypt_deinit_entries);
  162. EXPORT_SYMBOL(ieee80211_crypt_deinit_handler);
  163. EXPORT_SYMBOL(ieee80211_crypt_delayed_deinit);
  164. EXPORT_SYMBOL(ieee80211_crypt_quiescing);
  165. EXPORT_SYMBOL(ieee80211_register_crypto_ops);
  166. EXPORT_SYMBOL(ieee80211_unregister_crypto_ops);
  167. EXPORT_SYMBOL(ieee80211_get_crypto_ops);
  168. module_init(ieee80211_crypto_init);
  169. module_exit(ieee80211_crypto_deinit);