main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 "bridge_loop_avoidance.h"
  33. #include "vis.h"
  34. #include "hash.h"
  35. #include "bat_algo.h"
  36. /* List manipulations on hardif_list have to be rtnl_lock()'ed,
  37. * list traversals just rcu-locked */
  38. struct list_head hardif_list;
  39. char bat_routing_algo[20] = "BATMAN IV";
  40. static struct hlist_head bat_algo_list;
  41. unsigned char broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  42. struct workqueue_struct *bat_event_workqueue;
  43. static int __init batman_init(void)
  44. {
  45. INIT_LIST_HEAD(&hardif_list);
  46. INIT_HLIST_HEAD(&bat_algo_list);
  47. bat_iv_init();
  48. /* the name should not be longer than 10 chars - see
  49. * http://lwn.net/Articles/23634/ */
  50. bat_event_workqueue = create_singlethread_workqueue("bat_events");
  51. if (!bat_event_workqueue)
  52. return -ENOMEM;
  53. bat_socket_init();
  54. debugfs_init();
  55. register_netdevice_notifier(&hard_if_notifier);
  56. pr_info("B.A.T.M.A.N. advanced %s (compatibility version %i) loaded\n",
  57. SOURCE_VERSION, COMPAT_VERSION);
  58. return 0;
  59. }
  60. static void __exit batman_exit(void)
  61. {
  62. debugfs_destroy();
  63. unregister_netdevice_notifier(&hard_if_notifier);
  64. hardif_remove_interfaces();
  65. flush_workqueue(bat_event_workqueue);
  66. destroy_workqueue(bat_event_workqueue);
  67. bat_event_workqueue = NULL;
  68. rcu_barrier();
  69. }
  70. int mesh_init(struct net_device *soft_iface)
  71. {
  72. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  73. spin_lock_init(&bat_priv->forw_bat_list_lock);
  74. spin_lock_init(&bat_priv->forw_bcast_list_lock);
  75. spin_lock_init(&bat_priv->tt_changes_list_lock);
  76. spin_lock_init(&bat_priv->tt_req_list_lock);
  77. spin_lock_init(&bat_priv->tt_roam_list_lock);
  78. spin_lock_init(&bat_priv->tt_buff_lock);
  79. spin_lock_init(&bat_priv->gw_list_lock);
  80. spin_lock_init(&bat_priv->vis_hash_lock);
  81. spin_lock_init(&bat_priv->vis_list_lock);
  82. INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
  83. INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
  84. INIT_HLIST_HEAD(&bat_priv->gw_list);
  85. INIT_LIST_HEAD(&bat_priv->tt_changes_list);
  86. INIT_LIST_HEAD(&bat_priv->tt_req_list);
  87. INIT_LIST_HEAD(&bat_priv->tt_roam_list);
  88. if (originator_init(bat_priv) < 1)
  89. goto err;
  90. if (tt_init(bat_priv) < 1)
  91. goto err;
  92. tt_local_add(soft_iface, soft_iface->dev_addr, NULL_IFINDEX);
  93. if (vis_init(bat_priv) < 1)
  94. goto err;
  95. if (bla_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. bla_free(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_iface_enable ||
  165. !bat_algo_ops->bat_iface_disable ||
  166. !bat_algo_ops->bat_primary_iface_set ||
  167. !bat_algo_ops->bat_ogm_update_mac ||
  168. !bat_algo_ops->bat_ogm_schedule ||
  169. !bat_algo_ops->bat_ogm_emit ||
  170. !bat_algo_ops->bat_ogm_receive) {
  171. pr_info("Routing algo '%s' does not implement required ops\n",
  172. bat_algo_ops->name);
  173. goto out;
  174. }
  175. INIT_HLIST_NODE(&bat_algo_ops->list);
  176. hlist_add_head(&bat_algo_ops->list, &bat_algo_list);
  177. ret = 0;
  178. out:
  179. return ret;
  180. }
  181. int bat_algo_select(struct bat_priv *bat_priv, char *name)
  182. {
  183. struct bat_algo_ops *bat_algo_ops;
  184. int ret = -1;
  185. bat_algo_ops = bat_algo_get(name);
  186. if (!bat_algo_ops)
  187. goto out;
  188. bat_priv->bat_algo_ops = bat_algo_ops;
  189. ret = 0;
  190. out:
  191. return ret;
  192. }
  193. int bat_algo_seq_print_text(struct seq_file *seq, void *offset)
  194. {
  195. struct bat_algo_ops *bat_algo_ops;
  196. struct hlist_node *node;
  197. seq_printf(seq, "Available routing algorithms:\n");
  198. hlist_for_each_entry(bat_algo_ops, node, &bat_algo_list, list) {
  199. seq_printf(seq, "%s\n", bat_algo_ops->name);
  200. }
  201. return 0;
  202. }
  203. static int param_set_ra(const char *val, const struct kernel_param *kp)
  204. {
  205. struct bat_algo_ops *bat_algo_ops;
  206. bat_algo_ops = bat_algo_get((char *)val);
  207. if (!bat_algo_ops) {
  208. pr_err("Routing algorithm '%s' is not supported\n", val);
  209. return -EINVAL;
  210. }
  211. return param_set_copystring(val, kp);
  212. }
  213. static const struct kernel_param_ops param_ops_ra = {
  214. .set = param_set_ra,
  215. .get = param_get_string,
  216. };
  217. static struct kparam_string __param_string_ra = {
  218. .maxlen = sizeof(bat_routing_algo),
  219. .string = bat_routing_algo,
  220. };
  221. module_param_cb(routing_algo, &param_ops_ra, &__param_string_ra, 0644);
  222. module_init(batman_init);
  223. module_exit(batman_exit);
  224. MODULE_LICENSE("GPL");
  225. MODULE_AUTHOR(DRIVER_AUTHOR);
  226. MODULE_DESCRIPTION(DRIVER_DESC);
  227. MODULE_SUPPORTED_DEVICE(DRIVER_DEVICE);
  228. MODULE_VERSION(SOURCE_VERSION);