Revert "staging: batman-adv: Use linux/etherdevice.h address helper functions"
[linux-2.6/btrfs-unstable.git] / drivers / staging / batman-adv / main.c
blob0587940d27238146a67ed765482877557f445206
1 /*
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
18 * 02110-1301, USA
22 #include "main.h"
23 #include "bat_sysfs.h"
24 #include "bat_debugfs.h"
25 #include "routing.h"
26 #include "send.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"
32 #include "types.h"
33 #include "vis.h"
34 #include "hash.h"
36 struct list_head if_list;
38 unsigned char broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
40 struct workqueue_struct *bat_event_workqueue;
42 static int __init batman_init(void)
44 INIT_LIST_HEAD(&if_list);
46 /* the name should not be longer than 10 chars - see
47 * http://lwn.net/Articles/23634/ */
48 bat_event_workqueue = create_singlethread_workqueue("bat_events");
50 if (!bat_event_workqueue)
51 return -ENOMEM;
53 bat_socket_init();
54 debugfs_init();
56 register_netdevice_notifier(&hard_if_notifier);
58 pr_info("B.A.T.M.A.N. advanced %s%s (compatibility version %i) "
59 "loaded\n", SOURCE_VERSION, REVISION_VERSION_STR,
60 COMPAT_VERSION);
62 return 0;
65 static void __exit batman_exit(void)
67 debugfs_destroy();
68 unregister_netdevice_notifier(&hard_if_notifier);
69 hardif_remove_interfaces();
71 flush_workqueue(bat_event_workqueue);
72 destroy_workqueue(bat_event_workqueue);
73 bat_event_workqueue = NULL;
75 rcu_barrier();
78 int mesh_init(struct net_device *soft_iface)
80 struct bat_priv *bat_priv = netdev_priv(soft_iface);
82 spin_lock_init(&bat_priv->orig_hash_lock);
83 spin_lock_init(&bat_priv->forw_bat_list_lock);
84 spin_lock_init(&bat_priv->forw_bcast_list_lock);
85 spin_lock_init(&bat_priv->hna_lhash_lock);
86 spin_lock_init(&bat_priv->hna_ghash_lock);
87 spin_lock_init(&bat_priv->vis_hash_lock);
88 spin_lock_init(&bat_priv->vis_list_lock);
90 INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
91 INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
93 if (originator_init(bat_priv) < 1)
94 goto err;
96 if (hna_local_init(bat_priv) < 1)
97 goto err;
99 if (hna_global_init(bat_priv) < 1)
100 goto err;
102 hna_local_add(soft_iface, soft_iface->dev_addr);
104 if (vis_init(bat_priv) < 1)
105 goto err;
107 atomic_set(&bat_priv->mesh_state, MESH_ACTIVE);
108 goto end;
110 err:
111 pr_err("Unable to allocate memory for mesh information structures: "
112 "out of mem ?\n");
113 mesh_free(soft_iface);
114 return -1;
116 end:
117 return 0;
120 void mesh_free(struct net_device *soft_iface)
122 struct bat_priv *bat_priv = netdev_priv(soft_iface);
124 atomic_set(&bat_priv->mesh_state, MESH_DEACTIVATING);
126 purge_outstanding_packets(bat_priv, NULL);
128 vis_quit(bat_priv);
130 originator_free(bat_priv);
132 hna_local_free(bat_priv);
133 hna_global_free(bat_priv);
135 atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
138 void inc_module_count(void)
140 try_module_get(THIS_MODULE);
143 void dec_module_count(void)
145 module_put(THIS_MODULE);
148 /* returns 1 if they are the same originator */
150 int compare_orig(void *data1, void *data2)
152 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
155 /* hashfunction to choose an entry in a hash table of given size */
156 /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
157 int choose_orig(void *data, int32_t size)
159 unsigned char *key = data;
160 uint32_t hash = 0;
161 size_t i;
163 for (i = 0; i < 6; i++) {
164 hash += key[i];
165 hash += (hash << 10);
166 hash ^= (hash >> 6);
169 hash += (hash << 3);
170 hash ^= (hash >> 11);
171 hash += (hash << 15);
173 return hash % size;
176 int is_my_mac(uint8_t *addr)
178 struct batman_if *batman_if;
180 rcu_read_lock();
181 list_for_each_entry_rcu(batman_if, &if_list, list) {
182 if (batman_if->if_status != IF_ACTIVE)
183 continue;
185 if (compare_orig(batman_if->net_dev->dev_addr, addr)) {
186 rcu_read_unlock();
187 return 1;
190 rcu_read_unlock();
191 return 0;
195 int is_bcast(uint8_t *addr)
197 return (addr[0] == (uint8_t)0xff) && (addr[1] == (uint8_t)0xff);
200 int is_mcast(uint8_t *addr)
202 return *addr & 0x01;
205 module_init(batman_init);
206 module_exit(batman_exit);
208 MODULE_LICENSE("GPL");
210 MODULE_AUTHOR(DRIVER_AUTHOR);
211 MODULE_DESCRIPTION(DRIVER_DESC);
212 MODULE_SUPPORTED_DEVICE(DRIVER_DEVICE);
213 #ifdef REVISION_VERSION
214 MODULE_VERSION(SOURCE_VERSION "-" REVISION_VERSION);
215 #else
216 MODULE_VERSION(SOURCE_VERSION);
217 #endif