net/sh-eth: Check return value of sh_eth_reset when chip reset fail
[linux-2.6-block.git] / net / batman-adv / translation-table.c
CommitLineData
9cfc7bd6 1/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
c6c8fea2 2 *
35c133a0 3 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
c6c8fea2
SE
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
c6c8fea2
SE
18 */
19
20#include "main.h"
21#include "translation-table.h"
22#include "soft-interface.h"
32ae9b22 23#include "hard-interface.h"
a73105b8 24#include "send.h"
c6c8fea2
SE
25#include "hash.h"
26#include "originator.h"
a73105b8 27#include "routing.h"
20ff9d59 28#include "bridge_loop_avoidance.h"
c6c8fea2 29
a73105b8
AQ
30#include <linux/crc16.h>
31
a513088d
SE
32static void batadv_send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
33 struct orig_node *orig_node);
34static void batadv_tt_purge(struct work_struct *work);
35static void
36batadv_tt_global_del_orig_list(struct tt_global_entry *tt_global_entry);
c6c8fea2 37
7aadf889 38/* returns 1 if they are the same mac addr */
a513088d 39static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
7aadf889 40{
48100bac 41 const void *data1 = container_of(node, struct tt_common_entry,
747e4221 42 hash_entry);
7aadf889
ML
43
44 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
45}
46
a513088d 47static void batadv_tt_start_timer(struct bat_priv *bat_priv)
c6c8fea2 48{
a513088d 49 INIT_DELAYED_WORK(&bat_priv->tt_work, batadv_tt_purge);
3193e8fd 50 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt_work,
a73105b8 51 msecs_to_jiffies(5000));
c6c8fea2
SE
52}
53
a513088d
SE
54static struct tt_common_entry *batadv_tt_hash_find(struct hashtable_t *hash,
55 const void *data)
7aadf889 56{
7aadf889
ML
57 struct hlist_head *head;
58 struct hlist_node *node;
48100bac 59 struct tt_common_entry *tt_common_entry, *tt_common_entry_tmp = NULL;
c90681b8 60 uint32_t index;
7aadf889
ML
61
62 if (!hash)
63 return NULL;
64
da641193 65 index = batadv_choose_orig(data, hash->size);
7aadf889
ML
66 head = &hash->table[index];
67
68 rcu_read_lock();
48100bac 69 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
1eda58bf 70 if (!batadv_compare_eth(tt_common_entry, data))
7aadf889
ML
71 continue;
72
48100bac 73 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
7683fdc1
AQ
74 continue;
75
48100bac 76 tt_common_entry_tmp = tt_common_entry;
7aadf889
ML
77 break;
78 }
79 rcu_read_unlock();
80
48100bac 81 return tt_common_entry_tmp;
7aadf889
ML
82}
83
a513088d
SE
84static struct tt_local_entry *
85batadv_tt_local_hash_find(struct bat_priv *bat_priv, const void *data)
7aadf889 86{
48100bac
AQ
87 struct tt_common_entry *tt_common_entry;
88 struct tt_local_entry *tt_local_entry = NULL;
7aadf889 89
a513088d 90 tt_common_entry = batadv_tt_hash_find(bat_priv->tt_local_hash, data);
48100bac
AQ
91 if (tt_common_entry)
92 tt_local_entry = container_of(tt_common_entry,
93 struct tt_local_entry, common);
94 return tt_local_entry;
95}
7aadf889 96
a513088d
SE
97static struct tt_global_entry *
98batadv_tt_global_hash_find(struct bat_priv *bat_priv, const void *data)
48100bac
AQ
99{
100 struct tt_common_entry *tt_common_entry;
101 struct tt_global_entry *tt_global_entry = NULL;
7683fdc1 102
a513088d 103 tt_common_entry = batadv_tt_hash_find(bat_priv->tt_global_hash, data);
48100bac
AQ
104 if (tt_common_entry)
105 tt_global_entry = container_of(tt_common_entry,
106 struct tt_global_entry, common);
107 return tt_global_entry;
7aadf889 108
7aadf889
ML
109}
110
a513088d
SE
111static void
112batadv_tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
7683fdc1 113{
48100bac
AQ
114 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
115 kfree_rcu(tt_local_entry, common.rcu);
7683fdc1
AQ
116}
117
a513088d 118static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
531027fc 119{
48100bac 120 struct tt_common_entry *tt_common_entry;
531027fc
SW
121 struct tt_global_entry *tt_global_entry;
122
48100bac
AQ
123 tt_common_entry = container_of(rcu, struct tt_common_entry, rcu);
124 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
125 common);
531027fc 126
531027fc
SW
127 kfree(tt_global_entry);
128}
129
a513088d
SE
130static void
131batadv_tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
7683fdc1 132{
db08e6e5 133 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
a513088d 134 batadv_tt_global_del_orig_list(tt_global_entry);
48100bac 135 call_rcu(&tt_global_entry->common.rcu,
a513088d 136 batadv_tt_global_entry_free_rcu);
db08e6e5
SW
137 }
138}
139
a513088d 140static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
db08e6e5
SW
141{
142 struct tt_orig_list_entry *orig_entry;
143
144 orig_entry = container_of(rcu, struct tt_orig_list_entry, rcu);
7d211efc 145 batadv_orig_node_free_ref(orig_entry->orig_node);
db08e6e5
SW
146 kfree(orig_entry);
147}
148
a513088d
SE
149static void
150batadv_tt_orig_list_entry_free_ref(struct tt_orig_list_entry *orig_entry)
db08e6e5 151{
a513088d 152 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
7683fdc1
AQ
153}
154
a513088d
SE
155static void batadv_tt_local_event(struct bat_priv *bat_priv,
156 const uint8_t *addr, uint8_t flags)
a73105b8
AQ
157{
158 struct tt_change_node *tt_change_node;
159
160 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
161
162 if (!tt_change_node)
163 return;
164
ff66c975 165 tt_change_node->change.flags = flags;
a73105b8
AQ
166 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
167
168 spin_lock_bh(&bat_priv->tt_changes_list_lock);
169 /* track the change in the OGMinterval list */
170 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
171 atomic_inc(&bat_priv->tt_local_changes);
172 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
173
174 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
175}
176
08c36d3e 177int batadv_tt_len(int changes_num)
a73105b8
AQ
178{
179 return changes_num * sizeof(struct tt_change);
180}
181
a513088d 182static int batadv_tt_local_init(struct bat_priv *bat_priv)
c6c8fea2 183{
2dafb49d 184 if (bat_priv->tt_local_hash)
5346c35e 185 return 0;
c6c8fea2 186
1a8eaf07 187 bat_priv->tt_local_hash = batadv_hash_new(1024);
c6c8fea2 188
2dafb49d 189 if (!bat_priv->tt_local_hash)
5346c35e 190 return -ENOMEM;
c6c8fea2 191
5346c35e 192 return 0;
c6c8fea2
SE
193}
194
08c36d3e
SE
195void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
196 int ifindex)
c6c8fea2
SE
197{
198 struct bat_priv *bat_priv = netdev_priv(soft_iface);
7683fdc1
AQ
199 struct tt_local_entry *tt_local_entry = NULL;
200 struct tt_global_entry *tt_global_entry = NULL;
db08e6e5
SW
201 struct hlist_head *head;
202 struct hlist_node *node;
203 struct tt_orig_list_entry *orig_entry;
80b3f58c 204 int hash_added;
c6c8fea2 205
a513088d 206 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
c6c8fea2 207
2dafb49d
AQ
208 if (tt_local_entry) {
209 tt_local_entry->last_seen = jiffies;
521251f2
AQ
210 /* possibly unset the TT_CLIENT_PENDING flag */
211 tt_local_entry->common.flags &= ~TT_CLIENT_PENDING;
7683fdc1 212 goto out;
c6c8fea2
SE
213 }
214
704509b8 215 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
2dafb49d 216 if (!tt_local_entry)
7683fdc1 217 goto out;
a73105b8 218
1eda58bf
SE
219 batadv_dbg(DBG_TT, bat_priv,
220 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
221 (uint8_t)atomic_read(&bat_priv->ttvn));
c6c8fea2 222
48100bac
AQ
223 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
224 tt_local_entry->common.flags = NO_FLAGS;
9563877e 225 if (batadv_is_wifi_iface(ifindex))
48100bac
AQ
226 tt_local_entry->common.flags |= TT_CLIENT_WIFI;
227 atomic_set(&tt_local_entry->common.refcount, 2);
228 tt_local_entry->last_seen = jiffies;
c6c8fea2
SE
229
230 /* the batman interface mac address should never be purged */
1eda58bf 231 if (batadv_compare_eth(addr, soft_iface->dev_addr))
48100bac 232 tt_local_entry->common.flags |= TT_CLIENT_NOPURGE;
c6c8fea2 233
c40ed2bf
AQ
234 /* The local entry has to be marked as NEW to avoid to send it in
235 * a full table response going out before the next ttvn increment
9cfc7bd6
SE
236 * (consistency check)
237 */
c40ed2bf
AQ
238 tt_local_entry->common.flags |= TT_CLIENT_NEW;
239
a513088d 240 hash_added = batadv_hash_add(bat_priv->tt_local_hash, batadv_compare_tt,
da641193
SE
241 batadv_choose_orig,
242 &tt_local_entry->common,
c0a55929 243 &tt_local_entry->common.hash_entry);
80b3f58c
SW
244
245 if (unlikely(hash_added != 0)) {
246 /* remove the reference for the hash */
a513088d 247 batadv_tt_local_entry_free_ref(tt_local_entry);
80b3f58c
SW
248 goto out;
249 }
250
a513088d 251 batadv_tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
ff66c975 252
c6c8fea2 253 /* remove address from global hash if present */
a513088d 254 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
c6c8fea2 255
cc47f66e
AQ
256 /* Check whether it is a roaming! */
257 if (tt_global_entry) {
db08e6e5
SW
258 /* These node are probably going to update their tt table */
259 head = &tt_global_entry->orig_list;
260 rcu_read_lock();
261 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
262 orig_entry->orig_node->tt_poss_change = true;
263
a513088d
SE
264 batadv_send_roam_adv(bat_priv,
265 tt_global_entry->common.addr,
266 orig_entry->orig_node);
db08e6e5
SW
267 }
268 rcu_read_unlock();
269 /* The global entry has to be marked as ROAMING and
270 * has to be kept for consistency purpose
271 */
220b07e9 272 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
03fc3070 273 tt_global_entry->roam_at = jiffies;
7683fdc1
AQ
274 }
275out:
276 if (tt_local_entry)
a513088d 277 batadv_tt_local_entry_free_ref(tt_local_entry);
7683fdc1 278 if (tt_global_entry)
a513088d 279 batadv_tt_global_entry_free_ref(tt_global_entry);
c6c8fea2
SE
280}
281
a513088d
SE
282static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
283 int *packet_buff_len,
284 int min_packet_len,
285 int new_packet_len)
be9aa4c1
ML
286{
287 unsigned char *new_buff;
288
289 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
290
291 /* keep old buffer if kmalloc should fail */
292 if (new_buff) {
293 memcpy(new_buff, *packet_buff, min_packet_len);
294 kfree(*packet_buff);
295 *packet_buff = new_buff;
296 *packet_buff_len = new_packet_len;
297 }
298}
299
a513088d
SE
300static void batadv_tt_prepare_packet_buff(struct bat_priv *bat_priv,
301 unsigned char **packet_buff,
302 int *packet_buff_len,
303 int min_packet_len)
be9aa4c1
ML
304{
305 struct hard_iface *primary_if;
306 int req_len;
307
e5d89254 308 primary_if = batadv_primary_if_get_selected(bat_priv);
be9aa4c1
ML
309
310 req_len = min_packet_len;
08c36d3e 311 req_len += batadv_tt_len(atomic_read(&bat_priv->tt_local_changes));
be9aa4c1
ML
312
313 /* if we have too many changes for one packet don't send any
314 * and wait for the tt table request which will be fragmented
315 */
316 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
317 req_len = min_packet_len;
318
a513088d
SE
319 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
320 min_packet_len, req_len);
be9aa4c1
ML
321
322 if (primary_if)
e5d89254 323 batadv_hardif_free_ref(primary_if);
be9aa4c1
ML
324}
325
a513088d
SE
326static int batadv_tt_changes_fill_buff(struct bat_priv *bat_priv,
327 unsigned char **packet_buff,
328 int *packet_buff_len,
329 int min_packet_len)
c6c8fea2 330{
a73105b8 331 struct tt_change_node *entry, *safe;
be9aa4c1
ML
332 int count = 0, tot_changes = 0, new_len;
333 unsigned char *tt_buff;
334
a513088d
SE
335 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
336 packet_buff_len, min_packet_len);
c6c8fea2 337
be9aa4c1
ML
338 new_len = *packet_buff_len - min_packet_len;
339 tt_buff = *packet_buff + min_packet_len;
340
341 if (new_len > 0)
08c36d3e 342 tot_changes = new_len / batadv_tt_len(1);
c6c8fea2 343
a73105b8
AQ
344 spin_lock_bh(&bat_priv->tt_changes_list_lock);
345 atomic_set(&bat_priv->tt_local_changes, 0);
c6c8fea2 346
a73105b8 347 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
7c64fd98 348 list) {
a73105b8 349 if (count < tot_changes) {
08c36d3e 350 memcpy(tt_buff + batadv_tt_len(count),
a73105b8 351 &entry->change, sizeof(struct tt_change));
c6c8fea2
SE
352 count++;
353 }
a73105b8
AQ
354 list_del(&entry->list);
355 kfree(entry);
c6c8fea2 356 }
a73105b8
AQ
357 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
358
359 /* Keep the buffer for possible tt_request */
360 spin_lock_bh(&bat_priv->tt_buff_lock);
361 kfree(bat_priv->tt_buff);
362 bat_priv->tt_buff_len = 0;
363 bat_priv->tt_buff = NULL;
be9aa4c1
ML
364 /* check whether this new OGM has no changes due to size problems */
365 if (new_len > 0) {
366 /* if kmalloc() fails we will reply with the full table
a73105b8
AQ
367 * instead of providing the diff
368 */
be9aa4c1 369 bat_priv->tt_buff = kmalloc(new_len, GFP_ATOMIC);
a73105b8 370 if (bat_priv->tt_buff) {
be9aa4c1
ML
371 memcpy(bat_priv->tt_buff, tt_buff, new_len);
372 bat_priv->tt_buff_len = new_len;
a73105b8
AQ
373 }
374 }
375 spin_unlock_bh(&bat_priv->tt_buff_lock);
c6c8fea2 376
08ad76ec 377 return count;
c6c8fea2
SE
378}
379
08c36d3e 380int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2
SE
381{
382 struct net_device *net_dev = (struct net_device *)seq->private;
383 struct bat_priv *bat_priv = netdev_priv(net_dev);
2dafb49d 384 struct hashtable_t *hash = bat_priv->tt_local_hash;
48100bac 385 struct tt_common_entry *tt_common_entry;
32ae9b22 386 struct hard_iface *primary_if;
7aadf889 387 struct hlist_node *node;
c6c8fea2 388 struct hlist_head *head;
c90681b8
AQ
389 uint32_t i;
390 int ret = 0;
32ae9b22 391
e5d89254 392 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22 393 if (!primary_if) {
86ceb360
SE
394 ret = seq_printf(seq,
395 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
32ae9b22
ML
396 net_dev->name);
397 goto out;
398 }
c6c8fea2 399
32ae9b22 400 if (primary_if->if_status != IF_ACTIVE) {
86ceb360
SE
401 ret = seq_printf(seq,
402 "BATMAN mesh %s disabled - primary interface not active\n",
32ae9b22
ML
403 net_dev->name);
404 goto out;
c6c8fea2
SE
405 }
406
86ceb360
SE
407 seq_printf(seq,
408 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
a73105b8 409 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
c6c8fea2 410
c6c8fea2
SE
411 for (i = 0; i < hash->size; i++) {
412 head = &hash->table[i];
413
7aadf889 414 rcu_read_lock();
48100bac 415 hlist_for_each_entry_rcu(tt_common_entry, node,
7aadf889 416 head, hash_entry) {
d099c2c5 417 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
7c64fd98
SE
418 tt_common_entry->addr,
419 (tt_common_entry->flags &
420 TT_CLIENT_ROAM ? 'R' : '.'),
421 (tt_common_entry->flags &
422 TT_CLIENT_NOPURGE ? 'P' : '.'),
423 (tt_common_entry->flags &
424 TT_CLIENT_NEW ? 'N' : '.'),
425 (tt_common_entry->flags &
426 TT_CLIENT_PENDING ? 'X' : '.'),
427 (tt_common_entry->flags &
428 TT_CLIENT_WIFI ? 'W' : '.'));
c6c8fea2 429 }
7aadf889 430 rcu_read_unlock();
c6c8fea2 431 }
32ae9b22
ML
432out:
433 if (primary_if)
e5d89254 434 batadv_hardif_free_ref(primary_if);
32ae9b22 435 return ret;
c6c8fea2
SE
436}
437
a513088d
SE
438static void batadv_tt_local_set_pending(struct bat_priv *bat_priv,
439 struct tt_local_entry *tt_local_entry,
440 uint16_t flags, const char *message)
c6c8fea2 441{
a513088d
SE
442 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
443 tt_local_entry->common.flags | flags);
a73105b8 444
015758d0
AQ
445 /* The local client has to be marked as "pending to be removed" but has
446 * to be kept in the table in order to send it in a full table
9cfc7bd6
SE
447 * response issued before the net ttvn increment (consistency check)
448 */
48100bac 449 tt_local_entry->common.flags |= TT_CLIENT_PENDING;
c566dbbe 450
1eda58bf
SE
451 batadv_dbg(DBG_TT, bat_priv,
452 "Local tt entry (%pM) pending to be removed: %s\n",
453 tt_local_entry->common.addr, message);
c6c8fea2
SE
454}
455
08c36d3e
SE
456void batadv_tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
457 const char *message, bool roaming)
c6c8fea2 458{
7683fdc1 459 struct tt_local_entry *tt_local_entry = NULL;
c6c8fea2 460
a513088d 461 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
7683fdc1
AQ
462 if (!tt_local_entry)
463 goto out;
464
a513088d
SE
465 batadv_tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
466 (roaming ? TT_CLIENT_ROAM : NO_FLAGS),
467 message);
7683fdc1
AQ
468out:
469 if (tt_local_entry)
a513088d 470 batadv_tt_local_entry_free_ref(tt_local_entry);
c6c8fea2
SE
471}
472
a513088d 473static void batadv_tt_local_purge(struct bat_priv *bat_priv)
c6c8fea2 474{
2dafb49d
AQ
475 struct hashtable_t *hash = bat_priv->tt_local_hash;
476 struct tt_local_entry *tt_local_entry;
48100bac 477 struct tt_common_entry *tt_common_entry;
7aadf889 478 struct hlist_node *node, *node_tmp;
c6c8fea2 479 struct hlist_head *head;
7683fdc1 480 spinlock_t *list_lock; /* protects write access to the hash lists */
c90681b8 481 uint32_t i;
c6c8fea2 482
c6c8fea2
SE
483 for (i = 0; i < hash->size; i++) {
484 head = &hash->table[i];
7683fdc1 485 list_lock = &hash->list_locks[i];
c6c8fea2 486
7683fdc1 487 spin_lock_bh(list_lock);
48100bac 488 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
7aadf889 489 head, hash_entry) {
48100bac
AQ
490 tt_local_entry = container_of(tt_common_entry,
491 struct tt_local_entry,
492 common);
493 if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE)
7aadf889 494 continue;
c6c8fea2 495
058d0e26 496 /* entry already marked for deletion */
48100bac 497 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
058d0e26
AQ
498 continue;
499
1eda58bf
SE
500 if (!batadv_has_timed_out(tt_local_entry->last_seen,
501 TT_LOCAL_TIMEOUT))
7aadf889
ML
502 continue;
503
a513088d
SE
504 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
505 TT_CLIENT_DEL, "timed out");
c6c8fea2 506 }
7683fdc1 507 spin_unlock_bh(list_lock);
c6c8fea2
SE
508 }
509
c6c8fea2
SE
510}
511
a513088d 512static void batadv_tt_local_table_free(struct bat_priv *bat_priv)
c6c8fea2 513{
a73105b8 514 struct hashtable_t *hash;
a73105b8 515 spinlock_t *list_lock; /* protects write access to the hash lists */
48100bac 516 struct tt_common_entry *tt_common_entry;
a73105b8 517 struct tt_local_entry *tt_local_entry;
7683fdc1
AQ
518 struct hlist_node *node, *node_tmp;
519 struct hlist_head *head;
c90681b8 520 uint32_t i;
a73105b8 521
2dafb49d 522 if (!bat_priv->tt_local_hash)
c6c8fea2
SE
523 return;
524
a73105b8
AQ
525 hash = bat_priv->tt_local_hash;
526
527 for (i = 0; i < hash->size; i++) {
528 head = &hash->table[i];
529 list_lock = &hash->list_locks[i];
530
531 spin_lock_bh(list_lock);
48100bac 532 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
a73105b8
AQ
533 head, hash_entry) {
534 hlist_del_rcu(node);
48100bac
AQ
535 tt_local_entry = container_of(tt_common_entry,
536 struct tt_local_entry,
537 common);
a513088d 538 batadv_tt_local_entry_free_ref(tt_local_entry);
a73105b8
AQ
539 }
540 spin_unlock_bh(list_lock);
541 }
542
1a8eaf07 543 batadv_hash_destroy(hash);
a73105b8 544
2dafb49d 545 bat_priv->tt_local_hash = NULL;
c6c8fea2
SE
546}
547
a513088d 548static int batadv_tt_global_init(struct bat_priv *bat_priv)
c6c8fea2 549{
2dafb49d 550 if (bat_priv->tt_global_hash)
5346c35e 551 return 0;
c6c8fea2 552
1a8eaf07 553 bat_priv->tt_global_hash = batadv_hash_new(1024);
c6c8fea2 554
2dafb49d 555 if (!bat_priv->tt_global_hash)
5346c35e 556 return -ENOMEM;
c6c8fea2 557
5346c35e 558 return 0;
c6c8fea2
SE
559}
560
a513088d 561static void batadv_tt_changes_list_free(struct bat_priv *bat_priv)
c6c8fea2 562{
a73105b8 563 struct tt_change_node *entry, *safe;
c6c8fea2 564
a73105b8 565 spin_lock_bh(&bat_priv->tt_changes_list_lock);
c6c8fea2 566
a73105b8
AQ
567 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
568 list) {
569 list_del(&entry->list);
570 kfree(entry);
571 }
c6c8fea2 572
a73105b8
AQ
573 atomic_set(&bat_priv->tt_local_changes, 0);
574 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
575}
c6c8fea2 576
db08e6e5
SW
577/* find out if an orig_node is already in the list of a tt_global_entry.
578 * returns 1 if found, 0 otherwise
579 */
a513088d
SE
580static bool batadv_tt_global_entry_has_orig(const struct tt_global_entry *entry,
581 const struct orig_node *orig_node)
db08e6e5
SW
582{
583 struct tt_orig_list_entry *tmp_orig_entry;
584 const struct hlist_head *head;
585 struct hlist_node *node;
586 bool found = false;
587
588 rcu_read_lock();
589 head = &entry->orig_list;
590 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
591 if (tmp_orig_entry->orig_node == orig_node) {
592 found = true;
593 break;
594 }
595 }
596 rcu_read_unlock();
597 return found;
598}
599
a513088d
SE
600static void
601batadv_tt_global_add_orig_entry(struct tt_global_entry *tt_global_entry,
602 struct orig_node *orig_node, int ttvn)
db08e6e5
SW
603{
604 struct tt_orig_list_entry *orig_entry;
605
606 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
607 if (!orig_entry)
608 return;
609
610 INIT_HLIST_NODE(&orig_entry->list);
611 atomic_inc(&orig_node->refcount);
612 atomic_inc(&orig_node->tt_size);
613 orig_entry->orig_node = orig_node;
614 orig_entry->ttvn = ttvn;
615
616 spin_lock_bh(&tt_global_entry->list_lock);
617 hlist_add_head_rcu(&orig_entry->list,
618 &tt_global_entry->orig_list);
619 spin_unlock_bh(&tt_global_entry->list_lock);
620}
621
a73105b8 622/* caller must hold orig_node refcount */
08c36d3e
SE
623int batadv_tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
624 const unsigned char *tt_addr, uint8_t ttvn,
625 bool roaming, bool wifi)
a73105b8 626{
db08e6e5 627 struct tt_global_entry *tt_global_entry = NULL;
7683fdc1 628 int ret = 0;
80b3f58c 629 int hash_added;
c0a55929 630 struct tt_common_entry *common;
c6c8fea2 631
a513088d 632 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
a73105b8
AQ
633
634 if (!tt_global_entry) {
db08e6e5
SW
635 tt_global_entry = kzalloc(sizeof(*tt_global_entry),
636 GFP_ATOMIC);
a73105b8 637 if (!tt_global_entry)
7683fdc1
AQ
638 goto out;
639
c0a55929
SE
640 common = &tt_global_entry->common;
641 memcpy(common->addr, tt_addr, ETH_ALEN);
db08e6e5 642
c0a55929 643 common->flags = NO_FLAGS;
cc47f66e 644 tt_global_entry->roam_at = 0;
c0a55929 645 atomic_set(&common->refcount, 2);
db08e6e5
SW
646
647 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
648 spin_lock_init(&tt_global_entry->list_lock);
7683fdc1 649
c0a55929 650 hash_added = batadv_hash_add(bat_priv->tt_global_hash,
a513088d
SE
651 batadv_compare_tt,
652 batadv_choose_orig, common,
653 &common->hash_entry);
80b3f58c
SW
654
655 if (unlikely(hash_added != 0)) {
656 /* remove the reference for the hash */
a513088d 657 batadv_tt_global_entry_free_ref(tt_global_entry);
80b3f58c
SW
658 goto out_remove;
659 }
db08e6e5 660
a513088d
SE
661 batadv_tt_global_add_orig_entry(tt_global_entry, orig_node,
662 ttvn);
a73105b8 663 } else {
db08e6e5
SW
664 /* there is already a global entry, use this one. */
665
666 /* If there is the TT_CLIENT_ROAM flag set, there is only one
667 * originator left in the list and we previously received a
668 * delete + roaming change for this originator.
669 *
670 * We should first delete the old originator before adding the
671 * new one.
672 */
673 if (tt_global_entry->common.flags & TT_CLIENT_ROAM) {
a513088d 674 batadv_tt_global_del_orig_list(tt_global_entry);
db08e6e5
SW
675 tt_global_entry->common.flags &= ~TT_CLIENT_ROAM;
676 tt_global_entry->roam_at = 0;
a73105b8 677 }
db08e6e5 678
a513088d
SE
679 if (!batadv_tt_global_entry_has_orig(tt_global_entry,
680 orig_node))
681 batadv_tt_global_add_orig_entry(tt_global_entry,
682 orig_node, ttvn);
a73105b8 683 }
c6c8fea2 684
bc279080 685 if (wifi)
48100bac 686 tt_global_entry->common.flags |= TT_CLIENT_WIFI;
bc279080 687
1eda58bf
SE
688 batadv_dbg(DBG_TT, bat_priv,
689 "Creating new global tt entry: %pM (via %pM)\n",
690 tt_global_entry->common.addr, orig_node->orig);
c6c8fea2 691
80b3f58c 692out_remove:
a73105b8 693 /* remove address from local hash if present */
08c36d3e
SE
694 batadv_tt_local_remove(bat_priv, tt_global_entry->common.addr,
695 "global tt received", roaming);
7683fdc1
AQ
696 ret = 1;
697out:
698 if (tt_global_entry)
a513088d 699 batadv_tt_global_entry_free_ref(tt_global_entry);
7683fdc1 700 return ret;
c6c8fea2
SE
701}
702
db08e6e5
SW
703/* print all orig nodes who announce the address for this global entry.
704 * it is assumed that the caller holds rcu_read_lock();
705 */
a513088d
SE
706static void
707batadv_tt_global_print_entry(struct tt_global_entry *tt_global_entry,
708 struct seq_file *seq)
db08e6e5
SW
709{
710 struct hlist_head *head;
711 struct hlist_node *node;
712 struct tt_orig_list_entry *orig_entry;
713 struct tt_common_entry *tt_common_entry;
714 uint16_t flags;
715 uint8_t last_ttvn;
716
717 tt_common_entry = &tt_global_entry->common;
718
719 head = &tt_global_entry->orig_list;
720
721 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
722 flags = tt_common_entry->flags;
723 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
724 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c]\n",
725 tt_global_entry->common.addr, orig_entry->ttvn,
726 orig_entry->orig_node->orig, last_ttvn,
727 (flags & TT_CLIENT_ROAM ? 'R' : '.'),
728 (flags & TT_CLIENT_WIFI ? 'W' : '.'));
729 }
730}
731
08c36d3e 732int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2
SE
733{
734 struct net_device *net_dev = (struct net_device *)seq->private;
735 struct bat_priv *bat_priv = netdev_priv(net_dev);
2dafb49d 736 struct hashtable_t *hash = bat_priv->tt_global_hash;
48100bac 737 struct tt_common_entry *tt_common_entry;
2dafb49d 738 struct tt_global_entry *tt_global_entry;
32ae9b22 739 struct hard_iface *primary_if;
7aadf889 740 struct hlist_node *node;
c6c8fea2 741 struct hlist_head *head;
c90681b8
AQ
742 uint32_t i;
743 int ret = 0;
32ae9b22 744
e5d89254 745 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22 746 if (!primary_if) {
86ceb360
SE
747 ret = seq_printf(seq,
748 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
32ae9b22
ML
749 net_dev->name);
750 goto out;
751 }
c6c8fea2 752
32ae9b22 753 if (primary_if->if_status != IF_ACTIVE) {
86ceb360
SE
754 ret = seq_printf(seq,
755 "BATMAN mesh %s disabled - primary interface not active\n",
32ae9b22
ML
756 net_dev->name);
757 goto out;
c6c8fea2
SE
758 }
759
2dafb49d
AQ
760 seq_printf(seq,
761 "Globally announced TT entries received via the mesh %s\n",
c6c8fea2 762 net_dev->name);
df6edb9e
AQ
763 seq_printf(seq, " %-13s %s %-15s %s %s\n",
764 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
c6c8fea2 765
c6c8fea2
SE
766 for (i = 0; i < hash->size; i++) {
767 head = &hash->table[i];
768
7aadf889 769 rcu_read_lock();
48100bac 770 hlist_for_each_entry_rcu(tt_common_entry, node,
7aadf889 771 head, hash_entry) {
48100bac
AQ
772 tt_global_entry = container_of(tt_common_entry,
773 struct tt_global_entry,
774 common);
a513088d 775 batadv_tt_global_print_entry(tt_global_entry, seq);
c6c8fea2 776 }
7aadf889 777 rcu_read_unlock();
c6c8fea2 778 }
32ae9b22
ML
779out:
780 if (primary_if)
e5d89254 781 batadv_hardif_free_ref(primary_if);
32ae9b22 782 return ret;
c6c8fea2
SE
783}
784
db08e6e5 785/* deletes the orig list of a tt_global_entry */
a513088d
SE
786static void
787batadv_tt_global_del_orig_list(struct tt_global_entry *tt_global_entry)
c6c8fea2 788{
db08e6e5
SW
789 struct hlist_head *head;
790 struct hlist_node *node, *safe;
791 struct tt_orig_list_entry *orig_entry;
a73105b8 792
db08e6e5
SW
793 spin_lock_bh(&tt_global_entry->list_lock);
794 head = &tt_global_entry->orig_list;
795 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
796 hlist_del_rcu(node);
a513088d 797 batadv_tt_orig_list_entry_free_ref(orig_entry);
db08e6e5
SW
798 }
799 spin_unlock_bh(&tt_global_entry->list_lock);
c6c8fea2 800
db08e6e5
SW
801}
802
a513088d
SE
803static void
804batadv_tt_global_del_orig_entry(struct bat_priv *bat_priv,
805 struct tt_global_entry *tt_global_entry,
806 struct orig_node *orig_node,
807 const char *message)
db08e6e5
SW
808{
809 struct hlist_head *head;
810 struct hlist_node *node, *safe;
811 struct tt_orig_list_entry *orig_entry;
812
813 spin_lock_bh(&tt_global_entry->list_lock);
814 head = &tt_global_entry->orig_list;
815 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
816 if (orig_entry->orig_node == orig_node) {
1eda58bf
SE
817 batadv_dbg(DBG_TT, bat_priv,
818 "Deleting %pM from global tt entry %pM: %s\n",
819 orig_node->orig,
820 tt_global_entry->common.addr, message);
db08e6e5 821 hlist_del_rcu(node);
a513088d 822 batadv_tt_orig_list_entry_free_ref(orig_entry);
db08e6e5
SW
823 }
824 }
825 spin_unlock_bh(&tt_global_entry->list_lock);
826}
827
a513088d
SE
828static void batadv_tt_global_del_struct(struct bat_priv *bat_priv,
829 struct tt_global_entry *tt_global_entry,
830 const char *message)
db08e6e5 831{
1eda58bf
SE
832 batadv_dbg(DBG_TT, bat_priv, "Deleting global tt entry %pM: %s\n",
833 tt_global_entry->common.addr, message);
7683fdc1 834
a513088d 835 batadv_hash_remove(bat_priv->tt_global_hash, batadv_compare_tt,
da641193 836 batadv_choose_orig, tt_global_entry->common.addr);
a513088d 837 batadv_tt_global_entry_free_ref(tt_global_entry);
db08e6e5 838
c6c8fea2
SE
839}
840
db08e6e5
SW
841/* If the client is to be deleted, we check if it is the last origantor entry
842 * within tt_global entry. If yes, we set the TT_CLIENT_ROAM flag and the timer,
843 * otherwise we simply remove the originator scheduled for deletion.
844 */
a513088d
SE
845static void
846batadv_tt_global_del_roaming(struct bat_priv *bat_priv,
847 struct tt_global_entry *tt_global_entry,
848 struct orig_node *orig_node, const char *message)
db08e6e5
SW
849{
850 bool last_entry = true;
851 struct hlist_head *head;
852 struct hlist_node *node;
853 struct tt_orig_list_entry *orig_entry;
854
855 /* no local entry exists, case 1:
856 * Check if this is the last one or if other entries exist.
857 */
858
859 rcu_read_lock();
860 head = &tt_global_entry->orig_list;
861 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
862 if (orig_entry->orig_node != orig_node) {
863 last_entry = false;
864 break;
865 }
866 }
867 rcu_read_unlock();
868
869 if (last_entry) {
870 /* its the last one, mark for roaming. */
871 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
872 tt_global_entry->roam_at = jiffies;
873 } else
874 /* there is another entry, we can simply delete this
875 * one and can still use the other one.
876 */
a513088d
SE
877 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
878 orig_node, message);
db08e6e5
SW
879}
880
881
882
a513088d
SE
883static void batadv_tt_global_del(struct bat_priv *bat_priv,
884 struct orig_node *orig_node,
885 const unsigned char *addr,
886 const char *message, bool roaming)
a73105b8 887{
7683fdc1 888 struct tt_global_entry *tt_global_entry = NULL;
a513088d 889 struct tt_local_entry *local_entry = NULL;
a73105b8 890
a513088d 891 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
db08e6e5 892 if (!tt_global_entry)
7683fdc1 893 goto out;
a73105b8 894
db08e6e5 895 if (!roaming) {
a513088d
SE
896 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
897 orig_node, message);
db08e6e5
SW
898
899 if (hlist_empty(&tt_global_entry->orig_list))
a513088d
SE
900 batadv_tt_global_del_struct(bat_priv, tt_global_entry,
901 message);
db08e6e5
SW
902
903 goto out;
904 }
92f90f56
SE
905
906 /* if we are deleting a global entry due to a roam
907 * event, there are two possibilities:
db08e6e5
SW
908 * 1) the client roamed from node A to node B => if there
909 * is only one originator left for this client, we mark
92f90f56
SE
910 * it with TT_CLIENT_ROAM, we start a timer and we
911 * wait for node B to claim it. In case of timeout
912 * the entry is purged.
db08e6e5
SW
913 *
914 * If there are other originators left, we directly delete
915 * the originator.
92f90f56 916 * 2) the client roamed to us => we can directly delete
9cfc7bd6
SE
917 * the global entry, since it is useless now.
918 */
a513088d
SE
919 local_entry = batadv_tt_local_hash_find(bat_priv,
920 tt_global_entry->common.addr);
921 if (local_entry) {
db08e6e5 922 /* local entry exists, case 2: client roamed to us. */
a513088d
SE
923 batadv_tt_global_del_orig_list(tt_global_entry);
924 batadv_tt_global_del_struct(bat_priv, tt_global_entry, message);
db08e6e5
SW
925 } else
926 /* no local entry exists, case 1: check for roaming */
a513088d
SE
927 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
928 orig_node, message);
92f90f56 929
92f90f56 930
cc47f66e 931out:
7683fdc1 932 if (tt_global_entry)
a513088d
SE
933 batadv_tt_global_entry_free_ref(tt_global_entry);
934 if (local_entry)
935 batadv_tt_local_entry_free_ref(local_entry);
a73105b8
AQ
936}
937
08c36d3e
SE
938void batadv_tt_global_del_orig(struct bat_priv *bat_priv,
939 struct orig_node *orig_node, const char *message)
c6c8fea2 940{
a513088d 941 struct tt_global_entry *global_entry;
48100bac 942 struct tt_common_entry *tt_common_entry;
c90681b8 943 uint32_t i;
a73105b8
AQ
944 struct hashtable_t *hash = bat_priv->tt_global_hash;
945 struct hlist_node *node, *safe;
946 struct hlist_head *head;
7683fdc1 947 spinlock_t *list_lock; /* protects write access to the hash lists */
c6c8fea2 948
6e801494
SW
949 if (!hash)
950 return;
951
a73105b8
AQ
952 for (i = 0; i < hash->size; i++) {
953 head = &hash->table[i];
7683fdc1 954 list_lock = &hash->list_locks[i];
c6c8fea2 955
7683fdc1 956 spin_lock_bh(list_lock);
48100bac 957 hlist_for_each_entry_safe(tt_common_entry, node, safe,
7c64fd98 958 head, hash_entry) {
a513088d
SE
959 global_entry = container_of(tt_common_entry,
960 struct tt_global_entry,
961 common);
db08e6e5 962
a513088d
SE
963 batadv_tt_global_del_orig_entry(bat_priv, global_entry,
964 orig_node, message);
db08e6e5 965
a513088d 966 if (hlist_empty(&global_entry->orig_list)) {
1eda58bf
SE
967 batadv_dbg(DBG_TT, bat_priv,
968 "Deleting global tt entry %pM: %s\n",
a513088d 969 global_entry->common.addr, message);
7683fdc1 970 hlist_del_rcu(node);
a513088d 971 batadv_tt_global_entry_free_ref(global_entry);
7683fdc1 972 }
a73105b8 973 }
7683fdc1 974 spin_unlock_bh(list_lock);
c6c8fea2 975 }
17071578 976 orig_node->tt_initialised = false;
c6c8fea2
SE
977}
978
a513088d 979static void batadv_tt_global_roam_purge(struct bat_priv *bat_priv)
cc47f66e
AQ
980{
981 struct hashtable_t *hash = bat_priv->tt_global_hash;
48100bac 982 struct tt_common_entry *tt_common_entry;
cc47f66e
AQ
983 struct tt_global_entry *tt_global_entry;
984 struct hlist_node *node, *node_tmp;
985 struct hlist_head *head;
7683fdc1 986 spinlock_t *list_lock; /* protects write access to the hash lists */
c90681b8 987 uint32_t i;
cc47f66e 988
cc47f66e
AQ
989 for (i = 0; i < hash->size; i++) {
990 head = &hash->table[i];
7683fdc1 991 list_lock = &hash->list_locks[i];
cc47f66e 992
7683fdc1 993 spin_lock_bh(list_lock);
48100bac 994 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
cc47f66e 995 head, hash_entry) {
48100bac
AQ
996 tt_global_entry = container_of(tt_common_entry,
997 struct tt_global_entry,
998 common);
999 if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
cc47f66e 1000 continue;
1eda58bf
SE
1001 if (!batadv_has_timed_out(tt_global_entry->roam_at,
1002 TT_CLIENT_ROAM_TIMEOUT))
cc47f66e
AQ
1003 continue;
1004
1eda58bf
SE
1005 batadv_dbg(DBG_TT, bat_priv,
1006 "Deleting global tt entry (%pM): Roaming timeout\n",
1007 tt_global_entry->common.addr);
db08e6e5 1008
7683fdc1 1009 hlist_del_rcu(node);
a513088d 1010 batadv_tt_global_entry_free_ref(tt_global_entry);
cc47f66e 1011 }
7683fdc1 1012 spin_unlock_bh(list_lock);
cc47f66e
AQ
1013 }
1014
cc47f66e
AQ
1015}
1016
a513088d 1017static void batadv_tt_global_table_free(struct bat_priv *bat_priv)
c6c8fea2 1018{
7683fdc1
AQ
1019 struct hashtable_t *hash;
1020 spinlock_t *list_lock; /* protects write access to the hash lists */
48100bac 1021 struct tt_common_entry *tt_common_entry;
7683fdc1
AQ
1022 struct tt_global_entry *tt_global_entry;
1023 struct hlist_node *node, *node_tmp;
1024 struct hlist_head *head;
c90681b8 1025 uint32_t i;
7683fdc1 1026
2dafb49d 1027 if (!bat_priv->tt_global_hash)
c6c8fea2
SE
1028 return;
1029
7683fdc1
AQ
1030 hash = bat_priv->tt_global_hash;
1031
1032 for (i = 0; i < hash->size; i++) {
1033 head = &hash->table[i];
1034 list_lock = &hash->list_locks[i];
1035
1036 spin_lock_bh(list_lock);
48100bac 1037 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
7683fdc1
AQ
1038 head, hash_entry) {
1039 hlist_del_rcu(node);
48100bac
AQ
1040 tt_global_entry = container_of(tt_common_entry,
1041 struct tt_global_entry,
1042 common);
a513088d 1043 batadv_tt_global_entry_free_ref(tt_global_entry);
7683fdc1
AQ
1044 }
1045 spin_unlock_bh(list_lock);
1046 }
1047
1a8eaf07 1048 batadv_hash_destroy(hash);
7683fdc1 1049
2dafb49d 1050 bat_priv->tt_global_hash = NULL;
c6c8fea2
SE
1051}
1052
a513088d
SE
1053static bool _batadv_is_ap_isolated(struct tt_local_entry *tt_local_entry,
1054 struct tt_global_entry *tt_global_entry)
59b699cd
AQ
1055{
1056 bool ret = false;
1057
48100bac
AQ
1058 if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
1059 tt_global_entry->common.flags & TT_CLIENT_WIFI)
59b699cd
AQ
1060 ret = true;
1061
1062 return ret;
1063}
1064
08c36d3e
SE
1065struct orig_node *batadv_transtable_search(struct bat_priv *bat_priv,
1066 const uint8_t *src,
1067 const uint8_t *addr)
c6c8fea2 1068{
3d393e47
AQ
1069 struct tt_local_entry *tt_local_entry = NULL;
1070 struct tt_global_entry *tt_global_entry = NULL;
7b36e8ee 1071 struct orig_node *orig_node = NULL;
db08e6e5
SW
1072 struct neigh_node *router = NULL;
1073 struct hlist_head *head;
1074 struct hlist_node *node;
1075 struct tt_orig_list_entry *orig_entry;
1076 int best_tq;
c6c8fea2 1077
3d393e47 1078 if (src && atomic_read(&bat_priv->ap_isolation)) {
a513088d 1079 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
3d393e47
AQ
1080 if (!tt_local_entry)
1081 goto out;
1082 }
7aadf889 1083
a513088d 1084 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
2dafb49d 1085 if (!tt_global_entry)
7b36e8ee 1086 goto out;
7aadf889 1087
3d393e47 1088 /* check whether the clients should not communicate due to AP
9cfc7bd6
SE
1089 * isolation
1090 */
a513088d
SE
1091 if (tt_local_entry &&
1092 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3d393e47
AQ
1093 goto out;
1094
db08e6e5 1095 best_tq = 0;
c6c8fea2 1096
db08e6e5
SW
1097 rcu_read_lock();
1098 head = &tt_global_entry->orig_list;
1099 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
7d211efc 1100 router = batadv_orig_node_get_router(orig_entry->orig_node);
db08e6e5
SW
1101 if (!router)
1102 continue;
c6c8fea2 1103
db08e6e5
SW
1104 if (router->tq_avg > best_tq) {
1105 orig_node = orig_entry->orig_node;
1106 best_tq = router->tq_avg;
1107 }
7d211efc 1108 batadv_neigh_node_free_ref(router);
db08e6e5
SW
1109 }
1110 /* found anything? */
1111 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1112 orig_node = NULL;
1113 rcu_read_unlock();
7b36e8ee 1114out:
3d393e47 1115 if (tt_global_entry)
a513088d 1116 batadv_tt_global_entry_free_ref(tt_global_entry);
3d393e47 1117 if (tt_local_entry)
a513088d 1118 batadv_tt_local_entry_free_ref(tt_local_entry);
3d393e47 1119
7b36e8ee 1120 return orig_node;
c6c8fea2 1121}
a73105b8
AQ
1122
1123/* Calculates the checksum of the local table of a given orig_node */
a513088d
SE
1124static uint16_t batadv_tt_global_crc(struct bat_priv *bat_priv,
1125 struct orig_node *orig_node)
a73105b8
AQ
1126{
1127 uint16_t total = 0, total_one;
1128 struct hashtable_t *hash = bat_priv->tt_global_hash;
48100bac 1129 struct tt_common_entry *tt_common_entry;
a73105b8
AQ
1130 struct tt_global_entry *tt_global_entry;
1131 struct hlist_node *node;
1132 struct hlist_head *head;
c90681b8
AQ
1133 uint32_t i;
1134 int j;
a73105b8
AQ
1135
1136 for (i = 0; i < hash->size; i++) {
1137 head = &hash->table[i];
1138
1139 rcu_read_lock();
48100bac 1140 hlist_for_each_entry_rcu(tt_common_entry, node,
a73105b8 1141 head, hash_entry) {
48100bac
AQ
1142 tt_global_entry = container_of(tt_common_entry,
1143 struct tt_global_entry,
1144 common);
db08e6e5
SW
1145 /* Roaming clients are in the global table for
1146 * consistency only. They don't have to be
1147 * taken into account while computing the
1148 * global crc
1149 */
1150 if (tt_global_entry->common.flags & TT_CLIENT_ROAM)
1151 continue;
1152
1153 /* find out if this global entry is announced by this
1154 * originator
1155 */
a513088d
SE
1156 if (!batadv_tt_global_entry_has_orig(tt_global_entry,
1157 orig_node))
db08e6e5
SW
1158 continue;
1159
1160 total_one = 0;
1161 for (j = 0; j < ETH_ALEN; j++)
1162 total_one = crc16_byte(total_one,
1163 tt_global_entry->common.addr[j]);
1164 total ^= total_one;
a73105b8
AQ
1165 }
1166 rcu_read_unlock();
1167 }
1168
1169 return total;
1170}
1171
1172/* Calculates the checksum of the local table */
be9aa4c1 1173static uint16_t batadv_tt_local_crc(struct bat_priv *bat_priv)
a73105b8
AQ
1174{
1175 uint16_t total = 0, total_one;
1176 struct hashtable_t *hash = bat_priv->tt_local_hash;
48100bac 1177 struct tt_common_entry *tt_common_entry;
a73105b8
AQ
1178 struct hlist_node *node;
1179 struct hlist_head *head;
c90681b8
AQ
1180 uint32_t i;
1181 int j;
a73105b8
AQ
1182
1183 for (i = 0; i < hash->size; i++) {
1184 head = &hash->table[i];
1185
1186 rcu_read_lock();
48100bac 1187 hlist_for_each_entry_rcu(tt_common_entry, node,
a73105b8 1188 head, hash_entry) {
058d0e26 1189 /* not yet committed clients have not to be taken into
9cfc7bd6
SE
1190 * account while computing the CRC
1191 */
48100bac 1192 if (tt_common_entry->flags & TT_CLIENT_NEW)
058d0e26 1193 continue;
a73105b8
AQ
1194 total_one = 0;
1195 for (j = 0; j < ETH_ALEN; j++)
1196 total_one = crc16_byte(total_one,
48100bac 1197 tt_common_entry->addr[j]);
a73105b8
AQ
1198 total ^= total_one;
1199 }
a73105b8
AQ
1200 rcu_read_unlock();
1201 }
1202
1203 return total;
1204}
1205
a513088d 1206static void batadv_tt_req_list_free(struct bat_priv *bat_priv)
a73105b8
AQ
1207{
1208 struct tt_req_node *node, *safe;
1209
1210 spin_lock_bh(&bat_priv->tt_req_list_lock);
1211
1212 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1213 list_del(&node->list);
1214 kfree(node);
1215 }
1216
1217 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1218}
1219
a513088d
SE
1220static void batadv_tt_save_orig_buffer(struct bat_priv *bat_priv,
1221 struct orig_node *orig_node,
1222 const unsigned char *tt_buff,
1223 uint8_t tt_num_changes)
a73105b8 1224{
08c36d3e 1225 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
a73105b8
AQ
1226
1227 /* Replace the old buffer only if I received something in the
9cfc7bd6
SE
1228 * last OGM (the OGM could carry no changes)
1229 */
a73105b8
AQ
1230 spin_lock_bh(&orig_node->tt_buff_lock);
1231 if (tt_buff_len > 0) {
1232 kfree(orig_node->tt_buff);
1233 orig_node->tt_buff_len = 0;
1234 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1235 if (orig_node->tt_buff) {
1236 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1237 orig_node->tt_buff_len = tt_buff_len;
1238 }
1239 }
1240 spin_unlock_bh(&orig_node->tt_buff_lock);
1241}
1242
a513088d 1243static void batadv_tt_req_purge(struct bat_priv *bat_priv)
a73105b8
AQ
1244{
1245 struct tt_req_node *node, *safe;
1246
1247 spin_lock_bh(&bat_priv->tt_req_list_lock);
1248 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1eda58bf 1249 if (batadv_has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) {
a73105b8
AQ
1250 list_del(&node->list);
1251 kfree(node);
1252 }
1253 }
1254 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1255}
1256
1257/* returns the pointer to the new tt_req_node struct if no request
9cfc7bd6
SE
1258 * has already been issued for this orig_node, NULL otherwise
1259 */
a513088d
SE
1260static struct tt_req_node *batadv_new_tt_req_node(struct bat_priv *bat_priv,
1261 struct orig_node *orig_node)
a73105b8
AQ
1262{
1263 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1264
1265 spin_lock_bh(&bat_priv->tt_req_list_lock);
1266 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
1eda58bf
SE
1267 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1268 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1269 TT_REQUEST_TIMEOUT))
a73105b8
AQ
1270 goto unlock;
1271 }
1272
1273 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1274 if (!tt_req_node)
1275 goto unlock;
1276
1277 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1278 tt_req_node->issued_at = jiffies;
1279
1280 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1281unlock:
1282 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1283 return tt_req_node;
1284}
1285
058d0e26 1286/* data_ptr is useless here, but has to be kept to respect the prototype */
a513088d
SE
1287static int batadv_tt_local_valid_entry(const void *entry_ptr,
1288 const void *data_ptr)
058d0e26 1289{
48100bac 1290 const struct tt_common_entry *tt_common_entry = entry_ptr;
058d0e26 1291
48100bac 1292 if (tt_common_entry->flags & TT_CLIENT_NEW)
058d0e26
AQ
1293 return 0;
1294 return 1;
1295}
1296
a513088d
SE
1297static int batadv_tt_global_valid(const void *entry_ptr,
1298 const void *data_ptr)
a73105b8 1299{
48100bac
AQ
1300 const struct tt_common_entry *tt_common_entry = entry_ptr;
1301 const struct tt_global_entry *tt_global_entry;
a73105b8
AQ
1302 const struct orig_node *orig_node = data_ptr;
1303
48100bac 1304 if (tt_common_entry->flags & TT_CLIENT_ROAM)
cc47f66e
AQ
1305 return 0;
1306
48100bac
AQ
1307 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
1308 common);
1309
a513088d 1310 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
a73105b8
AQ
1311}
1312
a513088d
SE
1313static struct sk_buff *
1314batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1315 struct hashtable_t *hash,
1316 struct hard_iface *primary_if,
1317 int (*valid_cb)(const void *, const void *),
1318 void *cb_data)
a73105b8 1319{
48100bac 1320 struct tt_common_entry *tt_common_entry;
a73105b8
AQ
1321 struct tt_query_packet *tt_response;
1322 struct tt_change *tt_change;
1323 struct hlist_node *node;
1324 struct hlist_head *head;
1325 struct sk_buff *skb = NULL;
1326 uint16_t tt_tot, tt_count;
1327 ssize_t tt_query_size = sizeof(struct tt_query_packet);
c90681b8 1328 uint32_t i;
a73105b8
AQ
1329
1330 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1331 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1332 tt_len -= tt_len % sizeof(struct tt_change);
1333 }
1334 tt_tot = tt_len / sizeof(struct tt_change);
1335
1336 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1337 if (!skb)
1338 goto out;
1339
1340 skb_reserve(skb, ETH_HLEN);
1341 tt_response = (struct tt_query_packet *)skb_put(skb,
1342 tt_query_size + tt_len);
1343 tt_response->ttvn = ttvn;
a73105b8
AQ
1344
1345 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1346 tt_count = 0;
1347
1348 rcu_read_lock();
1349 for (i = 0; i < hash->size; i++) {
1350 head = &hash->table[i];
1351
48100bac 1352 hlist_for_each_entry_rcu(tt_common_entry, node,
a73105b8
AQ
1353 head, hash_entry) {
1354 if (tt_count == tt_tot)
1355 break;
1356
48100bac 1357 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
a73105b8
AQ
1358 continue;
1359
48100bac
AQ
1360 memcpy(tt_change->addr, tt_common_entry->addr,
1361 ETH_ALEN);
a73105b8
AQ
1362 tt_change->flags = NO_FLAGS;
1363
1364 tt_count++;
1365 tt_change++;
1366 }
1367 }
1368 rcu_read_unlock();
1369
9d852393 1370 /* store in the message the number of entries we have successfully
9cfc7bd6
SE
1371 * copied
1372 */
9d852393
AQ
1373 tt_response->tt_data = htons(tt_count);
1374
a73105b8
AQ
1375out:
1376 return skb;
1377}
1378
a513088d
SE
1379static int batadv_send_tt_request(struct bat_priv *bat_priv,
1380 struct orig_node *dst_orig_node,
1381 uint8_t ttvn, uint16_t tt_crc,
1382 bool full_table)
a73105b8
AQ
1383{
1384 struct sk_buff *skb = NULL;
1385 struct tt_query_packet *tt_request;
1386 struct neigh_node *neigh_node = NULL;
1387 struct hard_iface *primary_if;
1388 struct tt_req_node *tt_req_node = NULL;
1389 int ret = 1;
1390
e5d89254 1391 primary_if = batadv_primary_if_get_selected(bat_priv);
a73105b8
AQ
1392 if (!primary_if)
1393 goto out;
1394
1395 /* The new tt_req will be issued only if I'm not waiting for a
9cfc7bd6
SE
1396 * reply from the same orig_node yet
1397 */
a513088d 1398 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
a73105b8
AQ
1399 if (!tt_req_node)
1400 goto out;
1401
1402 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1403 if (!skb)
1404 goto out;
1405
1406 skb_reserve(skb, ETH_HLEN);
1407
1408 tt_request = (struct tt_query_packet *)skb_put(skb,
1409 sizeof(struct tt_query_packet));
1410
76543d14
SE
1411 tt_request->header.packet_type = BAT_TT_QUERY;
1412 tt_request->header.version = COMPAT_VERSION;
a73105b8
AQ
1413 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1414 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
76543d14 1415 tt_request->header.ttl = TTL;
a73105b8 1416 tt_request->ttvn = ttvn;
6d2003fc 1417 tt_request->tt_data = htons(tt_crc);
a73105b8
AQ
1418 tt_request->flags = TT_REQUEST;
1419
1420 if (full_table)
1421 tt_request->flags |= TT_FULL_TABLE;
1422
7d211efc 1423 neigh_node = batadv_orig_node_get_router(dst_orig_node);
a73105b8
AQ
1424 if (!neigh_node)
1425 goto out;
1426
1eda58bf
SE
1427 batadv_dbg(DBG_TT, bat_priv,
1428 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1429 dst_orig_node->orig, neigh_node->addr,
1430 (full_table ? 'F' : '.'));
a73105b8 1431
f8214865
MH
1432 batadv_inc_counter(bat_priv, BAT_CNT_TT_REQUEST_TX);
1433
9455e34c 1434 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
a73105b8
AQ
1435 ret = 0;
1436
1437out:
1438 if (neigh_node)
7d211efc 1439 batadv_neigh_node_free_ref(neigh_node);
a73105b8 1440 if (primary_if)
e5d89254 1441 batadv_hardif_free_ref(primary_if);
a73105b8
AQ
1442 if (ret)
1443 kfree_skb(skb);
1444 if (ret && tt_req_node) {
1445 spin_lock_bh(&bat_priv->tt_req_list_lock);
1446 list_del(&tt_req_node->list);
1447 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1448 kfree(tt_req_node);
1449 }
1450 return ret;
1451}
1452
a513088d
SE
1453static bool batadv_send_other_tt_response(struct bat_priv *bat_priv,
1454 struct tt_query_packet *tt_request)
a73105b8
AQ
1455{
1456 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1457 struct neigh_node *neigh_node = NULL;
1458 struct hard_iface *primary_if = NULL;
1459 uint8_t orig_ttvn, req_ttvn, ttvn;
1460 int ret = false;
1461 unsigned char *tt_buff;
1462 bool full_table;
1463 uint16_t tt_len, tt_tot;
1464 struct sk_buff *skb = NULL;
1465 struct tt_query_packet *tt_response;
1466
1eda58bf
SE
1467 batadv_dbg(DBG_TT, bat_priv,
1468 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1469 tt_request->src, tt_request->ttvn, tt_request->dst,
1470 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
a73105b8
AQ
1471
1472 /* Let's get the orig node of the REAL destination */
da641193 1473 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
a73105b8
AQ
1474 if (!req_dst_orig_node)
1475 goto out;
1476
da641193 1477 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
a73105b8
AQ
1478 if (!res_dst_orig_node)
1479 goto out;
1480
7d211efc 1481 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
a73105b8
AQ
1482 if (!neigh_node)
1483 goto out;
1484
e5d89254 1485 primary_if = batadv_primary_if_get_selected(bat_priv);
a73105b8
AQ
1486 if (!primary_if)
1487 goto out;
1488
1489 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1490 req_ttvn = tt_request->ttvn;
1491
015758d0 1492 /* I don't have the requested data */
a73105b8 1493 if (orig_ttvn != req_ttvn ||
f25bd58a 1494 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
a73105b8
AQ
1495 goto out;
1496
015758d0 1497 /* If the full table has been explicitly requested */
a73105b8
AQ
1498 if (tt_request->flags & TT_FULL_TABLE ||
1499 !req_dst_orig_node->tt_buff)
1500 full_table = true;
1501 else
1502 full_table = false;
1503
1504 /* In this version, fragmentation is not implemented, then
9cfc7bd6
SE
1505 * I'll send only one packet with as much TT entries as I can
1506 */
a73105b8
AQ
1507 if (!full_table) {
1508 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1509 tt_len = req_dst_orig_node->tt_buff_len;
1510 tt_tot = tt_len / sizeof(struct tt_change);
1511
1512 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1513 tt_len + ETH_HLEN);
1514 if (!skb)
1515 goto unlock;
1516
1517 skb_reserve(skb, ETH_HLEN);
1518 tt_response = (struct tt_query_packet *)skb_put(skb,
1519 sizeof(struct tt_query_packet) + tt_len);
1520 tt_response->ttvn = req_ttvn;
1521 tt_response->tt_data = htons(tt_tot);
1522
1523 tt_buff = skb->data + sizeof(struct tt_query_packet);
1524 /* Copy the last orig_node's OGM buffer */
1525 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1526 req_dst_orig_node->tt_buff_len);
1527
1528 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1529 } else {
1530 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1531 sizeof(struct tt_change);
1532 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1533
a513088d
SE
1534 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1535 bat_priv->tt_global_hash,
1536 primary_if,
1537 batadv_tt_global_valid,
1538 req_dst_orig_node);
a73105b8
AQ
1539 if (!skb)
1540 goto out;
1541
1542 tt_response = (struct tt_query_packet *)skb->data;
1543 }
1544
76543d14
SE
1545 tt_response->header.packet_type = BAT_TT_QUERY;
1546 tt_response->header.version = COMPAT_VERSION;
1547 tt_response->header.ttl = TTL;
a73105b8
AQ
1548 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1549 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1550 tt_response->flags = TT_RESPONSE;
1551
1552 if (full_table)
1553 tt_response->flags |= TT_FULL_TABLE;
1554
1eda58bf
SE
1555 batadv_dbg(DBG_TT, bat_priv,
1556 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1557 res_dst_orig_node->orig, neigh_node->addr,
1558 req_dst_orig_node->orig, req_ttvn);
a73105b8 1559
f8214865
MH
1560 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_TX);
1561
9455e34c 1562 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
a73105b8
AQ
1563 ret = true;
1564 goto out;
1565
1566unlock:
1567 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1568
1569out:
1570 if (res_dst_orig_node)
7d211efc 1571 batadv_orig_node_free_ref(res_dst_orig_node);
a73105b8 1572 if (req_dst_orig_node)
7d211efc 1573 batadv_orig_node_free_ref(req_dst_orig_node);
a73105b8 1574 if (neigh_node)
7d211efc 1575 batadv_neigh_node_free_ref(neigh_node);
a73105b8 1576 if (primary_if)
e5d89254 1577 batadv_hardif_free_ref(primary_if);
a73105b8
AQ
1578 if (!ret)
1579 kfree_skb(skb);
1580 return ret;
1581
1582}
a513088d
SE
1583static bool batadv_send_my_tt_response(struct bat_priv *bat_priv,
1584 struct tt_query_packet *tt_request)
a73105b8
AQ
1585{
1586 struct orig_node *orig_node = NULL;
1587 struct neigh_node *neigh_node = NULL;
1588 struct hard_iface *primary_if = NULL;
1589 uint8_t my_ttvn, req_ttvn, ttvn;
1590 int ret = false;
1591 unsigned char *tt_buff;
1592 bool full_table;
1593 uint16_t tt_len, tt_tot;
1594 struct sk_buff *skb = NULL;
1595 struct tt_query_packet *tt_response;
1596
1eda58bf
SE
1597 batadv_dbg(DBG_TT, bat_priv,
1598 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1599 tt_request->src, tt_request->ttvn,
1600 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
a73105b8
AQ
1601
1602
1603 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1604 req_ttvn = tt_request->ttvn;
1605
da641193 1606 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
a73105b8
AQ
1607 if (!orig_node)
1608 goto out;
1609
7d211efc 1610 neigh_node = batadv_orig_node_get_router(orig_node);
a73105b8
AQ
1611 if (!neigh_node)
1612 goto out;
1613
e5d89254 1614 primary_if = batadv_primary_if_get_selected(bat_priv);
a73105b8
AQ
1615 if (!primary_if)
1616 goto out;
1617
1618 /* If the full table has been explicitly requested or the gap
9cfc7bd6
SE
1619 * is too big send the whole local translation table
1620 */
a73105b8
AQ
1621 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1622 !bat_priv->tt_buff)
1623 full_table = true;
1624 else
1625 full_table = false;
1626
1627 /* In this version, fragmentation is not implemented, then
9cfc7bd6
SE
1628 * I'll send only one packet with as much TT entries as I can
1629 */
a73105b8
AQ
1630 if (!full_table) {
1631 spin_lock_bh(&bat_priv->tt_buff_lock);
1632 tt_len = bat_priv->tt_buff_len;
1633 tt_tot = tt_len / sizeof(struct tt_change);
1634
1635 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1636 tt_len + ETH_HLEN);
1637 if (!skb)
1638 goto unlock;
1639
1640 skb_reserve(skb, ETH_HLEN);
1641 tt_response = (struct tt_query_packet *)skb_put(skb,
1642 sizeof(struct tt_query_packet) + tt_len);
1643 tt_response->ttvn = req_ttvn;
1644 tt_response->tt_data = htons(tt_tot);
1645
1646 tt_buff = skb->data + sizeof(struct tt_query_packet);
1647 memcpy(tt_buff, bat_priv->tt_buff,
1648 bat_priv->tt_buff_len);
1649 spin_unlock_bh(&bat_priv->tt_buff_lock);
1650 } else {
1651 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1652 sizeof(struct tt_change);
1653 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1654
a513088d
SE
1655 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1656 bat_priv->tt_local_hash,
1657 primary_if,
1658 batadv_tt_local_valid_entry,
1659 NULL);
a73105b8
AQ
1660 if (!skb)
1661 goto out;
1662
1663 tt_response = (struct tt_query_packet *)skb->data;
1664 }
1665
76543d14
SE
1666 tt_response->header.packet_type = BAT_TT_QUERY;
1667 tt_response->header.version = COMPAT_VERSION;
1668 tt_response->header.ttl = TTL;
a73105b8
AQ
1669 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1670 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1671 tt_response->flags = TT_RESPONSE;
1672
1673 if (full_table)
1674 tt_response->flags |= TT_FULL_TABLE;
1675
1eda58bf
SE
1676 batadv_dbg(DBG_TT, bat_priv,
1677 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1678 orig_node->orig, neigh_node->addr,
1679 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
a73105b8 1680
f8214865
MH
1681 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_TX);
1682
9455e34c 1683 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
a73105b8
AQ
1684 ret = true;
1685 goto out;
1686
1687unlock:
1688 spin_unlock_bh(&bat_priv->tt_buff_lock);
1689out:
1690 if (orig_node)
7d211efc 1691 batadv_orig_node_free_ref(orig_node);
a73105b8 1692 if (neigh_node)
7d211efc 1693 batadv_neigh_node_free_ref(neigh_node);
a73105b8 1694 if (primary_if)
e5d89254 1695 batadv_hardif_free_ref(primary_if);
a73105b8
AQ
1696 if (!ret)
1697 kfree_skb(skb);
1698 /* This packet was for me, so it doesn't need to be re-routed */
1699 return true;
1700}
1701
08c36d3e
SE
1702bool batadv_send_tt_response(struct bat_priv *bat_priv,
1703 struct tt_query_packet *tt_request)
a73105b8 1704{
3193e8fd 1705 if (batadv_is_my_mac(tt_request->dst)) {
20ff9d59 1706 /* don't answer backbone gws! */
08adf151 1707 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
20ff9d59
SW
1708 return true;
1709
a513088d 1710 return batadv_send_my_tt_response(bat_priv, tt_request);
20ff9d59 1711 } else {
a513088d 1712 return batadv_send_other_tt_response(bat_priv, tt_request);
20ff9d59 1713 }
a73105b8
AQ
1714}
1715
a513088d
SE
1716static void _batadv_tt_update_changes(struct bat_priv *bat_priv,
1717 struct orig_node *orig_node,
1718 struct tt_change *tt_change,
1719 uint16_t tt_num_changes, uint8_t ttvn)
a73105b8
AQ
1720{
1721 int i;
08c36d3e 1722 int is_wifi;
a513088d 1723 int roams;
a73105b8
AQ
1724
1725 for (i = 0; i < tt_num_changes; i++) {
08c36d3e 1726 if ((tt_change + i)->flags & TT_CLIENT_DEL) {
a513088d
SE
1727 roams = (tt_change + i)->flags & TT_CLIENT_ROAM;
1728 batadv_tt_global_del(bat_priv, orig_node,
1729 (tt_change + i)->addr,
1730 "tt removed by changes",
1731 roams);
08c36d3e
SE
1732 } else {
1733 is_wifi = (tt_change + i)->flags & TT_CLIENT_WIFI;
1734 if (!batadv_tt_global_add(bat_priv, orig_node,
1735 (tt_change + i)->addr, ttvn,
1736 false, is_wifi))
a73105b8
AQ
1737 /* In case of problem while storing a
1738 * global_entry, we stop the updating
1739 * procedure without committing the
1740 * ttvn change. This will avoid to send
1741 * corrupted data on tt_request
1742 */
1743 return;
08c36d3e 1744 }
a73105b8 1745 }
17071578 1746 orig_node->tt_initialised = true;
a73105b8
AQ
1747}
1748
a513088d
SE
1749static void batadv_tt_fill_gtable(struct bat_priv *bat_priv,
1750 struct tt_query_packet *tt_response)
a73105b8
AQ
1751{
1752 struct orig_node *orig_node = NULL;
1753
da641193 1754 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
a73105b8
AQ
1755 if (!orig_node)
1756 goto out;
1757
1758 /* Purge the old table first.. */
08c36d3e 1759 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
a73105b8 1760
a513088d
SE
1761 _batadv_tt_update_changes(bat_priv, orig_node,
1762 (struct tt_change *)(tt_response + 1),
1763 ntohs(tt_response->tt_data),
1764 tt_response->ttvn);
a73105b8
AQ
1765
1766 spin_lock_bh(&orig_node->tt_buff_lock);
1767 kfree(orig_node->tt_buff);
1768 orig_node->tt_buff_len = 0;
1769 orig_node->tt_buff = NULL;
1770 spin_unlock_bh(&orig_node->tt_buff_lock);
1771
1772 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1773
1774out:
1775 if (orig_node)
7d211efc 1776 batadv_orig_node_free_ref(orig_node);
a73105b8
AQ
1777}
1778
a513088d
SE
1779static void batadv_tt_update_changes(struct bat_priv *bat_priv,
1780 struct orig_node *orig_node,
1781 uint16_t tt_num_changes, uint8_t ttvn,
1782 struct tt_change *tt_change)
a73105b8 1783{
a513088d
SE
1784 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
1785 tt_num_changes, ttvn);
a73105b8 1786
a513088d
SE
1787 batadv_tt_save_orig_buffer(bat_priv, orig_node,
1788 (unsigned char *)tt_change, tt_num_changes);
a73105b8
AQ
1789 atomic_set(&orig_node->last_ttvn, ttvn);
1790}
1791
08c36d3e 1792bool batadv_is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
a73105b8 1793{
7683fdc1
AQ
1794 struct tt_local_entry *tt_local_entry = NULL;
1795 bool ret = false;
a73105b8 1796
a513088d 1797 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
7683fdc1
AQ
1798 if (!tt_local_entry)
1799 goto out;
058d0e26 1800 /* Check if the client has been logically deleted (but is kept for
9cfc7bd6
SE
1801 * consistency purpose)
1802 */
48100bac 1803 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
058d0e26 1804 goto out;
7683fdc1
AQ
1805 ret = true;
1806out:
a73105b8 1807 if (tt_local_entry)
a513088d 1808 batadv_tt_local_entry_free_ref(tt_local_entry);
7683fdc1 1809 return ret;
a73105b8
AQ
1810}
1811
08c36d3e
SE
1812void batadv_handle_tt_response(struct bat_priv *bat_priv,
1813 struct tt_query_packet *tt_response)
a73105b8
AQ
1814{
1815 struct tt_req_node *node, *safe;
1816 struct orig_node *orig_node = NULL;
1817
1eda58bf
SE
1818 batadv_dbg(DBG_TT, bat_priv,
1819 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1820 tt_response->src, tt_response->ttvn,
1821 ntohs(tt_response->tt_data),
1822 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
a73105b8 1823
20ff9d59 1824 /* we should have never asked a backbone gw */
08adf151 1825 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
20ff9d59
SW
1826 goto out;
1827
da641193 1828 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
a73105b8
AQ
1829 if (!orig_node)
1830 goto out;
1831
1832 if (tt_response->flags & TT_FULL_TABLE)
a513088d 1833 batadv_tt_fill_gtable(bat_priv, tt_response);
a73105b8 1834 else
a513088d
SE
1835 batadv_tt_update_changes(bat_priv, orig_node,
1836 ntohs(tt_response->tt_data),
1837 tt_response->ttvn,
1838 (struct tt_change *)(tt_response + 1));
a73105b8
AQ
1839
1840 /* Delete the tt_req_node from pending tt_requests list */
1841 spin_lock_bh(&bat_priv->tt_req_list_lock);
1842 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1eda58bf 1843 if (!batadv_compare_eth(node->addr, tt_response->src))
a73105b8
AQ
1844 continue;
1845 list_del(&node->list);
1846 kfree(node);
1847 }
1848 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1849
1850 /* Recalculate the CRC for this orig_node and store it */
a513088d 1851 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
cc47f66e 1852 /* Roaming phase is over: tables are in sync again. I can
9cfc7bd6
SE
1853 * unset the flag
1854 */
cc47f66e 1855 orig_node->tt_poss_change = false;
a73105b8
AQ
1856out:
1857 if (orig_node)
7d211efc 1858 batadv_orig_node_free_ref(orig_node);
a73105b8
AQ
1859}
1860
08c36d3e 1861int batadv_tt_init(struct bat_priv *bat_priv)
a73105b8 1862{
5346c35e 1863 int ret;
a73105b8 1864
a513088d 1865 ret = batadv_tt_local_init(bat_priv);
5346c35e
SE
1866 if (ret < 0)
1867 return ret;
1868
a513088d 1869 ret = batadv_tt_global_init(bat_priv);
5346c35e
SE
1870 if (ret < 0)
1871 return ret;
a73105b8 1872
a513088d 1873 batadv_tt_start_timer(bat_priv);
a73105b8
AQ
1874
1875 return 1;
1876}
1877
a513088d 1878static void batadv_tt_roam_list_free(struct bat_priv *bat_priv)
a73105b8 1879{
cc47f66e 1880 struct tt_roam_node *node, *safe;
a73105b8 1881
cc47f66e 1882 spin_lock_bh(&bat_priv->tt_roam_list_lock);
a73105b8 1883
cc47f66e
AQ
1884 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1885 list_del(&node->list);
1886 kfree(node);
1887 }
1888
1889 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1890}
1891
a513088d 1892static void batadv_tt_roam_purge(struct bat_priv *bat_priv)
cc47f66e
AQ
1893{
1894 struct tt_roam_node *node, *safe;
1895
1896 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1897 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1eda58bf 1898 if (!batadv_has_timed_out(node->first_time, ROAMING_MAX_TIME))
cc47f66e
AQ
1899 continue;
1900
1901 list_del(&node->list);
1902 kfree(node);
1903 }
1904 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1905}
1906
1907/* This function checks whether the client already reached the
1908 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1909 * will not be sent.
1910 *
9cfc7bd6
SE
1911 * returns true if the ROAMING_ADV can be sent, false otherwise
1912 */
a513088d
SE
1913static bool batadv_tt_check_roam_count(struct bat_priv *bat_priv,
1914 uint8_t *client)
cc47f66e
AQ
1915{
1916 struct tt_roam_node *tt_roam_node;
1917 bool ret = false;
1918
1919 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1920 /* The new tt_req will be issued only if I'm not waiting for a
9cfc7bd6
SE
1921 * reply from the same orig_node yet
1922 */
cc47f66e 1923 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1eda58bf 1924 if (!batadv_compare_eth(tt_roam_node->addr, client))
cc47f66e
AQ
1925 continue;
1926
1eda58bf
SE
1927 if (batadv_has_timed_out(tt_roam_node->first_time,
1928 ROAMING_MAX_TIME))
cc47f66e
AQ
1929 continue;
1930
1931 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1932 /* Sorry, you roamed too many times! */
1933 goto unlock;
1934 ret = true;
1935 break;
1936 }
1937
1938 if (!ret) {
1939 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1940 if (!tt_roam_node)
1941 goto unlock;
1942
1943 tt_roam_node->first_time = jiffies;
1944 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1945 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1946
1947 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1948 ret = true;
1949 }
1950
1951unlock:
1952 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1953 return ret;
1954}
1955
a513088d
SE
1956static void batadv_send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1957 struct orig_node *orig_node)
cc47f66e
AQ
1958{
1959 struct neigh_node *neigh_node = NULL;
1960 struct sk_buff *skb = NULL;
1961 struct roam_adv_packet *roam_adv_packet;
1962 int ret = 1;
1963 struct hard_iface *primary_if;
1964
1965 /* before going on we have to check whether the client has
9cfc7bd6
SE
1966 * already roamed to us too many times
1967 */
a513088d 1968 if (!batadv_tt_check_roam_count(bat_priv, client))
cc47f66e
AQ
1969 goto out;
1970
1971 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1972 if (!skb)
1973 goto out;
1974
1975 skb_reserve(skb, ETH_HLEN);
1976
1977 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1978 sizeof(struct roam_adv_packet));
1979
76543d14
SE
1980 roam_adv_packet->header.packet_type = BAT_ROAM_ADV;
1981 roam_adv_packet->header.version = COMPAT_VERSION;
1982 roam_adv_packet->header.ttl = TTL;
e5d89254 1983 primary_if = batadv_primary_if_get_selected(bat_priv);
cc47f66e
AQ
1984 if (!primary_if)
1985 goto out;
1986 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
e5d89254 1987 batadv_hardif_free_ref(primary_if);
cc47f66e
AQ
1988 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1989 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1990
7d211efc 1991 neigh_node = batadv_orig_node_get_router(orig_node);
cc47f66e
AQ
1992 if (!neigh_node)
1993 goto out;
1994
1eda58bf
SE
1995 batadv_dbg(DBG_TT, bat_priv,
1996 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1997 orig_node->orig, client, neigh_node->addr);
cc47f66e 1998
f8214865
MH
1999 batadv_inc_counter(bat_priv, BAT_CNT_TT_ROAM_ADV_TX);
2000
9455e34c 2001 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
cc47f66e
AQ
2002 ret = 0;
2003
2004out:
2005 if (neigh_node)
7d211efc 2006 batadv_neigh_node_free_ref(neigh_node);
cc47f66e
AQ
2007 if (ret)
2008 kfree_skb(skb);
2009 return;
a73105b8
AQ
2010}
2011
a513088d 2012static void batadv_tt_purge(struct work_struct *work)
a73105b8
AQ
2013{
2014 struct delayed_work *delayed_work =
2015 container_of(work, struct delayed_work, work);
2016 struct bat_priv *bat_priv =
2017 container_of(delayed_work, struct bat_priv, tt_work);
2018
a513088d
SE
2019 batadv_tt_local_purge(bat_priv);
2020 batadv_tt_global_roam_purge(bat_priv);
2021 batadv_tt_req_purge(bat_priv);
2022 batadv_tt_roam_purge(bat_priv);
a73105b8 2023
a513088d 2024 batadv_tt_start_timer(bat_priv);
a73105b8 2025}
cc47f66e 2026
08c36d3e 2027void batadv_tt_free(struct bat_priv *bat_priv)
cc47f66e
AQ
2028{
2029 cancel_delayed_work_sync(&bat_priv->tt_work);
2030
a513088d
SE
2031 batadv_tt_local_table_free(bat_priv);
2032 batadv_tt_global_table_free(bat_priv);
2033 batadv_tt_req_list_free(bat_priv);
2034 batadv_tt_changes_list_free(bat_priv);
2035 batadv_tt_roam_list_free(bat_priv);
cc47f66e
AQ
2036
2037 kfree(bat_priv->tt_buff);
2038}
058d0e26 2039
697f2531 2040/* This function will enable or disable the specified flags for all the entries
9cfc7bd6
SE
2041 * in the given hash table and returns the number of modified entries
2042 */
a513088d
SE
2043static uint16_t batadv_tt_set_flags(struct hashtable_t *hash, uint16_t flags,
2044 bool enable)
058d0e26 2045{
c90681b8 2046 uint32_t i;
697f2531 2047 uint16_t changed_num = 0;
058d0e26
AQ
2048 struct hlist_head *head;
2049 struct hlist_node *node;
48100bac 2050 struct tt_common_entry *tt_common_entry;
058d0e26
AQ
2051
2052 if (!hash)
697f2531 2053 goto out;
058d0e26
AQ
2054
2055 for (i = 0; i < hash->size; i++) {
2056 head = &hash->table[i];
2057
2058 rcu_read_lock();
48100bac 2059 hlist_for_each_entry_rcu(tt_common_entry, node,
058d0e26 2060 head, hash_entry) {
697f2531
AQ
2061 if (enable) {
2062 if ((tt_common_entry->flags & flags) == flags)
2063 continue;
2064 tt_common_entry->flags |= flags;
2065 } else {
2066 if (!(tt_common_entry->flags & flags))
2067 continue;
2068 tt_common_entry->flags &= ~flags;
2069 }
2070 changed_num++;
058d0e26
AQ
2071 }
2072 rcu_read_unlock();
2073 }
697f2531
AQ
2074out:
2075 return changed_num;
058d0e26
AQ
2076}
2077
2078/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
a513088d 2079static void batadv_tt_local_purge_pending_clients(struct bat_priv *bat_priv)
058d0e26
AQ
2080{
2081 struct hashtable_t *hash = bat_priv->tt_local_hash;
48100bac 2082 struct tt_common_entry *tt_common_entry;
058d0e26
AQ
2083 struct tt_local_entry *tt_local_entry;
2084 struct hlist_node *node, *node_tmp;
2085 struct hlist_head *head;
2086 spinlock_t *list_lock; /* protects write access to the hash lists */
c90681b8 2087 uint32_t i;
058d0e26
AQ
2088
2089 if (!hash)
2090 return;
2091
2092 for (i = 0; i < hash->size; i++) {
2093 head = &hash->table[i];
2094 list_lock = &hash->list_locks[i];
2095
2096 spin_lock_bh(list_lock);
48100bac 2097 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
058d0e26 2098 head, hash_entry) {
48100bac 2099 if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
058d0e26
AQ
2100 continue;
2101
1eda58bf
SE
2102 batadv_dbg(DBG_TT, bat_priv,
2103 "Deleting local tt entry (%pM): pending\n",
2104 tt_common_entry->addr);
058d0e26
AQ
2105
2106 atomic_dec(&bat_priv->num_local_tt);
2107 hlist_del_rcu(node);
48100bac
AQ
2108 tt_local_entry = container_of(tt_common_entry,
2109 struct tt_local_entry,
2110 common);
a513088d 2111 batadv_tt_local_entry_free_ref(tt_local_entry);
058d0e26
AQ
2112 }
2113 spin_unlock_bh(list_lock);
2114 }
2115
2116}
2117
a513088d
SE
2118static int batadv_tt_commit_changes(struct bat_priv *bat_priv,
2119 unsigned char **packet_buff,
2120 int *packet_buff_len, int packet_min_len)
058d0e26 2121{
be9aa4c1
ML
2122 uint16_t changed_num = 0;
2123
2124 if (atomic_read(&bat_priv->tt_local_changes) < 1)
2125 return -ENOENT;
2126
a513088d
SE
2127 changed_num = batadv_tt_set_flags(bat_priv->tt_local_hash,
2128 TT_CLIENT_NEW, false);
be9aa4c1
ML
2129
2130 /* all reset entries have to be counted as local entries */
697f2531 2131 atomic_add(changed_num, &bat_priv->num_local_tt);
a513088d 2132 batadv_tt_local_purge_pending_clients(bat_priv);
be9aa4c1 2133 bat_priv->tt_crc = batadv_tt_local_crc(bat_priv);
058d0e26
AQ
2134
2135 /* Increment the TTVN only once per OGM interval */
2136 atomic_inc(&bat_priv->ttvn);
1eda58bf
SE
2137 batadv_dbg(DBG_TT, bat_priv,
2138 "Local changes committed, updating to ttvn %u\n",
2139 (uint8_t)atomic_read(&bat_priv->ttvn));
058d0e26 2140 bat_priv->tt_poss_change = false;
be9aa4c1
ML
2141
2142 /* reset the sending counter */
2143 atomic_set(&bat_priv->tt_ogm_append_cnt, TT_OGM_APPEND_MAX);
2144
a513088d
SE
2145 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2146 packet_buff_len, packet_min_len);
be9aa4c1
ML
2147}
2148
2149/* when calling this function (hard_iface == primary_if) has to be true */
2150int batadv_tt_append_diff(struct bat_priv *bat_priv,
2151 unsigned char **packet_buff, int *packet_buff_len,
2152 int packet_min_len)
2153{
2154 int tt_num_changes;
2155
2156 /* if at least one change happened */
a513088d
SE
2157 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2158 packet_buff_len,
2159 packet_min_len);
be9aa4c1
ML
2160
2161 /* if the changes have been sent often enough */
2162 if ((tt_num_changes < 0) &&
2163 (!atomic_dec_not_zero(&bat_priv->tt_ogm_append_cnt))) {
a513088d
SE
2164 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2165 packet_min_len, packet_min_len);
be9aa4c1
ML
2166 tt_num_changes = 0;
2167 }
2168
2169 return tt_num_changes;
058d0e26 2170}
59b699cd 2171
08c36d3e
SE
2172bool batadv_is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src,
2173 uint8_t *dst)
59b699cd
AQ
2174{
2175 struct tt_local_entry *tt_local_entry = NULL;
2176 struct tt_global_entry *tt_global_entry = NULL;
5870adc6 2177 bool ret = false;
59b699cd
AQ
2178
2179 if (!atomic_read(&bat_priv->ap_isolation))
5870adc6 2180 goto out;
59b699cd 2181
a513088d 2182 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
59b699cd
AQ
2183 if (!tt_local_entry)
2184 goto out;
2185
a513088d 2186 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
59b699cd
AQ
2187 if (!tt_global_entry)
2188 goto out;
2189
a513088d 2190 if (_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
59b699cd
AQ
2191 goto out;
2192
5870adc6 2193 ret = true;
59b699cd
AQ
2194
2195out:
2196 if (tt_global_entry)
a513088d 2197 batadv_tt_global_entry_free_ref(tt_global_entry);
59b699cd 2198 if (tt_local_entry)
a513088d 2199 batadv_tt_local_entry_free_ref(tt_local_entry);
59b699cd
AQ
2200 return ret;
2201}
a943cac1 2202
08c36d3e
SE
2203void batadv_tt_update_orig(struct bat_priv *bat_priv,
2204 struct orig_node *orig_node,
2205 const unsigned char *tt_buff, uint8_t tt_num_changes,
2206 uint8_t ttvn, uint16_t tt_crc)
a943cac1
ML
2207{
2208 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2209 bool full_table = true;
2210
20ff9d59 2211 /* don't care about a backbone gateways updates. */
08adf151 2212 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
20ff9d59
SW
2213 return;
2214
17071578 2215 /* orig table not initialised AND first diff is in the OGM OR the ttvn
9cfc7bd6
SE
2216 * increased by one -> we can apply the attached changes
2217 */
17071578
AQ
2218 if ((!orig_node->tt_initialised && ttvn == 1) ||
2219 ttvn - orig_ttvn == 1) {
a943cac1
ML
2220 /* the OGM could not contain the changes due to their size or
2221 * because they have already been sent TT_OGM_APPEND_MAX times.
9cfc7bd6
SE
2222 * In this case send a tt request
2223 */
a943cac1
ML
2224 if (!tt_num_changes) {
2225 full_table = false;
2226 goto request_table;
2227 }
2228
a513088d
SE
2229 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2230 ttvn, (struct tt_change *)tt_buff);
a943cac1
ML
2231
2232 /* Even if we received the precomputed crc with the OGM, we
2233 * prefer to recompute it to spot any possible inconsistency
9cfc7bd6
SE
2234 * in the global table
2235 */
a513088d 2236 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
a943cac1
ML
2237
2238 /* The ttvn alone is not enough to guarantee consistency
2239 * because a single value could represent different states
2240 * (due to the wrap around). Thus a node has to check whether
2241 * the resulting table (after applying the changes) is still
2242 * consistent or not. E.g. a node could disconnect while its
2243 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2244 * checking the CRC value is mandatory to detect the
9cfc7bd6
SE
2245 * inconsistency
2246 */
a943cac1
ML
2247 if (orig_node->tt_crc != tt_crc)
2248 goto request_table;
2249
2250 /* Roaming phase is over: tables are in sync again. I can
9cfc7bd6
SE
2251 * unset the flag
2252 */
a943cac1
ML
2253 orig_node->tt_poss_change = false;
2254 } else {
2255 /* if we missed more than one change or our tables are not
9cfc7bd6
SE
2256 * in sync anymore -> request fresh tt data
2257 */
17071578
AQ
2258 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2259 orig_node->tt_crc != tt_crc) {
a943cac1 2260request_table:
1eda58bf
SE
2261 batadv_dbg(DBG_TT, bat_priv,
2262 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2263 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2264 orig_node->tt_crc, tt_num_changes);
a513088d
SE
2265 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2266 tt_crc, full_table);
a943cac1
ML
2267 return;
2268 }
2269 }
2270}
3275e7cc
AQ
2271
2272/* returns true whether we know that the client has moved from its old
2273 * originator to another one. This entry is kept is still kept for consistency
2274 * purposes
2275 */
08c36d3e
SE
2276bool batadv_tt_global_client_is_roaming(struct bat_priv *bat_priv,
2277 uint8_t *addr)
3275e7cc
AQ
2278{
2279 struct tt_global_entry *tt_global_entry;
2280 bool ret = false;
2281
a513088d 2282 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
3275e7cc
AQ
2283 if (!tt_global_entry)
2284 goto out;
2285
2286 ret = tt_global_entry->common.flags & TT_CLIENT_ROAM;
a513088d 2287 batadv_tt_global_entry_free_ref(tt_global_entry);
3275e7cc
AQ
2288out:
2289 return ret;
2290}