lib80211.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. const char *print_ssid(char *buf, const char *ssid, u8 ssid_len)
  40. {
  41. const char *s = ssid;
  42. char *d = buf;
  43. ssid_len = min_t(u8, ssid_len, IEEE80211_MAX_SSID_LEN);
  44. while (ssid_len--) {
  45. if (isprint(*s)) {
  46. *d++ = *s++;
  47. continue;
  48. }
  49. *d++ = '\\';
  50. if (*s == '\0')
  51. *d++ = '0';
  52. else if (*s == '\n')
  53. *d++ = 'n';
  54. else if (*s == '\r')
  55. *d++ = 'r';
  56. else if (*s == '\t')
  57. *d++ = 't';
  58. else if (*s == '\\')
  59. *d++ = '\\';
  60. else
  61. d += snprintf(d, 3, "%03o", *s);
  62. s++;
  63. }
  64. *d = '\0';
  65. return buf;
  66. }
  67. EXPORT_SYMBOL(print_ssid);
  68. int lib80211_crypt_info_init(struct lib80211_crypt_info *info, char *name,
  69. spinlock_t *lock)
  70. {
  71. memset(info, 0, sizeof(*info));
  72. info->name = name;
  73. info->lock = lock;
  74. INIT_LIST_HEAD(&info->crypt_deinit_list);
  75. setup_timer(&info->crypt_deinit_timer, lib80211_crypt_deinit_handler,
  76. (unsigned long)info);
  77. return 0;
  78. }
  79. EXPORT_SYMBOL(lib80211_crypt_info_init);
  80. void lib80211_crypt_info_free(struct lib80211_crypt_info *info)
  81. {
  82. int i;
  83. lib80211_crypt_quiescing(info);
  84. del_timer_sync(&info->crypt_deinit_timer);
  85. lib80211_crypt_deinit_entries(info, 1);
  86. for (i = 0; i < NUM_WEP_KEYS; i++) {
  87. struct lib80211_crypt_data *crypt = info->crypt[i];
  88. if (crypt) {
  89. if (crypt->ops) {
  90. crypt->ops->deinit(crypt->priv);
  91. module_put(crypt->ops->owner);
  92. }
  93. kfree(crypt);
  94. info->crypt[i] = NULL;
  95. }
  96. }
  97. }
  98. EXPORT_SYMBOL(lib80211_crypt_info_free);
  99. static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
  100. int force)
  101. {
  102. struct lib80211_crypt_data *entry, *next;
  103. unsigned long flags;
  104. spin_lock_irqsave(info->lock, flags);
  105. list_for_each_entry_safe(entry, next, &info->crypt_deinit_list, list) {
  106. if (atomic_read(&entry->refcnt) != 0 && !force)
  107. continue;
  108. list_del(&entry->list);
  109. if (entry->ops) {
  110. entry->ops->deinit(entry->priv);
  111. module_put(entry->ops->owner);
  112. }
  113. kfree(entry);
  114. }
  115. spin_unlock_irqrestore(info->lock, flags);
  116. }
  117. /* After this, crypt_deinit_list won't accept new members */
  118. static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info)
  119. {
  120. unsigned long flags;
  121. spin_lock_irqsave(info->lock, flags);
  122. info->crypt_quiesced = 1;
  123. spin_unlock_irqrestore(info->lock, flags);
  124. }
  125. static void lib80211_crypt_deinit_handler(unsigned long data)
  126. {
  127. struct lib80211_crypt_info *info = (struct lib80211_crypt_info *)data;
  128. unsigned long flags;
  129. lib80211_crypt_deinit_entries(info, 0);
  130. spin_lock_irqsave(info->lock, flags);
  131. if (!list_empty(&info->crypt_deinit_list) && !info->crypt_quiesced) {
  132. printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
  133. "deletion list\n", info->name);
  134. info->crypt_deinit_timer.expires = jiffies + HZ;
  135. add_timer(&info->crypt_deinit_timer);
  136. }
  137. spin_unlock_irqrestore(info->lock, flags);
  138. }
  139. void lib80211_crypt_delayed_deinit(struct lib80211_crypt_info *info,
  140. struct lib80211_crypt_data **crypt)
  141. {
  142. struct lib80211_crypt_data *tmp;
  143. unsigned long flags;
  144. if (*crypt == NULL)
  145. return;
  146. tmp = *crypt;
  147. *crypt = NULL;
  148. /* must not run ops->deinit() while there may be pending encrypt or
  149. * decrypt operations. Use a list of delayed deinits to avoid needing
  150. * locking. */
  151. spin_lock_irqsave(info->lock, flags);
  152. if (!info->crypt_quiesced) {
  153. list_add(&tmp->list, &info->crypt_deinit_list);
  154. if (!timer_pending(&info->crypt_deinit_timer)) {
  155. info->crypt_deinit_timer.expires = jiffies + HZ;
  156. add_timer(&info->crypt_deinit_timer);
  157. }
  158. }
  159. spin_unlock_irqrestore(info->lock, flags);
  160. }
  161. EXPORT_SYMBOL(lib80211_crypt_delayed_deinit);
  162. int lib80211_register_crypto_ops(struct lib80211_crypto_ops *ops)
  163. {
  164. unsigned long flags;
  165. struct lib80211_crypto_alg *alg;
  166. alg = kzalloc(sizeof(*alg), GFP_KERNEL);
  167. if (alg == NULL)
  168. return -ENOMEM;
  169. alg->ops = ops;
  170. spin_lock_irqsave(&lib80211_crypto_lock, flags);
  171. list_add(&alg->list, &lib80211_crypto_algs);
  172. spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
  173. printk(KERN_DEBUG "lib80211_crypt: registered algorithm '%s'\n",
  174. ops->name);
  175. return 0;
  176. }
  177. EXPORT_SYMBOL(lib80211_register_crypto_ops);
  178. int lib80211_unregister_crypto_ops(struct lib80211_crypto_ops *ops)
  179. {
  180. struct lib80211_crypto_alg *alg;
  181. unsigned long flags;
  182. spin_lock_irqsave(&lib80211_crypto_lock, flags);
  183. list_for_each_entry(alg, &lib80211_crypto_algs, list) {
  184. if (alg->ops == ops)
  185. goto found;
  186. }
  187. spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
  188. return -EINVAL;
  189. found:
  190. printk(KERN_DEBUG "lib80211_crypt: unregistered algorithm '%s'\n",
  191. ops->name);
  192. list_del(&alg->list);
  193. spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
  194. kfree(alg);
  195. return 0;
  196. }
  197. EXPORT_SYMBOL(lib80211_unregister_crypto_ops);
  198. struct lib80211_crypto_ops *lib80211_get_crypto_ops(const char *name)
  199. {
  200. struct lib80211_crypto_alg *alg;
  201. unsigned long flags;
  202. spin_lock_irqsave(&lib80211_crypto_lock, flags);
  203. list_for_each_entry(alg, &lib80211_crypto_algs, list) {
  204. if (strcmp(alg->ops->name, name) == 0)
  205. goto found;
  206. }
  207. spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
  208. return NULL;
  209. found:
  210. spin_unlock_irqrestore(&lib80211_crypto_lock, flags);
  211. return alg->ops;
  212. }
  213. EXPORT_SYMBOL(lib80211_get_crypto_ops);
  214. static void *lib80211_crypt_null_init(int keyidx)
  215. {
  216. return (void *)1;
  217. }
  218. static void lib80211_crypt_null_deinit(void *priv)
  219. {
  220. }
  221. static struct lib80211_crypto_ops lib80211_crypt_null = {
  222. .name = "NULL",
  223. .init = lib80211_crypt_null_init,
  224. .deinit = lib80211_crypt_null_deinit,
  225. .owner = THIS_MODULE,
  226. };
  227. static int __init lib80211_init(void)
  228. {
  229. pr_info(DRV_DESCRIPTION "\n");
  230. return lib80211_register_crypto_ops(&lib80211_crypt_null);
  231. }
  232. static void __exit lib80211_exit(void)
  233. {
  234. lib80211_unregister_crypto_ops(&lib80211_crypt_null);
  235. BUG_ON(!list_empty(&lib80211_crypto_algs));
  236. }
  237. module_init(lib80211_init);
  238. module_exit(lib80211_exit);