2 * Copyright (C) 2007-2010 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
23 #include "bat_sysfs.h"
26 #include "originator.h"
27 #include "soft-interface.h"
29 #include "translation-table.h"
30 #include "hard-interface.h"
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;
40 DEFINE_SPINLOCK(orig_hash_lock);
41 DEFINE_SPINLOCK(forw_bat_list_lock);
42 DEFINE_SPINLOCK(forw_bcast_list_lock);
44 atomic_t vis_interval;
45 atomic_t bcast_queue_left;
46 atomic_t batman_queue_left;
50 struct net_device *soft_device;
52 unsigned char broadcastAddr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
53 atomic_t module_state;
55 static struct packet_type batman_adv_packet_type __read_mostly = {
56 .type = __constant_htons(ETH_P_BATMAN),
57 .func = batman_skb_recv,
60 struct workqueue_struct *bat_event_workqueue;
62 #ifdef CONFIG_BATMAN_ADV_DEBUG
65 module_param(debug, int, 0644);
67 int bat_debug_type(int type)
77 INIT_LIST_HEAD(&if_list);
78 INIT_HLIST_HEAD(&forw_bat_list);
79 INIT_HLIST_HEAD(&forw_bcast_list);
81 atomic_set(&module_state, MODULE_INACTIVE);
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);
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");
92 if (!bat_event_workqueue)
97 /* initialize layer 2 interface */
98 soft_device = alloc_netdev(sizeof(struct bat_priv) , "bat%d",
102 printk(KERN_ERR "batman-adv:Unable to allocate the batman interface\n");
106 retval = register_netdev(soft_device);
109 printk(KERN_ERR "batman-adv:Unable to register the batman interface: %i\n", retval);
110 goto free_soft_device;
113 retval = sysfs_add_meshif(soft_device);
116 goto unreg_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 unregister_netdevice(soft_device);
129 free_netdev(soft_device);
135 void cleanup_module(void)
139 unregister_netdevice_notifier(&hard_if_notifier);
140 hardif_remove_interfaces();
143 sysfs_del_meshif(soft_device);
144 unregister_netdev(soft_device);
148 dev_remove_pack(&batman_adv_packet_type);
150 destroy_workqueue(bat_event_workqueue);
151 bat_event_workqueue = NULL;
154 /* activates the module, creates bat device, starts timer ... */
155 void activate_module(void)
157 if (originator_init() < 1)
160 if (hna_local_init() < 1)
163 if (hna_global_init() < 1)
166 hna_local_add(soft_device->dev_addr);
168 if (bat_device_setup() < 1)
175 atomic_set(&module_state, MODULE_ACTIVE);
179 printk(KERN_ERR "batman-adv:Unable to allocate memory for mesh information structures: out of mem ?\n");
185 /* shuts down the whole module.*/
186 void deactivate_module(void)
188 atomic_set(&module_state, MODULE_DEACTIVATING);
190 purge_outstanding_packets(NULL);
191 flush_workqueue(bat_event_workqueue);
195 /* TODO: unregister BATMAN pack */
203 bat_device_destroy();
206 atomic_set(&module_state, MODULE_INACTIVE);
209 void inc_module_count(void)
211 try_module_get(THIS_MODULE);
214 void dec_module_count(void)
216 module_put(THIS_MODULE);
219 int addr_to_string(char *buff, uint8_t *addr)
221 return sprintf(buff, "%02x:%02x:%02x:%02x:%02x:%02x",
222 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
225 /* returns 1 if they are the same originator */
227 int compare_orig(void *data1, void *data2)
229 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
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)
236 unsigned char *key = data;
240 for (i = 0; i < 6; i++) {
242 hash += (hash << 10);
247 hash ^= (hash >> 11);
248 hash += (hash << 15);
253 int is_my_mac(uint8_t *addr)
255 struct batman_if *batman_if;
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))) {
269 int is_bcast(uint8_t *addr)
271 return (addr[0] == (uint8_t)0xff) && (addr[1] == (uint8_t)0xff);
274 int is_mcast(uint8_t *addr)
279 MODULE_LICENSE("GPL");
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);
287 MODULE_VERSION(SOURCE_VERSION);