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