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