blob: 7ab9d72ce978d5c69102b88f0c7bb8408eb91846 [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
Simon Wunderlich531027f2011-10-19 11:02:25 +0200140static void tt_global_entry_free_rcu(struct rcu_head *rcu)
141{
142 struct tt_global_entry *tt_global_entry;
143
144 tt_global_entry = container_of(rcu, struct tt_global_entry, rcu);
145
146 if (tt_global_entry->orig_node)
147 orig_node_free_ref(tt_global_entry->orig_node);
148
149 kfree(tt_global_entry);
150}
151
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200152static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
153{
154 if (atomic_dec_and_test(&tt_global_entry->refcount))
Simon Wunderlich531027f2011-10-19 11:02:25 +0200155 call_rcu(&tt_global_entry->rcu, tt_global_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200156}
157
Antonio Quartulliff66c972011-06-30 01:14:00 +0200158static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
159 uint8_t flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200160{
161 struct tt_change_node *tt_change_node;
162
163 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
164
165 if (!tt_change_node)
166 return;
167
Antonio Quartulliff66c972011-06-30 01:14:00 +0200168 tt_change_node->change.flags = flags;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200169 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
170
171 spin_lock_bh(&bat_priv->tt_changes_list_lock);
172 /* track the change in the OGMinterval list */
173 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
174 atomic_inc(&bat_priv->tt_local_changes);
175 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
176
177 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
178}
179
180int tt_len(int changes_num)
181{
182 return changes_num * sizeof(struct tt_change);
183}
184
185static int tt_local_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000186{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200187 if (bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188 return 1;
189
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200190 bat_priv->tt_local_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000191
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200192 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000193 return 0;
194
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000195 return 1;
196}
197
Antonio Quartullibc279082011-07-07 15:35:35 +0200198void tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
199 int ifindex)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000200{
201 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200202 struct tt_local_entry *tt_local_entry = NULL;
203 struct tt_global_entry *tt_global_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000204
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200205 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000206
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200207 if (tt_local_entry) {
208 tt_local_entry->last_seen = jiffies;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200209 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000210 }
211
Sven Eckelmann704509b2011-05-14 23:14:54 +0200212 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200213 if (!tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200214 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200215
Antonio Quartullia73105b2011-04-27 14:27:44 +0200216 bat_dbg(DBG_TT, bat_priv,
217 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
218 (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000219
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200220 memcpy(tt_local_entry->addr, addr, ETH_ALEN);
221 tt_local_entry->last_seen = jiffies;
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200222 tt_local_entry->flags = NO_FLAGS;
Antonio Quartullibc279082011-07-07 15:35:35 +0200223 if (is_wifi_iface(ifindex))
224 tt_local_entry->flags |= TT_CLIENT_WIFI;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200225 atomic_set(&tt_local_entry->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226
227 /* the batman interface mac address should never be purged */
Marek Lindner39901e72011-02-18 12:28:08 +0000228 if (compare_eth(addr, soft_iface->dev_addr))
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200229 tt_local_entry->flags |= TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230
Antonio Quartulliff66c972011-06-30 01:14:00 +0200231 tt_local_event(bat_priv, addr, tt_local_entry->flags);
232
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200233 /* The local entry has to be marked as NEW to avoid to send it in
234 * a full table response going out before the next ttvn increment
235 * (consistency check) */
236 tt_local_entry->flags |= TT_CLIENT_NEW;
237
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200238 hash_add(bat_priv->tt_local_hash, compare_ltt, choose_orig,
239 tt_local_entry, &tt_local_entry->hash_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200240
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000241 /* remove address from global hash if present */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200242 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243
Antonio Quartullicc47f662011-04-27 14:27:57 +0200244 /* Check whether it is a roaming! */
245 if (tt_global_entry) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200246 /* This node is probably going to update its tt table */
247 tt_global_entry->orig_node->tt_poss_change = true;
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200248 /* The global entry has to be marked as PENDING and has to be
249 * kept for consistency purpose */
250 tt_global_entry->flags |= TT_CLIENT_PENDING;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200251 send_roam_adv(bat_priv, tt_global_entry->addr,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200252 tt_global_entry->orig_node);
253 }
254out:
255 if (tt_local_entry)
256 tt_local_entry_free_ref(tt_local_entry);
257 if (tt_global_entry)
258 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259}
260
Antonio Quartullia73105b2011-04-27 14:27:44 +0200261int tt_changes_fill_buffer(struct bat_priv *bat_priv,
262 unsigned char *buff, int buff_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200264 int count = 0, tot_changes = 0;
265 struct tt_change_node *entry, *safe;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266
Antonio Quartullia73105b2011-04-27 14:27:44 +0200267 if (buff_len > 0)
268 tot_changes = buff_len / tt_len(1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269
Antonio Quartullia73105b2011-04-27 14:27:44 +0200270 spin_lock_bh(&bat_priv->tt_changes_list_lock);
271 atomic_set(&bat_priv->tt_local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272
Antonio Quartullia73105b2011-04-27 14:27:44 +0200273 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
274 list) {
275 if (count < tot_changes) {
276 memcpy(buff + tt_len(count),
277 &entry->change, sizeof(struct tt_change));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000278 count++;
279 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200280 list_del(&entry->list);
281 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000282 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200283 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000284
Antonio Quartullia73105b2011-04-27 14:27:44 +0200285 /* Keep the buffer for possible tt_request */
286 spin_lock_bh(&bat_priv->tt_buff_lock);
287 kfree(bat_priv->tt_buff);
288 bat_priv->tt_buff_len = 0;
289 bat_priv->tt_buff = NULL;
290 /* We check whether this new OGM has no changes due to size
291 * problems */
292 if (buff_len > 0) {
293 /**
294 * if kmalloc() fails we will reply with the full table
295 * instead of providing the diff
296 */
297 bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
298 if (bat_priv->tt_buff) {
299 memcpy(bat_priv->tt_buff, buff, buff_len);
300 bat_priv->tt_buff_len = buff_len;
301 }
302 }
303 spin_unlock_bh(&bat_priv->tt_buff_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000304
Antonio Quartullia73105b2011-04-27 14:27:44 +0200305 return tot_changes;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000306}
307
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200308int tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000309{
310 struct net_device *net_dev = (struct net_device *)seq->private;
311 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200312 struct hashtable_t *hash = bat_priv->tt_local_hash;
313 struct tt_local_entry *tt_local_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200314 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000315 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000316 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000317 size_t buf_size, pos;
318 char *buff;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200319 int i, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000320
Marek Lindner32ae9b22011-04-20 15:40:58 +0200321 primary_if = primary_if_get_selected(bat_priv);
322 if (!primary_if) {
323 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
324 "please specify interfaces to enable it\n",
325 net_dev->name);
326 goto out;
327 }
328
329 if (primary_if->if_status != IF_ACTIVE) {
330 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
331 "primary interface not active\n",
332 net_dev->name);
333 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334 }
335
336 seq_printf(seq, "Locally retrieved addresses (from %s) "
Antonio Quartullia73105b2011-04-27 14:27:44 +0200337 "announced via TT (TTVN: %u):\n",
338 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000339
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000340 buf_size = 1;
341 /* Estimate length for: " * xx:xx:xx:xx:xx:xx\n" */
342 for (i = 0; i < hash->size; i++) {
343 head = &hash->table[i];
344
Marek Lindner7aadf882011-02-18 12:28:09 +0000345 rcu_read_lock();
346 __hlist_for_each_rcu(node, head)
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200347 buf_size += 29;
Marek Lindner7aadf882011-02-18 12:28:09 +0000348 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000349 }
350
351 buff = kmalloc(buf_size, GFP_ATOMIC);
352 if (!buff) {
Marek Lindner32ae9b22011-04-20 15:40:58 +0200353 ret = -ENOMEM;
354 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000356
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357 buff[0] = '\0';
358 pos = 0;
359
360 for (i = 0; i < hash->size; i++) {
361 head = &hash->table[i];
362
Marek Lindner7aadf882011-02-18 12:28:09 +0000363 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200364 hlist_for_each_entry_rcu(tt_local_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000365 head, hash_entry) {
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200366 pos += snprintf(buff + pos, 30, " * %pM "
367 "[%c%c%c%c%c]\n",
368 tt_local_entry->addr,
369 (tt_local_entry->flags &
370 TT_CLIENT_ROAM ? 'R' : '.'),
371 (tt_local_entry->flags &
372 TT_CLIENT_NOPURGE ? 'P' : '.'),
373 (tt_local_entry->flags &
374 TT_CLIENT_NEW ? 'N' : '.'),
375 (tt_local_entry->flags &
376 TT_CLIENT_PENDING ? 'X' : '.'),
377 (tt_local_entry->flags &
378 TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000380 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381 }
382
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383 seq_printf(seq, "%s", buff);
384 kfree(buff);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200385out:
386 if (primary_if)
387 hardif_free_ref(primary_if);
388 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389}
390
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200391static void tt_local_set_pending(struct bat_priv *bat_priv,
392 struct tt_local_entry *tt_local_entry,
393 uint16_t flags)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000394{
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200395 tt_local_event(bat_priv, tt_local_entry->addr,
396 tt_local_entry->flags | flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397
Antonio Quartulli015758d2011-07-09 17:52:13 +0200398 /* The local client has to be marked as "pending to be removed" but has
399 * to be kept in the table in order to send it in a full table
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200400 * response issued before the net ttvn increment (consistency check) */
401 tt_local_entry->flags |= TT_CLIENT_PENDING;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000402}
403
Antonio Quartullia73105b2011-04-27 14:27:44 +0200404void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200405 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000406{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200407 struct tt_local_entry *tt_local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000408
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200409 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200410 if (!tt_local_entry)
411 goto out;
412
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200413 tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
414 (roaming ? TT_CLIENT_ROAM : NO_FLAGS));
415
416 bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) pending to be removed: "
417 "%s\n", tt_local_entry->addr, message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200418out:
419 if (tt_local_entry)
420 tt_local_entry_free_ref(tt_local_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421}
422
Antonio Quartullia73105b2011-04-27 14:27:44 +0200423static void tt_local_purge(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200425 struct hashtable_t *hash = bat_priv->tt_local_hash;
426 struct tt_local_entry *tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000427 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200429 spinlock_t *list_lock; /* protects write access to the hash lists */
Marek Lindner7aadf882011-02-18 12:28:09 +0000430 int i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000432 for (i = 0; i < hash->size; i++) {
433 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200434 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200436 spin_lock_bh(list_lock);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200437 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000438 head, hash_entry) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +0200439 if (tt_local_entry->flags & TT_CLIENT_NOPURGE)
Marek Lindner7aadf882011-02-18 12:28:09 +0000440 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200442 /* entry already marked for deletion */
443 if (tt_local_entry->flags & TT_CLIENT_PENDING)
444 continue;
445
Antonio Quartullia73105b2011-04-27 14:27:44 +0200446 if (!is_out_of_time(tt_local_entry->last_seen,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200447 TT_LOCAL_TIMEOUT * 1000))
Marek Lindner7aadf882011-02-18 12:28:09 +0000448 continue;
449
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200450 tt_local_set_pending(bat_priv, tt_local_entry,
451 TT_CLIENT_DEL);
452 bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) "
453 "pending to be removed: timed out\n",
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200454 tt_local_entry->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000455 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200456 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000457 }
458
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000459}
460
Antonio Quartullia73105b2011-04-27 14:27:44 +0200461static void tt_local_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000462{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200463 struct hashtable_t *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200464 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200465 struct tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200466 struct hlist_node *node, *node_tmp;
467 struct hlist_head *head;
468 int i;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200469
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200470 if (!bat_priv->tt_local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471 return;
472
Antonio Quartullia73105b2011-04-27 14:27:44 +0200473 hash = bat_priv->tt_local_hash;
474
475 for (i = 0; i < hash->size; i++) {
476 head = &hash->table[i];
477 list_lock = &hash->list_locks[i];
478
479 spin_lock_bh(list_lock);
480 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
481 head, hash_entry) {
482 hlist_del_rcu(node);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200483 tt_local_entry_free_ref(tt_local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200484 }
485 spin_unlock_bh(list_lock);
486 }
487
488 hash_destroy(hash);
489
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200490 bat_priv->tt_local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000491}
492
Antonio Quartullia73105b2011-04-27 14:27:44 +0200493static int tt_global_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000494{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200495 if (bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496 return 1;
497
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200498 bat_priv->tt_global_hash = hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000499
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200500 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000501 return 0;
502
503 return 1;
504}
505
Antonio Quartullia73105b2011-04-27 14:27:44 +0200506static void tt_changes_list_free(struct bat_priv *bat_priv)
507{
508 struct tt_change_node *entry, *safe;
509
510 spin_lock_bh(&bat_priv->tt_changes_list_lock);
511
512 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
513 list) {
514 list_del(&entry->list);
515 kfree(entry);
516 }
517
518 atomic_set(&bat_priv->tt_local_changes, 0);
519 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
520}
521
522/* caller must hold orig_node refcount */
523int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +0200524 const unsigned char *tt_addr, uint8_t ttvn, bool roaming,
525 bool wifi)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000526{
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200527 struct tt_global_entry *tt_global_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200528 struct orig_node *orig_node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200529 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000530
Antonio Quartullia73105b2011-04-27 14:27:44 +0200531 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000532
Antonio Quartullia73105b2011-04-27 14:27:44 +0200533 if (!tt_global_entry) {
534 tt_global_entry =
535 kmalloc(sizeof(*tt_global_entry),
536 GFP_ATOMIC);
537 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200538 goto out;
539
Antonio Quartullia73105b2011-04-27 14:27:44 +0200540 memcpy(tt_global_entry->addr, tt_addr, ETH_ALEN);
541 /* Assign the new orig_node */
542 atomic_inc(&orig_node->refcount);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200543 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200544 tt_global_entry->ttvn = ttvn;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200545 tt_global_entry->flags = NO_FLAGS;
546 tt_global_entry->roam_at = 0;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200547 atomic_set(&tt_global_entry->refcount, 2);
548
Antonio Quartullia73105b2011-04-27 14:27:44 +0200549 hash_add(bat_priv->tt_global_hash, compare_gtt,
550 choose_orig, tt_global_entry,
551 &tt_global_entry->hash_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200552 atomic_inc(&orig_node->tt_size);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200553 } else {
554 if (tt_global_entry->orig_node != orig_node) {
555 atomic_dec(&tt_global_entry->orig_node->tt_size);
556 orig_node_tmp = tt_global_entry->orig_node;
557 atomic_inc(&orig_node->refcount);
558 tt_global_entry->orig_node = orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200559 orig_node_free_ref(orig_node_tmp);
560 atomic_inc(&orig_node->tt_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200562 tt_global_entry->ttvn = ttvn;
563 tt_global_entry->flags = NO_FLAGS;
564 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000565 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200566
Antonio Quartullibc279082011-07-07 15:35:35 +0200567 if (wifi)
568 tt_global_entry->flags |= TT_CLIENT_WIFI;
569
Antonio Quartullia73105b2011-04-27 14:27:44 +0200570 bat_dbg(DBG_TT, bat_priv,
571 "Creating new global tt entry: %pM (via %pM)\n",
572 tt_global_entry->addr, orig_node->orig);
573
574 /* remove address from local hash if present */
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200575 tt_local_remove(bat_priv, tt_global_entry->addr,
576 "global tt received", roaming);
577 ret = 1;
578out:
579 if (tt_global_entry)
580 tt_global_entry_free_ref(tt_global_entry);
581 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000582}
583
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200584int tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000585{
586 struct net_device *net_dev = (struct net_device *)seq->private;
587 struct bat_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200588 struct hashtable_t *hash = bat_priv->tt_global_hash;
589 struct tt_global_entry *tt_global_entry;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200590 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000591 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000593 size_t buf_size, pos;
594 char *buff;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200595 int i, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000596
Marek Lindner32ae9b22011-04-20 15:40:58 +0200597 primary_if = primary_if_get_selected(bat_priv);
598 if (!primary_if) {
599 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
600 "specify interfaces to enable it\n",
601 net_dev->name);
602 goto out;
603 }
604
605 if (primary_if->if_status != IF_ACTIVE) {
606 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
607 "primary interface not active\n",
608 net_dev->name);
609 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000610 }
611
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200612 seq_printf(seq,
613 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000614 net_dev->name);
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200615 seq_printf(seq, " %-13s %s %-15s %s %s\n",
616 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000617
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000618 buf_size = 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200619 /* Estimate length for: " * xx:xx:xx:xx:xx:xx (ttvn) via
620 * xx:xx:xx:xx:xx:xx (cur_ttvn)\n"*/
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000621 for (i = 0; i < hash->size; i++) {
622 head = &hash->table[i];
623
Marek Lindner7aadf882011-02-18 12:28:09 +0000624 rcu_read_lock();
625 __hlist_for_each_rcu(node, head)
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200626 buf_size += 67;
Marek Lindner7aadf882011-02-18 12:28:09 +0000627 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000628 }
629
630 buff = kmalloc(buf_size, GFP_ATOMIC);
631 if (!buff) {
Marek Lindner32ae9b22011-04-20 15:40:58 +0200632 ret = -ENOMEM;
633 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000634 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200635
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000636 buff[0] = '\0';
637 pos = 0;
638
639 for (i = 0; i < hash->size; i++) {
640 head = &hash->table[i];
641
Marek Lindner7aadf882011-02-18 12:28:09 +0000642 rcu_read_lock();
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200643 hlist_for_each_entry_rcu(tt_global_entry, node,
Marek Lindner7aadf882011-02-18 12:28:09 +0000644 head, hash_entry) {
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200645 pos += snprintf(buff + pos, 69,
646 " * %pM (%3u) via %pM (%3u) "
647 "[%c%c%c]\n", tt_global_entry->addr,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200648 tt_global_entry->ttvn,
649 tt_global_entry->orig_node->orig,
650 (uint8_t) atomic_read(
651 &tt_global_entry->orig_node->
Antonio Quartullidf6edb92011-07-07 15:35:38 +0200652 last_ttvn),
653 (tt_global_entry->flags &
654 TT_CLIENT_ROAM ? 'R' : '.'),
655 (tt_global_entry->flags &
656 TT_CLIENT_PENDING ? 'X' : '.'),
657 (tt_global_entry->flags &
658 TT_CLIENT_WIFI ? 'W' : '.'));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000659 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000660 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000661 }
662
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000663 seq_printf(seq, "%s", buff);
664 kfree(buff);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200665out:
666 if (primary_if)
667 hardif_free_ref(primary_if);
668 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000669}
670
Antonio Quartullia73105b2011-04-27 14:27:44 +0200671static void _tt_global_del(struct bat_priv *bat_priv,
672 struct tt_global_entry *tt_global_entry,
673 const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000674{
Antonio Quartullia73105b2011-04-27 14:27:44 +0200675 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200676 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200677
678 bat_dbg(DBG_TT, bat_priv,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200679 "Deleting global tt entry %pM (via %pM): %s\n",
680 tt_global_entry->addr, tt_global_entry->orig_node->orig,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000681 message);
682
Antonio Quartullia73105b2011-04-27 14:27:44 +0200683 atomic_dec(&tt_global_entry->orig_node->tt_size);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200684
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200685 hash_remove(bat_priv->tt_global_hash, compare_gtt, choose_orig,
686 tt_global_entry->addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200687out:
688 if (tt_global_entry)
689 tt_global_entry_free_ref(tt_global_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000690}
691
Antonio Quartullia73105b2011-04-27 14:27:44 +0200692void tt_global_del(struct bat_priv *bat_priv,
693 struct orig_node *orig_node, const unsigned char *addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200694 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000695{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200696 struct tt_global_entry *tt_global_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000697
Antonio Quartullia73105b2011-04-27 14:27:44 +0200698 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200699 if (!tt_global_entry)
700 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200701
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200702 if (tt_global_entry->orig_node == orig_node) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200703 if (roaming) {
704 tt_global_entry->flags |= TT_CLIENT_ROAM;
705 tt_global_entry->roam_at = jiffies;
706 goto out;
707 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200708 _tt_global_del(bat_priv, tt_global_entry, message);
709 }
Antonio Quartullicc47f662011-04-27 14:27:57 +0200710out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200711 if (tt_global_entry)
712 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200713}
714
715void tt_global_del_orig(struct bat_priv *bat_priv,
716 struct orig_node *orig_node, const char *message)
717{
718 struct tt_global_entry *tt_global_entry;
719 int i;
720 struct hashtable_t *hash = bat_priv->tt_global_hash;
721 struct hlist_node *node, *safe;
722 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200723 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200724
Simon Wunderlich6e801492011-10-19 10:28:26 +0200725 if (!hash)
726 return;
727
Antonio Quartullia73105b2011-04-27 14:27:44 +0200728 for (i = 0; i < hash->size; i++) {
729 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200730 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000731
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200732 spin_lock_bh(list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200733 hlist_for_each_entry_safe(tt_global_entry, node, safe,
734 head, hash_entry) {
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200735 if (tt_global_entry->orig_node == orig_node) {
736 bat_dbg(DBG_TT, bat_priv,
737 "Deleting global tt entry %pM "
Antonio Quartulli87944972011-09-19 12:29:19 +0200738 "(via %pM): %s\n",
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200739 tt_global_entry->addr,
Antonio Quartulli87944972011-09-19 12:29:19 +0200740 tt_global_entry->orig_node->orig,
741 message);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200742 hlist_del_rcu(node);
743 tt_global_entry_free_ref(tt_global_entry);
744 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200745 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200746 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000747 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200748 atomic_set(&orig_node->tt_size, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000749}
750
Antonio Quartullicc47f662011-04-27 14:27:57 +0200751static void tt_global_roam_purge(struct bat_priv *bat_priv)
752{
753 struct hashtable_t *hash = bat_priv->tt_global_hash;
754 struct tt_global_entry *tt_global_entry;
755 struct hlist_node *node, *node_tmp;
756 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200757 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartullicc47f662011-04-27 14:27:57 +0200758 int i;
759
Antonio Quartullicc47f662011-04-27 14:27:57 +0200760 for (i = 0; i < hash->size; i++) {
761 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200762 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +0200763
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200764 spin_lock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200765 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
766 head, hash_entry) {
767 if (!(tt_global_entry->flags & TT_CLIENT_ROAM))
768 continue;
769 if (!is_out_of_time(tt_global_entry->roam_at,
770 TT_CLIENT_ROAM_TIMEOUT * 1000))
771 continue;
772
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200773 bat_dbg(DBG_TT, bat_priv, "Deleting global "
774 "tt entry (%pM): Roaming timeout\n",
775 tt_global_entry->addr);
776 atomic_dec(&tt_global_entry->orig_node->tt_size);
777 hlist_del_rcu(node);
778 tt_global_entry_free_ref(tt_global_entry);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200779 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200780 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200781 }
782
Antonio Quartullicc47f662011-04-27 14:27:57 +0200783}
784
Antonio Quartullia73105b2011-04-27 14:27:44 +0200785static void tt_global_table_free(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000786{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200787 struct hashtable_t *hash;
788 spinlock_t *list_lock; /* protects write access to the hash lists */
789 struct tt_global_entry *tt_global_entry;
790 struct hlist_node *node, *node_tmp;
791 struct hlist_head *head;
792 int i;
793
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200794 if (!bat_priv->tt_global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000795 return;
796
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200797 hash = bat_priv->tt_global_hash;
798
799 for (i = 0; i < hash->size; i++) {
800 head = &hash->table[i];
801 list_lock = &hash->list_locks[i];
802
803 spin_lock_bh(list_lock);
804 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
805 head, hash_entry) {
806 hlist_del_rcu(node);
807 tt_global_entry_free_ref(tt_global_entry);
808 }
809 spin_unlock_bh(list_lock);
810 }
811
812 hash_destroy(hash);
813
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200814 bat_priv->tt_global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000815}
816
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200817static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry,
818 struct tt_global_entry *tt_global_entry)
819{
820 bool ret = false;
821
822 if (tt_local_entry->flags & TT_CLIENT_WIFI &&
823 tt_global_entry->flags & TT_CLIENT_WIFI)
824 ret = true;
825
826 return ret;
827}
828
Sven Eckelmann747e4222011-05-14 23:14:50 +0200829struct orig_node *transtable_search(struct bat_priv *bat_priv,
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200830 const uint8_t *src, const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000831{
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200832 struct tt_local_entry *tt_local_entry = NULL;
833 struct tt_global_entry *tt_global_entry = NULL;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000834 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000835
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200836 if (src && atomic_read(&bat_priv->ap_isolation)) {
837 tt_local_entry = tt_local_hash_find(bat_priv, src);
838 if (!tt_local_entry)
839 goto out;
840 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000841
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200842 tt_global_entry = tt_global_hash_find(bat_priv, addr);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200843 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000844 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000845
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200846 /* check whether the clients should not communicate due to AP
847 * isolation */
848 if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry))
849 goto out;
850
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200851 if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200852 goto out;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000853
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200854 /* A global client marked as PENDING has already moved from that
855 * originator */
856 if (tt_global_entry->flags & TT_CLIENT_PENDING)
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200857 goto out;
Antonio Quartulli980d55b2011-07-07 01:40:59 +0200858
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200859 orig_node = tt_global_entry->orig_node;
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000860
861out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +0200862 if (tt_global_entry)
863 tt_global_entry_free_ref(tt_global_entry);
864 if (tt_local_entry)
865 tt_local_entry_free_ref(tt_local_entry);
866
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000867 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000868}
Antonio Quartullia73105b2011-04-27 14:27:44 +0200869
870/* Calculates the checksum of the local table of a given orig_node */
871uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
872{
873 uint16_t total = 0, total_one;
874 struct hashtable_t *hash = bat_priv->tt_global_hash;
875 struct tt_global_entry *tt_global_entry;
876 struct hlist_node *node;
877 struct hlist_head *head;
878 int i, j;
879
880 for (i = 0; i < hash->size; i++) {
881 head = &hash->table[i];
882
883 rcu_read_lock();
884 hlist_for_each_entry_rcu(tt_global_entry, node,
885 head, hash_entry) {
886 if (compare_eth(tt_global_entry->orig_node,
887 orig_node)) {
Antonio Quartullicc47f662011-04-27 14:27:57 +0200888 /* Roaming clients are in the global table for
889 * consistency only. They don't have to be
890 * taken into account while computing the
891 * global crc */
892 if (tt_global_entry->flags & TT_CLIENT_ROAM)
893 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200894 total_one = 0;
895 for (j = 0; j < ETH_ALEN; j++)
896 total_one = crc16_byte(total_one,
897 tt_global_entry->addr[j]);
898 total ^= total_one;
899 }
900 }
901 rcu_read_unlock();
902 }
903
904 return total;
905}
906
907/* Calculates the checksum of the local table */
908uint16_t tt_local_crc(struct bat_priv *bat_priv)
909{
910 uint16_t total = 0, total_one;
911 struct hashtable_t *hash = bat_priv->tt_local_hash;
912 struct tt_local_entry *tt_local_entry;
913 struct hlist_node *node;
914 struct hlist_head *head;
915 int i, j;
916
917 for (i = 0; i < hash->size; i++) {
918 head = &hash->table[i];
919
920 rcu_read_lock();
921 hlist_for_each_entry_rcu(tt_local_entry, node,
922 head, hash_entry) {
Antonio Quartulli058d0e22011-07-07 01:40:58 +0200923 /* not yet committed clients have not to be taken into
924 * account while computing the CRC */
925 if (tt_local_entry->flags & TT_CLIENT_NEW)
926 continue;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200927 total_one = 0;
928 for (j = 0; j < ETH_ALEN; j++)
929 total_one = crc16_byte(total_one,
930 tt_local_entry->addr[j]);
931 total ^= total_one;
932 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200933 rcu_read_unlock();
934 }
935
936 return total;
937}
938
939static void tt_req_list_free(struct bat_priv *bat_priv)
940{
941 struct tt_req_node *node, *safe;
942
943 spin_lock_bh(&bat_priv->tt_req_list_lock);
944
945 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
946 list_del(&node->list);
947 kfree(node);
948 }
949
950 spin_unlock_bh(&bat_priv->tt_req_list_lock);
951}
952
953void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node,
954 const unsigned char *tt_buff, uint8_t tt_num_changes)
955{
956 uint16_t tt_buff_len = tt_len(tt_num_changes);
957
958 /* Replace the old buffer only if I received something in the
959 * last OGM (the OGM could carry no changes) */
960 spin_lock_bh(&orig_node->tt_buff_lock);
961 if (tt_buff_len > 0) {
962 kfree(orig_node->tt_buff);
963 orig_node->tt_buff_len = 0;
964 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
965 if (orig_node->tt_buff) {
966 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
967 orig_node->tt_buff_len = tt_buff_len;
968 }
969 }
970 spin_unlock_bh(&orig_node->tt_buff_lock);
971}
972
973static void tt_req_purge(struct bat_priv *bat_priv)
974{
975 struct tt_req_node *node, *safe;
976
977 spin_lock_bh(&bat_priv->tt_req_list_lock);
978 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
979 if (is_out_of_time(node->issued_at,
980 TT_REQUEST_TIMEOUT * 1000)) {
981 list_del(&node->list);
982 kfree(node);
983 }
984 }
985 spin_unlock_bh(&bat_priv->tt_req_list_lock);
986}
987
988/* returns the pointer to the new tt_req_node struct if no request
989 * has already been issued for this orig_node, NULL otherwise */
990static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
991 struct orig_node *orig_node)
992{
993 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
994
995 spin_lock_bh(&bat_priv->tt_req_list_lock);
996 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
997 if (compare_eth(tt_req_node_tmp, orig_node) &&
998 !is_out_of_time(tt_req_node_tmp->issued_at,
999 TT_REQUEST_TIMEOUT * 1000))
1000 goto unlock;
1001 }
1002
1003 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1004 if (!tt_req_node)
1005 goto unlock;
1006
1007 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1008 tt_req_node->issued_at = jiffies;
1009
1010 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1011unlock:
1012 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1013 return tt_req_node;
1014}
1015
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001016/* data_ptr is useless here, but has to be kept to respect the prototype */
1017static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
1018{
1019 const struct tt_local_entry *tt_local_entry = entry_ptr;
1020
1021 if (tt_local_entry->flags & TT_CLIENT_NEW)
1022 return 0;
1023 return 1;
1024}
1025
Antonio Quartullia73105b2011-04-27 14:27:44 +02001026static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
1027{
1028 const struct tt_global_entry *tt_global_entry = entry_ptr;
1029 const struct orig_node *orig_node = data_ptr;
1030
Antonio Quartullicc47f662011-04-27 14:27:57 +02001031 if (tt_global_entry->flags & TT_CLIENT_ROAM)
1032 return 0;
1033
Antonio Quartullia73105b2011-04-27 14:27:44 +02001034 return (tt_global_entry->orig_node == orig_node);
1035}
1036
1037static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1038 struct hashtable_t *hash,
1039 struct hard_iface *primary_if,
1040 int (*valid_cb)(const void *,
1041 const void *),
1042 void *cb_data)
1043{
1044 struct tt_local_entry *tt_local_entry;
1045 struct tt_query_packet *tt_response;
1046 struct tt_change *tt_change;
1047 struct hlist_node *node;
1048 struct hlist_head *head;
1049 struct sk_buff *skb = NULL;
1050 uint16_t tt_tot, tt_count;
1051 ssize_t tt_query_size = sizeof(struct tt_query_packet);
1052 int i;
1053
1054 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1055 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1056 tt_len -= tt_len % sizeof(struct tt_change);
1057 }
1058 tt_tot = tt_len / sizeof(struct tt_change);
1059
1060 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1061 if (!skb)
1062 goto out;
1063
1064 skb_reserve(skb, ETH_HLEN);
1065 tt_response = (struct tt_query_packet *)skb_put(skb,
1066 tt_query_size + tt_len);
1067 tt_response->ttvn = ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001068
1069 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1070 tt_count = 0;
1071
1072 rcu_read_lock();
1073 for (i = 0; i < hash->size; i++) {
1074 head = &hash->table[i];
1075
1076 hlist_for_each_entry_rcu(tt_local_entry, node,
1077 head, hash_entry) {
1078 if (tt_count == tt_tot)
1079 break;
1080
1081 if ((valid_cb) && (!valid_cb(tt_local_entry, cb_data)))
1082 continue;
1083
1084 memcpy(tt_change->addr, tt_local_entry->addr, ETH_ALEN);
1085 tt_change->flags = NO_FLAGS;
1086
1087 tt_count++;
1088 tt_change++;
1089 }
1090 }
1091 rcu_read_unlock();
1092
Antonio Quartulli9d852392011-10-17 14:25:13 +02001093 /* store in the message the number of entries we have successfully
1094 * copied */
1095 tt_response->tt_data = htons(tt_count);
1096
Antonio Quartullia73105b2011-04-27 14:27:44 +02001097out:
1098 return skb;
1099}
1100
Marek Lindnera943cac2011-07-30 13:10:18 +02001101static int send_tt_request(struct bat_priv *bat_priv,
1102 struct orig_node *dst_orig_node,
1103 uint8_t ttvn, uint16_t tt_crc, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001104{
1105 struct sk_buff *skb = NULL;
1106 struct tt_query_packet *tt_request;
1107 struct neigh_node *neigh_node = NULL;
1108 struct hard_iface *primary_if;
1109 struct tt_req_node *tt_req_node = NULL;
1110 int ret = 1;
1111
1112 primary_if = primary_if_get_selected(bat_priv);
1113 if (!primary_if)
1114 goto out;
1115
1116 /* The new tt_req will be issued only if I'm not waiting for a
1117 * reply from the same orig_node yet */
1118 tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1119 if (!tt_req_node)
1120 goto out;
1121
1122 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1123 if (!skb)
1124 goto out;
1125
1126 skb_reserve(skb, ETH_HLEN);
1127
1128 tt_request = (struct tt_query_packet *)skb_put(skb,
1129 sizeof(struct tt_query_packet));
1130
1131 tt_request->packet_type = BAT_TT_QUERY;
1132 tt_request->version = COMPAT_VERSION;
1133 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1134 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1135 tt_request->ttl = TTL;
1136 tt_request->ttvn = ttvn;
1137 tt_request->tt_data = tt_crc;
1138 tt_request->flags = TT_REQUEST;
1139
1140 if (full_table)
1141 tt_request->flags |= TT_FULL_TABLE;
1142
1143 neigh_node = orig_node_get_router(dst_orig_node);
1144 if (!neigh_node)
1145 goto out;
1146
1147 bat_dbg(DBG_TT, bat_priv, "Sending TT_REQUEST to %pM via %pM "
1148 "[%c]\n", dst_orig_node->orig, neigh_node->addr,
1149 (full_table ? 'F' : '.'));
1150
1151 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1152 ret = 0;
1153
1154out:
1155 if (neigh_node)
1156 neigh_node_free_ref(neigh_node);
1157 if (primary_if)
1158 hardif_free_ref(primary_if);
1159 if (ret)
1160 kfree_skb(skb);
1161 if (ret && tt_req_node) {
1162 spin_lock_bh(&bat_priv->tt_req_list_lock);
1163 list_del(&tt_req_node->list);
1164 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1165 kfree(tt_req_node);
1166 }
1167 return ret;
1168}
1169
1170static bool send_other_tt_response(struct bat_priv *bat_priv,
1171 struct tt_query_packet *tt_request)
1172{
1173 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1174 struct neigh_node *neigh_node = NULL;
1175 struct hard_iface *primary_if = NULL;
1176 uint8_t orig_ttvn, req_ttvn, ttvn;
1177 int ret = false;
1178 unsigned char *tt_buff;
1179 bool full_table;
1180 uint16_t tt_len, tt_tot;
1181 struct sk_buff *skb = NULL;
1182 struct tt_query_packet *tt_response;
1183
1184 bat_dbg(DBG_TT, bat_priv,
1185 "Received TT_REQUEST from %pM for "
1186 "ttvn: %u (%pM) [%c]\n", tt_request->src,
1187 tt_request->ttvn, tt_request->dst,
1188 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1189
1190 /* Let's get the orig node of the REAL destination */
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001191 req_dst_orig_node = orig_hash_find(bat_priv, tt_request->dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001192 if (!req_dst_orig_node)
1193 goto out;
1194
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001195 res_dst_orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001196 if (!res_dst_orig_node)
1197 goto out;
1198
1199 neigh_node = orig_node_get_router(res_dst_orig_node);
1200 if (!neigh_node)
1201 goto out;
1202
1203 primary_if = primary_if_get_selected(bat_priv);
1204 if (!primary_if)
1205 goto out;
1206
1207 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1208 req_ttvn = tt_request->ttvn;
1209
Antonio Quartulli015758d2011-07-09 17:52:13 +02001210 /* I don't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001211 if (orig_ttvn != req_ttvn ||
1212 tt_request->tt_data != req_dst_orig_node->tt_crc)
1213 goto out;
1214
Antonio Quartulli015758d2011-07-09 17:52:13 +02001215 /* If the full table has been explicitly requested */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001216 if (tt_request->flags & TT_FULL_TABLE ||
1217 !req_dst_orig_node->tt_buff)
1218 full_table = true;
1219 else
1220 full_table = false;
1221
1222 /* In this version, fragmentation is not implemented, then
1223 * I'll send only one packet with as much TT entries as I can */
1224 if (!full_table) {
1225 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1226 tt_len = req_dst_orig_node->tt_buff_len;
1227 tt_tot = tt_len / sizeof(struct tt_change);
1228
1229 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1230 tt_len + ETH_HLEN);
1231 if (!skb)
1232 goto unlock;
1233
1234 skb_reserve(skb, ETH_HLEN);
1235 tt_response = (struct tt_query_packet *)skb_put(skb,
1236 sizeof(struct tt_query_packet) + tt_len);
1237 tt_response->ttvn = req_ttvn;
1238 tt_response->tt_data = htons(tt_tot);
1239
1240 tt_buff = skb->data + sizeof(struct tt_query_packet);
1241 /* Copy the last orig_node's OGM buffer */
1242 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1243 req_dst_orig_node->tt_buff_len);
1244
1245 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1246 } else {
1247 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1248 sizeof(struct tt_change);
1249 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1250
1251 skb = tt_response_fill_table(tt_len, ttvn,
1252 bat_priv->tt_global_hash,
1253 primary_if, tt_global_valid_entry,
1254 req_dst_orig_node);
1255 if (!skb)
1256 goto out;
1257
1258 tt_response = (struct tt_query_packet *)skb->data;
1259 }
1260
1261 tt_response->packet_type = BAT_TT_QUERY;
1262 tt_response->version = COMPAT_VERSION;
1263 tt_response->ttl = TTL;
1264 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1265 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1266 tt_response->flags = TT_RESPONSE;
1267
1268 if (full_table)
1269 tt_response->flags |= TT_FULL_TABLE;
1270
1271 bat_dbg(DBG_TT, bat_priv,
1272 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1273 res_dst_orig_node->orig, neigh_node->addr,
1274 req_dst_orig_node->orig, req_ttvn);
1275
1276 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1277 ret = true;
1278 goto out;
1279
1280unlock:
1281 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1282
1283out:
1284 if (res_dst_orig_node)
1285 orig_node_free_ref(res_dst_orig_node);
1286 if (req_dst_orig_node)
1287 orig_node_free_ref(req_dst_orig_node);
1288 if (neigh_node)
1289 neigh_node_free_ref(neigh_node);
1290 if (primary_if)
1291 hardif_free_ref(primary_if);
1292 if (!ret)
1293 kfree_skb(skb);
1294 return ret;
1295
1296}
1297static bool send_my_tt_response(struct bat_priv *bat_priv,
1298 struct tt_query_packet *tt_request)
1299{
1300 struct orig_node *orig_node = NULL;
1301 struct neigh_node *neigh_node = NULL;
1302 struct hard_iface *primary_if = NULL;
1303 uint8_t my_ttvn, req_ttvn, ttvn;
1304 int ret = false;
1305 unsigned char *tt_buff;
1306 bool full_table;
1307 uint16_t tt_len, tt_tot;
1308 struct sk_buff *skb = NULL;
1309 struct tt_query_packet *tt_response;
1310
1311 bat_dbg(DBG_TT, bat_priv,
1312 "Received TT_REQUEST from %pM for "
1313 "ttvn: %u (me) [%c]\n", tt_request->src,
1314 tt_request->ttvn,
1315 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1316
1317
1318 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1319 req_ttvn = tt_request->ttvn;
1320
Antonio Quartullieb7e2a12011-10-12 14:54:50 +02001321 orig_node = orig_hash_find(bat_priv, tt_request->src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001322 if (!orig_node)
1323 goto out;
1324
1325 neigh_node = orig_node_get_router(orig_node);
1326 if (!neigh_node)
1327 goto out;
1328
1329 primary_if = primary_if_get_selected(bat_priv);
1330 if (!primary_if)
1331 goto out;
1332
1333 /* If the full table has been explicitly requested or the gap
1334 * is too big send the whole local translation table */
1335 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1336 !bat_priv->tt_buff)
1337 full_table = true;
1338 else
1339 full_table = false;
1340
1341 /* In this version, fragmentation is not implemented, then
1342 * I'll send only one packet with as much TT entries as I can */
1343 if (!full_table) {
1344 spin_lock_bh(&bat_priv->tt_buff_lock);
1345 tt_len = bat_priv->tt_buff_len;
1346 tt_tot = tt_len / sizeof(struct tt_change);
1347
1348 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1349 tt_len + ETH_HLEN);
1350 if (!skb)
1351 goto unlock;
1352
1353 skb_reserve(skb, ETH_HLEN);
1354 tt_response = (struct tt_query_packet *)skb_put(skb,
1355 sizeof(struct tt_query_packet) + tt_len);
1356 tt_response->ttvn = req_ttvn;
1357 tt_response->tt_data = htons(tt_tot);
1358
1359 tt_buff = skb->data + sizeof(struct tt_query_packet);
1360 memcpy(tt_buff, bat_priv->tt_buff,
1361 bat_priv->tt_buff_len);
1362 spin_unlock_bh(&bat_priv->tt_buff_lock);
1363 } else {
1364 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1365 sizeof(struct tt_change);
1366 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1367
1368 skb = tt_response_fill_table(tt_len, ttvn,
1369 bat_priv->tt_local_hash,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001370 primary_if, tt_local_valid_entry,
1371 NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001372 if (!skb)
1373 goto out;
1374
1375 tt_response = (struct tt_query_packet *)skb->data;
1376 }
1377
1378 tt_response->packet_type = BAT_TT_QUERY;
1379 tt_response->version = COMPAT_VERSION;
1380 tt_response->ttl = TTL;
1381 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1382 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1383 tt_response->flags = TT_RESPONSE;
1384
1385 if (full_table)
1386 tt_response->flags |= TT_FULL_TABLE;
1387
1388 bat_dbg(DBG_TT, bat_priv,
1389 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1390 orig_node->orig, neigh_node->addr,
1391 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1392
1393 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1394 ret = true;
1395 goto out;
1396
1397unlock:
1398 spin_unlock_bh(&bat_priv->tt_buff_lock);
1399out:
1400 if (orig_node)
1401 orig_node_free_ref(orig_node);
1402 if (neigh_node)
1403 neigh_node_free_ref(neigh_node);
1404 if (primary_if)
1405 hardif_free_ref(primary_if);
1406 if (!ret)
1407 kfree_skb(skb);
1408 /* This packet was for me, so it doesn't need to be re-routed */
1409 return true;
1410}
1411
1412bool send_tt_response(struct bat_priv *bat_priv,
1413 struct tt_query_packet *tt_request)
1414{
1415 if (is_my_mac(tt_request->dst))
1416 return send_my_tt_response(bat_priv, tt_request);
1417 else
1418 return send_other_tt_response(bat_priv, tt_request);
1419}
1420
1421static void _tt_update_changes(struct bat_priv *bat_priv,
1422 struct orig_node *orig_node,
1423 struct tt_change *tt_change,
1424 uint16_t tt_num_changes, uint8_t ttvn)
1425{
1426 int i;
1427
1428 for (i = 0; i < tt_num_changes; i++) {
Antonio Quartulli5fbc1592011-06-17 16:11:27 +02001429 if ((tt_change + i)->flags & TT_CLIENT_DEL)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001430 tt_global_del(bat_priv, orig_node,
1431 (tt_change + i)->addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +02001432 "tt removed by changes",
1433 (tt_change + i)->flags & TT_CLIENT_ROAM);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001434 else
1435 if (!tt_global_add(bat_priv, orig_node,
Antonio Quartullibc279082011-07-07 15:35:35 +02001436 (tt_change + i)->addr, ttvn, false,
1437 (tt_change + i)->flags &
1438 TT_CLIENT_WIFI))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001439 /* In case of problem while storing a
1440 * global_entry, we stop the updating
1441 * procedure without committing the
1442 * ttvn change. This will avoid to send
1443 * corrupted data on tt_request
1444 */
1445 return;
1446 }
1447}
1448
1449static void tt_fill_gtable(struct bat_priv *bat_priv,
1450 struct tt_query_packet *tt_response)
1451{
1452 struct orig_node *orig_node = NULL;
1453
1454 orig_node = orig_hash_find(bat_priv, tt_response->src);
1455 if (!orig_node)
1456 goto out;
1457
1458 /* Purge the old table first.. */
1459 tt_global_del_orig(bat_priv, orig_node, "Received full table");
1460
1461 _tt_update_changes(bat_priv, orig_node,
1462 (struct tt_change *)(tt_response + 1),
1463 tt_response->tt_data, tt_response->ttvn);
1464
1465 spin_lock_bh(&orig_node->tt_buff_lock);
1466 kfree(orig_node->tt_buff);
1467 orig_node->tt_buff_len = 0;
1468 orig_node->tt_buff = NULL;
1469 spin_unlock_bh(&orig_node->tt_buff_lock);
1470
1471 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1472
1473out:
1474 if (orig_node)
1475 orig_node_free_ref(orig_node);
1476}
1477
Marek Lindnera943cac2011-07-30 13:10:18 +02001478static void tt_update_changes(struct bat_priv *bat_priv,
1479 struct orig_node *orig_node,
1480 uint16_t tt_num_changes, uint8_t ttvn,
1481 struct tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001482{
1483 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1484 ttvn);
1485
1486 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1487 tt_num_changes);
1488 atomic_set(&orig_node->last_ttvn, ttvn);
1489}
1490
1491bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1492{
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001493 struct tt_local_entry *tt_local_entry = NULL;
1494 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001495
Antonio Quartullia73105b2011-04-27 14:27:44 +02001496 tt_local_entry = tt_local_hash_find(bat_priv, addr);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001497 if (!tt_local_entry)
1498 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001499 /* Check if the client has been logically deleted (but is kept for
1500 * consistency purpose) */
1501 if (tt_local_entry->flags & TT_CLIENT_PENDING)
1502 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001503 ret = true;
1504out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02001505 if (tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001506 tt_local_entry_free_ref(tt_local_entry);
1507 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001508}
1509
1510void handle_tt_response(struct bat_priv *bat_priv,
1511 struct tt_query_packet *tt_response)
1512{
1513 struct tt_req_node *node, *safe;
1514 struct orig_node *orig_node = NULL;
1515
1516 bat_dbg(DBG_TT, bat_priv, "Received TT_RESPONSE from %pM for "
1517 "ttvn %d t_size: %d [%c]\n",
1518 tt_response->src, tt_response->ttvn,
1519 tt_response->tt_data,
1520 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1521
1522 orig_node = orig_hash_find(bat_priv, tt_response->src);
1523 if (!orig_node)
1524 goto out;
1525
1526 if (tt_response->flags & TT_FULL_TABLE)
1527 tt_fill_gtable(bat_priv, tt_response);
1528 else
1529 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1530 tt_response->ttvn,
1531 (struct tt_change *)(tt_response + 1));
1532
1533 /* Delete the tt_req_node from pending tt_requests list */
1534 spin_lock_bh(&bat_priv->tt_req_list_lock);
1535 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1536 if (!compare_eth(node->addr, tt_response->src))
1537 continue;
1538 list_del(&node->list);
1539 kfree(node);
1540 }
1541 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1542
1543 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001544 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001545 /* Roaming phase is over: tables are in sync again. I can
1546 * unset the flag */
1547 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001548out:
1549 if (orig_node)
1550 orig_node_free_ref(orig_node);
1551}
1552
1553int tt_init(struct bat_priv *bat_priv)
1554{
1555 if (!tt_local_init(bat_priv))
1556 return 0;
1557
1558 if (!tt_global_init(bat_priv))
1559 return 0;
1560
1561 tt_start_timer(bat_priv);
1562
1563 return 1;
1564}
1565
Antonio Quartullicc47f662011-04-27 14:27:57 +02001566static void tt_roam_list_free(struct bat_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001567{
Antonio Quartullicc47f662011-04-27 14:27:57 +02001568 struct tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001569
Antonio Quartullicc47f662011-04-27 14:27:57 +02001570 spin_lock_bh(&bat_priv->tt_roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001571
Antonio Quartullicc47f662011-04-27 14:27:57 +02001572 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1573 list_del(&node->list);
1574 kfree(node);
1575 }
1576
1577 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1578}
1579
1580static void tt_roam_purge(struct bat_priv *bat_priv)
1581{
1582 struct tt_roam_node *node, *safe;
1583
1584 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1585 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1586 if (!is_out_of_time(node->first_time,
1587 ROAMING_MAX_TIME * 1000))
1588 continue;
1589
1590 list_del(&node->list);
1591 kfree(node);
1592 }
1593 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1594}
1595
1596/* This function checks whether the client already reached the
1597 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1598 * will not be sent.
1599 *
1600 * returns true if the ROAMING_ADV can be sent, false otherwise */
1601static bool tt_check_roam_count(struct bat_priv *bat_priv,
1602 uint8_t *client)
1603{
1604 struct tt_roam_node *tt_roam_node;
1605 bool ret = false;
1606
1607 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1608 /* The new tt_req will be issued only if I'm not waiting for a
1609 * reply from the same orig_node yet */
1610 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1611 if (!compare_eth(tt_roam_node->addr, client))
1612 continue;
1613
1614 if (is_out_of_time(tt_roam_node->first_time,
1615 ROAMING_MAX_TIME * 1000))
1616 continue;
1617
1618 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1619 /* Sorry, you roamed too many times! */
1620 goto unlock;
1621 ret = true;
1622 break;
1623 }
1624
1625 if (!ret) {
1626 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1627 if (!tt_roam_node)
1628 goto unlock;
1629
1630 tt_roam_node->first_time = jiffies;
1631 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1632 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1633
1634 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1635 ret = true;
1636 }
1637
1638unlock:
1639 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1640 return ret;
1641}
1642
1643void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1644 struct orig_node *orig_node)
1645{
1646 struct neigh_node *neigh_node = NULL;
1647 struct sk_buff *skb = NULL;
1648 struct roam_adv_packet *roam_adv_packet;
1649 int ret = 1;
1650 struct hard_iface *primary_if;
1651
1652 /* before going on we have to check whether the client has
1653 * already roamed to us too many times */
1654 if (!tt_check_roam_count(bat_priv, client))
1655 goto out;
1656
1657 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1658 if (!skb)
1659 goto out;
1660
1661 skb_reserve(skb, ETH_HLEN);
1662
1663 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1664 sizeof(struct roam_adv_packet));
1665
1666 roam_adv_packet->packet_type = BAT_ROAM_ADV;
1667 roam_adv_packet->version = COMPAT_VERSION;
1668 roam_adv_packet->ttl = TTL;
1669 primary_if = primary_if_get_selected(bat_priv);
1670 if (!primary_if)
1671 goto out;
1672 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1673 hardif_free_ref(primary_if);
1674 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1675 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1676
1677 neigh_node = orig_node_get_router(orig_node);
1678 if (!neigh_node)
1679 goto out;
1680
1681 bat_dbg(DBG_TT, bat_priv,
1682 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1683 orig_node->orig, client, neigh_node->addr);
1684
1685 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1686 ret = 0;
1687
1688out:
1689 if (neigh_node)
1690 neigh_node_free_ref(neigh_node);
1691 if (ret)
1692 kfree_skb(skb);
1693 return;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001694}
1695
1696static void tt_purge(struct work_struct *work)
1697{
1698 struct delayed_work *delayed_work =
1699 container_of(work, struct delayed_work, work);
1700 struct bat_priv *bat_priv =
1701 container_of(delayed_work, struct bat_priv, tt_work);
1702
1703 tt_local_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001704 tt_global_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001705 tt_req_purge(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001706 tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001707
1708 tt_start_timer(bat_priv);
1709}
Antonio Quartullicc47f662011-04-27 14:27:57 +02001710
1711void tt_free(struct bat_priv *bat_priv)
1712{
1713 cancel_delayed_work_sync(&bat_priv->tt_work);
1714
1715 tt_local_table_free(bat_priv);
1716 tt_global_table_free(bat_priv);
1717 tt_req_list_free(bat_priv);
1718 tt_changes_list_free(bat_priv);
1719 tt_roam_list_free(bat_priv);
1720
1721 kfree(bat_priv->tt_buff);
1722}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001723
1724/* This function will reset the specified flags from all the entries in
1725 * the given hash table and will increment num_local_tt for each involved
1726 * entry */
1727static void tt_local_reset_flags(struct bat_priv *bat_priv, uint16_t flags)
1728{
1729 int i;
1730 struct hashtable_t *hash = bat_priv->tt_local_hash;
1731 struct hlist_head *head;
1732 struct hlist_node *node;
1733 struct tt_local_entry *tt_local_entry;
1734
1735 if (!hash)
1736 return;
1737
1738 for (i = 0; i < hash->size; i++) {
1739 head = &hash->table[i];
1740
1741 rcu_read_lock();
1742 hlist_for_each_entry_rcu(tt_local_entry, node,
1743 head, hash_entry) {
Antonio Quartulli31901262011-10-16 18:53:37 +02001744 if (!(tt_local_entry->flags & flags))
1745 continue;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02001746 tt_local_entry->flags &= ~flags;
1747 atomic_inc(&bat_priv->num_local_tt);
1748 }
1749 rcu_read_unlock();
1750 }
1751
1752}
1753
1754/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
1755static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
1756{
1757 struct hashtable_t *hash = bat_priv->tt_local_hash;
1758 struct tt_local_entry *tt_local_entry;
1759 struct hlist_node *node, *node_tmp;
1760 struct hlist_head *head;
1761 spinlock_t *list_lock; /* protects write access to the hash lists */
1762 int i;
1763
1764 if (!hash)
1765 return;
1766
1767 for (i = 0; i < hash->size; i++) {
1768 head = &hash->table[i];
1769 list_lock = &hash->list_locks[i];
1770
1771 spin_lock_bh(list_lock);
1772 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
1773 head, hash_entry) {
1774 if (!(tt_local_entry->flags & TT_CLIENT_PENDING))
1775 continue;
1776
1777 bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry "
1778 "(%pM): pending\n", tt_local_entry->addr);
1779
1780 atomic_dec(&bat_priv->num_local_tt);
1781 hlist_del_rcu(node);
1782 tt_local_entry_free_ref(tt_local_entry);
1783 }
1784 spin_unlock_bh(list_lock);
1785 }
1786
1787}
1788
1789void tt_commit_changes(struct bat_priv *bat_priv)
1790{
1791 tt_local_reset_flags(bat_priv, TT_CLIENT_NEW);
1792 tt_local_purge_pending_clients(bat_priv);
1793
1794 /* Increment the TTVN only once per OGM interval */
1795 atomic_inc(&bat_priv->ttvn);
1796 bat_priv->tt_poss_change = false;
1797}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02001798
1799bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst)
1800{
1801 struct tt_local_entry *tt_local_entry = NULL;
1802 struct tt_global_entry *tt_global_entry = NULL;
1803 bool ret = true;
1804
1805 if (!atomic_read(&bat_priv->ap_isolation))
1806 return false;
1807
1808 tt_local_entry = tt_local_hash_find(bat_priv, dst);
1809 if (!tt_local_entry)
1810 goto out;
1811
1812 tt_global_entry = tt_global_hash_find(bat_priv, src);
1813 if (!tt_global_entry)
1814 goto out;
1815
1816 if (_is_ap_isolated(tt_local_entry, tt_global_entry))
1817 goto out;
1818
1819 ret = false;
1820
1821out:
1822 if (tt_global_entry)
1823 tt_global_entry_free_ref(tt_global_entry);
1824 if (tt_local_entry)
1825 tt_local_entry_free_ref(tt_local_entry);
1826 return ret;
1827}
Marek Lindnera943cac2011-07-30 13:10:18 +02001828
1829void tt_update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
1830 const unsigned char *tt_buff, uint8_t tt_num_changes,
1831 uint8_t ttvn, uint16_t tt_crc)
1832{
1833 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
1834 bool full_table = true;
1835
1836 /* the ttvn increased by one -> we can apply the attached changes */
1837 if (ttvn - orig_ttvn == 1) {
1838 /* the OGM could not contain the changes due to their size or
1839 * because they have already been sent TT_OGM_APPEND_MAX times.
1840 * In this case send a tt request */
1841 if (!tt_num_changes) {
1842 full_table = false;
1843 goto request_table;
1844 }
1845
1846 tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
1847 (struct tt_change *)tt_buff);
1848
1849 /* Even if we received the precomputed crc with the OGM, we
1850 * prefer to recompute it to spot any possible inconsistency
1851 * in the global table */
1852 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
1853
1854 /* The ttvn alone is not enough to guarantee consistency
1855 * because a single value could represent different states
1856 * (due to the wrap around). Thus a node has to check whether
1857 * the resulting table (after applying the changes) is still
1858 * consistent or not. E.g. a node could disconnect while its
1859 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
1860 * checking the CRC value is mandatory to detect the
1861 * inconsistency */
1862 if (orig_node->tt_crc != tt_crc)
1863 goto request_table;
1864
1865 /* Roaming phase is over: tables are in sync again. I can
1866 * unset the flag */
1867 orig_node->tt_poss_change = false;
1868 } else {
1869 /* if we missed more than one change or our tables are not
1870 * in sync anymore -> request fresh tt data */
1871 if (ttvn != orig_ttvn || orig_node->tt_crc != tt_crc) {
1872request_table:
1873 bat_dbg(DBG_TT, bat_priv, "TT inconsistency for %pM. "
1874 "Need to retrieve the correct information "
1875 "(ttvn: %u last_ttvn: %u crc: %u last_crc: "
1876 "%u num_changes: %u)\n", orig_node->orig, ttvn,
1877 orig_ttvn, tt_crc, orig_node->tt_crc,
1878 tt_num_changes);
1879 send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
1880 full_table);
1881 return;
1882 }
1883 }
1884}