lib80211.c 7.0 KB

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