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"
24 #include "bat_debugfs.h"
27 #include "originator.h"
28 #include "soft-interface.h"
29 #include "icmp_socket.h"
30 #include "translation-table.h"
31 #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 bcast_queue_left;
46 atomic_t batman_queue_left;
50 struct net_device *soft_device;
52 unsigned char broadcast_addr[] = {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 static int __init batman_init(void)
66 INIT_LIST_HEAD(&if_list);
67 INIT_HLIST_HEAD(&forw_bat_list);
68 INIT_HLIST_HEAD(&forw_bcast_list);
70 atomic_set(&module_state, MODULE_INACTIVE);
72 atomic_set(&bcast_queue_left, BCAST_QUEUE_LEN);
73 atomic_set(&batman_queue_left, BATMAN_QUEUE_LEN);
75 /* the name should not be longer than 10 chars - see
76 * http://lwn.net/Articles/23634/ */
77 bat_event_workqueue = create_singlethread_workqueue("bat_events");
79 if (!bat_event_workqueue)
85 /* initialize layer 2 interface */
86 soft_device = alloc_netdev(sizeof(struct bat_priv) , "bat%d",
90 pr_err("Unable to allocate the batman interface\n");
94 retval = register_netdev(soft_device);
97 pr_err("Unable to register the batman interface: %i\n", retval);
98 goto free_soft_device;
101 retval = sysfs_add_meshif(soft_device);
104 goto unreg_soft_device;
106 retval = debugfs_add_meshif(soft_device);
111 register_netdevice_notifier(&hard_if_notifier);
112 dev_add_pack(&batman_adv_packet_type);
114 pr_info("B.A.T.M.A.N. advanced %s%s (compatibility version %i) "
115 "loaded\n", SOURCE_VERSION, REVISION_VERSION_STR,
121 sysfs_del_meshif(soft_device);
123 unregister_netdev(soft_device);
128 free_netdev(soft_device);
134 static void __exit batman_exit(void)
139 unregister_netdevice_notifier(&hard_if_notifier);
140 hardif_remove_interfaces();
143 debugfs_del_meshif(soft_device);
144 sysfs_del_meshif(soft_device);
145 unregister_netdev(soft_device);
149 dev_remove_pack(&batman_adv_packet_type);
151 destroy_workqueue(bat_event_workqueue);
152 bat_event_workqueue = NULL;
155 /* activates the module, starts timer ... */
156 void activate_module(void)
158 if (originator_init() < 1)
161 if (hna_local_init() < 1)
164 if (hna_global_init() < 1)
167 hna_local_add(soft_device->dev_addr);
173 atomic_set(&module_state, MODULE_ACTIVE);
177 pr_err("Unable to allocate memory for mesh information structures: "
184 /* shuts down the whole module.*/
185 void deactivate_module(void)
187 atomic_set(&module_state, MODULE_DEACTIVATING);
189 purge_outstanding_packets(NULL);
190 flush_workqueue(bat_event_workqueue);
194 /* TODO: unregister BATMAN pack */
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, "%pM", addr);
222 /* returns 1 if they are the same originator */
224 int compare_orig(void *data1, void *data2)
226 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
229 /* hashfunction to choose an entry in a hash table of given size */
230 /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
231 int choose_orig(void *data, int32_t size)
233 unsigned char *key = data;
237 for (i = 0; i < 6; i++) {
239 hash += (hash << 10);
244 hash ^= (hash >> 11);
245 hash += (hash << 15);
250 int is_my_mac(uint8_t *addr)
252 struct batman_if *batman_if;
255 list_for_each_entry_rcu(batman_if, &if_list, list) {
256 if (batman_if->if_status != IF_ACTIVE)
259 if (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_init(batman_init);
280 module_exit(batman_exit);
282 MODULE_LICENSE("GPL");
284 MODULE_AUTHOR(DRIVER_AUTHOR);
285 MODULE_DESCRIPTION(DRIVER_DESC);
286 MODULE_SUPPORTED_DEVICE(DRIVER_DEVICE);
287 #ifdef REVISION_VERSION
288 MODULE_VERSION(SOURCE_VERSION "-" REVISION_VERSION);
290 MODULE_VERSION(SOURCE_VERSION);