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