main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include "bat_sysfs.h"
  23. #include "bat_debugfs.h"
  24. #include "routing.h"
  25. #include "send.h"
  26. #include "originator.h"
  27. #include "soft-interface.h"
  28. #include "icmp_socket.h"
  29. #include "translation-table.h"
  30. #include "hard-interface.h"
  31. #include "gateway_client.h"
  32. #include "vis.h"
  33. #include "hash.h"
  34. #include "bat_algo.h"
  35. /* List manipulations on hardif_list have to be rtnl_lock()'ed,
  36. * list traversals just rcu-locked */
  37. struct list_head hardif_list;
  38. char bat_routing_algo[20] = "BATMAN IV";
  39. static struct hlist_head bat_algo_list;
  40. unsigned char broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  41. struct workqueue_struct *bat_event_workqueue;
  42. static int __init batman_init(void)
  43. {
  44. INIT_LIST_HEAD(&hardif_list);
  45. INIT_HLIST_HEAD(&bat_algo_list);
  46. bat_iv_init();
  47. /* the name should not be longer than 10 chars - see
  48. * http://lwn.net/Articles/23634/ */
  49. bat_event_workqueue = create_singlethread_workqueue("bat_events");
  50. if (!bat_event_workqueue)
  51. return -ENOMEM;
  52. bat_socket_init();
  53. debugfs_init();
  54. register_netdevice_notifier(&hard_if_notifier);
  55. pr_info("B.A.T.M.A.N. advanced %s (compatibility version %i) loaded\n",
  56. SOURCE_VERSION, COMPAT_VERSION);
  57. return 0;
  58. }
  59. static void __exit batman_exit(void)
  60. {
  61. debugfs_destroy();
  62. unregister_netdevice_notifier(&hard_if_notifier);
  63. hardif_remove_interfaces();
  64. flush_workqueue(bat_event_workqueue);
  65. destroy_workqueue(bat_event_workqueue);
  66. bat_event_workqueue = NULL;
  67. rcu_barrier();
  68. }
  69. int mesh_init(struct net_device *soft_iface)
  70. {
  71. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  72. spin_lock_init(&bat_priv->forw_bat_list_lock);
  73. spin_lock_init(&bat_priv->forw_bcast_list_lock);
  74. spin_lock_init(&bat_priv->tt_changes_list_lock);
  75. spin_lock_init(&bat_priv->tt_req_list_lock);
  76. spin_lock_init(&bat_priv->tt_roam_list_lock);
  77. spin_lock_init(&bat_priv->tt_buff_lock);
  78. spin_lock_init(&bat_priv->gw_list_lock);
  79. spin_lock_init(&bat_priv->vis_hash_lock);
  80. spin_lock_init(&bat_priv->vis_list_lock);
  81. spin_lock_init(&bat_priv->softif_neigh_lock);
  82. spin_lock_init(&bat_priv->softif_neigh_vid_lock);
  83. INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
  84. INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
  85. INIT_HLIST_HEAD(&bat_priv->gw_list);
  86. INIT_HLIST_HEAD(&bat_priv->softif_neigh_vids);
  87. INIT_LIST_HEAD(&bat_priv->tt_changes_list);
  88. INIT_LIST_HEAD(&bat_priv->tt_req_list);
  89. INIT_LIST_HEAD(&bat_priv->tt_roam_list);
  90. if (originator_init(bat_priv) < 1)
  91. goto err;
  92. if (tt_init(bat_priv) < 1)
  93. goto err;
  94. tt_local_add(soft_iface, soft_iface->dev_addr, NULL_IFINDEX);
  95. if (vis_init(bat_priv) < 1)
  96. goto err;
  97. atomic_set(&bat_priv->gw_reselect, 0);
  98. atomic_set(&bat_priv->mesh_state, MESH_ACTIVE);
  99. goto end;
  100. err:
  101. mesh_free(soft_iface);
  102. return -1;
  103. end:
  104. return 0;
  105. }
  106. void mesh_free(struct net_device *soft_iface)
  107. {
  108. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  109. atomic_set(&bat_priv->mesh_state, MESH_DEACTIVATING);
  110. purge_outstanding_packets(bat_priv, NULL);
  111. vis_quit(bat_priv);
  112. gw_node_purge(bat_priv);
  113. originator_free(bat_priv);
  114. tt_free(bat_priv);
  115. softif_neigh_purge(bat_priv);
  116. atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
  117. }
  118. void inc_module_count(void)
  119. {
  120. try_module_get(THIS_MODULE);
  121. }
  122. void dec_module_count(void)
  123. {
  124. module_put(THIS_MODULE);
  125. }
  126. int is_my_mac(const uint8_t *addr)
  127. {
  128. const struct hard_iface *hard_iface;
  129. rcu_read_lock();
  130. list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
  131. if (hard_iface->if_status != IF_ACTIVE)
  132. continue;
  133. if (compare_eth(hard_iface->net_dev->dev_addr, addr)) {
  134. rcu_read_unlock();
  135. return 1;
  136. }
  137. }
  138. rcu_read_unlock();
  139. return 0;
  140. }
  141. static struct bat_algo_ops *bat_algo_get(char *name)
  142. {
  143. struct bat_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
  144. struct hlist_node *node;
  145. hlist_for_each_entry(bat_algo_ops_tmp, node, &bat_algo_list, list) {
  146. if (strcmp(bat_algo_ops_tmp->name, name) != 0)
  147. continue;
  148. bat_algo_ops = bat_algo_ops_tmp;
  149. break;
  150. }
  151. return bat_algo_ops;
  152. }
  153. int bat_algo_register(struct bat_algo_ops *bat_algo_ops)
  154. {
  155. struct bat_algo_ops *bat_algo_ops_tmp;
  156. int ret = -1;
  157. bat_algo_ops_tmp = bat_algo_get(bat_algo_ops->name);
  158. if (bat_algo_ops_tmp) {
  159. pr_info("Trying to register already registered routing algorithm: %s\n",
  160. bat_algo_ops->name);
  161. goto out;
  162. }
  163. /* all algorithms must implement all ops (for now) */
  164. if (!bat_algo_ops->bat_ogm_init ||
  165. !bat_algo_ops->bat_ogm_init_primary ||
  166. !bat_algo_ops->bat_ogm_update_mac ||
  167. !bat_algo_ops->bat_ogm_schedule ||
  168. !bat_algo_ops->bat_ogm_emit ||
  169. !bat_algo_ops->bat_ogm_receive) {
  170. pr_info("Routing algo '%s' does not implement required ops\n",
  171. bat_algo_ops->name);
  172. goto out;
  173. }
  174. INIT_HLIST_NODE(&bat_algo_ops->list);
  175. hlist_add_head(&bat_algo_ops->list, &bat_algo_list);
  176. ret = 0;
  177. out:
  178. return ret;
  179. }
  180. int bat_algo_select(struct bat_priv *bat_priv, char *name)
  181. {
  182. struct bat_algo_ops *bat_algo_ops;
  183. int ret = -1;
  184. bat_algo_ops = bat_algo_get(name);
  185. if (!bat_algo_ops)
  186. goto out;
  187. bat_priv->bat_algo_ops = bat_algo_ops;
  188. ret = 0;
  189. out:
  190. return ret;
  191. }
  192. int bat_algo_seq_print_text(struct seq_file *seq, void *offset)
  193. {
  194. struct bat_algo_ops *bat_algo_ops;
  195. struct hlist_node *node;
  196. seq_printf(seq, "Available routing algorithms:\n");
  197. hlist_for_each_entry(bat_algo_ops, node, &bat_algo_list, list) {
  198. seq_printf(seq, "%s\n", bat_algo_ops->name);
  199. }
  200. return 0;
  201. }
  202. static int param_set_ra(const char *val, const struct kernel_param *kp)
  203. {
  204. struct bat_algo_ops *bat_algo_ops;
  205. bat_algo_ops = bat_algo_get((char *)val);
  206. if (!bat_algo_ops) {
  207. pr_err("Routing algorithm '%s' is not supported\n", val);
  208. return -EINVAL;
  209. }
  210. return param_set_copystring(val, kp);
  211. }
  212. static const struct kernel_param_ops param_ops_ra = {
  213. .set = param_set_ra,
  214. .get = param_get_string,
  215. };
  216. static struct kparam_string __param_string_ra = {
  217. .maxlen = sizeof(bat_routing_algo),
  218. .string = bat_routing_algo,
  219. };
  220. module_param_cb(routing_algo, &param_ops_ra, &__param_string_ra, 0644);
  221. module_init(batman_init);
  222. module_exit(batman_exit);
  223. MODULE_LICENSE("GPL");
  224. MODULE_AUTHOR(DRIVER_AUTHOR);
  225. MODULE_DESCRIPTION(DRIVER_DESC);
  226. MODULE_SUPPORTED_DEVICE(DRIVER_DEVICE);
  227. MODULE_VERSION(SOURCE_VERSION);