PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/source/kernel/android-2.6.35/net/wireless/lib80211.c

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