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