xref: /linux/net/batman-adv/main.c (revision 64afe35398269577ef9809474dd7dc0e5d265176)
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 
22 #include "main.h"
23 #include "bat_sysfs.h"
24 #include "bat_debugfs.h"
25 #include "routing.h"
26 #include "send.h"
27 #include "originator.h"
28 #include "soft-interface.h"
29 #include "icmp_socket.h"
30 #include "translation-table.h"
31 #include "hard-interface.h"
32 #include "gateway_client.h"
33 #include "types.h"
34 #include "vis.h"
35 #include "hash.h"
36 
37 struct list_head if_list;
38 
39 unsigned char broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
40 
41 struct workqueue_struct *bat_event_workqueue;
42 
43 static int __init batman_init(void)
44 {
45 	INIT_LIST_HEAD(&if_list);
46 
47 	/* the name should not be longer than 10 chars - see
48 	 * http://lwn.net/Articles/23634/ */
49 	bat_event_workqueue = create_singlethread_workqueue("bat_events");
50 
51 	if (!bat_event_workqueue)
52 		return -ENOMEM;
53 
54 	bat_socket_init();
55 	debugfs_init();
56 
57 	register_netdevice_notifier(&hard_if_notifier);
58 
59 	pr_info("B.A.T.M.A.N. advanced %s%s (compatibility version %i) "
60 		"loaded\n", SOURCE_VERSION, REVISION_VERSION_STR,
61 		COMPAT_VERSION);
62 
63 	return 0;
64 }
65 
66 static void __exit batman_exit(void)
67 {
68 	debugfs_destroy();
69 	unregister_netdevice_notifier(&hard_if_notifier);
70 	hardif_remove_interfaces();
71 
72 	flush_workqueue(bat_event_workqueue);
73 	destroy_workqueue(bat_event_workqueue);
74 	bat_event_workqueue = NULL;
75 
76 	rcu_barrier();
77 }
78 
79 int mesh_init(struct net_device *soft_iface)
80 {
81 	struct bat_priv *bat_priv = netdev_priv(soft_iface);
82 
83 	spin_lock_init(&bat_priv->orig_hash_lock);
84 	spin_lock_init(&bat_priv->forw_bat_list_lock);
85 	spin_lock_init(&bat_priv->forw_bcast_list_lock);
86 	spin_lock_init(&bat_priv->hna_lhash_lock);
87 	spin_lock_init(&bat_priv->hna_ghash_lock);
88 	spin_lock_init(&bat_priv->gw_list_lock);
89 	spin_lock_init(&bat_priv->vis_hash_lock);
90 	spin_lock_init(&bat_priv->vis_list_lock);
91 	spin_lock_init(&bat_priv->softif_neigh_lock);
92 
93 	INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
94 	INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
95 	INIT_HLIST_HEAD(&bat_priv->gw_list);
96 	INIT_HLIST_HEAD(&bat_priv->softif_neigh_list);
97 
98 	if (originator_init(bat_priv) < 1)
99 		goto err;
100 
101 	if (hna_local_init(bat_priv) < 1)
102 		goto err;
103 
104 	if (hna_global_init(bat_priv) < 1)
105 		goto err;
106 
107 	hna_local_add(soft_iface, soft_iface->dev_addr);
108 
109 	if (vis_init(bat_priv) < 1)
110 		goto err;
111 
112 	atomic_set(&bat_priv->mesh_state, MESH_ACTIVE);
113 	goto end;
114 
115 err:
116 	pr_err("Unable to allocate memory for mesh information structures: "
117 	       "out of mem ?\n");
118 	mesh_free(soft_iface);
119 	return -1;
120 
121 end:
122 	return 0;
123 }
124 
125 void mesh_free(struct net_device *soft_iface)
126 {
127 	struct bat_priv *bat_priv = netdev_priv(soft_iface);
128 
129 	atomic_set(&bat_priv->mesh_state, MESH_DEACTIVATING);
130 
131 	purge_outstanding_packets(bat_priv, NULL);
132 
133 	vis_quit(bat_priv);
134 
135 	gw_node_purge(bat_priv);
136 	originator_free(bat_priv);
137 
138 	hna_local_free(bat_priv);
139 	hna_global_free(bat_priv);
140 
141 	softif_neigh_purge(bat_priv);
142 
143 	atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
144 }
145 
146 void inc_module_count(void)
147 {
148 	try_module_get(THIS_MODULE);
149 }
150 
151 void dec_module_count(void)
152 {
153 	module_put(THIS_MODULE);
154 }
155 
156 int is_my_mac(uint8_t *addr)
157 {
158 	struct batman_if *batman_if;
159 
160 	rcu_read_lock();
161 	list_for_each_entry_rcu(batman_if, &if_list, list) {
162 		if (batman_if->if_status != IF_ACTIVE)
163 			continue;
164 
165 		if (compare_orig(batman_if->net_dev->dev_addr, addr)) {
166 			rcu_read_unlock();
167 			return 1;
168 		}
169 	}
170 	rcu_read_unlock();
171 	return 0;
172 
173 }
174 
175 module_init(batman_init);
176 module_exit(batman_exit);
177 
178 MODULE_LICENSE("GPL");
179 
180 MODULE_AUTHOR(DRIVER_AUTHOR);
181 MODULE_DESCRIPTION(DRIVER_DESC);
182 MODULE_SUPPORTED_DEVICE(DRIVER_DEVICE);
183 #ifdef REVISION_VERSION
184 MODULE_VERSION(SOURCE_VERSION "-" REVISION_VERSION);
185 #else
186 MODULE_VERSION(SOURCE_VERSION);
187 #endif
188