2 * Copyright (C) 2007-2009 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
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.
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.
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
26 #include "originator.h"
27 #include "soft-interface.h"
29 #include "translation-table.h"
30 #include "hard-interface.h"
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;
41 DEFINE_SPINLOCK(orig_hash_lock);
42 DEFINE_SPINLOCK(forw_bat_list_lock);
43 DEFINE_SPINLOCK(forw_bcast_list_lock);
45 atomic_t originator_interval;
46 atomic_t vis_interval;
47 atomic_t aggregation_enabled;
51 struct net_device *soft_device;
53 static struct task_struct *kthread_task;
55 unsigned char broadcastAddr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
56 atomic_t module_state;
58 struct workqueue_struct *bat_event_workqueue;
60 #ifdef CONFIG_BATMAN_ADV_DEBUG
63 module_param(debug, int, 0644);
65 int bat_debug_type(int type)
75 INIT_LIST_HEAD(&if_list);
76 INIT_HLIST_HEAD(&forw_bat_list);
77 INIT_HLIST_HEAD(&forw_bcast_list);
79 atomic_set(&module_state, MODULE_INACTIVE);
81 atomic_set(&originator_interval, 1000);
82 atomic_set(&vis_interval, 1000);/* TODO: raise this later, this is only
83 * for debugging now. */
84 atomic_set(&aggregation_enabled, 1);
86 /* the name should not be longer than 10 chars - see
87 * http://lwn.net/Articles/23634/ */
88 bat_event_workqueue = create_singlethread_workqueue("bat_events");
90 if (!bat_event_workqueue)
93 retval = setup_procfs();
99 /* initialize layer 2 interface */
100 soft_device = alloc_netdev(sizeof(struct bat_priv) , "bat%d",
104 printk(KERN_ERR "batman-adv:Unable to allocate the batman interface\n");
108 retval = register_netdev(soft_device);
111 printk(KERN_ERR "batman-adv:Unable to register the batman interface: %i\n", retval);
112 goto free_soft_device;
115 register_netdevice_notifier(&hard_if_notifier);
117 printk(KERN_INFO "batman-adv:B.A.T.M.A.N. advanced %s%s (compatibility version %i) loaded \n",
118 SOURCE_VERSION, REVISION_VERSION_STR, COMPAT_VERSION);
123 free_netdev(soft_device);
129 void cleanup_module(void)
134 unregister_netdev(soft_device);
138 unregister_netdevice_notifier(&hard_if_notifier);
141 destroy_workqueue(bat_event_workqueue);
142 bat_event_workqueue = NULL;
145 /* activates the module, creates bat device, starts timer ... */
146 void activate_module(void)
148 if (originator_init() < 1)
151 if (hna_local_init() < 1)
154 if (hna_global_init() < 1)
157 hna_local_add(soft_device->dev_addr);
159 if (bat_device_setup() < 1)
165 /* (re)start kernel thread for packet processing */
167 kthread_task = kthread_run(packet_recv_thread, NULL, "batman-adv");
169 if (IS_ERR(kthread_task)) {
170 printk(KERN_ERR "batman-adv:Unable to start packet receive thread\n");
176 atomic_set(&module_state, MODULE_ACTIVE);
180 printk(KERN_ERR "batman-adv:Unable to allocate memory for mesh information structures: out of mem ?\n");
186 /* shuts down the whole module.*/
187 void shutdown_module(void)
189 atomic_set(&module_state, MODULE_DEACTIVATING);
191 purge_outstanding_packets();
192 flush_workqueue(bat_event_workqueue);
196 /* deactivate kernel thread for packet processing (if running) */
198 atomic_set(&exit_cond, 1);
199 wake_up_interruptible(&thread_wait);
200 kthread_stop(kthread_task);
211 bat_device_destroy();
213 hardif_remove_interfaces();
215 atomic_set(&module_state, MODULE_INACTIVE);
218 void inc_module_count(void)
220 try_module_get(THIS_MODULE);
223 void dec_module_count(void)
225 module_put(THIS_MODULE);
228 int addr_to_string(char *buff, uint8_t *addr)
230 return sprintf(buff, "%02x:%02x:%02x:%02x:%02x:%02x",
231 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
234 /* returns 1 if they are the same originator */
236 int compare_orig(void *data1, void *data2)
238 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
241 /* hashfunction to choose an entry in a hash table of given size */
242 /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
243 int choose_orig(void *data, int32_t size)
245 unsigned char *key = data;
249 for (i = 0; i < 6; i++) {
251 hash += (hash << 10);
256 hash ^= (hash >> 11);
257 hash += (hash << 15);
262 int is_my_mac(uint8_t *addr)
264 struct batman_if *batman_if;
266 list_for_each_entry_rcu(batman_if, &if_list, list) {
267 if ((batman_if->net_dev) &&
268 (compare_orig(batman_if->net_dev->dev_addr, addr))) {
278 int is_bcast(uint8_t *addr)
280 return (addr[0] == (uint8_t)0xff) && (addr[1] == (uint8_t)0xff);
283 int is_mcast(uint8_t *addr)
288 MODULE_LICENSE("GPL");
290 MODULE_AUTHOR(DRIVER_AUTHOR);
291 MODULE_DESCRIPTION(DRIVER_DESC);
292 MODULE_SUPPORTED_DEVICE(DRIVER_DEVICE);
293 #ifdef REVISION_VERSION
294 MODULE_VERSION(SOURCE_VERSION "-" REVISION_VERSION);
296 MODULE_VERSION(SOURCE_VERSION);