blob: ef1acfd7653c14dac92b5ee942c2a19728cae9e3 [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann64afe352011-01-27 10:38:15 +01002 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner, Simon Wunderlich
5 *
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.
9 *
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.
14 *
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
19 *
20 */
21
22#include "main.h"
23#include "translation-table.h"
24#include "soft-interface.h"
Marek Lindner32ae9b22011-04-20 15:40:58 +020025#include "hard-interface.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020026#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000027#include "hash.h"
28#include "originator.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020029#include "routing.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000030
Antonio Quartullia73105b2011-04-27 14:27:44 +020031#include <linux/crc16.h>
32
33static void _tt_global_del(struct bat_priv *bat_priv,
34 struct tt_global_entry *tt_global_entry,
35 const char *message);
36static void tt_purge(struct work_struct *work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000037
Marek Lindner7aadf882011-02-18 12:28:09 +000038/* returns 1 if they are the same mac addr */
Sven Eckelmann747e4222011-05-14 23:14:50 +020039static int compare_ltt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000040{
Sven Eckelmann747e4222011-05-14 23:14:50 +020041 const void *data1 = container_of(node, struct tt_local_entry,
42 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000043
44 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
45}
46
47/* returns 1 if they are the same mac addr */
Sven Eckelmann747e4222011-05-14 23:14:50 +020048static int compare_gtt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000049{
Sven Eckelmann747e4222011-05-14 23:14:50 +020050 const void *data1 = container_of(node, struct tt_global_entry,
51 hash_entry);
Marek Lindner7aadf882011-02-18 12:28:09 +000052
53 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
54}
55
Antonio Quartullia73105b2011-04-27 14:27:44 +020056static void tt_start_timer(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057{
Antonio Quartullia73105b2011-04-27 14:27:44 +020058 INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge);
59 queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work,
60 msecs_to_jiffies(5000));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061}
62
Antonio Quartulli2dafb492011-05-05 08:42:45 +020063static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +020064 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000065{
Antonio Quartulli2dafb492011-05-05 08:42:45 +020066 struct hashtable_t *hash = bat_priv->tt_local_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +000067 struct hlist_head *head;
68 struct hlist_node *node;
Antonio Quartulli2dafb492011-05-05 08:42:45 +020069 struct tt_local_entry *tt_local_entry, *tt_local_entry_tmp = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +000070 int index;
71
72 if (!hash)
73 return NULL;
74
75 index = choose_orig(data, hash->size);
76 head = &hash->table[index];
77
78 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +020079 hlist_for_each_entry_rcu(tt_local_entry, node, head, hash_entry) {
80 if (!compare_eth(tt_local_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +000081 continue;
82
Antonio Quartulli7683fdc2011-04-27 14:28:07 +020083 if (!atomic_inc_not_zero(&tt_local_entry->refcount))
84 continue;
85
Antonio Quartulli2dafb492011-05-05 08:42:45 +020086 tt_local_entry_tmp = tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +000087 break;
88 }
89 rcu_read_unlock();
90
Antonio Quartulli2dafb492011-05-05 08:42:45 +020091 return tt_local_entry_tmp;
Marek Lindner7aadf882011-02-18 12:28:09 +000092}
93
Antonio Quartulli2dafb492011-05-05 08:42:45 +020094static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +020095 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000096{
Antonio Quartulli2dafb492011-05-05 08:42:45 +020097 struct hashtable_t *hash = bat_priv->tt_global_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +000098 struct hlist_head *head;
99 struct hlist_node *node;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200100 struct tt_global_entry *tt_global_entry;
101 struct tt_global_entry *tt_global_entry_tmp = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000102 int index;
103
104 if (!hash)
105 return NULL;
106
107 index = choose_orig(data, hash->size);
108 head = &hash->table[index];
109
110 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200111 hlist_for_each_entry_rcu(tt_global_entry, node, head, hash_entry) {
112 if (!compare_eth(tt_global_entry, data))
Marek Lindner7aadf882011-02-18 12:28:09 +0000113 continue;
114
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200115 if (!atomic_inc_not_zero(&tt_global_entry->refcount))
116 continue;
117
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200118 tt_global_entry_tmp = tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000119 break;
120 }
121 rcu_read_unlock();
122
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200123 return tt_global_entry_tmp;
Marek Lindner7aadf882011-02-18 12:28:09 +0000124}
125
Antonio Quartullia73105b2011-04-27 14:27:44 +0200126static bool is_out_of_time(unsigned long starting_time, unsigned long timeout)
127{
128 unsigned long deadline;
129 deadline = starting_time + msecs_to_jiffies(timeout);
130
131 return time_after(jiffies, deadline);
132}
133
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200134static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
135{
136 if (atomic_dec_and_test(&tt_local_entry->refcount))
137 kfree_rcu(tt_local_entry, rcu);
138}
139
140static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
141{
142 if (atomic_dec_and_test(&tt_global_entry->refcount))
143 kfree_rcu(tt_global_entry, rcu);
144}
145
Antonio Quartulliff66c972011-06-30 01:14:00 +0200146static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
147 uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200148{
149 struct tt_change_node *tt_change_node;
150
151 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
152
153 if (!tt_change_node)
154 return;
155
Antonio Quartulliff66c972011-06-30 01:14:00 +0200156 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200157 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
158
159 spin_lock_bh(&bat_priv->tt_changes_list_lock);
160 /* track the change in the OGMinterval list */
161 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
162 atomic_inc(&bat_priv->tt_local_changes);
163 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
164
165 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
166}
167
168int tt_len(int changes_num)
169{
170 return changes_num * sizeof(struct tt_change);
171}
172
173static int tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000174{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200175 if (bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000176 return 1;
177
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200178 bat_priv->tt_local_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000179
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200180 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000181 return 0;
182
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000183 return 1;
184}
185
Sven Eckelmann747e4222011-05-14 23:14:50 +0200186void tt_local_add(struct net_device *soft_iface, const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000187{
188 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200189 struct tt_local_entry *tt_local_entry = NULL;
190 struct tt_global_entry *tt_global_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000191
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200192 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000193
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200194 if (tt_local_entry) {
195 tt_local_entry->last_seen = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200196 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197 }
198
Sven Eckelmann704509b2011-05-14 23:14:54 +0200199 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200200 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200201 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200202
Antonio Quartullia73105b2011-04-27 14:27:44 +0200203 bat_dbg(DBG_TT, bat_priv,
204 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
205 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000206
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200207 memcpy(tt_local_entry->addr, addr, ETH_ALEN);
208 tt_local_entry->last_seen = jiffies;
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200209 tt_local_entry->flags = NO_FLAGS;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200210 atomic_set(&tt_local_entry->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000211
212 /* the batman interface mac address should never be purged */
Marek Lindner39901e72011-02-18 12:28:08 +0000213 if (compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200214 tt_local_entry->flags |= TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000215
Antonio Quartulliff66c972011-06-30 01:14:00 +0200216 tt_local_event(bat_priv, addr, tt_local_entry->flags);
217
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200218 /* The local entry has to be marked as NEW to avoid to send it in
219 * a full table response going out before the next ttvn increment
220 * (consistency check) */
221 tt_local_entry->flags |= TT_CLIENT_NEW;
222
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200223 hash_add(bat_priv->tt_local_hash, compare_ltt, choose_orig,
224 tt_local_entry, &tt_local_entry->hash_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200225
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226 /* remove address from global hash if present */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200227 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000228
Antonio Quartullicc47f662011-04-27 14:27:57 +0200229 /* Check whether it is a roaming! */
230 if (tt_global_entry) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200231 /* This node is probably going to update its tt table */
232 tt_global_entry->orig_node->tt_poss_change = true;
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200233 /* The global entry has to be marked as PENDING and has to be
234 * kept for consistency purpose */
235 tt_global_entry->flags |= TT_CLIENT_PENDING;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200236 send_roam_adv(bat_priv, tt_global_entry->addr,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200237 tt_global_entry->orig_node);
238 }
239out:
240 if (tt_local_entry)
241 tt_local_entry_free_ref(tt_local_entry);
242 if (tt_global_entry)
243 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244}
245
Antonio Quartullia73105b2011-04-27 14:27:44 +0200246int tt_changes_fill_buffer(struct bat_priv *bat_priv,
247 unsigned char *buff, int buff_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000248{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200249 int count = 0, tot_changes = 0;
250 struct tt_change_node *entry, *safe;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251
Antonio Quartullia73105b2011-04-27 14:27:44 +0200252 if (buff_len > 0)
253 tot_changes = buff_len / tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254
Antonio Quartullia73105b2011-04-27 14:27:44 +0200255 spin_lock_bh(&bat_priv->tt_changes_list_lock);
256 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000257
Antonio Quartullia73105b2011-04-27 14:27:44 +0200258 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
259 list) {
260 if (count < tot_changes) {
261 memcpy(buff + tt_len(count),
262 &entry->change, sizeof(struct tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263 count++;
264 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200265 list_del(&entry->list);
266 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000267 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200268 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269
Antonio Quartullia73105b2011-04-27 14:27:44 +0200270 /* Keep the buffer for possible tt_request */
271 spin_lock_bh(&bat_priv->tt_buff_lock);
272 kfree(bat_priv->tt_buff);
273 bat_priv->tt_buff_len = 0;
274 bat_priv->tt_buff = NULL;
275 /* We check whether this new OGM has no changes due to size
276 * problems */
277 if (buff_len > 0) {
278 /**
279 * if kmalloc() fails we will reply with the full table
280 * instead of providing the diff
281 */
282 bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
283 if (bat_priv->tt_buff) {
284 memcpy(bat_priv->tt_buff, buff, buff_len);
285 bat_priv->tt_buff_len = buff_len;
286 }
287 }
288 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289
Antonio Quartullia73105b2011-04-27 14:27:44 +0200290 return tot_changes;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000291}
292
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200293int tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000294{
295 struct net_device *net_dev = (struct net_device *)seq->private;
296 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200297 struct hashtable_t *hash = bat_priv->tt_local_hash;
298 struct tt_local_entry *tt_local_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200299 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000300 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302 size_t buf_size, pos;
303 char *buff;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200304 int i, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305
Marek Lindner32ae9b22011-04-20 15:40:58 +0200306 primary_if = primary_if_get_selected(bat_priv);
307 if (!primary_if) {
308 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
309 "please specify interfaces to enable it\n",
310 net_dev->name);
311 goto out;
312 }
313
314 if (primary_if->if_status != IF_ACTIVE) {
315 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
316 "primary interface not active\n",
317 net_dev->name);
318 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319 }
320
321 seq_printf(seq, "Locally retrieved addresses (from %s) "
Antonio Quartullia73105b2011-04-27 14:27:44 +0200322 "announced via TT (TTVN: %u):\n",
323 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000325 buf_size = 1;
326 /* Estimate length for: " * xx:xx:xx:xx:xx:xx\n" */
327 for (i = 0; i < hash->size; i++) {
328 head = &hash->table[i];
329
Marek Lindner7aadf882011-02-18 12:28:09 +0000330 rcu_read_lock();
331 __hlist_for_each_rcu(node, head)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000332 buf_size += 21;
Marek Lindner7aadf882011-02-18 12:28:09 +0000333 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334 }
335
336 buff = kmalloc(buf_size, GFP_ATOMIC);
337 if (!buff) {
Marek Lindner32ae9b22011-04-20 15:40:58 +0200338 ret = -ENOMEM;
339 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000340 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000341
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000342 buff[0] = '\0';
343 pos = 0;
344
345 for (i = 0; i < hash->size; i++) {
346 head = &hash->table[i];
347
Marek Lindner7aadf882011-02-18 12:28:09 +0000348 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200349 hlist_for_each_entry_rcu(tt_local_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000350 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351 pos += snprintf(buff + pos, 22, " * %pM\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200352 tt_local_entry->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000353 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000354 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355 }
356
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357 seq_printf(seq, "%s", buff);
358 kfree(buff);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200359out:
360 if (primary_if)
361 hardif_free_ref(primary_if);
362 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363}
364
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200365static void tt_local_set_pending(struct bat_priv *bat_priv,
366 struct tt_local_entry *tt_local_entry,
367 uint16_t flags)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000368{
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200369 tt_local_event(bat_priv, tt_local_entry->addr,
370 tt_local_entry->flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000371
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200372 /* The local client has to be merked as "pending to be removed" but has
373 * to be kept in the table in order to send it in an full tables
374 * response issued before the net ttvn increment (consistency check) */
375 tt_local_entry->flags |= TT_CLIENT_PENDING;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000376}
377
Antonio Quartullia73105b2011-04-27 14:27:44 +0200378void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200379 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000380{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200381 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200383 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200384 if (!tt_local_entry)
385 goto out;
386
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200387 tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
388 (roaming ? TT_CLIENT_ROAM : NO_FLAGS));
389
390 bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) pending to be removed: "
391 "%s\n", tt_local_entry->addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200392out:
393 if (tt_local_entry)
394 tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395}
396
Antonio Quartullia73105b2011-04-27 14:27:44 +0200397static void tt_local_purge(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200399 struct hashtable_t *hash = bat_priv->tt_local_hash;
400 struct tt_local_entry *tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000401 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000402 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200403 spinlock_t *list_lock; /* protects write access to the hash lists */
Marek Lindner7aadf882011-02-18 12:28:09 +0000404 int i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000406 for (i = 0; i < hash->size; i++) {
407 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200408 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000409
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200410 spin_lock_bh(list_lock);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200411 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000412 head, hash_entry) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200413 if (tt_local_entry->flags & TT_CLIENT_NOPURGE)
Marek Lindner7aadf882011-02-18 12:28:09 +0000414 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200416 /* entry already marked for deletion */
417 if (tt_local_entry->flags & TT_CLIENT_PENDING)
418 continue;
419
Antonio Quartullia73105b2011-04-27 14:27:44 +0200420 if (!is_out_of_time(tt_local_entry->last_seen,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200421 TT_LOCAL_TIMEOUT * 1000))
Marek Lindner7aadf882011-02-18 12:28:09 +0000422 continue;
423
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200424 tt_local_set_pending(bat_priv, tt_local_entry,
425 TT_CLIENT_DEL);
426 bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) "
427 "pending to be removed: timed out\n",
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200428 tt_local_entry->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000429 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200430 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431 }
432
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433}
434
Antonio Quartullia73105b2011-04-27 14:27:44 +0200435static void tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000436{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200437 struct hashtable_t *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200438 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200439 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200440 struct hlist_node *node, *node_tmp;
441 struct hlist_head *head;
442 int i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200443
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200444 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445 return;
446
Antonio Quartullia73105b2011-04-27 14:27:44 +0200447 hash = bat_priv->tt_local_hash;
448
449 for (i = 0; i < hash->size; i++) {
450 head = &hash->table[i];
451 list_lock = &hash->list_locks[i];
452
453 spin_lock_bh(list_lock);
454 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
455 head, hash_entry) {
456 hlist_del_rcu(node);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200457 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200458 }
459 spin_unlock_bh(list_lock);
460 }
461
462 hash_destroy(hash);
463
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200464 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000465}
466
Antonio Quartullia73105b2011-04-27 14:27:44 +0200467static int tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000468{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200469 if (bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470 return 1;
471
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200472 bat_priv->tt_global_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200474 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000475 return 0;
476
477 return 1;
478}
479
Antonio Quartullia73105b2011-04-27 14:27:44 +0200480static void tt_changes_list_free(struct bat_priv *bat_priv)
481{
482 struct tt_change_node *entry, *safe;
483
484 spin_lock_bh(&bat_priv->tt_changes_list_lock);
485
486 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
487 list) {
488 list_del(&entry->list);
489 kfree(entry);
490 }
491
492 atomic_set(&bat_priv->tt_local_changes, 0);
493 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
494}
495
496/* caller must hold orig_node refcount */
497int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200498 const unsigned char *tt_addr, uint8_t ttvn, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000499{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200500 struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200501 struct orig_node *orig_node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200502 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000503
Antonio Quartullia73105b2011-04-27 14:27:44 +0200504 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000505
Antonio Quartullia73105b2011-04-27 14:27:44 +0200506 if (!tt_global_entry) {
507 tt_global_entry =
508 kmalloc(sizeof(*tt_global_entry),
509 GFP_ATOMIC);
510 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200511 goto out;
512
Antonio Quartullia73105b2011-04-27 14:27:44 +0200513 memcpy(tt_global_entry->addr, tt_addr, ETH_ALEN);
514 /* Assign the new orig_node */
515 atomic_inc(&orig_node->refcount);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200516 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200517 tt_global_entry->ttvn = ttvn;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200518 tt_global_entry->flags = NO_FLAGS;
519 tt_global_entry->roam_at = 0;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200520 atomic_set(&tt_global_entry->refcount, 2);
521
Antonio Quartullia73105b2011-04-27 14:27:44 +0200522 hash_add(bat_priv->tt_global_hash, compare_gtt,
523 choose_orig, tt_global_entry,
524 &tt_global_entry->hash_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200525 atomic_inc(&orig_node->tt_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200526 } else {
527 if (tt_global_entry->orig_node != orig_node) {
528 atomic_dec(&tt_global_entry->orig_node->tt_size);
529 orig_node_tmp = tt_global_entry->orig_node;
530 atomic_inc(&orig_node->refcount);
531 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200532 orig_node_free_ref(orig_node_tmp);
533 atomic_inc(&orig_node->tt_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000534 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200535 tt_global_entry->ttvn = ttvn;
536 tt_global_entry->flags = NO_FLAGS;
537 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000538 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200539
Antonio Quartullia73105b2011-04-27 14:27:44 +0200540 bat_dbg(DBG_TT, bat_priv,
541 "Creating new global tt entry: %pM (via %pM)\n",
542 tt_global_entry->addr, orig_node->orig);
543
544 /* remove address from local hash if present */
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200545 tt_local_remove(bat_priv, tt_global_entry->addr,
546 "global tt received", roaming);
547 ret = 1;
548out:
549 if (tt_global_entry)
550 tt_global_entry_free_ref(tt_global_entry);
551 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000552}
553
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200554int tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000555{
556 struct net_device *net_dev = (struct net_device *)seq->private;
557 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200558 struct hashtable_t *hash = bat_priv->tt_global_hash;
559 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200560 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000561 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000562 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000563 size_t buf_size, pos;
564 char *buff;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200565 int i, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000566
Marek Lindner32ae9b22011-04-20 15:40:58 +0200567 primary_if = primary_if_get_selected(bat_priv);
568 if (!primary_if) {
569 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
570 "specify interfaces to enable it\n",
571 net_dev->name);
572 goto out;
573 }
574
575 if (primary_if->if_status != IF_ACTIVE) {
576 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
577 "primary interface not active\n",
578 net_dev->name);
579 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000580 }
581
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200582 seq_printf(seq,
583 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000584 net_dev->name);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200585 seq_printf(seq, " %-13s %s %-15s %s\n",
586 "Client", "(TTVN)", "Originator", "(Curr TTVN)");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000587
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000588 buf_size = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200589 /* Estimate length for: " * xx:xx:xx:xx:xx:xx (ttvn) via
590 * xx:xx:xx:xx:xx:xx (cur_ttvn)\n"*/
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000591 for (i = 0; i < hash->size; i++) {
592 head = &hash->table[i];
593
Marek Lindner7aadf882011-02-18 12:28:09 +0000594 rcu_read_lock();
595 __hlist_for_each_rcu(node, head)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200596 buf_size += 59;
Marek Lindner7aadf882011-02-18 12:28:09 +0000597 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000598 }
599
600 buff = kmalloc(buf_size, GFP_ATOMIC);
601 if (!buff) {
Marek Lindner32ae9b22011-04-20 15:40:58 +0200602 ret = -ENOMEM;
603 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000604 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200605
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000606 buff[0] = '\0';
607 pos = 0;
608
609 for (i = 0; i < hash->size; i++) {
610 head = &hash->table[i];
611
Marek Lindner7aadf882011-02-18 12:28:09 +0000612 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200613 hlist_for_each_entry_rcu(tt_global_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000614 head, hash_entry) {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200615 pos += snprintf(buff + pos, 61,
616 " * %pM (%3u) via %pM (%3u)\n",
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200617 tt_global_entry->addr,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200618 tt_global_entry->ttvn,
619 tt_global_entry->orig_node->orig,
620 (uint8_t) atomic_read(
621 &tt_global_entry->orig_node->
622 last_ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000623 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000624 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000625 }
626
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000627 seq_printf(seq, "%s", buff);
628 kfree(buff);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200629out:
630 if (primary_if)
631 hardif_free_ref(primary_if);
632 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000633}
634
Antonio Quartullia73105b2011-04-27 14:27:44 +0200635static void _tt_global_del(struct bat_priv *bat_priv,
636 struct tt_global_entry *tt_global_entry,
637 const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000638{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200639 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200640 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200641
642 bat_dbg(DBG_TT, bat_priv,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200643 "Deleting global tt entry %pM (via %pM): %s\n",
644 tt_global_entry->addr, tt_global_entry->orig_node->orig,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000645 message);
646
Antonio Quartullia73105b2011-04-27 14:27:44 +0200647 atomic_dec(&tt_global_entry->orig_node->tt_size);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200648
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200649 hash_remove(bat_priv->tt_global_hash, compare_gtt, choose_orig,
650 tt_global_entry->addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200651out:
652 if (tt_global_entry)
653 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000654}
655
Antonio Quartullia73105b2011-04-27 14:27:44 +0200656void tt_global_del(struct bat_priv *bat_priv,
657 struct orig_node *orig_node, const unsigned char *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200658 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000659{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200660 struct tt_global_entry *tt_global_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000661
Antonio Quartullia73105b2011-04-27 14:27:44 +0200662 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200663 if (!tt_global_entry)
664 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200665
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200666 if (tt_global_entry->orig_node == orig_node) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200667 if (roaming) {
668 tt_global_entry->flags |= TT_CLIENT_ROAM;
669 tt_global_entry->roam_at = jiffies;
670 goto out;
671 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200672 _tt_global_del(bat_priv, tt_global_entry, message);
673 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200674out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200675 if (tt_global_entry)
676 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200677}
678
679void tt_global_del_orig(struct bat_priv *bat_priv,
680 struct orig_node *orig_node, const char *message)
681{
682 struct tt_global_entry *tt_global_entry;
683 int i;
684 struct hashtable_t *hash = bat_priv->tt_global_hash;
685 struct hlist_node *node, *safe;
686 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200687 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200688
Antonio Quartullia73105b2011-04-27 14:27:44 +0200689 for (i = 0; i < hash->size; i++) {
690 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200691 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000692
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200693 spin_lock_bh(list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200694 hlist_for_each_entry_safe(tt_global_entry, node, safe,
695 head, hash_entry) {
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200696 if (tt_global_entry->orig_node == orig_node) {
697 bat_dbg(DBG_TT, bat_priv,
698 "Deleting global tt entry %pM "
699 "(via %pM): originator time out\n",
700 tt_global_entry->addr,
701 tt_global_entry->orig_node->orig);
702 hlist_del_rcu(node);
703 tt_global_entry_free_ref(tt_global_entry);
704 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200705 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200706 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000707 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200708 atomic_set(&orig_node->tt_size, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000709}
710
Antonio Quartullicc47f662011-04-27 14:27:57 +0200711static void tt_global_roam_purge(struct bat_priv *bat_priv)
712{
713 struct hashtable_t *hash = bat_priv->tt_global_hash;
714 struct tt_global_entry *tt_global_entry;
715 struct hlist_node *node, *node_tmp;
716 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200717 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullicc47f662011-04-27 14:27:57 +0200718 int i;
719
Antonio Quartullicc47f662011-04-27 14:27:57 +0200720 for (i = 0; i < hash->size; i++) {
721 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200722 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +0200723
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200724 spin_lock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200725 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
726 head, hash_entry) {
727 if (!(tt_global_entry->flags & TT_CLIENT_ROAM))
728 continue;
729 if (!is_out_of_time(tt_global_entry->roam_at,
730 TT_CLIENT_ROAM_TIMEOUT * 1000))
731 continue;
732
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200733 bat_dbg(DBG_TT, bat_priv, "Deleting global "
734 "tt entry (%pM): Roaming timeout\n",
735 tt_global_entry->addr);
736 atomic_dec(&tt_global_entry->orig_node->tt_size);
737 hlist_del_rcu(node);
738 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200739 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200740 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200741 }
742
Antonio Quartullicc47f662011-04-27 14:27:57 +0200743}
744
Antonio Quartullia73105b2011-04-27 14:27:44 +0200745static void tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000746{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200747 struct hashtable_t *hash;
748 spinlock_t *list_lock; /* protects write access to the hash lists */
749 struct tt_global_entry *tt_global_entry;
750 struct hlist_node *node, *node_tmp;
751 struct hlist_head *head;
752 int i;
753
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200754 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000755 return;
756
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200757 hash = bat_priv->tt_global_hash;
758
759 for (i = 0; i < hash->size; i++) {
760 head = &hash->table[i];
761 list_lock = &hash->list_locks[i];
762
763 spin_lock_bh(list_lock);
764 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
765 head, hash_entry) {
766 hlist_del_rcu(node);
767 tt_global_entry_free_ref(tt_global_entry);
768 }
769 spin_unlock_bh(list_lock);
770 }
771
772 hash_destroy(hash);
773
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200774 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000775}
776
Sven Eckelmann747e4222011-05-14 23:14:50 +0200777struct orig_node *transtable_search(struct bat_priv *bat_priv,
778 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000779{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200780 struct tt_global_entry *tt_global_entry;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000781 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000782
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200783 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Marek Lindner7aadf882011-02-18 12:28:09 +0000784
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200785 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000786 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000787
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200788 if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200789 goto free_tt;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000790
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200791 /* A global client marked as PENDING has already moved from that
792 * originator */
793 if (tt_global_entry->flags & TT_CLIENT_PENDING)
794 goto free_tt;
795
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200796 orig_node = tt_global_entry->orig_node;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000797
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200798free_tt:
799 tt_global_entry_free_ref(tt_global_entry);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000800out:
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000801 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000802}
Antonio Quartullia73105b2011-04-27 14:27:44 +0200803
804/* Calculates the checksum of the local table of a given orig_node */
805uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
806{
807 uint16_t total = 0, total_one;
808 struct hashtable_t *hash = bat_priv->tt_global_hash;
809 struct tt_global_entry *tt_global_entry;
810 struct hlist_node *node;
811 struct hlist_head *head;
812 int i, j;
813
814 for (i = 0; i < hash->size; i++) {
815 head = &hash->table[i];
816
817 rcu_read_lock();
818 hlist_for_each_entry_rcu(tt_global_entry, node,
819 head, hash_entry) {
820 if (compare_eth(tt_global_entry->orig_node,
821 orig_node)) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200822 /* Roaming clients are in the global table for
823 * consistency only. They don't have to be
824 * taken into account while computing the
825 * global crc */
826 if (tt_global_entry->flags & TT_CLIENT_ROAM)
827 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200828 total_one = 0;
829 for (j = 0; j < ETH_ALEN; j++)
830 total_one = crc16_byte(total_one,
831 tt_global_entry->addr[j]);
832 total ^= total_one;
833 }
834 }
835 rcu_read_unlock();
836 }
837
838 return total;
839}
840
841/* Calculates the checksum of the local table */
842uint16_t tt_local_crc(struct bat_priv *bat_priv)
843{
844 uint16_t total = 0, total_one;
845 struct hashtable_t *hash = bat_priv->tt_local_hash;
846 struct tt_local_entry *tt_local_entry;
847 struct hlist_node *node;
848 struct hlist_head *head;
849 int i, j;
850
851 for (i = 0; i < hash->size; i++) {
852 head = &hash->table[i];
853
854 rcu_read_lock();
855 hlist_for_each_entry_rcu(tt_local_entry, node,
856 head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200857 /* not yet committed clients have not to be taken into
858 * account while computing the CRC */
859 if (tt_local_entry->flags & TT_CLIENT_NEW)
860 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200861 total_one = 0;
862 for (j = 0; j < ETH_ALEN; j++)
863 total_one = crc16_byte(total_one,
864 tt_local_entry->addr[j]);
865 total ^= total_one;
866 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200867 rcu_read_unlock();
868 }
869
870 return total;
871}
872
873static void tt_req_list_free(struct bat_priv *bat_priv)
874{
875 struct tt_req_node *node, *safe;
876
877 spin_lock_bh(&bat_priv->tt_req_list_lock);
878
879 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
880 list_del(&node->list);
881 kfree(node);
882 }
883
884 spin_unlock_bh(&bat_priv->tt_req_list_lock);
885}
886
887void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node,
888 const unsigned char *tt_buff, uint8_t tt_num_changes)
889{
890 uint16_t tt_buff_len = tt_len(tt_num_changes);
891
892 /* Replace the old buffer only if I received something in the
893 * last OGM (the OGM could carry no changes) */
894 spin_lock_bh(&orig_node->tt_buff_lock);
895 if (tt_buff_len > 0) {
896 kfree(orig_node->tt_buff);
897 orig_node->tt_buff_len = 0;
898 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
899 if (orig_node->tt_buff) {
900 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
901 orig_node->tt_buff_len = tt_buff_len;
902 }
903 }
904 spin_unlock_bh(&orig_node->tt_buff_lock);
905}
906
907static void tt_req_purge(struct bat_priv *bat_priv)
908{
909 struct tt_req_node *node, *safe;
910
911 spin_lock_bh(&bat_priv->tt_req_list_lock);
912 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
913 if (is_out_of_time(node->issued_at,
914 TT_REQUEST_TIMEOUT * 1000)) {
915 list_del(&node->list);
916 kfree(node);
917 }
918 }
919 spin_unlock_bh(&bat_priv->tt_req_list_lock);
920}
921
922/* returns the pointer to the new tt_req_node struct if no request
923 * has already been issued for this orig_node, NULL otherwise */
924static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
925 struct orig_node *orig_node)
926{
927 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
928
929 spin_lock_bh(&bat_priv->tt_req_list_lock);
930 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
931 if (compare_eth(tt_req_node_tmp, orig_node) &&
932 !is_out_of_time(tt_req_node_tmp->issued_at,
933 TT_REQUEST_TIMEOUT * 1000))
934 goto unlock;
935 }
936
937 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
938 if (!tt_req_node)
939 goto unlock;
940
941 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
942 tt_req_node->issued_at = jiffies;
943
944 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
945unlock:
946 spin_unlock_bh(&bat_priv->tt_req_list_lock);
947 return tt_req_node;
948}
949
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200950/* data_ptr is useless here, but has to be kept to respect the prototype */
951static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
952{
953 const struct tt_local_entry *tt_local_entry = entry_ptr;
954
955 if (tt_local_entry->flags & TT_CLIENT_NEW)
956 return 0;
957 return 1;
958}
959
Antonio Quartullia73105b2011-04-27 14:27:44 +0200960static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
961{
962 const struct tt_global_entry *tt_global_entry = entry_ptr;
963 const struct orig_node *orig_node = data_ptr;
964
Antonio Quartullicc47f662011-04-27 14:27:57 +0200965 if (tt_global_entry->flags & TT_CLIENT_ROAM)
966 return 0;
967
Antonio Quartullia73105b2011-04-27 14:27:44 +0200968 return (tt_global_entry->orig_node == orig_node);
969}
970
971static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
972 struct hashtable_t *hash,
973 struct hard_iface *primary_if,
974 int (*valid_cb)(const void *,
975 const void *),
976 void *cb_data)
977{
978 struct tt_local_entry *tt_local_entry;
979 struct tt_query_packet *tt_response;
980 struct tt_change *tt_change;
981 struct hlist_node *node;
982 struct hlist_head *head;
983 struct sk_buff *skb = NULL;
984 uint16_t tt_tot, tt_count;
985 ssize_t tt_query_size = sizeof(struct tt_query_packet);
986 int i;
987
988 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
989 tt_len = primary_if->soft_iface->mtu - tt_query_size;
990 tt_len -= tt_len % sizeof(struct tt_change);
991 }
992 tt_tot = tt_len / sizeof(struct tt_change);
993
994 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
995 if (!skb)
996 goto out;
997
998 skb_reserve(skb, ETH_HLEN);
999 tt_response = (struct tt_query_packet *)skb_put(skb,
1000 tt_query_size + tt_len);
1001 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001002
1003 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1004 tt_count = 0;
1005
1006 rcu_read_lock();
1007 for (i = 0; i < hash->size; i++) {
1008 head = &hash->table[i];
1009
1010 hlist_for_each_entry_rcu(tt_local_entry, node,
1011 head, hash_entry) {
1012 if (tt_count == tt_tot)
1013 break;
1014
1015 if ((valid_cb) && (!valid_cb(tt_local_entry, cb_data)))
1016 continue;
1017
1018 memcpy(tt_change->addr, tt_local_entry->addr, ETH_ALEN);
1019 tt_change->flags = NO_FLAGS;
1020
1021 tt_count++;
1022 tt_change++;
1023 }
1024 }
1025 rcu_read_unlock();
1026
Antonio Quartulli9d852392011-10-17 14:25:13 +02001027 /* store in the message the number of entries we have successfully
1028 * copied */
1029 tt_response->tt_data = htons(tt_count);
1030
Antonio Quartullia73105b2011-04-27 14:27:44 +02001031out:
1032 return skb;
1033}
1034
1035int send_tt_request(struct bat_priv *bat_priv, struct orig_node *dst_orig_node,
1036 uint8_t ttvn, uint16_t tt_crc, bool full_table)
1037{
1038 struct sk_buff *skb = NULL;
1039 struct tt_query_packet *tt_request;
1040 struct neigh_node *neigh_node = NULL;
1041 struct hard_iface *primary_if;
1042 struct tt_req_node *tt_req_node = NULL;
1043 int ret = 1;
1044
1045 primary_if = primary_if_get_selected(bat_priv);
1046 if (!primary_if)
1047 goto out;
1048
1049 /* The new tt_req will be issued only if I'm not waiting for a
1050 * reply from the same orig_node yet */
1051 tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1052 if (!tt_req_node)
1053 goto out;
1054
1055 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1056 if (!skb)
1057 goto out;
1058
1059 skb_reserve(skb, ETH_HLEN);
1060
1061 tt_request = (struct tt_query_packet *)skb_put(skb,
1062 sizeof(struct tt_query_packet));
1063
1064 tt_request->packet_type = BAT_TT_QUERY;
1065 tt_request->version = COMPAT_VERSION;
1066 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1067 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1068 tt_request->ttl = TTL;
1069 tt_request->ttvn = ttvn;
1070 tt_request->tt_data = tt_crc;
1071 tt_request->flags = TT_REQUEST;
1072
1073 if (full_table)
1074 tt_request->flags |= TT_FULL_TABLE;
1075
1076 neigh_node = orig_node_get_router(dst_orig_node);
1077 if (!neigh_node)
1078 goto out;
1079
1080 bat_dbg(DBG_TT, bat_priv, "Sending TT_REQUEST to %pM via %pM "
1081 "[%c]\n", dst_orig_node->orig, neigh_node->addr,
1082 (full_table ? 'F' : '.'));
1083
1084 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1085 ret = 0;
1086
1087out:
1088 if (neigh_node)
1089 neigh_node_free_ref(neigh_node);
1090 if (primary_if)
1091 hardif_free_ref(primary_if);
1092 if (ret)
1093 kfree_skb(skb);
1094 if (ret && tt_req_node) {
1095 spin_lock_bh(&bat_priv->tt_req_list_lock);
1096 list_del(&tt_req_node->list);
1097 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1098 kfree(tt_req_node);
1099 }
1100 return ret;
1101}
1102
1103static bool send_other_tt_response(struct bat_priv *bat_priv,
1104 struct tt_query_packet *tt_request)
1105{
1106 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1107 struct neigh_node *neigh_node = NULL;
1108 struct hard_iface *primary_if = NULL;
1109 uint8_t orig_ttvn, req_ttvn, ttvn;
1110 int ret = false;
1111 unsigned char *tt_buff;
1112 bool full_table;
1113 uint16_t tt_len, tt_tot;
1114 struct sk_buff *skb = NULL;
1115 struct tt_query_packet *tt_response;
1116
1117 bat_dbg(DBG_TT, bat_priv,
1118 "Received TT_REQUEST from %pM for "
1119 "ttvn: %u (%pM) [%c]\n", tt_request->src,
1120 tt_request->ttvn, tt_request->dst,
1121 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1122
1123 /* Let's get the orig node of the REAL destination */
1124 req_dst_orig_node = get_orig_node(bat_priv, tt_request->dst);
1125 if (!req_dst_orig_node)
1126 goto out;
1127
1128 res_dst_orig_node = get_orig_node(bat_priv, tt_request->src);
1129 if (!res_dst_orig_node)
1130 goto out;
1131
1132 neigh_node = orig_node_get_router(res_dst_orig_node);
1133 if (!neigh_node)
1134 goto out;
1135
1136 primary_if = primary_if_get_selected(bat_priv);
1137 if (!primary_if)
1138 goto out;
1139
1140 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1141 req_ttvn = tt_request->ttvn;
1142
1143 /* I have not the requested data */
1144 if (orig_ttvn != req_ttvn ||
1145 tt_request->tt_data != req_dst_orig_node->tt_crc)
1146 goto out;
1147
1148 /* If it has explicitly been requested the full table */
1149 if (tt_request->flags & TT_FULL_TABLE ||
1150 !req_dst_orig_node->tt_buff)
1151 full_table = true;
1152 else
1153 full_table = false;
1154
1155 /* In this version, fragmentation is not implemented, then
1156 * I'll send only one packet with as much TT entries as I can */
1157 if (!full_table) {
1158 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1159 tt_len = req_dst_orig_node->tt_buff_len;
1160 tt_tot = tt_len / sizeof(struct tt_change);
1161
1162 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1163 tt_len + ETH_HLEN);
1164 if (!skb)
1165 goto unlock;
1166
1167 skb_reserve(skb, ETH_HLEN);
1168 tt_response = (struct tt_query_packet *)skb_put(skb,
1169 sizeof(struct tt_query_packet) + tt_len);
1170 tt_response->ttvn = req_ttvn;
1171 tt_response->tt_data = htons(tt_tot);
1172
1173 tt_buff = skb->data + sizeof(struct tt_query_packet);
1174 /* Copy the last orig_node's OGM buffer */
1175 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1176 req_dst_orig_node->tt_buff_len);
1177
1178 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1179 } else {
1180 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1181 sizeof(struct tt_change);
1182 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1183
1184 skb = tt_response_fill_table(tt_len, ttvn,
1185 bat_priv->tt_global_hash,
1186 primary_if, tt_global_valid_entry,
1187 req_dst_orig_node);
1188 if (!skb)
1189 goto out;
1190
1191 tt_response = (struct tt_query_packet *)skb->data;
1192 }
1193
1194 tt_response->packet_type = BAT_TT_QUERY;
1195 tt_response->version = COMPAT_VERSION;
1196 tt_response->ttl = TTL;
1197 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1198 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1199 tt_response->flags = TT_RESPONSE;
1200
1201 if (full_table)
1202 tt_response->flags |= TT_FULL_TABLE;
1203
1204 bat_dbg(DBG_TT, bat_priv,
1205 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1206 res_dst_orig_node->orig, neigh_node->addr,
1207 req_dst_orig_node->orig, req_ttvn);
1208
1209 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1210 ret = true;
1211 goto out;
1212
1213unlock:
1214 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1215
1216out:
1217 if (res_dst_orig_node)
1218 orig_node_free_ref(res_dst_orig_node);
1219 if (req_dst_orig_node)
1220 orig_node_free_ref(req_dst_orig_node);
1221 if (neigh_node)
1222 neigh_node_free_ref(neigh_node);
1223 if (primary_if)
1224 hardif_free_ref(primary_if);
1225 if (!ret)
1226 kfree_skb(skb);
1227 return ret;
1228
1229}
1230static bool send_my_tt_response(struct bat_priv *bat_priv,
1231 struct tt_query_packet *tt_request)
1232{
1233 struct orig_node *orig_node = NULL;
1234 struct neigh_node *neigh_node = NULL;
1235 struct hard_iface *primary_if = NULL;
1236 uint8_t my_ttvn, req_ttvn, ttvn;
1237 int ret = false;
1238 unsigned char *tt_buff;
1239 bool full_table;
1240 uint16_t tt_len, tt_tot;
1241 struct sk_buff *skb = NULL;
1242 struct tt_query_packet *tt_response;
1243
1244 bat_dbg(DBG_TT, bat_priv,
1245 "Received TT_REQUEST from %pM for "
1246 "ttvn: %u (me) [%c]\n", tt_request->src,
1247 tt_request->ttvn,
1248 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1249
1250
1251 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1252 req_ttvn = tt_request->ttvn;
1253
1254 orig_node = get_orig_node(bat_priv, tt_request->src);
1255 if (!orig_node)
1256 goto out;
1257
1258 neigh_node = orig_node_get_router(orig_node);
1259 if (!neigh_node)
1260 goto out;
1261
1262 primary_if = primary_if_get_selected(bat_priv);
1263 if (!primary_if)
1264 goto out;
1265
1266 /* If the full table has been explicitly requested or the gap
1267 * is too big send the whole local translation table */
1268 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1269 !bat_priv->tt_buff)
1270 full_table = true;
1271 else
1272 full_table = false;
1273
1274 /* In this version, fragmentation is not implemented, then
1275 * I'll send only one packet with as much TT entries as I can */
1276 if (!full_table) {
1277 spin_lock_bh(&bat_priv->tt_buff_lock);
1278 tt_len = bat_priv->tt_buff_len;
1279 tt_tot = tt_len / sizeof(struct tt_change);
1280
1281 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1282 tt_len + ETH_HLEN);
1283 if (!skb)
1284 goto unlock;
1285
1286 skb_reserve(skb, ETH_HLEN);
1287 tt_response = (struct tt_query_packet *)skb_put(skb,
1288 sizeof(struct tt_query_packet) + tt_len);
1289 tt_response->ttvn = req_ttvn;
1290 tt_response->tt_data = htons(tt_tot);
1291
1292 tt_buff = skb->data + sizeof(struct tt_query_packet);
1293 memcpy(tt_buff, bat_priv->tt_buff,
1294 bat_priv->tt_buff_len);
1295 spin_unlock_bh(&bat_priv->tt_buff_lock);
1296 } else {
1297 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1298 sizeof(struct tt_change);
1299 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1300
1301 skb = tt_response_fill_table(tt_len, ttvn,
1302 bat_priv->tt_local_hash,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001303 primary_if, tt_local_valid_entry,
1304 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001305 if (!skb)
1306 goto out;
1307
1308 tt_response = (struct tt_query_packet *)skb->data;
1309 }
1310
1311 tt_response->packet_type = BAT_TT_QUERY;
1312 tt_response->version = COMPAT_VERSION;
1313 tt_response->ttl = TTL;
1314 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1315 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1316 tt_response->flags = TT_RESPONSE;
1317
1318 if (full_table)
1319 tt_response->flags |= TT_FULL_TABLE;
1320
1321 bat_dbg(DBG_TT, bat_priv,
1322 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1323 orig_node->orig, neigh_node->addr,
1324 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1325
1326 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1327 ret = true;
1328 goto out;
1329
1330unlock:
1331 spin_unlock_bh(&bat_priv->tt_buff_lock);
1332out:
1333 if (orig_node)
1334 orig_node_free_ref(orig_node);
1335 if (neigh_node)
1336 neigh_node_free_ref(neigh_node);
1337 if (primary_if)
1338 hardif_free_ref(primary_if);
1339 if (!ret)
1340 kfree_skb(skb);
1341 /* This packet was for me, so it doesn't need to be re-routed */
1342 return true;
1343}
1344
1345bool send_tt_response(struct bat_priv *bat_priv,
1346 struct tt_query_packet *tt_request)
1347{
1348 if (is_my_mac(tt_request->dst))
1349 return send_my_tt_response(bat_priv, tt_request);
1350 else
1351 return send_other_tt_response(bat_priv, tt_request);
1352}
1353
1354static void _tt_update_changes(struct bat_priv *bat_priv,
1355 struct orig_node *orig_node,
1356 struct tt_change *tt_change,
1357 uint16_t tt_num_changes, uint8_t ttvn)
1358{
1359 int i;
1360
1361 for (i = 0; i < tt_num_changes; i++) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +02001362 if ((tt_change + i)->flags & TT_CLIENT_DEL)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001363 tt_global_del(bat_priv, orig_node,
1364 (tt_change + i)->addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001365 "tt removed by changes",
1366 (tt_change + i)->flags & TT_CLIENT_ROAM);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001367 else
1368 if (!tt_global_add(bat_priv, orig_node,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001369 (tt_change + i)->addr, ttvn, false))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001370 /* In case of problem while storing a
1371 * global_entry, we stop the updating
1372 * procedure without committing the
1373 * ttvn change. This will avoid to send
1374 * corrupted data on tt_request
1375 */
1376 return;
1377 }
1378}
1379
1380static void tt_fill_gtable(struct bat_priv *bat_priv,
1381 struct tt_query_packet *tt_response)
1382{
1383 struct orig_node *orig_node = NULL;
1384
1385 orig_node = orig_hash_find(bat_priv, tt_response->src);
1386 if (!orig_node)
1387 goto out;
1388
1389 /* Purge the old table first.. */
1390 tt_global_del_orig(bat_priv, orig_node, "Received full table");
1391
1392 _tt_update_changes(bat_priv, orig_node,
1393 (struct tt_change *)(tt_response + 1),
1394 tt_response->tt_data, tt_response->ttvn);
1395
1396 spin_lock_bh(&orig_node->tt_buff_lock);
1397 kfree(orig_node->tt_buff);
1398 orig_node->tt_buff_len = 0;
1399 orig_node->tt_buff = NULL;
1400 spin_unlock_bh(&orig_node->tt_buff_lock);
1401
1402 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1403
1404out:
1405 if (orig_node)
1406 orig_node_free_ref(orig_node);
1407}
1408
1409void tt_update_changes(struct bat_priv *bat_priv, struct orig_node *orig_node,
1410 uint16_t tt_num_changes, uint8_t ttvn,
1411 struct tt_change *tt_change)
1412{
1413 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1414 ttvn);
1415
1416 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1417 tt_num_changes);
1418 atomic_set(&orig_node->last_ttvn, ttvn);
1419}
1420
1421bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1422{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001423 struct tt_local_entry *tt_local_entry = NULL;
1424 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001425
Antonio Quartullia73105b2011-04-27 14:27:44 +02001426 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001427 if (!tt_local_entry)
1428 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001429 /* Check if the client has been logically deleted (but is kept for
1430 * consistency purpose) */
1431 if (tt_local_entry->flags & TT_CLIENT_PENDING)
1432 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001433 ret = true;
1434out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001435 if (tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001436 tt_local_entry_free_ref(tt_local_entry);
1437 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001438}
1439
1440void handle_tt_response(struct bat_priv *bat_priv,
1441 struct tt_query_packet *tt_response)
1442{
1443 struct tt_req_node *node, *safe;
1444 struct orig_node *orig_node = NULL;
1445
1446 bat_dbg(DBG_TT, bat_priv, "Received TT_RESPONSE from %pM for "
1447 "ttvn %d t_size: %d [%c]\n",
1448 tt_response->src, tt_response->ttvn,
1449 tt_response->tt_data,
1450 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1451
1452 orig_node = orig_hash_find(bat_priv, tt_response->src);
1453 if (!orig_node)
1454 goto out;
1455
1456 if (tt_response->flags & TT_FULL_TABLE)
1457 tt_fill_gtable(bat_priv, tt_response);
1458 else
1459 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1460 tt_response->ttvn,
1461 (struct tt_change *)(tt_response + 1));
1462
1463 /* Delete the tt_req_node from pending tt_requests list */
1464 spin_lock_bh(&bat_priv->tt_req_list_lock);
1465 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1466 if (!compare_eth(node->addr, tt_response->src))
1467 continue;
1468 list_del(&node->list);
1469 kfree(node);
1470 }
1471 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1472
1473 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001474 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001475 /* Roaming phase is over: tables are in sync again. I can
1476 * unset the flag */
1477 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001478out:
1479 if (orig_node)
1480 orig_node_free_ref(orig_node);
1481}
1482
1483int tt_init(struct bat_priv *bat_priv)
1484{
1485 if (!tt_local_init(bat_priv))
1486 return 0;
1487
1488 if (!tt_global_init(bat_priv))
1489 return 0;
1490
1491 tt_start_timer(bat_priv);
1492
1493 return 1;
1494}
1495
Antonio Quartullicc47f662011-04-27 14:27:57 +02001496static void tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001497{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001498 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001499
Antonio Quartullicc47f662011-04-27 14:27:57 +02001500 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001501
Antonio Quartullicc47f662011-04-27 14:27:57 +02001502 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1503 list_del(&node->list);
1504 kfree(node);
1505 }
1506
1507 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1508}
1509
1510static void tt_roam_purge(struct bat_priv *bat_priv)
1511{
1512 struct tt_roam_node *node, *safe;
1513
1514 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1515 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1516 if (!is_out_of_time(node->first_time,
1517 ROAMING_MAX_TIME * 1000))
1518 continue;
1519
1520 list_del(&node->list);
1521 kfree(node);
1522 }
1523 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1524}
1525
1526/* This function checks whether the client already reached the
1527 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1528 * will not be sent.
1529 *
1530 * returns true if the ROAMING_ADV can be sent, false otherwise */
1531static bool tt_check_roam_count(struct bat_priv *bat_priv,
1532 uint8_t *client)
1533{
1534 struct tt_roam_node *tt_roam_node;
1535 bool ret = false;
1536
1537 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1538 /* The new tt_req will be issued only if I'm not waiting for a
1539 * reply from the same orig_node yet */
1540 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1541 if (!compare_eth(tt_roam_node->addr, client))
1542 continue;
1543
1544 if (is_out_of_time(tt_roam_node->first_time,
1545 ROAMING_MAX_TIME * 1000))
1546 continue;
1547
1548 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1549 /* Sorry, you roamed too many times! */
1550 goto unlock;
1551 ret = true;
1552 break;
1553 }
1554
1555 if (!ret) {
1556 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1557 if (!tt_roam_node)
1558 goto unlock;
1559
1560 tt_roam_node->first_time = jiffies;
1561 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1562 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1563
1564 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1565 ret = true;
1566 }
1567
1568unlock:
1569 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1570 return ret;
1571}
1572
1573void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1574 struct orig_node *orig_node)
1575{
1576 struct neigh_node *neigh_node = NULL;
1577 struct sk_buff *skb = NULL;
1578 struct roam_adv_packet *roam_adv_packet;
1579 int ret = 1;
1580 struct hard_iface *primary_if;
1581
1582 /* before going on we have to check whether the client has
1583 * already roamed to us too many times */
1584 if (!tt_check_roam_count(bat_priv, client))
1585 goto out;
1586
1587 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1588 if (!skb)
1589 goto out;
1590
1591 skb_reserve(skb, ETH_HLEN);
1592
1593 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1594 sizeof(struct roam_adv_packet));
1595
1596 roam_adv_packet->packet_type = BAT_ROAM_ADV;
1597 roam_adv_packet->version = COMPAT_VERSION;
1598 roam_adv_packet->ttl = TTL;
1599 primary_if = primary_if_get_selected(bat_priv);
1600 if (!primary_if)
1601 goto out;
1602 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1603 hardif_free_ref(primary_if);
1604 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1605 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1606
1607 neigh_node = orig_node_get_router(orig_node);
1608 if (!neigh_node)
1609 goto out;
1610
1611 bat_dbg(DBG_TT, bat_priv,
1612 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1613 orig_node->orig, client, neigh_node->addr);
1614
1615 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1616 ret = 0;
1617
1618out:
1619 if (neigh_node)
1620 neigh_node_free_ref(neigh_node);
1621 if (ret)
1622 kfree_skb(skb);
1623 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001624}
1625
1626static void tt_purge(struct work_struct *work)
1627{
1628 struct delayed_work *delayed_work =
1629 container_of(work, struct delayed_work, work);
1630 struct bat_priv *bat_priv =
1631 container_of(delayed_work, struct bat_priv, tt_work);
1632
1633 tt_local_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001634 tt_global_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001635 tt_req_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001636 tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001637
1638 tt_start_timer(bat_priv);
1639}
Antonio Quartullicc47f662011-04-27 14:27:57 +02001640
1641void tt_free(struct bat_priv *bat_priv)
1642{
1643 cancel_delayed_work_sync(&bat_priv->tt_work);
1644
1645 tt_local_table_free(bat_priv);
1646 tt_global_table_free(bat_priv);
1647 tt_req_list_free(bat_priv);
1648 tt_changes_list_free(bat_priv);
1649 tt_roam_list_free(bat_priv);
1650
1651 kfree(bat_priv->tt_buff);
1652}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001653
1654/* This function will reset the specified flags from all the entries in
1655 * the given hash table and will increment num_local_tt for each involved
1656 * entry */
1657static void tt_local_reset_flags(struct bat_priv *bat_priv, uint16_t flags)
1658{
1659 int i;
1660 struct hashtable_t *hash = bat_priv->tt_local_hash;
1661 struct hlist_head *head;
1662 struct hlist_node *node;
1663 struct tt_local_entry *tt_local_entry;
1664
1665 if (!hash)
1666 return;
1667
1668 for (i = 0; i < hash->size; i++) {
1669 head = &hash->table[i];
1670
1671 rcu_read_lock();
1672 hlist_for_each_entry_rcu(tt_local_entry, node,
1673 head, hash_entry) {
Antonio Quartulli31901262011-10-16 18:53:37 +02001674 if (!(tt_local_entry->flags & flags))
1675 continue;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001676 tt_local_entry->flags &= ~flags;
1677 atomic_inc(&bat_priv->num_local_tt);
1678 }
1679 rcu_read_unlock();
1680 }
1681
1682}
1683
1684/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
1685static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
1686{
1687 struct hashtable_t *hash = bat_priv->tt_local_hash;
1688 struct tt_local_entry *tt_local_entry;
1689 struct hlist_node *node, *node_tmp;
1690 struct hlist_head *head;
1691 spinlock_t *list_lock; /* protects write access to the hash lists */
1692 int i;
1693
1694 if (!hash)
1695 return;
1696
1697 for (i = 0; i < hash->size; i++) {
1698 head = &hash->table[i];
1699 list_lock = &hash->list_locks[i];
1700
1701 spin_lock_bh(list_lock);
1702 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
1703 head, hash_entry) {
1704 if (!(tt_local_entry->flags & TT_CLIENT_PENDING))
1705 continue;
1706
1707 bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry "
1708 "(%pM): pending\n", tt_local_entry->addr);
1709
1710 atomic_dec(&bat_priv->num_local_tt);
1711 hlist_del_rcu(node);
1712 tt_local_entry_free_ref(tt_local_entry);
1713 }
1714 spin_unlock_bh(list_lock);
1715 }
1716
1717}
1718
1719void tt_commit_changes(struct bat_priv *bat_priv)
1720{
1721 tt_local_reset_flags(bat_priv, TT_CLIENT_NEW);
1722 tt_local_purge_pending_clients(bat_priv);
1723
1724 /* Increment the TTVN only once per OGM interval */
1725 atomic_inc(&bat_priv->ttvn);
1726 bat_priv->tt_poss_change = false;
1727}