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 unsigned char broadcastAddr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
54 atomic_t module_state;
56 static struct packet_type batman_adv_packet_type __read_mostly = {
57 .type = cpu_to_be16(ETH_P_BATMAN),
58 .func = batman_skb_recv,
61 struct workqueue_struct *bat_event_workqueue;
63 #ifdef CONFIG_BATMAN_ADV_DEBUG
66 module_param(debug, int, 0644);
68 int bat_debug_type(int type)
78 INIT_LIST_HEAD(&if_list);
79 INIT_HLIST_HEAD(&forw_bat_list);
80 INIT_HLIST_HEAD(&forw_bcast_list);
82 atomic_set(&module_state, MODULE_INACTIVE);
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(&aggregation_enabled, 1);
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");
93 if (!bat_event_workqueue)
96 retval = setup_procfs();
102 /* initialize layer 2 interface */
103 soft_device = alloc_netdev(sizeof(struct bat_priv) , "bat%d",
107 printk(KERN_ERR "batman-adv:Unable to allocate the batman interface\n");
111 retval = register_netdev(soft_device);
114 printk(KERN_ERR "batman-adv:Unable to register the batman interface: %i\n", retval);
115 goto free_soft_device;
118 register_netdevice_notifier(&hard_if_notifier);
119 dev_add_pack(&batman_adv_packet_type);
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);
127 free_netdev(soft_device);
133 void cleanup_module(void)
138 unregister_netdev(soft_device);
142 dev_remove_pack(&batman_adv_packet_type);
144 unregister_netdevice_notifier(&hard_if_notifier);
147 destroy_workqueue(bat_event_workqueue);
148 bat_event_workqueue = NULL;
151 /* activates the module, creates bat device, starts timer ... */
152 void activate_module(void)
154 if (originator_init() < 1)
157 if (hna_local_init() < 1)
160 if (hna_global_init() < 1)
163 hna_local_add(soft_device->dev_addr);
165 if (bat_device_setup() < 1)
172 atomic_set(&module_state, MODULE_ACTIVE);
176 printk(KERN_ERR "batman-adv:Unable to allocate memory for mesh information structures: out of mem ?\n");
182 /* shuts down the whole module.*/
183 void shutdown_module(void)
185 atomic_set(&module_state, MODULE_DEACTIVATING);
187 purge_outstanding_packets();
188 flush_workqueue(bat_event_workqueue);
192 /* TODO: unregister BATMAN pack */
200 bat_device_destroy();
202 hardif_remove_interfaces();
204 atomic_set(&module_state, MODULE_INACTIVE);
207 void inc_module_count(void)
209 try_module_get(THIS_MODULE);
212 void dec_module_count(void)
214 module_put(THIS_MODULE);
217 int addr_to_string(char *buff, uint8_t *addr)
219 return sprintf(buff, "%02x:%02x:%02x:%02x:%02x:%02x",
220 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
223 /* returns 1 if they are the same originator */
225 int compare_orig(void *data1, void *data2)
227 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
230 /* hashfunction to choose an entry in a hash table of given size */
231 /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
232 int choose_orig(void *data, int32_t size)
234 unsigned char *key = data;
238 for (i = 0; i < 6; i++) {
240 hash += (hash << 10);
245 hash ^= (hash >> 11);
246 hash += (hash << 15);
251 int is_my_mac(uint8_t *addr)
253 struct batman_if *batman_if;
255 list_for_each_entry_rcu(batman_if, &if_list, list) {
256 if ((batman_if->net_dev) &&
257 (compare_orig(batman_if->net_dev->dev_addr, addr))) {
267 int is_bcast(uint8_t *addr)
269 return (addr[0] == (uint8_t)0xff) && (addr[1] == (uint8_t)0xff);
272 int is_mcast(uint8_t *addr)
277 MODULE_LICENSE("GPL");
279 MODULE_AUTHOR(DRIVER_AUTHOR);
280 MODULE_DESCRIPTION(DRIVER_DESC);
281 MODULE_SUPPORTED_DEVICE(DRIVER_DEVICE);
282 #ifdef REVISION_VERSION
283 MODULE_VERSION(SOURCE_VERSION "-" REVISION_VERSION);
285 MODULE_VERSION(SOURCE_VERSION);