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
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 originator_interval
;
45 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
= __constant_htons(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(&vis_mode
, VIS_TYPE_CLIENT_UPDATE
);
88 atomic_set(&aggregation_enabled
, 1);
90 /* the name should not be longer than 10 chars - see
91 * http://lwn.net/Articles/23634/ */
92 bat_event_workqueue
= create_singlethread_workqueue("bat_events");
94 if (!bat_event_workqueue
)
97 retval
= setup_procfs();
103 /* initialize layer 2 interface */
104 soft_device
= alloc_netdev(sizeof(struct bat_priv
) , "bat%d",
108 printk(KERN_ERR
"batman-adv:Unable to allocate the batman interface\n");
112 retval
= register_netdev(soft_device
);
115 printk(KERN_ERR
"batman-adv:Unable to register the batman interface: %i\n", retval
);
116 goto free_soft_device
;
119 register_netdevice_notifier(&hard_if_notifier
);
120 dev_add_pack(&batman_adv_packet_type
);
122 printk(KERN_INFO
"batman-adv:B.A.T.M.A.N. advanced %s%s (compatibility version %i) loaded \n",
123 SOURCE_VERSION
, REVISION_VERSION_STR
, COMPAT_VERSION
);
128 free_netdev(soft_device
);
134 void cleanup_module(void)
139 unregister_netdev(soft_device
);
143 dev_remove_pack(&batman_adv_packet_type
);
145 unregister_netdevice_notifier(&hard_if_notifier
);
148 destroy_workqueue(bat_event_workqueue
);
149 bat_event_workqueue
= NULL
;
152 /* activates the module, creates bat device, starts timer ... */
153 void activate_module(void)
155 if (originator_init() < 1)
158 if (hna_local_init() < 1)
161 if (hna_global_init() < 1)
164 hna_local_add(soft_device
->dev_addr
);
166 if (bat_device_setup() < 1)
173 atomic_set(&module_state
, MODULE_ACTIVE
);
177 printk(KERN_ERR
"batman-adv:Unable to allocate memory for mesh information structures: out of mem ?\n");
183 /* shuts down the whole module.*/
184 void shutdown_module(void)
186 atomic_set(&module_state
, MODULE_DEACTIVATING
);
188 purge_outstanding_packets();
189 flush_workqueue(bat_event_workqueue
);
193 /* TODO: unregister BATMAN pack */
201 bat_device_destroy();
203 hardif_remove_interfaces();
205 atomic_set(&module_state
, MODULE_INACTIVE
);
208 void inc_module_count(void)
210 try_module_get(THIS_MODULE
);
213 void dec_module_count(void)
215 module_put(THIS_MODULE
);
218 int addr_to_string(char *buff
, uint8_t *addr
)
220 return sprintf(buff
, "%02x:%02x:%02x:%02x:%02x:%02x",
221 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
224 /* returns 1 if they are the same originator */
226 int compare_orig(void *data1
, void *data2
)
228 return (memcmp(data1
, data2
, ETH_ALEN
) == 0 ? 1 : 0);
231 /* hashfunction to choose an entry in a hash table of given size */
232 /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
233 int choose_orig(void *data
, int32_t size
)
235 unsigned char *key
= data
;
239 for (i
= 0; i
< 6; i
++) {
241 hash
+= (hash
<< 10);
246 hash
^= (hash
>> 11);
247 hash
+= (hash
<< 15);
252 int is_my_mac(uint8_t *addr
)
254 struct batman_if
*batman_if
;
256 list_for_each_entry_rcu(batman_if
, &if_list
, list
) {
257 if ((batman_if
->net_dev
) &&
258 (compare_orig(batman_if
->net_dev
->dev_addr
, addr
))) {
268 int is_bcast(uint8_t *addr
)
270 return (addr
[0] == (uint8_t)0xff) && (addr
[1] == (uint8_t)0xff);
273 int is_mcast(uint8_t *addr
)
278 MODULE_LICENSE("GPL");
280 MODULE_AUTHOR(DRIVER_AUTHOR
);
281 MODULE_DESCRIPTION(DRIVER_DESC
);
282 MODULE_SUPPORTED_DEVICE(DRIVER_DEVICE
);
283 #ifdef REVISION_VERSION
284 MODULE_VERSION(SOURCE_VERSION
"-" REVISION_VERSION
);
286 MODULE_VERSION(SOURCE_VERSION
);