main.c 4.2 KB

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