aa072d15134cce238f95127bbf9a69815769e84b
[pandora-kernel.git] / drivers / staging / batman-adv / main.c
1 /*
2  * Copyright (C) 2007-2010 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 "routing.h"
25 #include "send.h"
26 #include "originator.h"
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"
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
44 atomic_t vis_interval;
45 atomic_t bcast_queue_left;
46 atomic_t batman_queue_left;
47
48 int16_t num_hna;
49
50 struct net_device *soft_device;
51
52 unsigned char broadcastAddr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
53 atomic_t module_state;
54
55 static struct packet_type batman_adv_packet_type __read_mostly = {
56         .type = __constant_htons(ETH_P_BATMAN),
57         .func = batman_skb_recv,
58 };
59
60 struct workqueue_struct *bat_event_workqueue;
61
62 #ifdef CONFIG_BATMAN_ADV_DEBUG
63 int debug;
64
65 module_param(debug, int, 0644);
66
67 int bat_debug_type(int type)
68 {
69         return debug & type;
70 }
71 #endif
72
73 int 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
83         atomic_set(&vis_interval, 1000);/* TODO: raise this later, this is only
84                                          * for debugging now. */
85         atomic_set(&bcast_queue_left, BCAST_QUEUE_LEN);
86         atomic_set(&batman_queue_left, BATMAN_QUEUE_LEN);
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
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) {
102                 printk(KERN_ERR "batman-adv:Unable to allocate the batman interface\n");
103                 goto end;
104         }
105
106         retval = register_netdev(soft_device);
107
108         if (retval < 0) {
109                 printk(KERN_ERR "batman-adv:Unable to register the batman interface: %i\n", retval);
110                 goto free_soft_device;
111         }
112
113         retval = sysfs_add_meshif(soft_device);
114
115         if (retval < 0)
116                 goto unreg_soft_device;
117
118         register_netdevice_notifier(&hard_if_notifier);
119         dev_add_pack(&batman_adv_packet_type);
120
121         printk(KERN_INFO "batman-adv:B.A.T.M.A.N. advanced %s%s (compatibility version %i) loaded\n",
122                   SOURCE_VERSION, REVISION_VERSION_STR, COMPAT_VERSION);
123
124         return 0;
125
126 unreg_soft_device:
127         unregister_netdevice(soft_device);
128 free_soft_device:
129         free_netdev(soft_device);
130         soft_device = NULL;
131 end:
132         return -ENOMEM;
133 }
134
135 void cleanup_module(void)
136 {
137         deactivate_module();
138
139         unregister_netdevice_notifier(&hard_if_notifier);
140         hardif_remove_interfaces();
141
142         if (soft_device) {
143                 sysfs_del_meshif(soft_device);
144                 unregister_netdev(soft_device);
145                 soft_device = NULL;
146         }
147
148         dev_remove_pack(&batman_adv_packet_type);
149
150         destroy_workqueue(bat_event_workqueue);
151         bat_event_workqueue = NULL;
152 }
153
154 /* activates the module, creates bat device, starts timer ... */
155 void activate_module(void)
156 {
157         if (originator_init() < 1)
158                 goto err;
159
160         if (hna_local_init() < 1)
161                 goto err;
162
163         if (hna_global_init() < 1)
164                 goto err;
165
166         hna_local_add(soft_device->dev_addr);
167
168         if (bat_device_setup() < 1)
169                 goto end;
170
171         if (vis_init() < 1)
172                 goto err;
173
174         update_min_mtu();
175         atomic_set(&module_state, MODULE_ACTIVE);
176         goto end;
177
178 err:
179         printk(KERN_ERR "batman-adv:Unable to allocate memory for mesh information structures: out of mem ?\n");
180         deactivate_module();
181 end:
182         return;
183 }
184
185 /* shuts down the whole module.*/
186 void deactivate_module(void)
187 {
188         atomic_set(&module_state, MODULE_DEACTIVATING);
189
190         purge_outstanding_packets(NULL);
191         flush_workqueue(bat_event_workqueue);
192
193         vis_quit();
194
195         /* TODO: unregister BATMAN pack */
196
197         originator_free();
198
199         hna_local_free();
200         hna_global_free();
201
202         synchronize_net();
203         bat_device_destroy();
204
205         synchronize_rcu();
206         atomic_set(&module_state, MODULE_INACTIVE);
207 }
208
209 void inc_module_count(void)
210 {
211         try_module_get(THIS_MODULE);
212 }
213
214 void dec_module_count(void)
215 {
216         module_put(THIS_MODULE);
217 }
218
219 int addr_to_string(char *buff, uint8_t *addr)
220 {
221         return sprintf(buff, "%02x:%02x:%02x:%02x:%02x:%02x",
222                        addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
223 }
224
225 /* returns 1 if they are the same originator */
226
227 int compare_orig(void *data1, void *data2)
228 {
229         return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
230 }
231
232 /* hashfunction to choose an entry in a hash table of given size */
233 /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
234 int choose_orig(void *data, int32_t size)
235 {
236         unsigned char *key = data;
237         uint32_t hash = 0;
238         size_t i;
239
240         for (i = 0; i < 6; i++) {
241                 hash += key[i];
242                 hash += (hash << 10);
243                 hash ^= (hash >> 6);
244         }
245
246         hash += (hash << 3);
247         hash ^= (hash >> 11);
248         hash += (hash << 15);
249
250         return hash % size;
251 }
252
253 int is_my_mac(uint8_t *addr)
254 {
255         struct batman_if *batman_if;
256         rcu_read_lock();
257         list_for_each_entry_rcu(batman_if, &if_list, list) {
258                 if ((batman_if->net_dev) &&
259                     (compare_orig(batman_if->net_dev->dev_addr, addr))) {
260                         rcu_read_unlock();
261                         return 1;
262                 }
263         }
264         rcu_read_unlock();
265         return 0;
266
267 }
268
269 int is_bcast(uint8_t *addr)
270 {
271         return (addr[0] == (uint8_t)0xff) && (addr[1] == (uint8_t)0xff);
272 }
273
274 int is_mcast(uint8_t *addr)
275 {
276         return *addr & 0x01;
277 }
278
279 MODULE_LICENSE("GPL");
280
281 MODULE_AUTHOR(DRIVER_AUTHOR);
282 MODULE_DESCRIPTION(DRIVER_DESC);
283 MODULE_SUPPORTED_DEVICE(DRIVER_DEVICE);
284 #ifdef REVISION_VERSION
285 MODULE_VERSION(SOURCE_VERSION "-" REVISION_VERSION);
286 #else
287 MODULE_VERSION(SOURCE_VERSION);
288 #endif