main.c 4.3 KB

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