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