lib80211.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * lib80211 -- common bits for IEEE802.11 drivers
  3. *
  4. * Copyright(c) 2008 John W. Linville <linville@tuxdriver.com>
  5. *
  6. * Portions copied from old ieee80211 component, w/ original copyright
  7. * notices below:
  8. *
  9. * Host AP crypto routines
  10. *
  11. * Copyright (c) 2002-2003, Jouni Malinen <j@w1.fi>
  12. * Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com>
  13. *
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/module.h>
  17. #include <linux/ctype.h>
  18. #include <linux/ieee80211.h>
  19. #include <linux/errno.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/string.h>
  23. #include <net/lib80211.h>
  24. #define DRV_NAME "lib80211"
  25. #define DRV_DESCRIPTION "common routines for IEEE802.11 drivers"
  26. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  27. MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
  28. MODULE_LICENSE("GPL");
  29. struct lib80211_crypto_alg {
  30. struct list_head list;
  31. struct lib80211_crypto_ops *ops;
  32. };
  33. static LIST_HEAD(lib80211_crypto_algs);
  34. static DEFINE_SPINLOCK(lib80211_crypto_lock);
  35. static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
  36. int force);
  37. static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info);
  38. static void lib80211_crypt_deinit_handler(unsigned long data);
  39. int lib80211_crypt_info_init(struct lib80211_crypt_info *info, char *name,
  40. spinlock_t *lock)
  41. {
  42. memset(info, 0, sizeof(*info));
  43. info->name = name;
  44. info->lock = lock;
  45. INIT_LIST_HEAD(&info->crypt_deinit_list);
  46. setup_timer(&info->crypt_deinit_timer, lib80211_crypt_deinit_handler,
  47. (unsigned long)info);
  48. return 0;
  49. }
  50. EXPORT_SYMBOL(lib80211_crypt_info_init);
  51. void lib80211_crypt_info_free(struct lib80211_crypt_info *info)
  52. {
  53. int i;
  54. lib80211_crypt_quiescing(info);
  55. del_timer_sync(&info->crypt_deinit_timer);
  56. lib80211_crypt_deinit_entries(info, 1);
  57. for (i = 0; i < NUM_WEP_KEYS; i++) {
  58. struct lib80211_crypt_data *crypt = info->crypt[i];
  59. if (crypt) {
  60. if (crypt->ops) {
  61. crypt->ops->deinit(crypt->priv);
  62. module_put(crypt->ops->owner);
  63. }
  64. kfree(crypt);
  65. info->crypt[i] = NULL;
  66. }
  67. }
  68. }
  69. EXPORT_SYMBOL(lib80211_crypt_info_free);
  70. static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
  71. int force)
  72. {
  73. struct lib80211_crypt_data *entry, *next;
  74. unsigned long flags;
  75. spin_lock_irqsave(info->lock, flags);
  76. list_for_each_entry_safe(entry, next, &info->crypt_deinit_list, list) {
  77. if (atomic_read(&entry->refcnt) != 0 && !force)
  78. continue;
  79. list_del(&entry->list);
  80. if (entry->ops) {
  81. entry->ops->deinit(entry->priv);
  82. module_put(entry->ops->owner);
  83. }
  84. kfree(entry);
  85. }
  86. spin_unlock_irqrestore(info->lock, flags);
  87. }
  88. /* After this, crypt_deinit_list won't accept new members */
  89. static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info)
  90. {
  91. unsigned long flags;
  92. spin_lock_irqsave(info->lock, flags);
  93. info->crypt_quiesced = 1;
  94. spin_unlock_irqrestore(info->lock, flags);
  95. }
  96. static void lib80211_crypt_deinit_handler(unsigned long data)
  97. {
  98. struct lib80211_crypt_info *info = (struct lib80211_crypt_info *)data;
  99. unsigned long flags;
  100. lib80211_crypt_deinit_entries(info, 0);
  101. spin_lock_irqsave(info->lock, flags);
  102. if (!list_empty(&info->crypt_deinit_list) && !info->crypt_quiesced) {
  103. printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
  104. "deletion list\n", info->name);
  105. info->crypt_deinit_timer.expires = jiffies + HZ;
  106. add_timer(&info->crypt_deinit_timer);
  107. }
  108. spin_unlock_irqrestore(info->lock, flags);
  109. }
  110. void lib80211_crypt_delayed_deinit(struct lib80211_crypt_info *info,
  111. struct lib80211_crypt_data **crypt)
  112. {
  113. struct lib80211_crypt_data *tmp;
  114. unsigned long flags;
  115. if (*crypt == NULL)
  116. return;
  117. tmp = *crypt;
  118. *crypt = NULL;
  119. /* must not run ops->deinit() while there may be pending encrypt or
  120. * decrypt operations. Use a list of delayed deinits to avoid needing
  121. * locking. */
  122. spin_lock_irqsave(info->lock, flags);
  123. if (!info->crypt_quiesced) {
  124. list_add(&tmp->list, &info->crypt_deinit_list);
  125. if (!timer_pending(&info->crypt_deinit_timer)) {
  126. info->crypt_deinit_timer.expires = jiffies + HZ;
  127. add_timer(&info->crypt_deinit_timer);
  128. }
  129. }
  130. spin_unlock_irqrestore(info->lock, flags);
  131. }
  132. EXPORT_SYMBOL(lib80211_crypt_delayed_deinit);
  133. int lib80211_register_crypto_ops(struct lib80211_crypto_ops *ops)
  134. {
  135. unsigned long flags;
  136. struct lib80211_crypto_alg *alg;
  137. alg = kzalloc(sizeof(*alg), GFP_KERNEL);
  138. if (alg == NULL)
  139. return -ENOMEM;
  140. alg->ops = ops;
  141. spin_lock_irqsave(&lib80211_crypto_lock, flags);
  142. list_add(&alg->list, &lib80211_crypto_algs);
  143. spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
  144. printk(KERN_DEBUG "lib80211_crypt: registered algorithm '%s'\n",
  145. ops->name);
  146. return 0;
  147. }
  148. EXPORT_SYMBOL(lib80211_register_crypto_ops);
  149. int lib80211_unregister_crypto_ops(struct lib80211_crypto_ops *ops)
  150. {
  151. struct lib80211_crypto_alg *alg;
  152. unsigned long flags;
  153. spin_lock_irqsave(&lib80211_crypto_lock, flags);
  154. list_for_each_entry(alg, &lib80211_crypto_algs, list) {
  155. if (alg->ops == ops)
  156. goto found;
  157. }
  158. spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
  159. return -EINVAL;
  160. found:
  161. printk(KERN_DEBUG "lib80211_crypt: unregistered algorithm '%s'\n",
  162. ops->name);
  163. list_del(&alg->list);
  164. spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
  165. kfree(alg);
  166. return 0;
  167. }
  168. EXPORT_SYMBOL(lib80211_unregister_crypto_ops);
  169. struct lib80211_crypto_ops *lib80211_get_crypto_ops(const char *name)
  170. {
  171. struct lib80211_crypto_alg *alg;
  172. unsigned long flags;
  173. spin_lock_irqsave(&lib80211_crypto_lock, flags);
  174. list_for_each_entry(alg, &lib80211_crypto_algs, list) {
  175. if (strcmp(alg->ops->name, name) == 0)
  176. goto found;
  177. }
  178. spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
  179. return NULL;
  180. found:
  181. spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
  182. return alg->ops;
  183. }
  184. EXPORT_SYMBOL(lib80211_get_crypto_ops);
  185. static void *lib80211_crypt_null_init(int keyidx)
  186. {
  187. return (void *)1;
  188. }
  189. static void lib80211_crypt_null_deinit(void *priv)
  190. {
  191. }
  192. static struct lib80211_crypto_ops lib80211_crypt_null = {
  193. .name = "NULL",
  194. .init = lib80211_crypt_null_init,
  195. .deinit = lib80211_crypt_null_deinit,
  196. .owner = THIS_MODULE,
  197. };
  198. static int __init lib80211_init(void)
  199. {
  200. pr_info(DRV_DESCRIPTION "\n");
  201. return lib80211_register_crypto_ops(&lib80211_crypt_null);
  202. }
  203. static void __exit lib80211_exit(void)
  204. {
  205. lib80211_unregister_crypto_ops(&lib80211_crypt_null);
  206. BUG_ON(!list_empty(&lib80211_crypto_algs));
  207. }
  208. module_init(lib80211_init);
  209. module_exit(lib80211_exit);