]>
Commit | Line | Data |
---|---|---|
5beef3c9 | 1 | /* |
9b6d10b7 | 2 | * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors: |
5beef3c9 AL |
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" | |
47fdf097 | 23 | #include "bat_sysfs.h" |
5beef3c9 AL |
24 | #include "routing.h" |
25 | #include "send.h" | |
8a2e042c | 26 | #include "originator.h" |
5beef3c9 AL |
27 | #include "soft-interface.h" |
28 | #include "device.h" | |
29 | #include "translation-table.h" | |
30 | #include "hard-interface.h" | |
31 | #include "types.h" | |
32 | #include "vis.h" | |
33 | #include "hash.h" | |
5beef3c9 AL |
34 | |
35 | struct list_head if_list; | |
36 | struct hlist_head forw_bat_list; | |
37 | struct hlist_head forw_bcast_list; | |
38 | struct hashtable_t *orig_hash; | |
39 | ||
40 | DEFINE_SPINLOCK(orig_hash_lock); | |
41 | DEFINE_SPINLOCK(forw_bat_list_lock); | |
42 | DEFINE_SPINLOCK(forw_bcast_list_lock); | |
43 | ||
5beef3c9 | 44 | atomic_t vis_interval; |
5beef3c9 | 45 | int16_t num_hna; |
5beef3c9 AL |
46 | |
47 | struct net_device *soft_device; | |
48 | ||
5beef3c9 AL |
49 | unsigned char broadcastAddr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
50 | atomic_t module_state; | |
51 | ||
e7017195 | 52 | static struct packet_type batman_adv_packet_type __read_mostly = { |
b9b27e4e | 53 | .type = __constant_htons(ETH_P_BATMAN), |
e7017195 SW |
54 | .func = batman_skb_recv, |
55 | }; | |
56 | ||
5beef3c9 AL |
57 | struct workqueue_struct *bat_event_workqueue; |
58 | ||
734cc82a | 59 | #ifdef CONFIG_BATMAN_ADV_DEBUG |
bad2239e AL |
60 | int debug; |
61 | ||
62 | module_param(debug, int, 0644); | |
63 | ||
64 | int bat_debug_type(int type) | |
65 | { | |
66 | return debug & type; | |
67 | } | |
68 | #endif | |
69 | ||
5beef3c9 AL |
70 | int init_module(void) |
71 | { | |
72 | int retval; | |
73 | ||
74 | INIT_LIST_HEAD(&if_list); | |
75 | INIT_HLIST_HEAD(&forw_bat_list); | |
76 | INIT_HLIST_HEAD(&forw_bcast_list); | |
77 | ||
78 | atomic_set(&module_state, MODULE_INACTIVE); | |
79 | ||
5beef3c9 AL |
80 | atomic_set(&vis_interval, 1000);/* TODO: raise this later, this is only |
81 | * for debugging now. */ | |
5beef3c9 AL |
82 | |
83 | /* the name should not be longer than 10 chars - see | |
84 | * http://lwn.net/Articles/23634/ */ | |
85 | bat_event_workqueue = create_singlethread_workqueue("bat_events"); | |
86 | ||
87 | if (!bat_event_workqueue) | |
88 | return -ENOMEM; | |
89 | ||
5beef3c9 AL |
90 | bat_device_init(); |
91 | ||
92 | /* initialize layer 2 interface */ | |
93 | soft_device = alloc_netdev(sizeof(struct bat_priv) , "bat%d", | |
94 | interface_setup); | |
95 | ||
96 | if (!soft_device) { | |
bad2239e | 97 | printk(KERN_ERR "batman-adv:Unable to allocate the batman interface\n"); |
5beef3c9 AL |
98 | goto end; |
99 | } | |
100 | ||
101 | retval = register_netdev(soft_device); | |
102 | ||
103 | if (retval < 0) { | |
bad2239e | 104 | printk(KERN_ERR "batman-adv:Unable to register the batman interface: %i\n", retval); |
5beef3c9 AL |
105 | goto free_soft_device; |
106 | } | |
107 | ||
47fdf097 ML |
108 | retval = sysfs_add_meshif(soft_device); |
109 | ||
110 | if (retval < 0) | |
111 | goto unreg_soft_device; | |
112 | ||
5beef3c9 | 113 | register_netdevice_notifier(&hard_if_notifier); |
e7017195 | 114 | dev_add_pack(&batman_adv_packet_type); |
5beef3c9 | 115 | |
0887635b | 116 | printk(KERN_INFO "batman-adv:B.A.T.M.A.N. advanced %s%s (compatibility version %i) loaded\n", |
bad2239e | 117 | SOURCE_VERSION, REVISION_VERSION_STR, COMPAT_VERSION); |
5beef3c9 AL |
118 | |
119 | return 0; | |
120 | ||
47fdf097 ML |
121 | unreg_soft_device: |
122 | unregister_netdevice(soft_device); | |
5beef3c9 AL |
123 | free_soft_device: |
124 | free_netdev(soft_device); | |
125 | soft_device = NULL; | |
126 | end: | |
127 | return -ENOMEM; | |
128 | } | |
129 | ||
130 | void cleanup_module(void) | |
131 | { | |
208e13e4 ML |
132 | deactivate_module(); |
133 | ||
134 | unregister_netdevice_notifier(&hard_if_notifier); | |
135 | hardif_remove_interfaces(); | |
5beef3c9 AL |
136 | |
137 | if (soft_device) { | |
47fdf097 | 138 | sysfs_del_meshif(soft_device); |
5beef3c9 AL |
139 | unregister_netdev(soft_device); |
140 | soft_device = NULL; | |
141 | } | |
142 | ||
e7017195 SW |
143 | dev_remove_pack(&batman_adv_packet_type); |
144 | ||
5beef3c9 AL |
145 | destroy_workqueue(bat_event_workqueue); |
146 | bat_event_workqueue = NULL; | |
147 | } | |
148 | ||
149 | /* activates the module, creates bat device, starts timer ... */ | |
150 | void activate_module(void) | |
151 | { | |
152 | if (originator_init() < 1) | |
153 | goto err; | |
154 | ||
155 | if (hna_local_init() < 1) | |
156 | goto err; | |
157 | ||
158 | if (hna_global_init() < 1) | |
159 | goto err; | |
160 | ||
161 | hna_local_add(soft_device->dev_addr); | |
162 | ||
163 | if (bat_device_setup() < 1) | |
164 | goto end; | |
165 | ||
166 | if (vis_init() < 1) | |
167 | goto err; | |
168 | ||
5beef3c9 AL |
169 | update_min_mtu(); |
170 | atomic_set(&module_state, MODULE_ACTIVE); | |
171 | goto end; | |
172 | ||
173 | err: | |
bad2239e | 174 | printk(KERN_ERR "batman-adv:Unable to allocate memory for mesh information structures: out of mem ?\n"); |
208e13e4 | 175 | deactivate_module(); |
5beef3c9 AL |
176 | end: |
177 | return; | |
178 | } | |
179 | ||
180 | /* shuts down the whole module.*/ | |
208e13e4 | 181 | void deactivate_module(void) |
5beef3c9 AL |
182 | { |
183 | atomic_set(&module_state, MODULE_DEACTIVATING); | |
184 | ||
208e13e4 | 185 | purge_outstanding_packets(NULL); |
5beef3c9 AL |
186 | flush_workqueue(bat_event_workqueue); |
187 | ||
188 | vis_quit(); | |
189 | ||
e7017195 | 190 | /* TODO: unregister BATMAN pack */ |
5beef3c9 AL |
191 | |
192 | originator_free(); | |
193 | ||
194 | hna_local_free(); | |
195 | hna_global_free(); | |
196 | ||
197 | synchronize_net(); | |
198 | bat_device_destroy(); | |
199 | ||
5beef3c9 AL |
200 | synchronize_rcu(); |
201 | atomic_set(&module_state, MODULE_INACTIVE); | |
202 | } | |
203 | ||
204 | void inc_module_count(void) | |
205 | { | |
206 | try_module_get(THIS_MODULE); | |
207 | } | |
208 | ||
209 | void dec_module_count(void) | |
210 | { | |
211 | module_put(THIS_MODULE); | |
212 | } | |
213 | ||
214 | int addr_to_string(char *buff, uint8_t *addr) | |
215 | { | |
216 | return sprintf(buff, "%02x:%02x:%02x:%02x:%02x:%02x", | |
217 | addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); | |
218 | } | |
219 | ||
220 | /* returns 1 if they are the same originator */ | |
221 | ||
222 | int compare_orig(void *data1, void *data2) | |
223 | { | |
224 | return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0); | |
225 | } | |
226 | ||
227 | /* hashfunction to choose an entry in a hash table of given size */ | |
228 | /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */ | |
229 | int choose_orig(void *data, int32_t size) | |
230 | { | |
231 | unsigned char *key = data; | |
232 | uint32_t hash = 0; | |
233 | size_t i; | |
234 | ||
235 | for (i = 0; i < 6; i++) { | |
236 | hash += key[i]; | |
237 | hash += (hash << 10); | |
238 | hash ^= (hash >> 6); | |
239 | } | |
240 | ||
241 | hash += (hash << 3); | |
242 | hash ^= (hash >> 11); | |
243 | hash += (hash << 15); | |
244 | ||
245 | return hash % size; | |
246 | } | |
247 | ||
248 | int is_my_mac(uint8_t *addr) | |
249 | { | |
250 | struct batman_if *batman_if; | |
251 | rcu_read_lock(); | |
252 | list_for_each_entry_rcu(batman_if, &if_list, list) { | |
253 | if ((batman_if->net_dev) && | |
254 | (compare_orig(batman_if->net_dev->dev_addr, addr))) { | |
255 | rcu_read_unlock(); | |
256 | return 1; | |
257 | } | |
258 | } | |
259 | rcu_read_unlock(); | |
260 | return 0; | |
261 | ||
262 | } | |
263 | ||
264 | int is_bcast(uint8_t *addr) | |
265 | { | |
266 | return (addr[0] == (uint8_t)0xff) && (addr[1] == (uint8_t)0xff); | |
267 | } | |
268 | ||
269 | int is_mcast(uint8_t *addr) | |
270 | { | |
271 | return *addr & 0x01; | |
272 | } | |
273 | ||
274 | MODULE_LICENSE("GPL"); | |
275 | ||
276 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
277 | MODULE_DESCRIPTION(DRIVER_DESC); | |
278 | MODULE_SUPPORTED_DEVICE(DRIVER_DEVICE); | |
279 | #ifdef REVISION_VERSION | |
280 | MODULE_VERSION(SOURCE_VERSION "-" REVISION_VERSION); | |
281 | #else | |
282 | MODULE_VERSION(SOURCE_VERSION); | |
283 | #endif |