ieee80211_crypt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 = kzalloc(sizeof(*alg), GFP_KERNEL);
  94. if (alg == NULL)
  95. return -ENOMEM;
  96. alg->ops = ops;
  97. spin_lock_irqsave(&ieee80211_crypto_lock, flags);
  98. list_add(&alg->list, &ieee80211_crypto_algs);
  99. spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
  100. printk(KERN_DEBUG "ieee80211_crypt: registered algorithm '%s'\n",
  101. ops->name);
  102. return 0;
  103. }
  104. int ieee80211_unregister_crypto_ops(struct ieee80211_crypto_ops *ops)
  105. {
  106. struct ieee80211_crypto_alg *alg;
  107. unsigned long flags;
  108. spin_lock_irqsave(&ieee80211_crypto_lock, flags);
  109. list_for_each_entry(alg, &ieee80211_crypto_algs, list) {
  110. if (alg->ops == ops)
  111. goto found;
  112. }
  113. spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
  114. return -EINVAL;
  115. found:
  116. printk(KERN_DEBUG "ieee80211_crypt: unregistered algorithm "
  117. "'%s'\n", ops->name);
  118. list_del(&alg->list);
  119. spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
  120. kfree(alg);
  121. return 0;
  122. }
  123. struct ieee80211_crypto_ops *ieee80211_get_crypto_ops(const char *name)
  124. {
  125. struct ieee80211_crypto_alg *alg;
  126. unsigned long flags;
  127. spin_lock_irqsave(&ieee80211_crypto_lock, flags);
  128. list_for_each_entry(alg, &ieee80211_crypto_algs, list) {
  129. if (strcmp(alg->ops->name, name) == 0)
  130. goto found;
  131. }
  132. spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
  133. return NULL;
  134. found:
  135. spin_unlock_irqrestore(&ieee80211_crypto_lock, flags);
  136. return alg->ops;
  137. }
  138. static void *ieee80211_crypt_null_init(int keyidx)
  139. {
  140. return (void *)1;
  141. }
  142. static void ieee80211_crypt_null_deinit(void *priv)
  143. {
  144. }
  145. static struct ieee80211_crypto_ops ieee80211_crypt_null = {
  146. .name = "NULL",
  147. .init = ieee80211_crypt_null_init,
  148. .deinit = ieee80211_crypt_null_deinit,
  149. .owner = THIS_MODULE,
  150. };
  151. static int __init ieee80211_crypto_init(void)
  152. {
  153. return ieee80211_register_crypto_ops(&ieee80211_crypt_null);
  154. }
  155. static void __exit ieee80211_crypto_deinit(void)
  156. {
  157. ieee80211_unregister_crypto_ops(&ieee80211_crypt_null);
  158. BUG_ON(!list_empty(&ieee80211_crypto_algs));
  159. }
  160. EXPORT_SYMBOL(ieee80211_crypt_deinit_entries);
  161. EXPORT_SYMBOL(ieee80211_crypt_deinit_handler);
  162. EXPORT_SYMBOL(ieee80211_crypt_delayed_deinit);
  163. EXPORT_SYMBOL(ieee80211_crypt_quiescing);
  164. EXPORT_SYMBOL(ieee80211_register_crypto_ops);
  165. EXPORT_SYMBOL(ieee80211_unregister_crypto_ops);
  166. EXPORT_SYMBOL(ieee80211_get_crypto_ops);
  167. module_init(ieee80211_crypto_init);
  168. module_exit(ieee80211_crypto_deinit);