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