Staging: batman-adv: convert multiple /proc files to use sysfs
[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 "proc.h"
24 #include "bat_sysfs.h"
25 #include "routing.h"
26 #include "send.h"
27 #include "originator.h"
28 #include "soft-interface.h"
29 #include "device.h"
30 #include "translation-table.h"
31 #include "hard-interface.h"
32 #include "types.h"
33 #include "vis.h"
34 #include "hash.h"
35
36 struct list_head if_list;
37 struct hlist_head forw_bat_list;
38 struct hlist_head forw_bcast_list;
39 struct hashtable_t *orig_hash;
40
41 DEFINE_SPINLOCK(orig_hash_lock);
42 DEFINE_SPINLOCK(forw_bat_list_lock);
43 DEFINE_SPINLOCK(forw_bcast_list_lock);
44
45 atomic_t originator_interval;
46 atomic_t vis_interval;
47 atomic_t vis_mode;
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
89         /* the name should not be longer than 10 chars - see
90          * http://lwn.net/Articles/23634/ */
91         bat_event_workqueue = create_singlethread_workqueue("bat_events");
92
93         if (!bat_event_workqueue)
94                 return -ENOMEM;
95
96         retval = setup_procfs();
97         if (retval < 0)
98                 return retval;
99
100         bat_device_init();
101
102         /* initialize layer 2 interface */
103         soft_device = alloc_netdev(sizeof(struct bat_priv) , "bat%d",
104                                    interface_setup);
105
106         if (!soft_device) {
107                 printk(KERN_ERR "batman-adv:Unable to allocate the batman interface\n");
108                 goto end;
109         }
110
111         retval = register_netdev(soft_device);
112
113         if (retval < 0) {
114                 printk(KERN_ERR "batman-adv:Unable to register the batman interface: %i\n", retval);
115                 goto free_soft_device;
116         }
117
118         retval = sysfs_add_meshif(soft_device);
119
120         if (retval < 0)
121                 goto unreg_soft_device;
122
123         register_netdevice_notifier(&hard_if_notifier);
124         dev_add_pack(&batman_adv_packet_type);
125
126         printk(KERN_INFO "batman-adv:B.A.T.M.A.N. advanced %s%s (compatibility version %i) loaded\n",
127                   SOURCE_VERSION, REVISION_VERSION_STR, COMPAT_VERSION);
128
129         return 0;
130
131 unreg_soft_device:
132         unregister_netdevice(soft_device);
133 free_soft_device:
134         free_netdev(soft_device);
135         soft_device = NULL;
136 end:
137         return -ENOMEM;
138 }
139
140 void cleanup_module(void)
141 {
142         shutdown_module();
143
144         if (soft_device) {
145                 sysfs_del_meshif(soft_device);
146                 unregister_netdev(soft_device);
147                 soft_device = NULL;
148         }
149
150         dev_remove_pack(&batman_adv_packet_type);
151
152         unregister_netdevice_notifier(&hard_if_notifier);
153         cleanup_procfs();
154
155         destroy_workqueue(bat_event_workqueue);
156         bat_event_workqueue = NULL;
157 }
158
159 /* activates the module, creates bat device, starts timer ... */
160 void activate_module(void)
161 {
162         if (originator_init() < 1)
163                 goto err;
164
165         if (hna_local_init() < 1)
166                 goto err;
167
168         if (hna_global_init() < 1)
169                 goto err;
170
171         hna_local_add(soft_device->dev_addr);
172
173         if (bat_device_setup() < 1)
174                 goto end;
175
176         if (vis_init() < 1)
177                 goto err;
178
179         update_min_mtu();
180         atomic_set(&module_state, MODULE_ACTIVE);
181         goto end;
182
183 err:
184         printk(KERN_ERR "batman-adv:Unable to allocate memory for mesh information structures: out of mem ?\n");
185         shutdown_module();
186 end:
187         return;
188 }
189
190 /* shuts down the whole module.*/
191 void shutdown_module(void)
192 {
193         atomic_set(&module_state, MODULE_DEACTIVATING);
194
195         purge_outstanding_packets();
196         flush_workqueue(bat_event_workqueue);
197
198         vis_quit();
199
200         /* TODO: unregister BATMAN pack */
201
202         originator_free();
203
204         hna_local_free();
205         hna_global_free();
206
207         synchronize_net();
208         bat_device_destroy();
209
210         hardif_remove_interfaces();
211         synchronize_rcu();
212         atomic_set(&module_state, MODULE_INACTIVE);
213 }
214
215 void inc_module_count(void)
216 {
217         try_module_get(THIS_MODULE);
218 }
219
220 void dec_module_count(void)
221 {
222         module_put(THIS_MODULE);
223 }
224
225 int addr_to_string(char *buff, uint8_t *addr)
226 {
227         return sprintf(buff, "%02x:%02x:%02x:%02x:%02x:%02x",
228                        addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
229 }
230
231 /* returns 1 if they are the same originator */
232
233 int compare_orig(void *data1, void *data2)
234 {
235         return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
236 }
237
238 /* hashfunction to choose an entry in a hash table of given size */
239 /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
240 int choose_orig(void *data, int32_t size)
241 {
242         unsigned char *key = data;
243         uint32_t hash = 0;
244         size_t i;
245
246         for (i = 0; i < 6; i++) {
247                 hash += key[i];
248                 hash += (hash << 10);
249                 hash ^= (hash >> 6);
250         }
251
252         hash += (hash << 3);
253         hash ^= (hash >> 11);
254         hash += (hash << 15);
255
256         return hash % size;
257 }
258
259 int is_my_mac(uint8_t *addr)
260 {
261         struct batman_if *batman_if;
262         rcu_read_lock();
263         list_for_each_entry_rcu(batman_if, &if_list, list) {
264                 if ((batman_if->net_dev) &&
265                     (compare_orig(batman_if->net_dev->dev_addr, addr))) {
266                         rcu_read_unlock();
267                         return 1;
268                 }
269         }
270         rcu_read_unlock();
271         return 0;
272
273 }
274
275 int is_bcast(uint8_t *addr)
276 {
277         return (addr[0] == (uint8_t)0xff) && (addr[1] == (uint8_t)0xff);
278 }
279
280 int is_mcast(uint8_t *addr)
281 {
282         return *addr & 0x01;
283 }
284
285 MODULE_LICENSE("GPL");
286
287 MODULE_AUTHOR(DRIVER_AUTHOR);
288 MODULE_DESCRIPTION(DRIVER_DESC);
289 MODULE_SUPPORTED_DEVICE(DRIVER_DEVICE);
290 #ifdef REVISION_VERSION
291 MODULE_VERSION(SOURCE_VERSION "-" REVISION_VERSION);
292 #else
293 MODULE_VERSION(SOURCE_VERSION);
294 #endif