main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 hardif_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(&hardif_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->forw_bat_list_lock);
  67. spin_lock_init(&bat_priv->forw_bcast_list_lock);
  68. spin_lock_init(&bat_priv->hna_lhash_lock);
  69. spin_lock_init(&bat_priv->hna_ghash_lock);
  70. spin_lock_init(&bat_priv->gw_list_lock);
  71. spin_lock_init(&bat_priv->vis_hash_lock);
  72. spin_lock_init(&bat_priv->vis_list_lock);
  73. spin_lock_init(&bat_priv->softif_neigh_lock);
  74. INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
  75. INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
  76. INIT_HLIST_HEAD(&bat_priv->gw_list);
  77. INIT_HLIST_HEAD(&bat_priv->softif_neigh_list);
  78. if (originator_init(bat_priv) < 1)
  79. goto err;
  80. if (hna_local_init(bat_priv) < 1)
  81. goto err;
  82. if (hna_global_init(bat_priv) < 1)
  83. goto err;
  84. hna_local_add(soft_iface, soft_iface->dev_addr);
  85. if (vis_init(bat_priv) < 1)
  86. goto err;
  87. atomic_set(&bat_priv->mesh_state, MESH_ACTIVE);
  88. goto end;
  89. err:
  90. pr_err("Unable to allocate memory for mesh information structures: "
  91. "out of mem ?\n");
  92. mesh_free(soft_iface);
  93. return -1;
  94. end:
  95. return 0;
  96. }
  97. void mesh_free(struct net_device *soft_iface)
  98. {
  99. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  100. atomic_set(&bat_priv->mesh_state, MESH_DEACTIVATING);
  101. purge_outstanding_packets(bat_priv, NULL);
  102. vis_quit(bat_priv);
  103. gw_node_purge(bat_priv);
  104. originator_free(bat_priv);
  105. hna_local_free(bat_priv);
  106. hna_global_free(bat_priv);
  107. softif_neigh_purge(bat_priv);
  108. atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
  109. }
  110. void inc_module_count(void)
  111. {
  112. try_module_get(THIS_MODULE);
  113. }
  114. void dec_module_count(void)
  115. {
  116. module_put(THIS_MODULE);
  117. }
  118. int is_my_mac(uint8_t *addr)
  119. {
  120. struct hard_iface *hard_iface;
  121. rcu_read_lock();
  122. list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
  123. if (hard_iface->if_status != IF_ACTIVE)
  124. continue;
  125. if (compare_eth(hard_iface->net_dev->dev_addr, addr)) {
  126. rcu_read_unlock();
  127. return 1;
  128. }
  129. }
  130. rcu_read_unlock();
  131. return 0;
  132. }
  133. module_init(batman_init);
  134. module_exit(batman_exit);
  135. MODULE_LICENSE("GPL");
  136. MODULE_AUTHOR(DRIVER_AUTHOR);
  137. MODULE_DESCRIPTION(DRIVER_DESC);
  138. MODULE_SUPPORTED_DEVICE(DRIVER_DEVICE);
  139. #ifdef REVISION_VERSION
  140. MODULE_VERSION(SOURCE_VERSION "-" REVISION_VERSION);
  141. #else
  142. MODULE_VERSION(SOURCE_VERSION);
  143. #endif