main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (C) 2007-2011 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. struct list_head if_list;
  35. unsigned char broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  36. struct workqueue_struct *bat_event_workqueue;
  37. static int __init batman_init(void)
  38. {
  39. INIT_LIST_HEAD(&if_list);
  40. /* the name should not be longer than 10 chars - see
  41. * http://lwn.net/Articles/23634/ */
  42. bat_event_workqueue = create_singlethread_workqueue("bat_events");
  43. if (!bat_event_workqueue)
  44. return -ENOMEM;
  45. bat_socket_init();
  46. debugfs_init();
  47. register_netdevice_notifier(&hard_if_notifier);
  48. pr_info("B.A.T.M.A.N. advanced %s%s (compatibility version %i) "
  49. "loaded\n", SOURCE_VERSION, REVISION_VERSION_STR,
  50. COMPAT_VERSION);
  51. return 0;
  52. }
  53. static void __exit batman_exit(void)
  54. {
  55. debugfs_destroy();
  56. unregister_netdevice_notifier(&hard_if_notifier);
  57. hardif_remove_interfaces();
  58. flush_workqueue(bat_event_workqueue);
  59. destroy_workqueue(bat_event_workqueue);
  60. bat_event_workqueue = NULL;
  61. rcu_barrier();
  62. }
  63. int mesh_init(struct net_device *soft_iface)
  64. {
  65. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  66. spin_lock_init(&bat_priv->orig_hash_lock);
  67. spin_lock_init(&bat_priv->forw_bat_list_lock);
  68. spin_lock_init(&bat_priv->forw_bcast_list_lock);
  69. spin_lock_init(&bat_priv->hna_lhash_lock);
  70. spin_lock_init(&bat_priv->hna_ghash_lock);
  71. spin_lock_init(&bat_priv->gw_list_lock);
  72. spin_lock_init(&bat_priv->vis_hash_lock);
  73. spin_lock_init(&bat_priv->vis_list_lock);
  74. spin_lock_init(&bat_priv->softif_neigh_lock);
  75. INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
  76. INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
  77. INIT_HLIST_HEAD(&bat_priv->gw_list);
  78. INIT_HLIST_HEAD(&bat_priv->softif_neigh_list);
  79. if (originator_init(bat_priv) < 1)
  80. goto err;
  81. if (hna_local_init(bat_priv) < 1)
  82. goto err;
  83. if (hna_global_init(bat_priv) < 1)
  84. goto err;
  85. hna_local_add(soft_iface, soft_iface->dev_addr);
  86. if (vis_init(bat_priv) < 1)
  87. goto err;
  88. atomic_set(&bat_priv->mesh_state, MESH_ACTIVE);
  89. goto end;
  90. err:
  91. pr_err("Unable to allocate memory for mesh information structures: "
  92. "out of mem ?\n");
  93. mesh_free(soft_iface);
  94. return -1;
  95. end:
  96. return 0;
  97. }
  98. void mesh_free(struct net_device *soft_iface)
  99. {
  100. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  101. atomic_set(&bat_priv->mesh_state, MESH_DEACTIVATING);
  102. purge_outstanding_packets(bat_priv, NULL);
  103. vis_quit(bat_priv);
  104. gw_node_purge(bat_priv);
  105. originator_free(bat_priv);
  106. hna_local_free(bat_priv);
  107. hna_global_free(bat_priv);
  108. softif_neigh_purge(bat_priv);
  109. atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
  110. }
  111. void inc_module_count(void)
  112. {
  113. try_module_get(THIS_MODULE);
  114. }
  115. void dec_module_count(void)
  116. {
  117. module_put(THIS_MODULE);
  118. }
  119. int is_my_mac(uint8_t *addr)
  120. {
  121. struct batman_if *batman_if;
  122. rcu_read_lock();
  123. list_for_each_entry_rcu(batman_if, &if_list, list) {
  124. if (batman_if->if_status != IF_ACTIVE)
  125. continue;
  126. if (compare_orig(batman_if->net_dev->dev_addr, addr)) {
  127. rcu_read_unlock();
  128. return 1;
  129. }
  130. }
  131. rcu_read_unlock();
  132. return 0;
  133. }
  134. module_init(batman_init);
  135. module_exit(batman_exit);
  136. MODULE_LICENSE("GPL");
  137. MODULE_AUTHOR(DRIVER_AUTHOR);
  138. MODULE_DESCRIPTION(DRIVER_DESC);
  139. MODULE_SUPPORTED_DEVICE(DRIVER_DEVICE);
  140. #ifdef REVISION_VERSION
  141. MODULE_VERSION(SOURCE_VERSION "-" REVISION_VERSION);
  142. #else
  143. MODULE_VERSION(SOURCE_VERSION);
  144. #endif