batman-adv: update copyright years for 2016
[deliverable/linux.git] / net / batman-adv / translation-table.c
... / ...
CommitLineData
1/* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
2 *
3 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
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, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "translation-table.h"
19#include "main.h"
20
21#include <linux/atomic.h>
22#include <linux/bitops.h>
23#include <linux/bug.h>
24#include <linux/byteorder/generic.h>
25#include <linux/compiler.h>
26#include <linux/crc32c.h>
27#include <linux/errno.h>
28#include <linux/etherdevice.h>
29#include <linux/fs.h>
30#include <linux/if_ether.h>
31#include <linux/jhash.h>
32#include <linux/jiffies.h>
33#include <linux/kernel.h>
34#include <linux/list.h>
35#include <linux/lockdep.h>
36#include <linux/netdevice.h>
37#include <linux/rculist.h>
38#include <linux/rcupdate.h>
39#include <linux/seq_file.h>
40#include <linux/slab.h>
41#include <linux/spinlock.h>
42#include <linux/stddef.h>
43#include <linux/string.h>
44#include <linux/workqueue.h>
45#include <net/net_namespace.h>
46
47#include "bridge_loop_avoidance.h"
48#include "hard-interface.h"
49#include "hash.h"
50#include "multicast.h"
51#include "originator.h"
52#include "packet.h"
53#include "soft-interface.h"
54
55/* hash class keys */
56static struct lock_class_key batadv_tt_local_hash_lock_class_key;
57static struct lock_class_key batadv_tt_global_hash_lock_class_key;
58
59static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
60 unsigned short vid,
61 struct batadv_orig_node *orig_node);
62static void batadv_tt_purge(struct work_struct *work);
63static void
64batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
65static void batadv_tt_global_del(struct batadv_priv *bat_priv,
66 struct batadv_orig_node *orig_node,
67 const unsigned char *addr,
68 unsigned short vid, const char *message,
69 bool roaming);
70
71/**
72 * batadv_compare_tt - check if two TT entries are the same
73 * @node: the list element pointer of the first TT entry
74 * @data2: pointer to the tt_common_entry of the second TT entry
75 *
76 * Compare the MAC address and the VLAN ID of the two TT entries and check if
77 * they are the same TT client.
78 * Return: 1 if the two TT clients are the same, 0 otherwise
79 */
80static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
81{
82 const void *data1 = container_of(node, struct batadv_tt_common_entry,
83 hash_entry);
84 const struct batadv_tt_common_entry *tt1 = data1;
85 const struct batadv_tt_common_entry *tt2 = data2;
86
87 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
88}
89
90/**
91 * batadv_choose_tt - return the index of the tt entry in the hash table
92 * @data: pointer to the tt_common_entry object to map
93 * @size: the size of the hash table
94 *
95 * Return: the hash index where the object represented by 'data' should be
96 * stored at.
97 */
98static inline u32 batadv_choose_tt(const void *data, u32 size)
99{
100 struct batadv_tt_common_entry *tt;
101 u32 hash = 0;
102
103 tt = (struct batadv_tt_common_entry *)data;
104 hash = jhash(&tt->addr, ETH_ALEN, hash);
105 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
106
107 return hash % size;
108}
109
110/**
111 * batadv_tt_hash_find - look for a client in the given hash table
112 * @hash: the hash table to search
113 * @addr: the mac address of the client to look for
114 * @vid: VLAN identifier
115 *
116 * Return: a pointer to the tt_common struct belonging to the searched client if
117 * found, NULL otherwise.
118 */
119static struct batadv_tt_common_entry *
120batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
121 unsigned short vid)
122{
123 struct hlist_head *head;
124 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
125 u32 index;
126
127 if (!hash)
128 return NULL;
129
130 ether_addr_copy(to_search.addr, addr);
131 to_search.vid = vid;
132
133 index = batadv_choose_tt(&to_search, hash->size);
134 head = &hash->table[index];
135
136 rcu_read_lock();
137 hlist_for_each_entry_rcu(tt, head, hash_entry) {
138 if (!batadv_compare_eth(tt, addr))
139 continue;
140
141 if (tt->vid != vid)
142 continue;
143
144 if (!atomic_inc_not_zero(&tt->refcount))
145 continue;
146
147 tt_tmp = tt;
148 break;
149 }
150 rcu_read_unlock();
151
152 return tt_tmp;
153}
154
155/**
156 * batadv_tt_local_hash_find - search the local table for a given client
157 * @bat_priv: the bat priv with all the soft interface information
158 * @addr: the mac address of the client to look for
159 * @vid: VLAN identifier
160 *
161 * Return: a pointer to the corresponding tt_local_entry struct if the client is
162 * found, NULL otherwise.
163 */
164static struct batadv_tt_local_entry *
165batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
166 unsigned short vid)
167{
168 struct batadv_tt_common_entry *tt_common_entry;
169 struct batadv_tt_local_entry *tt_local_entry = NULL;
170
171 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
172 vid);
173 if (tt_common_entry)
174 tt_local_entry = container_of(tt_common_entry,
175 struct batadv_tt_local_entry,
176 common);
177 return tt_local_entry;
178}
179
180/**
181 * batadv_tt_global_hash_find - search the global table for a given client
182 * @bat_priv: the bat priv with all the soft interface information
183 * @addr: the mac address of the client to look for
184 * @vid: VLAN identifier
185 *
186 * Return: a pointer to the corresponding tt_global_entry struct if the client
187 * is found, NULL otherwise.
188 */
189static struct batadv_tt_global_entry *
190batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
191 unsigned short vid)
192{
193 struct batadv_tt_common_entry *tt_common_entry;
194 struct batadv_tt_global_entry *tt_global_entry = NULL;
195
196 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
197 vid);
198 if (tt_common_entry)
199 tt_global_entry = container_of(tt_common_entry,
200 struct batadv_tt_global_entry,
201 common);
202 return tt_global_entry;
203}
204
205static void
206batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
207{
208 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
209 kfree_rcu(tt_local_entry, common.rcu);
210}
211
212/**
213 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
214 * tt_global_entry and possibly free it
215 * @tt_global_entry: the object to free
216 */
217static void
218batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
219{
220 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
221 batadv_tt_global_del_orig_list(tt_global_entry);
222 kfree_rcu(tt_global_entry, common.rcu);
223 }
224}
225
226/**
227 * batadv_tt_global_hash_count - count the number of orig entries
228 * @bat_priv: the bat priv with all the soft interface information
229 * @addr: the mac address of the client to count entries for
230 * @vid: VLAN identifier
231 *
232 * Return: the number of originators advertising the given address/data
233 * (excluding ourself).
234 */
235int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
236 const u8 *addr, unsigned short vid)
237{
238 struct batadv_tt_global_entry *tt_global_entry;
239 int count;
240
241 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
242 if (!tt_global_entry)
243 return 0;
244
245 count = atomic_read(&tt_global_entry->orig_list_count);
246 batadv_tt_global_entry_free_ref(tt_global_entry);
247
248 return count;
249}
250
251/**
252 * batadv_tt_local_size_mod - change the size by v of the local table identified
253 * by vid
254 * @bat_priv: the bat priv with all the soft interface information
255 * @vid: the VLAN identifier of the sub-table to change
256 * @v: the amount to sum to the local table size
257 */
258static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
259 unsigned short vid, int v)
260{
261 struct batadv_softif_vlan *vlan;
262
263 vlan = batadv_softif_vlan_get(bat_priv, vid);
264 if (!vlan)
265 return;
266
267 atomic_add(v, &vlan->tt.num_entries);
268
269 batadv_softif_vlan_free_ref(vlan);
270}
271
272/**
273 * batadv_tt_local_size_inc - increase by one the local table size for the given
274 * vid
275 * @bat_priv: the bat priv with all the soft interface information
276 * @vid: the VLAN identifier
277 */
278static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
279 unsigned short vid)
280{
281 batadv_tt_local_size_mod(bat_priv, vid, 1);
282}
283
284/**
285 * batadv_tt_local_size_dec - decrease by one the local table size for the given
286 * vid
287 * @bat_priv: the bat priv with all the soft interface information
288 * @vid: the VLAN identifier
289 */
290static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
291 unsigned short vid)
292{
293 batadv_tt_local_size_mod(bat_priv, vid, -1);
294}
295
296/**
297 * batadv_tt_global_size_mod - change the size by v of the global table
298 * for orig_node identified by vid
299 * @orig_node: the originator for which the table has to be modified
300 * @vid: the VLAN identifier
301 * @v: the amount to sum to the global table size
302 */
303static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
304 unsigned short vid, int v)
305{
306 struct batadv_orig_node_vlan *vlan;
307
308 vlan = batadv_orig_node_vlan_new(orig_node, vid);
309 if (!vlan)
310 return;
311
312 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
313 spin_lock_bh(&orig_node->vlan_list_lock);
314 hlist_del_init_rcu(&vlan->list);
315 spin_unlock_bh(&orig_node->vlan_list_lock);
316 batadv_orig_node_vlan_free_ref(vlan);
317 }
318
319 batadv_orig_node_vlan_free_ref(vlan);
320}
321
322/**
323 * batadv_tt_global_size_inc - increase by one the global table size for the
324 * given vid
325 * @orig_node: the originator which global table size has to be decreased
326 * @vid: the vlan identifier
327 */
328static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
329 unsigned short vid)
330{
331 batadv_tt_global_size_mod(orig_node, vid, 1);
332}
333
334/**
335 * batadv_tt_global_size_dec - decrease by one the global table size for the
336 * given vid
337 * @orig_node: the originator which global table size has to be decreased
338 * @vid: the vlan identifier
339 */
340static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
341 unsigned short vid)
342{
343 batadv_tt_global_size_mod(orig_node, vid, -1);
344}
345
346/**
347 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
348 * queue for free after rcu grace period
349 * @orig_entry: tt orig entry to be free'd
350 */
351static void
352batadv_tt_orig_list_entry_release(struct batadv_tt_orig_list_entry *orig_entry)
353{
354 batadv_orig_node_free_ref(orig_entry->orig_node);
355 kfree_rcu(orig_entry, rcu);
356}
357
358static void
359batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
360{
361 if (!atomic_dec_and_test(&orig_entry->refcount))
362 return;
363
364 batadv_tt_orig_list_entry_release(orig_entry);
365}
366
367/**
368 * batadv_tt_local_event - store a local TT event (ADD/DEL)
369 * @bat_priv: the bat priv with all the soft interface information
370 * @tt_local_entry: the TT entry involved in the event
371 * @event_flags: flags to store in the event structure
372 */
373static void batadv_tt_local_event(struct batadv_priv *bat_priv,
374 struct batadv_tt_local_entry *tt_local_entry,
375 u8 event_flags)
376{
377 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
378 struct batadv_tt_common_entry *common = &tt_local_entry->common;
379 u8 flags = common->flags | event_flags;
380 bool event_removed = false;
381 bool del_op_requested, del_op_entry;
382
383 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
384 if (!tt_change_node)
385 return;
386
387 tt_change_node->change.flags = flags;
388 memset(tt_change_node->change.reserved, 0,
389 sizeof(tt_change_node->change.reserved));
390 ether_addr_copy(tt_change_node->change.addr, common->addr);
391 tt_change_node->change.vid = htons(common->vid);
392
393 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
394
395 /* check for ADD+DEL or DEL+ADD events */
396 spin_lock_bh(&bat_priv->tt.changes_list_lock);
397 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
398 list) {
399 if (!batadv_compare_eth(entry->change.addr, common->addr))
400 continue;
401
402 /* DEL+ADD in the same orig interval have no effect and can be
403 * removed to avoid silly behaviour on the receiver side. The
404 * other way around (ADD+DEL) can happen in case of roaming of
405 * a client still in the NEW state. Roaming of NEW clients is
406 * now possible due to automatically recognition of "temporary"
407 * clients
408 */
409 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
410 if (!del_op_requested && del_op_entry)
411 goto del;
412 if (del_op_requested && !del_op_entry)
413 goto del;
414
415 /* this is a second add in the same originator interval. It
416 * means that flags have been changed: update them!
417 */
418 if (!del_op_requested && !del_op_entry)
419 entry->change.flags = flags;
420
421 continue;
422del:
423 list_del(&entry->list);
424 kfree(entry);
425 kfree(tt_change_node);
426 event_removed = true;
427 goto unlock;
428 }
429
430 /* track the change in the OGMinterval list */
431 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
432
433unlock:
434 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
435
436 if (event_removed)
437 atomic_dec(&bat_priv->tt.local_changes);
438 else
439 atomic_inc(&bat_priv->tt.local_changes);
440}
441
442/**
443 * batadv_tt_len - compute length in bytes of given number of tt changes
444 * @changes_num: number of tt changes
445 *
446 * Return: computed length in bytes.
447 */
448static int batadv_tt_len(int changes_num)
449{
450 return changes_num * sizeof(struct batadv_tvlv_tt_change);
451}
452
453/**
454 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
455 * @tt_len: available space
456 *
457 * Return: the number of entries.
458 */
459static u16 batadv_tt_entries(u16 tt_len)
460{
461 return tt_len / batadv_tt_len(1);
462}
463
464/**
465 * batadv_tt_local_table_transmit_size - calculates the local translation table
466 * size when transmitted over the air
467 * @bat_priv: the bat priv with all the soft interface information
468 *
469 * Return: local translation table size in bytes.
470 */
471static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
472{
473 u16 num_vlan = 0;
474 u16 tt_local_entries = 0;
475 struct batadv_softif_vlan *vlan;
476 int hdr_size;
477
478 rcu_read_lock();
479 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
480 num_vlan++;
481 tt_local_entries += atomic_read(&vlan->tt.num_entries);
482 }
483 rcu_read_unlock();
484
485 /* header size of tvlv encapsulated tt response payload */
486 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
487 hdr_size += sizeof(struct batadv_tvlv_hdr);
488 hdr_size += sizeof(struct batadv_tvlv_tt_data);
489 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
490
491 return hdr_size + batadv_tt_len(tt_local_entries);
492}
493
494static int batadv_tt_local_init(struct batadv_priv *bat_priv)
495{
496 if (bat_priv->tt.local_hash)
497 return 0;
498
499 bat_priv->tt.local_hash = batadv_hash_new(1024);
500
501 if (!bat_priv->tt.local_hash)
502 return -ENOMEM;
503
504 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
505 &batadv_tt_local_hash_lock_class_key);
506
507 return 0;
508}
509
510static void batadv_tt_global_free(struct batadv_priv *bat_priv,
511 struct batadv_tt_global_entry *tt_global,
512 const char *message)
513{
514 batadv_dbg(BATADV_DBG_TT, bat_priv,
515 "Deleting global tt entry %pM (vid: %d): %s\n",
516 tt_global->common.addr,
517 BATADV_PRINT_VID(tt_global->common.vid), message);
518
519 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
520 batadv_choose_tt, &tt_global->common);
521 batadv_tt_global_entry_free_ref(tt_global);
522}
523
524/**
525 * batadv_tt_local_add - add a new client to the local table or update an
526 * existing client
527 * @soft_iface: netdev struct of the mesh interface
528 * @addr: the mac address of the client to add
529 * @vid: VLAN identifier
530 * @ifindex: index of the interface where the client is connected to (useful to
531 * identify wireless clients)
532 * @mark: the value contained in the skb->mark field of the received packet (if
533 * any)
534 *
535 * Return: true if the client was successfully added, false otherwise.
536 */
537bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
538 unsigned short vid, int ifindex, u32 mark)
539{
540 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
541 struct batadv_tt_local_entry *tt_local;
542 struct batadv_tt_global_entry *tt_global = NULL;
543 struct batadv_softif_vlan *vlan;
544 struct net_device *in_dev = NULL;
545 struct hlist_head *head;
546 struct batadv_tt_orig_list_entry *orig_entry;
547 int hash_added, table_size, packet_size_max;
548 bool ret = false;
549 bool roamed_back = false;
550 u8 remote_flags;
551 u32 match_mark;
552
553 if (ifindex != BATADV_NULL_IFINDEX)
554 in_dev = dev_get_by_index(&init_net, ifindex);
555
556 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
557
558 if (!is_multicast_ether_addr(addr))
559 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
560
561 if (tt_local) {
562 tt_local->last_seen = jiffies;
563 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
564 batadv_dbg(BATADV_DBG_TT, bat_priv,
565 "Re-adding pending client %pM (vid: %d)\n",
566 addr, BATADV_PRINT_VID(vid));
567 /* whatever the reason why the PENDING flag was set,
568 * this is a client which was enqueued to be removed in
569 * this orig_interval. Since it popped up again, the
570 * flag can be reset like it was never enqueued
571 */
572 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
573 goto add_event;
574 }
575
576 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
577 batadv_dbg(BATADV_DBG_TT, bat_priv,
578 "Roaming client %pM (vid: %d) came back to its original location\n",
579 addr, BATADV_PRINT_VID(vid));
580 /* the ROAM flag is set because this client roamed away
581 * and the node got a roaming_advertisement message. Now
582 * that the client popped up again at its original
583 * location such flag can be unset
584 */
585 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
586 roamed_back = true;
587 }
588 goto check_roaming;
589 }
590
591 /* Ignore the client if we cannot send it in a full table response. */
592 table_size = batadv_tt_local_table_transmit_size(bat_priv);
593 table_size += batadv_tt_len(1);
594 packet_size_max = atomic_read(&bat_priv->packet_size_max);
595 if (table_size > packet_size_max) {
596 net_ratelimited_function(batadv_info, soft_iface,
597 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
598 table_size, packet_size_max, addr);
599 goto out;
600 }
601
602 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
603 if (!tt_local)
604 goto out;
605
606 /* increase the refcounter of the related vlan */
607 vlan = batadv_softif_vlan_get(bat_priv, vid);
608 if (WARN(!vlan, "adding TT local entry %pM to non-existent VLAN %d",
609 addr, BATADV_PRINT_VID(vid))) {
610 kfree(tt_local);
611 tt_local = NULL;
612 goto out;
613 }
614
615 batadv_dbg(BATADV_DBG_TT, bat_priv,
616 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
617 addr, BATADV_PRINT_VID(vid),
618 (u8)atomic_read(&bat_priv->tt.vn));
619
620 ether_addr_copy(tt_local->common.addr, addr);
621 /* The local entry has to be marked as NEW to avoid to send it in
622 * a full table response going out before the next ttvn increment
623 * (consistency check)
624 */
625 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
626 tt_local->common.vid = vid;
627 if (batadv_is_wifi_netdev(in_dev))
628 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
629 atomic_set(&tt_local->common.refcount, 2);
630 tt_local->last_seen = jiffies;
631 tt_local->common.added_at = tt_local->last_seen;
632
633 /* the batman interface mac and multicast addresses should never be
634 * purged
635 */
636 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
637 is_multicast_ether_addr(addr))
638 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
639
640 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
641 batadv_choose_tt, &tt_local->common,
642 &tt_local->common.hash_entry);
643
644 if (unlikely(hash_added != 0)) {
645 /* remove the reference for the hash */
646 batadv_tt_local_entry_free_ref(tt_local);
647 batadv_softif_vlan_free_ref(vlan);
648 goto out;
649 }
650
651add_event:
652 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
653
654check_roaming:
655 /* Check whether it is a roaming, but don't do anything if the roaming
656 * process has already been handled
657 */
658 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
659 /* These node are probably going to update their tt table */
660 head = &tt_global->orig_list;
661 rcu_read_lock();
662 hlist_for_each_entry_rcu(orig_entry, head, list) {
663 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
664 tt_global->common.vid,
665 orig_entry->orig_node);
666 }
667 rcu_read_unlock();
668 if (roamed_back) {
669 batadv_tt_global_free(bat_priv, tt_global,
670 "Roaming canceled");
671 tt_global = NULL;
672 } else {
673 /* The global entry has to be marked as ROAMING and
674 * has to be kept for consistency purpose
675 */
676 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
677 tt_global->roam_at = jiffies;
678 }
679 }
680
681 /* store the current remote flags before altering them. This helps
682 * understanding is flags are changing or not
683 */
684 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
685
686 if (batadv_is_wifi_netdev(in_dev))
687 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
688 else
689 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
690
691 /* check the mark in the skb: if it's equal to the configured
692 * isolation_mark, it means the packet is coming from an isolated
693 * non-mesh client
694 */
695 match_mark = (mark & bat_priv->isolation_mark_mask);
696 if (bat_priv->isolation_mark_mask &&
697 match_mark == bat_priv->isolation_mark)
698 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
699 else
700 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
701
702 /* if any "dynamic" flag has been modified, resend an ADD event for this
703 * entry so that all the nodes can get the new flags
704 */
705 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
706 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
707
708 ret = true;
709out:
710 if (in_dev)
711 dev_put(in_dev);
712 if (tt_local)
713 batadv_tt_local_entry_free_ref(tt_local);
714 if (tt_global)
715 batadv_tt_global_entry_free_ref(tt_global);
716 return ret;
717}
718
719/**
720 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
721 * within a TT Response directed to another node
722 * @orig_node: originator for which the TT data has to be prepared
723 * @tt_data: uninitialised pointer to the address of the TVLV buffer
724 * @tt_change: uninitialised pointer to the address of the area where the TT
725 * changed can be stored
726 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
727 * function reserves the amount of space needed to send the entire global TT
728 * table. In case of success the value is updated with the real amount of
729 * reserved bytes
730 * Allocate the needed amount of memory for the entire TT TVLV and write its
731 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
732 * objects, one per active VLAN served by the originator node.
733 *
734 * Return: the size of the allocated buffer or 0 in case of failure.
735 */
736static u16
737batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
738 struct batadv_tvlv_tt_data **tt_data,
739 struct batadv_tvlv_tt_change **tt_change,
740 s32 *tt_len)
741{
742 u16 num_vlan = 0;
743 u16 num_entries = 0;
744 u16 change_offset;
745 u16 tvlv_len;
746 struct batadv_tvlv_tt_vlan_data *tt_vlan;
747 struct batadv_orig_node_vlan *vlan;
748 u8 *tt_change_ptr;
749
750 rcu_read_lock();
751 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
752 num_vlan++;
753 num_entries += atomic_read(&vlan->tt.num_entries);
754 }
755
756 change_offset = sizeof(**tt_data);
757 change_offset += num_vlan * sizeof(*tt_vlan);
758
759 /* if tt_len is negative, allocate the space needed by the full table */
760 if (*tt_len < 0)
761 *tt_len = batadv_tt_len(num_entries);
762
763 tvlv_len = *tt_len;
764 tvlv_len += change_offset;
765
766 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
767 if (!*tt_data) {
768 *tt_len = 0;
769 goto out;
770 }
771
772 (*tt_data)->flags = BATADV_NO_FLAGS;
773 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
774 (*tt_data)->num_vlan = htons(num_vlan);
775
776 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
777 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
778 tt_vlan->vid = htons(vlan->vid);
779 tt_vlan->crc = htonl(vlan->tt.crc);
780
781 tt_vlan++;
782 }
783
784 tt_change_ptr = (u8 *)*tt_data + change_offset;
785 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
786
787out:
788 rcu_read_unlock();
789 return tvlv_len;
790}
791
792/**
793 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
794 * node
795 * @bat_priv: the bat priv with all the soft interface information
796 * @tt_data: uninitialised pointer to the address of the TVLV buffer
797 * @tt_change: uninitialised pointer to the address of the area where the TT
798 * changes can be stored
799 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
800 * function reserves the amount of space needed to send the entire local TT
801 * table. In case of success the value is updated with the real amount of
802 * reserved bytes
803 *
804 * Allocate the needed amount of memory for the entire TT TVLV and write its
805 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
806 * objects, one per active VLAN.
807 *
808 * Return: the size of the allocated buffer or 0 in case of failure.
809 */
810static u16
811batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
812 struct batadv_tvlv_tt_data **tt_data,
813 struct batadv_tvlv_tt_change **tt_change,
814 s32 *tt_len)
815{
816 struct batadv_tvlv_tt_vlan_data *tt_vlan;
817 struct batadv_softif_vlan *vlan;
818 u16 num_vlan = 0;
819 u16 num_entries = 0;
820 u16 tvlv_len;
821 u8 *tt_change_ptr;
822 int change_offset;
823
824 rcu_read_lock();
825 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
826 num_vlan++;
827 num_entries += atomic_read(&vlan->tt.num_entries);
828 }
829
830 change_offset = sizeof(**tt_data);
831 change_offset += num_vlan * sizeof(*tt_vlan);
832
833 /* if tt_len is negative, allocate the space needed by the full table */
834 if (*tt_len < 0)
835 *tt_len = batadv_tt_len(num_entries);
836
837 tvlv_len = *tt_len;
838 tvlv_len += change_offset;
839
840 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
841 if (!*tt_data) {
842 tvlv_len = 0;
843 goto out;
844 }
845
846 (*tt_data)->flags = BATADV_NO_FLAGS;
847 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
848 (*tt_data)->num_vlan = htons(num_vlan);
849
850 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
851 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
852 tt_vlan->vid = htons(vlan->vid);
853 tt_vlan->crc = htonl(vlan->tt.crc);
854
855 tt_vlan++;
856 }
857
858 tt_change_ptr = (u8 *)*tt_data + change_offset;
859 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
860
861out:
862 rcu_read_unlock();
863 return tvlv_len;
864}
865
866/**
867 * batadv_tt_tvlv_container_update - update the translation table tvlv container
868 * after local tt changes have been committed
869 * @bat_priv: the bat priv with all the soft interface information
870 */
871static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
872{
873 struct batadv_tt_change_node *entry, *safe;
874 struct batadv_tvlv_tt_data *tt_data;
875 struct batadv_tvlv_tt_change *tt_change;
876 int tt_diff_len, tt_change_len = 0;
877 int tt_diff_entries_num = 0;
878 int tt_diff_entries_count = 0;
879 u16 tvlv_len;
880
881 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
882 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
883
884 /* if we have too many changes for one packet don't send any
885 * and wait for the tt table request which will be fragmented
886 */
887 if (tt_diff_len > bat_priv->soft_iface->mtu)
888 tt_diff_len = 0;
889
890 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
891 &tt_change, &tt_diff_len);
892 if (!tvlv_len)
893 return;
894
895 tt_data->flags = BATADV_TT_OGM_DIFF;
896
897 if (tt_diff_len == 0)
898 goto container_register;
899
900 spin_lock_bh(&bat_priv->tt.changes_list_lock);
901 atomic_set(&bat_priv->tt.local_changes, 0);
902
903 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
904 list) {
905 if (tt_diff_entries_count < tt_diff_entries_num) {
906 memcpy(tt_change + tt_diff_entries_count,
907 &entry->change,
908 sizeof(struct batadv_tvlv_tt_change));
909 tt_diff_entries_count++;
910 }
911 list_del(&entry->list);
912 kfree(entry);
913 }
914 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
915
916 /* Keep the buffer for possible tt_request */
917 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
918 kfree(bat_priv->tt.last_changeset);
919 bat_priv->tt.last_changeset_len = 0;
920 bat_priv->tt.last_changeset = NULL;
921 tt_change_len = batadv_tt_len(tt_diff_entries_count);
922 /* check whether this new OGM has no changes due to size problems */
923 if (tt_diff_entries_count > 0) {
924 /* if kmalloc() fails we will reply with the full table
925 * instead of providing the diff
926 */
927 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
928 if (bat_priv->tt.last_changeset) {
929 memcpy(bat_priv->tt.last_changeset,
930 tt_change, tt_change_len);
931 bat_priv->tt.last_changeset_len = tt_diff_len;
932 }
933 }
934 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
935
936container_register:
937 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
938 tvlv_len);
939 kfree(tt_data);
940}
941
942int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
943{
944 struct net_device *net_dev = (struct net_device *)seq->private;
945 struct batadv_priv *bat_priv = netdev_priv(net_dev);
946 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
947 struct batadv_tt_common_entry *tt_common_entry;
948 struct batadv_tt_local_entry *tt_local;
949 struct batadv_hard_iface *primary_if;
950 struct batadv_softif_vlan *vlan;
951 struct hlist_head *head;
952 unsigned short vid;
953 u32 i;
954 int last_seen_secs;
955 int last_seen_msecs;
956 unsigned long last_seen_jiffies;
957 bool no_purge;
958 u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
959
960 primary_if = batadv_seq_print_text_primary_if_get(seq);
961 if (!primary_if)
962 goto out;
963
964 seq_printf(seq,
965 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
966 net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
967 seq_printf(seq, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
968 "Flags", "Last seen", "CRC");
969
970 for (i = 0; i < hash->size; i++) {
971 head = &hash->table[i];
972
973 rcu_read_lock();
974 hlist_for_each_entry_rcu(tt_common_entry,
975 head, hash_entry) {
976 tt_local = container_of(tt_common_entry,
977 struct batadv_tt_local_entry,
978 common);
979 vid = tt_common_entry->vid;
980 last_seen_jiffies = jiffies - tt_local->last_seen;
981 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
982 last_seen_secs = last_seen_msecs / 1000;
983 last_seen_msecs = last_seen_msecs % 1000;
984
985 no_purge = tt_common_entry->flags & np_flag;
986
987 vlan = batadv_softif_vlan_get(bat_priv, vid);
988 if (!vlan) {
989 seq_printf(seq, "Cannot retrieve VLAN %d\n",
990 BATADV_PRINT_VID(vid));
991 continue;
992 }
993
994 seq_printf(seq,
995 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
996 tt_common_entry->addr,
997 BATADV_PRINT_VID(tt_common_entry->vid),
998 ((tt_common_entry->flags &
999 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1000 no_purge ? 'P' : '.',
1001 ((tt_common_entry->flags &
1002 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
1003 ((tt_common_entry->flags &
1004 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
1005 ((tt_common_entry->flags &
1006 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1007 ((tt_common_entry->flags &
1008 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1009 no_purge ? 0 : last_seen_secs,
1010 no_purge ? 0 : last_seen_msecs,
1011 vlan->tt.crc);
1012
1013 batadv_softif_vlan_free_ref(vlan);
1014 }
1015 rcu_read_unlock();
1016 }
1017out:
1018 if (primary_if)
1019 batadv_hardif_free_ref(primary_if);
1020 return 0;
1021}
1022
1023static void
1024batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1025 struct batadv_tt_local_entry *tt_local_entry,
1026 u16 flags, const char *message)
1027{
1028 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
1029
1030 /* The local client has to be marked as "pending to be removed" but has
1031 * to be kept in the table in order to send it in a full table
1032 * response issued before the net ttvn increment (consistency check)
1033 */
1034 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
1035
1036 batadv_dbg(BATADV_DBG_TT, bat_priv,
1037 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1038 tt_local_entry->common.addr,
1039 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
1040}
1041
1042/**
1043 * batadv_tt_local_remove - logically remove an entry from the local table
1044 * @bat_priv: the bat priv with all the soft interface information
1045 * @addr: the MAC address of the client to remove
1046 * @vid: VLAN identifier
1047 * @message: message to append to the log on deletion
1048 * @roaming: true if the deletion is due to a roaming event
1049 *
1050 * Return: the flags assigned to the local entry before being deleted
1051 */
1052u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1053 unsigned short vid, const char *message,
1054 bool roaming)
1055{
1056 struct batadv_tt_local_entry *tt_local_entry;
1057 u16 flags, curr_flags = BATADV_NO_FLAGS;
1058 struct batadv_softif_vlan *vlan;
1059 void *tt_entry_exists;
1060
1061 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1062 if (!tt_local_entry)
1063 goto out;
1064
1065 curr_flags = tt_local_entry->common.flags;
1066
1067 flags = BATADV_TT_CLIENT_DEL;
1068 /* if this global entry addition is due to a roaming, the node has to
1069 * mark the local entry as "roamed" in order to correctly reroute
1070 * packets later
1071 */
1072 if (roaming) {
1073 flags |= BATADV_TT_CLIENT_ROAM;
1074 /* mark the local client as ROAMed */
1075 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1076 }
1077
1078 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1079 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1080 message);
1081 goto out;
1082 }
1083 /* if this client has been added right now, it is possible to
1084 * immediately purge it
1085 */
1086 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1087
1088 tt_entry_exists = batadv_hash_remove(bat_priv->tt.local_hash,
1089 batadv_compare_tt,
1090 batadv_choose_tt,
1091 &tt_local_entry->common);
1092 if (!tt_entry_exists)
1093 goto out;
1094
1095 /* extra call to free the local tt entry */
1096 batadv_tt_local_entry_free_ref(tt_local_entry);
1097
1098 /* decrease the reference held for this vlan */
1099 vlan = batadv_softif_vlan_get(bat_priv, vid);
1100 if (!vlan)
1101 goto out;
1102
1103 batadv_softif_vlan_free_ref(vlan);
1104 batadv_softif_vlan_free_ref(vlan);
1105
1106out:
1107 if (tt_local_entry)
1108 batadv_tt_local_entry_free_ref(tt_local_entry);
1109
1110 return curr_flags;
1111}
1112
1113/**
1114 * batadv_tt_local_purge_list - purge inactive tt local entries
1115 * @bat_priv: the bat priv with all the soft interface information
1116 * @head: pointer to the list containing the local tt entries
1117 * @timeout: parameter deciding whether a given tt local entry is considered
1118 * inactive or not
1119 */
1120static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1121 struct hlist_head *head,
1122 int timeout)
1123{
1124 struct batadv_tt_local_entry *tt_local_entry;
1125 struct batadv_tt_common_entry *tt_common_entry;
1126 struct hlist_node *node_tmp;
1127
1128 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1129 hash_entry) {
1130 tt_local_entry = container_of(tt_common_entry,
1131 struct batadv_tt_local_entry,
1132 common);
1133 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1134 continue;
1135
1136 /* entry already marked for deletion */
1137 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1138 continue;
1139
1140 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1141 continue;
1142
1143 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1144 BATADV_TT_CLIENT_DEL, "timed out");
1145 }
1146}
1147
1148/**
1149 * batadv_tt_local_purge - purge inactive tt local entries
1150 * @bat_priv: the bat priv with all the soft interface information
1151 * @timeout: parameter deciding whether a given tt local entry is considered
1152 * inactive or not
1153 */
1154static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1155 int timeout)
1156{
1157 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1158 struct hlist_head *head;
1159 spinlock_t *list_lock; /* protects write access to the hash lists */
1160 u32 i;
1161
1162 for (i = 0; i < hash->size; i++) {
1163 head = &hash->table[i];
1164 list_lock = &hash->list_locks[i];
1165
1166 spin_lock_bh(list_lock);
1167 batadv_tt_local_purge_list(bat_priv, head, timeout);
1168 spin_unlock_bh(list_lock);
1169 }
1170}
1171
1172static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1173{
1174 struct batadv_hashtable *hash;
1175 spinlock_t *list_lock; /* protects write access to the hash lists */
1176 struct batadv_tt_common_entry *tt_common_entry;
1177 struct batadv_tt_local_entry *tt_local;
1178 struct batadv_softif_vlan *vlan;
1179 struct hlist_node *node_tmp;
1180 struct hlist_head *head;
1181 u32 i;
1182
1183 if (!bat_priv->tt.local_hash)
1184 return;
1185
1186 hash = bat_priv->tt.local_hash;
1187
1188 for (i = 0; i < hash->size; i++) {
1189 head = &hash->table[i];
1190 list_lock = &hash->list_locks[i];
1191
1192 spin_lock_bh(list_lock);
1193 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1194 head, hash_entry) {
1195 hlist_del_rcu(&tt_common_entry->hash_entry);
1196 tt_local = container_of(tt_common_entry,
1197 struct batadv_tt_local_entry,
1198 common);
1199
1200 /* decrease the reference held for this vlan */
1201 vlan = batadv_softif_vlan_get(bat_priv,
1202 tt_common_entry->vid);
1203 if (vlan) {
1204 batadv_softif_vlan_free_ref(vlan);
1205 batadv_softif_vlan_free_ref(vlan);
1206 }
1207
1208 batadv_tt_local_entry_free_ref(tt_local);
1209 }
1210 spin_unlock_bh(list_lock);
1211 }
1212
1213 batadv_hash_destroy(hash);
1214
1215 bat_priv->tt.local_hash = NULL;
1216}
1217
1218static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1219{
1220 if (bat_priv->tt.global_hash)
1221 return 0;
1222
1223 bat_priv->tt.global_hash = batadv_hash_new(1024);
1224
1225 if (!bat_priv->tt.global_hash)
1226 return -ENOMEM;
1227
1228 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1229 &batadv_tt_global_hash_lock_class_key);
1230
1231 return 0;
1232}
1233
1234static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1235{
1236 struct batadv_tt_change_node *entry, *safe;
1237
1238 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1239
1240 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1241 list) {
1242 list_del(&entry->list);
1243 kfree(entry);
1244 }
1245
1246 atomic_set(&bat_priv->tt.local_changes, 0);
1247 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1248}
1249
1250/**
1251 * batadv_tt_global_orig_entry_find - find a TT orig_list_entry
1252 * @entry: the TT global entry where the orig_list_entry has to be
1253 * extracted from
1254 * @orig_node: the originator for which the orig_list_entry has to be found
1255 *
1256 * retrieve the orig_tt_list_entry belonging to orig_node from the
1257 * batadv_tt_global_entry list
1258 *
1259 * Return: it with an increased refcounter, NULL if not found
1260 */
1261static struct batadv_tt_orig_list_entry *
1262batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1263 const struct batadv_orig_node *orig_node)
1264{
1265 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1266 const struct hlist_head *head;
1267
1268 rcu_read_lock();
1269 head = &entry->orig_list;
1270 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1271 if (tmp_orig_entry->orig_node != orig_node)
1272 continue;
1273 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1274 continue;
1275
1276 orig_entry = tmp_orig_entry;
1277 break;
1278 }
1279 rcu_read_unlock();
1280
1281 return orig_entry;
1282}
1283
1284/**
1285 * batadv_tt_global_entry_has_orig - check if a TT global entry is also handled
1286 * by a given originator
1287 * @entry: the TT global entry to check
1288 * @orig_node: the originator to search in the list
1289 *
1290 * find out if an orig_node is already in the list of a tt_global_entry.
1291 *
1292 * Return: true if found, false otherwise
1293 */
1294static bool
1295batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1296 const struct batadv_orig_node *orig_node)
1297{
1298 struct batadv_tt_orig_list_entry *orig_entry;
1299 bool found = false;
1300
1301 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1302 if (orig_entry) {
1303 found = true;
1304 batadv_tt_orig_list_entry_free_ref(orig_entry);
1305 }
1306
1307 return found;
1308}
1309
1310static void
1311batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1312 struct batadv_orig_node *orig_node, int ttvn)
1313{
1314 struct batadv_tt_orig_list_entry *orig_entry;
1315
1316 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1317 if (orig_entry) {
1318 /* refresh the ttvn: the current value could be a bogus one that
1319 * was added during a "temporary client detection"
1320 */
1321 orig_entry->ttvn = ttvn;
1322 goto out;
1323 }
1324
1325 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1326 if (!orig_entry)
1327 goto out;
1328
1329 INIT_HLIST_NODE(&orig_entry->list);
1330 atomic_inc(&orig_node->refcount);
1331 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1332 orig_entry->orig_node = orig_node;
1333 orig_entry->ttvn = ttvn;
1334 atomic_set(&orig_entry->refcount, 2);
1335
1336 spin_lock_bh(&tt_global->list_lock);
1337 hlist_add_head_rcu(&orig_entry->list,
1338 &tt_global->orig_list);
1339 spin_unlock_bh(&tt_global->list_lock);
1340 atomic_inc(&tt_global->orig_list_count);
1341
1342out:
1343 if (orig_entry)
1344 batadv_tt_orig_list_entry_free_ref(orig_entry);
1345}
1346
1347/**
1348 * batadv_tt_global_add - add a new TT global entry or update an existing one
1349 * @bat_priv: the bat priv with all the soft interface information
1350 * @orig_node: the originator announcing the client
1351 * @tt_addr: the mac address of the non-mesh client
1352 * @vid: VLAN identifier
1353 * @flags: TT flags that have to be set for this non-mesh client
1354 * @ttvn: the tt version number ever announcing this non-mesh client
1355 *
1356 * Add a new TT global entry for the given originator. If the entry already
1357 * exists add a new reference to the given originator (a global entry can have
1358 * references to multiple originators) and adjust the flags attribute to reflect
1359 * the function argument.
1360 * If a TT local entry exists for this non-mesh client remove it.
1361 *
1362 * The caller must hold orig_node refcount.
1363 *
1364 * Return: true if the new entry has been added, false otherwise
1365 */
1366static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1367 struct batadv_orig_node *orig_node,
1368 const unsigned char *tt_addr,
1369 unsigned short vid, u16 flags, u8 ttvn)
1370{
1371 struct batadv_tt_global_entry *tt_global_entry;
1372 struct batadv_tt_local_entry *tt_local_entry;
1373 bool ret = false;
1374 int hash_added;
1375 struct batadv_tt_common_entry *common;
1376 u16 local_flags;
1377
1378 /* ignore global entries from backbone nodes */
1379 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1380 return true;
1381
1382 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1383 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1384
1385 /* if the node already has a local client for this entry, it has to wait
1386 * for a roaming advertisement instead of manually messing up the global
1387 * table
1388 */
1389 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1390 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1391 goto out;
1392
1393 if (!tt_global_entry) {
1394 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
1395 if (!tt_global_entry)
1396 goto out;
1397
1398 common = &tt_global_entry->common;
1399 ether_addr_copy(common->addr, tt_addr);
1400 common->vid = vid;
1401
1402 common->flags = flags;
1403 tt_global_entry->roam_at = 0;
1404 /* node must store current time in case of roaming. This is
1405 * needed to purge this entry out on timeout (if nobody claims
1406 * it)
1407 */
1408 if (flags & BATADV_TT_CLIENT_ROAM)
1409 tt_global_entry->roam_at = jiffies;
1410 atomic_set(&common->refcount, 2);
1411 common->added_at = jiffies;
1412
1413 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1414 atomic_set(&tt_global_entry->orig_list_count, 0);
1415 spin_lock_init(&tt_global_entry->list_lock);
1416
1417 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1418 batadv_compare_tt,
1419 batadv_choose_tt, common,
1420 &common->hash_entry);
1421
1422 if (unlikely(hash_added != 0)) {
1423 /* remove the reference for the hash */
1424 batadv_tt_global_entry_free_ref(tt_global_entry);
1425 goto out_remove;
1426 }
1427 } else {
1428 common = &tt_global_entry->common;
1429 /* If there is already a global entry, we can use this one for
1430 * our processing.
1431 * But if we are trying to add a temporary client then here are
1432 * two options at this point:
1433 * 1) the global client is not a temporary client: the global
1434 * client has to be left as it is, temporary information
1435 * should never override any already known client state
1436 * 2) the global client is a temporary client: purge the
1437 * originator list and add the new one orig_entry
1438 */
1439 if (flags & BATADV_TT_CLIENT_TEMP) {
1440 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1441 goto out;
1442 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1443 orig_node))
1444 goto out_remove;
1445 batadv_tt_global_del_orig_list(tt_global_entry);
1446 goto add_orig_entry;
1447 }
1448
1449 /* if the client was temporary added before receiving the first
1450 * OGM announcing it, we have to clear the TEMP flag. Also,
1451 * remove the previous temporary orig node and re-add it
1452 * if required. If the orig entry changed, the new one which
1453 * is a non-temporary entry is preferred.
1454 */
1455 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1456 batadv_tt_global_del_orig_list(tt_global_entry);
1457 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1458 }
1459
1460 /* the change can carry possible "attribute" flags like the
1461 * TT_CLIENT_WIFI, therefore they have to be copied in the
1462 * client entry
1463 */
1464 common->flags |= flags;
1465
1466 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1467 * one originator left in the list and we previously received a
1468 * delete + roaming change for this originator.
1469 *
1470 * We should first delete the old originator before adding the
1471 * new one.
1472 */
1473 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1474 batadv_tt_global_del_orig_list(tt_global_entry);
1475 common->flags &= ~BATADV_TT_CLIENT_ROAM;
1476 tt_global_entry->roam_at = 0;
1477 }
1478 }
1479add_orig_entry:
1480 /* add the new orig_entry (if needed) or update it */
1481 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
1482
1483 batadv_dbg(BATADV_DBG_TT, bat_priv,
1484 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1485 common->addr, BATADV_PRINT_VID(common->vid),
1486 orig_node->orig);
1487 ret = true;
1488
1489out_remove:
1490 /* Do not remove multicast addresses from the local hash on
1491 * global additions
1492 */
1493 if (is_multicast_ether_addr(tt_addr))
1494 goto out;
1495
1496 /* remove address from local hash if present */
1497 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1498 "global tt received",
1499 flags & BATADV_TT_CLIENT_ROAM);
1500 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1501
1502 if (!(flags & BATADV_TT_CLIENT_ROAM))
1503 /* this is a normal global add. Therefore the client is not in a
1504 * roaming state anymore.
1505 */
1506 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1507
1508out:
1509 if (tt_global_entry)
1510 batadv_tt_global_entry_free_ref(tt_global_entry);
1511 if (tt_local_entry)
1512 batadv_tt_local_entry_free_ref(tt_local_entry);
1513 return ret;
1514}
1515
1516/**
1517 * batadv_transtable_best_orig - Get best originator list entry from tt entry
1518 * @bat_priv: the bat priv with all the soft interface information
1519 * @tt_global_entry: global translation table entry to be analyzed
1520 *
1521 * This functon assumes the caller holds rcu_read_lock().
1522 * Return: best originator list entry or NULL on errors.
1523 */
1524static struct batadv_tt_orig_list_entry *
1525batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1526 struct batadv_tt_global_entry *tt_global_entry)
1527{
1528 struct batadv_neigh_node *router, *best_router = NULL;
1529 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1530 struct hlist_head *head;
1531 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1532
1533 head = &tt_global_entry->orig_list;
1534 hlist_for_each_entry_rcu(orig_entry, head, list) {
1535 router = batadv_orig_router_get(orig_entry->orig_node,
1536 BATADV_IF_DEFAULT);
1537 if (!router)
1538 continue;
1539
1540 if (best_router &&
1541 bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
1542 best_router, BATADV_IF_DEFAULT) <= 0) {
1543 batadv_neigh_node_free_ref(router);
1544 continue;
1545 }
1546
1547 /* release the refcount for the "old" best */
1548 if (best_router)
1549 batadv_neigh_node_free_ref(best_router);
1550
1551 best_entry = orig_entry;
1552 best_router = router;
1553 }
1554
1555 if (best_router)
1556 batadv_neigh_node_free_ref(best_router);
1557
1558 return best_entry;
1559}
1560
1561/**
1562 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1563 * for this global entry
1564 * @bat_priv: the bat priv with all the soft interface information
1565 * @tt_global_entry: global translation table entry to be printed
1566 * @seq: debugfs table seq_file struct
1567 *
1568 * This functon assumes the caller holds rcu_read_lock().
1569 */
1570static void
1571batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1572 struct batadv_tt_global_entry *tt_global_entry,
1573 struct seq_file *seq)
1574{
1575 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1576 struct batadv_tt_common_entry *tt_common_entry;
1577 struct batadv_orig_node_vlan *vlan;
1578 struct hlist_head *head;
1579 u8 last_ttvn;
1580 u16 flags;
1581
1582 tt_common_entry = &tt_global_entry->common;
1583 flags = tt_common_entry->flags;
1584
1585 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1586 if (best_entry) {
1587 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1588 tt_common_entry->vid);
1589 if (!vlan) {
1590 seq_printf(seq,
1591 " * Cannot retrieve VLAN %d for originator %pM\n",
1592 BATADV_PRINT_VID(tt_common_entry->vid),
1593 best_entry->orig_node->orig);
1594 goto print_list;
1595 }
1596
1597 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1598 seq_printf(seq,
1599 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1600 '*', tt_global_entry->common.addr,
1601 BATADV_PRINT_VID(tt_global_entry->common.vid),
1602 best_entry->ttvn, best_entry->orig_node->orig,
1603 last_ttvn, vlan->tt.crc,
1604 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1605 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1606 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1607 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1608
1609 batadv_orig_node_vlan_free_ref(vlan);
1610 }
1611
1612print_list:
1613 head = &tt_global_entry->orig_list;
1614
1615 hlist_for_each_entry_rcu(orig_entry, head, list) {
1616 if (best_entry == orig_entry)
1617 continue;
1618
1619 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1620 tt_common_entry->vid);
1621 if (!vlan) {
1622 seq_printf(seq,
1623 " + Cannot retrieve VLAN %d for originator %pM\n",
1624 BATADV_PRINT_VID(tt_common_entry->vid),
1625 orig_entry->orig_node->orig);
1626 continue;
1627 }
1628
1629 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1630 seq_printf(seq,
1631 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1632 '+', tt_global_entry->common.addr,
1633 BATADV_PRINT_VID(tt_global_entry->common.vid),
1634 orig_entry->ttvn, orig_entry->orig_node->orig,
1635 last_ttvn, vlan->tt.crc,
1636 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1637 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1638 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1639 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1640
1641 batadv_orig_node_vlan_free_ref(vlan);
1642 }
1643}
1644
1645int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1646{
1647 struct net_device *net_dev = (struct net_device *)seq->private;
1648 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1649 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1650 struct batadv_tt_common_entry *tt_common_entry;
1651 struct batadv_tt_global_entry *tt_global;
1652 struct batadv_hard_iface *primary_if;
1653 struct hlist_head *head;
1654 u32 i;
1655
1656 primary_if = batadv_seq_print_text_primary_if_get(seq);
1657 if (!primary_if)
1658 goto out;
1659
1660 seq_printf(seq,
1661 "Globally announced TT entries received via the mesh %s\n",
1662 net_dev->name);
1663 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1664 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1665 "CRC", "Flags");
1666
1667 for (i = 0; i < hash->size; i++) {
1668 head = &hash->table[i];
1669
1670 rcu_read_lock();
1671 hlist_for_each_entry_rcu(tt_common_entry,
1672 head, hash_entry) {
1673 tt_global = container_of(tt_common_entry,
1674 struct batadv_tt_global_entry,
1675 common);
1676 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
1677 }
1678 rcu_read_unlock();
1679 }
1680out:
1681 if (primary_if)
1682 batadv_hardif_free_ref(primary_if);
1683 return 0;
1684}
1685
1686/**
1687 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
1688 * @tt_global_entry: the global entry to remove the orig_entry from
1689 * @orig_entry: the orig entry to remove and free
1690 *
1691 * Remove an orig_entry from its list in the given tt_global_entry and
1692 * free this orig_entry afterwards.
1693 *
1694 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
1695 * part of a list.
1696 */
1697static void
1698_batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1699 struct batadv_tt_orig_list_entry *orig_entry)
1700{
1701 lockdep_assert_held(&tt_global_entry->list_lock);
1702
1703 batadv_tt_global_size_dec(orig_entry->orig_node,
1704 tt_global_entry->common.vid);
1705 atomic_dec(&tt_global_entry->orig_list_count);
1706 /* requires holding tt_global_entry->list_lock and orig_entry->list
1707 * being part of a list
1708 */
1709 hlist_del_rcu(&orig_entry->list);
1710 batadv_tt_orig_list_entry_free_ref(orig_entry);
1711}
1712
1713/* deletes the orig list of a tt_global_entry */
1714static void
1715batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1716{
1717 struct hlist_head *head;
1718 struct hlist_node *safe;
1719 struct batadv_tt_orig_list_entry *orig_entry;
1720
1721 spin_lock_bh(&tt_global_entry->list_lock);
1722 head = &tt_global_entry->orig_list;
1723 hlist_for_each_entry_safe(orig_entry, safe, head, list)
1724 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
1725 spin_unlock_bh(&tt_global_entry->list_lock);
1726}
1727
1728/**
1729 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1730 * @bat_priv: the bat priv with all the soft interface information
1731 * @tt_global_entry: the global entry to remove the orig_node from
1732 * @orig_node: the originator announcing the client
1733 * @message: message to append to the log on deletion
1734 *
1735 * Remove the given orig_node and its according orig_entry from the given
1736 * global tt entry.
1737 */
1738static void
1739batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1740 struct batadv_tt_global_entry *tt_global_entry,
1741 struct batadv_orig_node *orig_node,
1742 const char *message)
1743{
1744 struct hlist_head *head;
1745 struct hlist_node *safe;
1746 struct batadv_tt_orig_list_entry *orig_entry;
1747 unsigned short vid;
1748
1749 spin_lock_bh(&tt_global_entry->list_lock);
1750 head = &tt_global_entry->orig_list;
1751 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1752 if (orig_entry->orig_node == orig_node) {
1753 vid = tt_global_entry->common.vid;
1754 batadv_dbg(BATADV_DBG_TT, bat_priv,
1755 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1756 orig_node->orig,
1757 tt_global_entry->common.addr,
1758 BATADV_PRINT_VID(vid), message);
1759 _batadv_tt_global_del_orig_entry(tt_global_entry,
1760 orig_entry);
1761 }
1762 }
1763 spin_unlock_bh(&tt_global_entry->list_lock);
1764}
1765
1766/* If the client is to be deleted, we check if it is the last origantor entry
1767 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1768 * timer, otherwise we simply remove the originator scheduled for deletion.
1769 */
1770static void
1771batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1772 struct batadv_tt_global_entry *tt_global_entry,
1773 struct batadv_orig_node *orig_node,
1774 const char *message)
1775{
1776 bool last_entry = true;
1777 struct hlist_head *head;
1778 struct batadv_tt_orig_list_entry *orig_entry;
1779
1780 /* no local entry exists, case 1:
1781 * Check if this is the last one or if other entries exist.
1782 */
1783
1784 rcu_read_lock();
1785 head = &tt_global_entry->orig_list;
1786 hlist_for_each_entry_rcu(orig_entry, head, list) {
1787 if (orig_entry->orig_node != orig_node) {
1788 last_entry = false;
1789 break;
1790 }
1791 }
1792 rcu_read_unlock();
1793
1794 if (last_entry) {
1795 /* its the last one, mark for roaming. */
1796 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1797 tt_global_entry->roam_at = jiffies;
1798 } else
1799 /* there is another entry, we can simply delete this
1800 * one and can still use the other one.
1801 */
1802 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1803 orig_node, message);
1804}
1805
1806/**
1807 * batadv_tt_global_del - remove a client from the global table
1808 * @bat_priv: the bat priv with all the soft interface information
1809 * @orig_node: an originator serving this client
1810 * @addr: the mac address of the client
1811 * @vid: VLAN identifier
1812 * @message: a message explaining the reason for deleting the client to print
1813 * for debugging purpose
1814 * @roaming: true if the deletion has been triggered by a roaming event
1815 */
1816static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1817 struct batadv_orig_node *orig_node,
1818 const unsigned char *addr, unsigned short vid,
1819 const char *message, bool roaming)
1820{
1821 struct batadv_tt_global_entry *tt_global_entry;
1822 struct batadv_tt_local_entry *local_entry = NULL;
1823
1824 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1825 if (!tt_global_entry)
1826 goto out;
1827
1828 if (!roaming) {
1829 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1830 orig_node, message);
1831
1832 if (hlist_empty(&tt_global_entry->orig_list))
1833 batadv_tt_global_free(bat_priv, tt_global_entry,
1834 message);
1835
1836 goto out;
1837 }
1838
1839 /* if we are deleting a global entry due to a roam
1840 * event, there are two possibilities:
1841 * 1) the client roamed from node A to node B => if there
1842 * is only one originator left for this client, we mark
1843 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1844 * wait for node B to claim it. In case of timeout
1845 * the entry is purged.
1846 *
1847 * If there are other originators left, we directly delete
1848 * the originator.
1849 * 2) the client roamed to us => we can directly delete
1850 * the global entry, since it is useless now.
1851 */
1852 local_entry = batadv_tt_local_hash_find(bat_priv,
1853 tt_global_entry->common.addr,
1854 vid);
1855 if (local_entry) {
1856 /* local entry exists, case 2: client roamed to us. */
1857 batadv_tt_global_del_orig_list(tt_global_entry);
1858 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1859 } else
1860 /* no local entry exists, case 1: check for roaming */
1861 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1862 orig_node, message);
1863
1864out:
1865 if (tt_global_entry)
1866 batadv_tt_global_entry_free_ref(tt_global_entry);
1867 if (local_entry)
1868 batadv_tt_local_entry_free_ref(local_entry);
1869}
1870
1871/**
1872 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1873 * given originator matching the provided vid
1874 * @bat_priv: the bat priv with all the soft interface information
1875 * @orig_node: the originator owning the entries to remove
1876 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1877 * removed
1878 * @message: debug message to print as "reason"
1879 */
1880void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1881 struct batadv_orig_node *orig_node,
1882 s32 match_vid,
1883 const char *message)
1884{
1885 struct batadv_tt_global_entry *tt_global;
1886 struct batadv_tt_common_entry *tt_common_entry;
1887 u32 i;
1888 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1889 struct hlist_node *safe;
1890 struct hlist_head *head;
1891 spinlock_t *list_lock; /* protects write access to the hash lists */
1892 unsigned short vid;
1893
1894 if (!hash)
1895 return;
1896
1897 for (i = 0; i < hash->size; i++) {
1898 head = &hash->table[i];
1899 list_lock = &hash->list_locks[i];
1900
1901 spin_lock_bh(list_lock);
1902 hlist_for_each_entry_safe(tt_common_entry, safe,
1903 head, hash_entry) {
1904 /* remove only matching entries */
1905 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1906 continue;
1907
1908 tt_global = container_of(tt_common_entry,
1909 struct batadv_tt_global_entry,
1910 common);
1911
1912 batadv_tt_global_del_orig_node(bat_priv, tt_global,
1913 orig_node, message);
1914
1915 if (hlist_empty(&tt_global->orig_list)) {
1916 vid = tt_global->common.vid;
1917 batadv_dbg(BATADV_DBG_TT, bat_priv,
1918 "Deleting global tt entry %pM (vid: %d): %s\n",
1919 tt_global->common.addr,
1920 BATADV_PRINT_VID(vid), message);
1921 hlist_del_rcu(&tt_common_entry->hash_entry);
1922 batadv_tt_global_entry_free_ref(tt_global);
1923 }
1924 }
1925 spin_unlock_bh(list_lock);
1926 }
1927 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
1928}
1929
1930static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1931 char **msg)
1932{
1933 bool purge = false;
1934 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1935 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1936
1937 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1938 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1939 purge = true;
1940 *msg = "Roaming timeout\n";
1941 }
1942
1943 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1944 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1945 purge = true;
1946 *msg = "Temporary client timeout\n";
1947 }
1948
1949 return purge;
1950}
1951
1952static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1953{
1954 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1955 struct hlist_head *head;
1956 struct hlist_node *node_tmp;
1957 spinlock_t *list_lock; /* protects write access to the hash lists */
1958 u32 i;
1959 char *msg = NULL;
1960 struct batadv_tt_common_entry *tt_common;
1961 struct batadv_tt_global_entry *tt_global;
1962
1963 for (i = 0; i < hash->size; i++) {
1964 head = &hash->table[i];
1965 list_lock = &hash->list_locks[i];
1966
1967 spin_lock_bh(list_lock);
1968 hlist_for_each_entry_safe(tt_common, node_tmp, head,
1969 hash_entry) {
1970 tt_global = container_of(tt_common,
1971 struct batadv_tt_global_entry,
1972 common);
1973
1974 if (!batadv_tt_global_to_purge(tt_global, &msg))
1975 continue;
1976
1977 batadv_dbg(BATADV_DBG_TT, bat_priv,
1978 "Deleting global tt entry %pM (vid: %d): %s\n",
1979 tt_global->common.addr,
1980 BATADV_PRINT_VID(tt_global->common.vid),
1981 msg);
1982
1983 hlist_del_rcu(&tt_common->hash_entry);
1984
1985 batadv_tt_global_entry_free_ref(tt_global);
1986 }
1987 spin_unlock_bh(list_lock);
1988 }
1989}
1990
1991static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1992{
1993 struct batadv_hashtable *hash;
1994 spinlock_t *list_lock; /* protects write access to the hash lists */
1995 struct batadv_tt_common_entry *tt_common_entry;
1996 struct batadv_tt_global_entry *tt_global;
1997 struct hlist_node *node_tmp;
1998 struct hlist_head *head;
1999 u32 i;
2000
2001 if (!bat_priv->tt.global_hash)
2002 return;
2003
2004 hash = bat_priv->tt.global_hash;
2005
2006 for (i = 0; i < hash->size; i++) {
2007 head = &hash->table[i];
2008 list_lock = &hash->list_locks[i];
2009
2010 spin_lock_bh(list_lock);
2011 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
2012 head, hash_entry) {
2013 hlist_del_rcu(&tt_common_entry->hash_entry);
2014 tt_global = container_of(tt_common_entry,
2015 struct batadv_tt_global_entry,
2016 common);
2017 batadv_tt_global_entry_free_ref(tt_global);
2018 }
2019 spin_unlock_bh(list_lock);
2020 }
2021
2022 batadv_hash_destroy(hash);
2023
2024 bat_priv->tt.global_hash = NULL;
2025}
2026
2027static bool
2028_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2029 struct batadv_tt_global_entry *tt_global_entry)
2030{
2031 bool ret = false;
2032
2033 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2034 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2035 ret = true;
2036
2037 /* check if the two clients are marked as isolated */
2038 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2039 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2040 ret = true;
2041
2042 return ret;
2043}
2044
2045/**
2046 * batadv_transtable_search - get the mesh destination for a given client
2047 * @bat_priv: the bat priv with all the soft interface information
2048 * @src: mac address of the source client
2049 * @addr: mac address of the destination client
2050 * @vid: VLAN identifier
2051 *
2052 * Return: a pointer to the originator that was selected as destination in the
2053 * mesh for contacting the client 'addr', NULL otherwise.
2054 * In case of multiple originators serving the same client, the function returns
2055 * the best one (best in terms of metric towards the destination node).
2056 *
2057 * If the two clients are AP isolated the function returns NULL.
2058 */
2059struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2060 const u8 *src,
2061 const u8 *addr,
2062 unsigned short vid)
2063{
2064 struct batadv_tt_local_entry *tt_local_entry = NULL;
2065 struct batadv_tt_global_entry *tt_global_entry = NULL;
2066 struct batadv_orig_node *orig_node = NULL;
2067 struct batadv_tt_orig_list_entry *best_entry;
2068
2069 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2070 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2071 if (!tt_local_entry ||
2072 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2073 goto out;
2074 }
2075
2076 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2077 if (!tt_global_entry)
2078 goto out;
2079
2080 /* check whether the clients should not communicate due to AP
2081 * isolation
2082 */
2083 if (tt_local_entry &&
2084 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2085 goto out;
2086
2087 rcu_read_lock();
2088 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2089 /* found anything? */
2090 if (best_entry)
2091 orig_node = best_entry->orig_node;
2092 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
2093 orig_node = NULL;
2094 rcu_read_unlock();
2095
2096out:
2097 if (tt_global_entry)
2098 batadv_tt_global_entry_free_ref(tt_global_entry);
2099 if (tt_local_entry)
2100 batadv_tt_local_entry_free_ref(tt_local_entry);
2101
2102 return orig_node;
2103}
2104
2105/**
2106 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2107 * to the given orig_node
2108 * @bat_priv: the bat priv with all the soft interface information
2109 * @orig_node: originator for which the CRC should be computed
2110 * @vid: VLAN identifier for which the CRC32 has to be computed
2111 *
2112 * This function computes the checksum for the global table corresponding to a
2113 * specific originator. In particular, the checksum is computed as follows: For
2114 * each client connected to the originator the CRC32C of the MAC address and the
2115 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2116 * together.
2117 *
2118 * The idea behind is that CRC32C should be used as much as possible in order to
2119 * produce a unique hash of the table, but since the order which is used to feed
2120 * the CRC32C function affects the result and since every node in the network
2121 * probably sorts the clients differently, the hash function cannot be directly
2122 * computed over the entire table. Hence the CRC32C is used only on
2123 * the single client entry, while all the results are then xor'ed together
2124 * because the XOR operation can combine them all while trying to reduce the
2125 * noise as much as possible.
2126 *
2127 * Return: the checksum of the global table of a given originator.
2128 */
2129static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2130 struct batadv_orig_node *orig_node,
2131 unsigned short vid)
2132{
2133 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2134 struct batadv_tt_common_entry *tt_common;
2135 struct batadv_tt_global_entry *tt_global;
2136 struct hlist_head *head;
2137 u32 i, crc_tmp, crc = 0;
2138 u8 flags;
2139 __be16 tmp_vid;
2140
2141 for (i = 0; i < hash->size; i++) {
2142 head = &hash->table[i];
2143
2144 rcu_read_lock();
2145 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2146 tt_global = container_of(tt_common,
2147 struct batadv_tt_global_entry,
2148 common);
2149 /* compute the CRC only for entries belonging to the
2150 * VLAN identified by the vid passed as parameter
2151 */
2152 if (tt_common->vid != vid)
2153 continue;
2154
2155 /* Roaming clients are in the global table for
2156 * consistency only. They don't have to be
2157 * taken into account while computing the
2158 * global crc
2159 */
2160 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2161 continue;
2162 /* Temporary clients have not been announced yet, so
2163 * they have to be skipped while computing the global
2164 * crc
2165 */
2166 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2167 continue;
2168
2169 /* find out if this global entry is announced by this
2170 * originator
2171 */
2172 if (!batadv_tt_global_entry_has_orig(tt_global,
2173 orig_node))
2174 continue;
2175
2176 /* use network order to read the VID: this ensures that
2177 * every node reads the bytes in the same order.
2178 */
2179 tmp_vid = htons(tt_common->vid);
2180 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2181
2182 /* compute the CRC on flags that have to be kept in sync
2183 * among nodes
2184 */
2185 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2186 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2187
2188 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2189 }
2190 rcu_read_unlock();
2191 }
2192
2193 return crc;
2194}
2195
2196/**
2197 * batadv_tt_local_crc - calculates the checksum of the local table
2198 * @bat_priv: the bat priv with all the soft interface information
2199 * @vid: VLAN identifier for which the CRC32 has to be computed
2200 *
2201 * For details about the computation, please refer to the documentation for
2202 * batadv_tt_global_crc().
2203 *
2204 * Return: the checksum of the local table
2205 */
2206static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2207 unsigned short vid)
2208{
2209 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2210 struct batadv_tt_common_entry *tt_common;
2211 struct hlist_head *head;
2212 u32 i, crc_tmp, crc = 0;
2213 u8 flags;
2214 __be16 tmp_vid;
2215
2216 for (i = 0; i < hash->size; i++) {
2217 head = &hash->table[i];
2218
2219 rcu_read_lock();
2220 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2221 /* compute the CRC only for entries belonging to the
2222 * VLAN identified by vid
2223 */
2224 if (tt_common->vid != vid)
2225 continue;
2226
2227 /* not yet committed clients have not to be taken into
2228 * account while computing the CRC
2229 */
2230 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2231 continue;
2232
2233 /* use network order to read the VID: this ensures that
2234 * every node reads the bytes in the same order.
2235 */
2236 tmp_vid = htons(tt_common->vid);
2237 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2238
2239 /* compute the CRC on flags that have to be kept in sync
2240 * among nodes
2241 */
2242 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2243 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2244
2245 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2246 }
2247 rcu_read_unlock();
2248 }
2249
2250 return crc;
2251}
2252
2253static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2254{
2255 struct batadv_tt_req_node *node;
2256 struct hlist_node *safe;
2257
2258 spin_lock_bh(&bat_priv->tt.req_list_lock);
2259
2260 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2261 hlist_del_init(&node->list);
2262 kfree(node);
2263 }
2264
2265 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2266}
2267
2268static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2269 struct batadv_orig_node *orig_node,
2270 const void *tt_buff,
2271 u16 tt_buff_len)
2272{
2273 /* Replace the old buffer only if I received something in the
2274 * last OGM (the OGM could carry no changes)
2275 */
2276 spin_lock_bh(&orig_node->tt_buff_lock);
2277 if (tt_buff_len > 0) {
2278 kfree(orig_node->tt_buff);
2279 orig_node->tt_buff_len = 0;
2280 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2281 if (orig_node->tt_buff) {
2282 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2283 orig_node->tt_buff_len = tt_buff_len;
2284 }
2285 }
2286 spin_unlock_bh(&orig_node->tt_buff_lock);
2287}
2288
2289static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2290{
2291 struct batadv_tt_req_node *node;
2292 struct hlist_node *safe;
2293
2294 spin_lock_bh(&bat_priv->tt.req_list_lock);
2295 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2296 if (batadv_has_timed_out(node->issued_at,
2297 BATADV_TT_REQUEST_TIMEOUT)) {
2298 hlist_del_init(&node->list);
2299 kfree(node);
2300 }
2301 }
2302 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2303}
2304
2305/**
2306 * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2307 * @bat_priv: the bat priv with all the soft interface information
2308 * @orig_node: orig node this request is being issued for
2309 *
2310 * Return: the pointer to the new tt_req_node struct if no request
2311 * has already been issued for this orig_node, NULL otherwise.
2312 */
2313static struct batadv_tt_req_node *
2314batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2315 struct batadv_orig_node *orig_node)
2316{
2317 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2318
2319 spin_lock_bh(&bat_priv->tt.req_list_lock);
2320 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2321 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2322 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2323 BATADV_TT_REQUEST_TIMEOUT))
2324 goto unlock;
2325 }
2326
2327 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2328 if (!tt_req_node)
2329 goto unlock;
2330
2331 ether_addr_copy(tt_req_node->addr, orig_node->orig);
2332 tt_req_node->issued_at = jiffies;
2333
2334 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2335unlock:
2336 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2337 return tt_req_node;
2338}
2339
2340/**
2341 * batadv_tt_local_valid - verify that given tt entry is a valid one
2342 * @entry_ptr: to be checked local tt entry
2343 * @data_ptr: not used but definition required to satisfy the callback prototype
2344 *
2345 * Return: 1 if the entry is a valid, 0 otherwise.
2346 */
2347static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
2348{
2349 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2350
2351 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2352 return 0;
2353 return 1;
2354}
2355
2356static int batadv_tt_global_valid(const void *entry_ptr,
2357 const void *data_ptr)
2358{
2359 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2360 const struct batadv_tt_global_entry *tt_global_entry;
2361 const struct batadv_orig_node *orig_node = data_ptr;
2362
2363 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2364 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2365 return 0;
2366
2367 tt_global_entry = container_of(tt_common_entry,
2368 struct batadv_tt_global_entry,
2369 common);
2370
2371 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
2372}
2373
2374/**
2375 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2376 * specified tt hash
2377 * @bat_priv: the bat priv with all the soft interface information
2378 * @hash: hash table containing the tt entries
2379 * @tt_len: expected tvlv tt data buffer length in number of bytes
2380 * @tvlv_buff: pointer to the buffer to fill with the TT data
2381 * @valid_cb: function to filter tt change entries
2382 * @cb_data: data passed to the filter function as argument
2383 */
2384static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2385 struct batadv_hashtable *hash,
2386 void *tvlv_buff, u16 tt_len,
2387 int (*valid_cb)(const void *, const void *),
2388 void *cb_data)
2389{
2390 struct batadv_tt_common_entry *tt_common_entry;
2391 struct batadv_tvlv_tt_change *tt_change;
2392 struct hlist_head *head;
2393 u16 tt_tot, tt_num_entries = 0;
2394 u32 i;
2395
2396 tt_tot = batadv_tt_entries(tt_len);
2397 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
2398
2399 rcu_read_lock();
2400 for (i = 0; i < hash->size; i++) {
2401 head = &hash->table[i];
2402
2403 hlist_for_each_entry_rcu(tt_common_entry,
2404 head, hash_entry) {
2405 if (tt_tot == tt_num_entries)
2406 break;
2407
2408 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
2409 continue;
2410
2411 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
2412 tt_change->flags = tt_common_entry->flags;
2413 tt_change->vid = htons(tt_common_entry->vid);
2414 memset(tt_change->reserved, 0,
2415 sizeof(tt_change->reserved));
2416
2417 tt_num_entries++;
2418 tt_change++;
2419 }
2420 }
2421 rcu_read_unlock();
2422}
2423
2424/**
2425 * batadv_tt_global_check_crc - check if all the CRCs are correct
2426 * @orig_node: originator for which the CRCs have to be checked
2427 * @tt_vlan: pointer to the first tvlv VLAN entry
2428 * @num_vlan: number of tvlv VLAN entries
2429 *
2430 * Return: true if all the received CRCs match the locally stored ones, false
2431 * otherwise
2432 */
2433static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2434 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2435 u16 num_vlan)
2436{
2437 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2438 struct batadv_orig_node_vlan *vlan;
2439 int i, orig_num_vlan;
2440 u32 crc;
2441
2442 /* check if each received CRC matches the locally stored one */
2443 for (i = 0; i < num_vlan; i++) {
2444 tt_vlan_tmp = tt_vlan + i;
2445
2446 /* if orig_node is a backbone node for this VLAN, don't check
2447 * the CRC as we ignore all the global entries over it
2448 */
2449 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2450 orig_node->orig,
2451 ntohs(tt_vlan_tmp->vid)))
2452 continue;
2453
2454 vlan = batadv_orig_node_vlan_get(orig_node,
2455 ntohs(tt_vlan_tmp->vid));
2456 if (!vlan)
2457 return false;
2458
2459 crc = vlan->tt.crc;
2460 batadv_orig_node_vlan_free_ref(vlan);
2461
2462 if (crc != ntohl(tt_vlan_tmp->crc))
2463 return false;
2464 }
2465
2466 /* check if any excess VLANs exist locally for the originator
2467 * which are not mentioned in the TVLV from the originator.
2468 */
2469 rcu_read_lock();
2470 orig_num_vlan = 0;
2471 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
2472 orig_num_vlan++;
2473 rcu_read_unlock();
2474
2475 if (orig_num_vlan > num_vlan)
2476 return false;
2477
2478 return true;
2479}
2480
2481/**
2482 * batadv_tt_local_update_crc - update all the local CRCs
2483 * @bat_priv: the bat priv with all the soft interface information
2484 */
2485static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2486{
2487 struct batadv_softif_vlan *vlan;
2488
2489 /* recompute the global CRC for each VLAN */
2490 rcu_read_lock();
2491 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2492 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2493 }
2494 rcu_read_unlock();
2495}
2496
2497/**
2498 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2499 * @bat_priv: the bat priv with all the soft interface information
2500 * @orig_node: the orig_node for which the CRCs have to be updated
2501 */
2502static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2503 struct batadv_orig_node *orig_node)
2504{
2505 struct batadv_orig_node_vlan *vlan;
2506 u32 crc;
2507
2508 /* recompute the global CRC for each VLAN */
2509 rcu_read_lock();
2510 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2511 /* if orig_node is a backbone node for this VLAN, don't compute
2512 * the CRC as we ignore all the global entries over it
2513 */
2514 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2515 vlan->vid))
2516 continue;
2517
2518 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2519 vlan->tt.crc = crc;
2520 }
2521 rcu_read_unlock();
2522}
2523
2524/**
2525 * batadv_send_tt_request - send a TT Request message to a given node
2526 * @bat_priv: the bat priv with all the soft interface information
2527 * @dst_orig_node: the destination of the message
2528 * @ttvn: the version number that the source of the message is looking for
2529 * @tt_vlan: pointer to the first tvlv VLAN object to request
2530 * @num_vlan: number of tvlv VLAN entries
2531 * @full_table: ask for the entire translation table if true, while only for the
2532 * last TT diff otherwise
2533 *
2534 * Return: true if the TT Request was sent, false otherwise
2535 */
2536static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2537 struct batadv_orig_node *dst_orig_node,
2538 u8 ttvn,
2539 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2540 u16 num_vlan, bool full_table)
2541{
2542 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2543 struct batadv_tt_req_node *tt_req_node = NULL;
2544 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2545 struct batadv_hard_iface *primary_if;
2546 bool ret = false;
2547 int i, size;
2548
2549 primary_if = batadv_primary_if_get_selected(bat_priv);
2550 if (!primary_if)
2551 goto out;
2552
2553 /* The new tt_req will be issued only if I'm not waiting for a
2554 * reply from the same orig_node yet
2555 */
2556 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
2557 if (!tt_req_node)
2558 goto out;
2559
2560 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2561 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2562 if (!tvlv_tt_data)
2563 goto out;
2564
2565 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2566 tvlv_tt_data->ttvn = ttvn;
2567 tvlv_tt_data->num_vlan = htons(num_vlan);
2568
2569 /* send all the CRCs within the request. This is needed by intermediate
2570 * nodes to ensure they have the correct table before replying
2571 */
2572 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2573 for (i = 0; i < num_vlan; i++) {
2574 tt_vlan_req->vid = tt_vlan->vid;
2575 tt_vlan_req->crc = tt_vlan->crc;
2576
2577 tt_vlan_req++;
2578 tt_vlan++;
2579 }
2580
2581 if (full_table)
2582 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2583
2584 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
2585 dst_orig_node->orig, full_table ? 'F' : '.');
2586
2587 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
2588 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2589 dst_orig_node->orig, BATADV_TVLV_TT, 1,
2590 tvlv_tt_data, size);
2591 ret = true;
2592
2593out:
2594 if (primary_if)
2595 batadv_hardif_free_ref(primary_if);
2596 if (ret && tt_req_node) {
2597 spin_lock_bh(&bat_priv->tt.req_list_lock);
2598 /* hlist_del_init() verifies tt_req_node still is in the list */
2599 hlist_del_init(&tt_req_node->list);
2600 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2601 kfree(tt_req_node);
2602 }
2603 kfree(tvlv_tt_data);
2604 return ret;
2605}
2606
2607/**
2608 * batadv_send_other_tt_response - send reply to tt request concerning another
2609 * node's translation table
2610 * @bat_priv: the bat priv with all the soft interface information
2611 * @tt_data: tt data containing the tt request information
2612 * @req_src: mac address of tt request sender
2613 * @req_dst: mac address of tt request recipient
2614 *
2615 * Return: true if tt request reply was sent, false otherwise.
2616 */
2617static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2618 struct batadv_tvlv_tt_data *tt_data,
2619 u8 *req_src, u8 *req_dst)
2620{
2621 struct batadv_orig_node *req_dst_orig_node;
2622 struct batadv_orig_node *res_dst_orig_node = NULL;
2623 struct batadv_tvlv_tt_change *tt_change;
2624 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2625 struct batadv_tvlv_tt_vlan_data *tt_vlan;
2626 bool ret = false, full_table;
2627 u8 orig_ttvn, req_ttvn;
2628 u16 tvlv_len;
2629 s32 tt_len;
2630
2631 batadv_dbg(BATADV_DBG_TT, bat_priv,
2632 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2633 req_src, tt_data->ttvn, req_dst,
2634 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2635
2636 /* Let's get the orig node of the REAL destination */
2637 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
2638 if (!req_dst_orig_node)
2639 goto out;
2640
2641 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
2642 if (!res_dst_orig_node)
2643 goto out;
2644
2645 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
2646 req_ttvn = tt_data->ttvn;
2647
2648 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
2649 /* this node doesn't have the requested data */
2650 if (orig_ttvn != req_ttvn ||
2651 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2652 ntohs(tt_data->num_vlan)))
2653 goto out;
2654
2655 /* If the full table has been explicitly requested */
2656 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
2657 !req_dst_orig_node->tt_buff)
2658 full_table = true;
2659 else
2660 full_table = false;
2661
2662 /* TT fragmentation hasn't been implemented yet, so send as many
2663 * TT entries fit a single packet as possible only
2664 */
2665 if (!full_table) {
2666 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2667 tt_len = req_dst_orig_node->tt_buff_len;
2668
2669 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2670 &tvlv_tt_data,
2671 &tt_change,
2672 &tt_len);
2673 if (!tt_len)
2674 goto unlock;
2675
2676 /* Copy the last orig_node's OGM buffer */
2677 memcpy(tt_change, req_dst_orig_node->tt_buff,
2678 req_dst_orig_node->tt_buff_len);
2679 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2680 } else {
2681 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2682 * in the initial part
2683 */
2684 tt_len = -1;
2685 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2686 &tvlv_tt_data,
2687 &tt_change,
2688 &tt_len);
2689 if (!tt_len)
2690 goto out;
2691
2692 /* fill the rest of the tvlv with the real TT entries */
2693 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2694 tt_change, tt_len,
2695 batadv_tt_global_valid,
2696 req_dst_orig_node);
2697 }
2698
2699 /* Don't send the response, if larger than fragmented packet. */
2700 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2701 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2702 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2703 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2704 res_dst_orig_node->orig);
2705 goto out;
2706 }
2707
2708 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2709 tvlv_tt_data->ttvn = req_ttvn;
2710
2711 if (full_table)
2712 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2713
2714 batadv_dbg(BATADV_DBG_TT, bat_priv,
2715 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2716 res_dst_orig_node->orig, req_dst_orig_node->orig,
2717 full_table ? 'F' : '.', req_ttvn);
2718
2719 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2720
2721 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
2722 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2723 tvlv_len);
2724
2725 ret = true;
2726 goto out;
2727
2728unlock:
2729 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2730
2731out:
2732 if (res_dst_orig_node)
2733 batadv_orig_node_free_ref(res_dst_orig_node);
2734 if (req_dst_orig_node)
2735 batadv_orig_node_free_ref(req_dst_orig_node);
2736 kfree(tvlv_tt_data);
2737 return ret;
2738}
2739
2740/**
2741 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2742 * translation table
2743 * @bat_priv: the bat priv with all the soft interface information
2744 * @tt_data: tt data containing the tt request information
2745 * @req_src: mac address of tt request sender
2746 *
2747 * Return: true if tt request reply was sent, false otherwise.
2748 */
2749static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2750 struct batadv_tvlv_tt_data *tt_data,
2751 u8 *req_src)
2752{
2753 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2754 struct batadv_hard_iface *primary_if = NULL;
2755 struct batadv_tvlv_tt_change *tt_change;
2756 struct batadv_orig_node *orig_node;
2757 u8 my_ttvn, req_ttvn;
2758 u16 tvlv_len;
2759 bool full_table;
2760 s32 tt_len;
2761
2762 batadv_dbg(BATADV_DBG_TT, bat_priv,
2763 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2764 req_src, tt_data->ttvn,
2765 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2766
2767 spin_lock_bh(&bat_priv->tt.commit_lock);
2768
2769 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2770 req_ttvn = tt_data->ttvn;
2771
2772 orig_node = batadv_orig_hash_find(bat_priv, req_src);
2773 if (!orig_node)
2774 goto out;
2775
2776 primary_if = batadv_primary_if_get_selected(bat_priv);
2777 if (!primary_if)
2778 goto out;
2779
2780 /* If the full table has been explicitly requested or the gap
2781 * is too big send the whole local translation table
2782 */
2783 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
2784 !bat_priv->tt.last_changeset)
2785 full_table = true;
2786 else
2787 full_table = false;
2788
2789 /* TT fragmentation hasn't been implemented yet, so send as many
2790 * TT entries fit a single packet as possible only
2791 */
2792 if (!full_table) {
2793 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
2794
2795 tt_len = bat_priv->tt.last_changeset_len;
2796 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2797 &tvlv_tt_data,
2798 &tt_change,
2799 &tt_len);
2800 if (!tt_len)
2801 goto unlock;
2802
2803 /* Copy the last orig_node's OGM buffer */
2804 memcpy(tt_change, bat_priv->tt.last_changeset,
2805 bat_priv->tt.last_changeset_len);
2806 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2807 } else {
2808 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2809
2810 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2811 * in the initial part
2812 */
2813 tt_len = -1;
2814 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2815 &tvlv_tt_data,
2816 &tt_change,
2817 &tt_len);
2818 if (!tt_len)
2819 goto out;
2820
2821 /* fill the rest of the tvlv with the real TT entries */
2822 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2823 tt_change, tt_len,
2824 batadv_tt_local_valid, NULL);
2825 }
2826
2827 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2828 tvlv_tt_data->ttvn = req_ttvn;
2829
2830 if (full_table)
2831 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2832
2833 batadv_dbg(BATADV_DBG_TT, bat_priv,
2834 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2835 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
2836
2837 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2838
2839 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2840 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2841 tvlv_len);
2842
2843 goto out;
2844
2845unlock:
2846 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2847out:
2848 spin_unlock_bh(&bat_priv->tt.commit_lock);
2849 if (orig_node)
2850 batadv_orig_node_free_ref(orig_node);
2851 if (primary_if)
2852 batadv_hardif_free_ref(primary_if);
2853 kfree(tvlv_tt_data);
2854 /* The packet was for this host, so it doesn't need to be re-routed */
2855 return true;
2856}
2857
2858/**
2859 * batadv_send_tt_response - send reply to tt request
2860 * @bat_priv: the bat priv with all the soft interface information
2861 * @tt_data: tt data containing the tt request information
2862 * @req_src: mac address of tt request sender
2863 * @req_dst: mac address of tt request recipient
2864 *
2865 * Return: true if tt request reply was sent, false otherwise.
2866 */
2867static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2868 struct batadv_tvlv_tt_data *tt_data,
2869 u8 *req_src, u8 *req_dst)
2870{
2871 if (batadv_is_my_mac(bat_priv, req_dst))
2872 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
2873 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2874 req_dst);
2875}
2876
2877static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2878 struct batadv_orig_node *orig_node,
2879 struct batadv_tvlv_tt_change *tt_change,
2880 u16 tt_num_changes, u8 ttvn)
2881{
2882 int i;
2883 int roams;
2884
2885 for (i = 0; i < tt_num_changes; i++) {
2886 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2887 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
2888 batadv_tt_global_del(bat_priv, orig_node,
2889 (tt_change + i)->addr,
2890 ntohs((tt_change + i)->vid),
2891 "tt removed by changes",
2892 roams);
2893 } else {
2894 if (!batadv_tt_global_add(bat_priv, orig_node,
2895 (tt_change + i)->addr,
2896 ntohs((tt_change + i)->vid),
2897 (tt_change + i)->flags, ttvn))
2898 /* In case of problem while storing a
2899 * global_entry, we stop the updating
2900 * procedure without committing the
2901 * ttvn change. This will avoid to send
2902 * corrupted data on tt_request
2903 */
2904 return;
2905 }
2906 }
2907 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2908}
2909
2910static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2911 struct batadv_tvlv_tt_change *tt_change,
2912 u8 ttvn, u8 *resp_src,
2913 u16 num_entries)
2914{
2915 struct batadv_orig_node *orig_node;
2916
2917 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2918 if (!orig_node)
2919 goto out;
2920
2921 /* Purge the old table first.. */
2922 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2923 "Received full table");
2924
2925 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2926 ttvn);
2927
2928 spin_lock_bh(&orig_node->tt_buff_lock);
2929 kfree(orig_node->tt_buff);
2930 orig_node->tt_buff_len = 0;
2931 orig_node->tt_buff = NULL;
2932 spin_unlock_bh(&orig_node->tt_buff_lock);
2933
2934 atomic_set(&orig_node->last_ttvn, ttvn);
2935
2936out:
2937 if (orig_node)
2938 batadv_orig_node_free_ref(orig_node);
2939}
2940
2941static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2942 struct batadv_orig_node *orig_node,
2943 u16 tt_num_changes, u8 ttvn,
2944 struct batadv_tvlv_tt_change *tt_change)
2945{
2946 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2947 tt_num_changes, ttvn);
2948
2949 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2950 batadv_tt_len(tt_num_changes));
2951 atomic_set(&orig_node->last_ttvn, ttvn);
2952}
2953
2954/**
2955 * batadv_is_my_client - check if a client is served by the local node
2956 * @bat_priv: the bat priv with all the soft interface information
2957 * @addr: the mac address of the client to check
2958 * @vid: VLAN identifier
2959 *
2960 * Return: true if the client is served by this node, false otherwise.
2961 */
2962bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
2963 unsigned short vid)
2964{
2965 struct batadv_tt_local_entry *tt_local_entry;
2966 bool ret = false;
2967
2968 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
2969 if (!tt_local_entry)
2970 goto out;
2971 /* Check if the client has been logically deleted (but is kept for
2972 * consistency purpose)
2973 */
2974 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2975 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
2976 goto out;
2977 ret = true;
2978out:
2979 if (tt_local_entry)
2980 batadv_tt_local_entry_free_ref(tt_local_entry);
2981 return ret;
2982}
2983
2984/**
2985 * batadv_handle_tt_response - process incoming tt reply
2986 * @bat_priv: the bat priv with all the soft interface information
2987 * @tt_data: tt data containing the tt request information
2988 * @resp_src: mac address of tt reply sender
2989 * @num_entries: number of tt change entries appended to the tt data
2990 */
2991static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2992 struct batadv_tvlv_tt_data *tt_data,
2993 u8 *resp_src, u16 num_entries)
2994{
2995 struct batadv_tt_req_node *node;
2996 struct hlist_node *safe;
2997 struct batadv_orig_node *orig_node = NULL;
2998 struct batadv_tvlv_tt_change *tt_change;
2999 u8 *tvlv_ptr = (u8 *)tt_data;
3000 u16 change_offset;
3001
3002 batadv_dbg(BATADV_DBG_TT, bat_priv,
3003 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3004 resp_src, tt_data->ttvn, num_entries,
3005 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3006
3007 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3008 if (!orig_node)
3009 goto out;
3010
3011 spin_lock_bh(&orig_node->tt_lock);
3012
3013 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3014 change_offset *= ntohs(tt_data->num_vlan);
3015 change_offset += sizeof(*tt_data);
3016 tvlv_ptr += change_offset;
3017
3018 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
3019 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
3020 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3021 resp_src, num_entries);
3022 } else {
3023 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3024 tt_data->ttvn, tt_change);
3025 }
3026
3027 /* Recalculate the CRC for this orig_node and store it */
3028 batadv_tt_global_update_crc(bat_priv, orig_node);
3029
3030 spin_unlock_bh(&orig_node->tt_lock);
3031
3032 /* Delete the tt_req_node from pending tt_requests list */
3033 spin_lock_bh(&bat_priv->tt.req_list_lock);
3034 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
3035 if (!batadv_compare_eth(node->addr, resp_src))
3036 continue;
3037 hlist_del_init(&node->list);
3038 kfree(node);
3039 }
3040
3041 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3042out:
3043 if (orig_node)
3044 batadv_orig_node_free_ref(orig_node);
3045}
3046
3047static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3048{
3049 struct batadv_tt_roam_node *node, *safe;
3050
3051 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3052
3053 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3054 list_del(&node->list);
3055 kfree(node);
3056 }
3057
3058 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3059}
3060
3061static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3062{
3063 struct batadv_tt_roam_node *node, *safe;
3064
3065 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3066 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3067 if (!batadv_has_timed_out(node->first_time,
3068 BATADV_ROAMING_MAX_TIME))
3069 continue;
3070
3071 list_del(&node->list);
3072 kfree(node);
3073 }
3074 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3075}
3076
3077/**
3078 * batadv_tt_check_roam_count - check if a client has roamed too frequently
3079 * @bat_priv: the bat priv with all the soft interface information
3080 * @client: mac address of the roaming client
3081 *
3082 * This function checks whether the client already reached the
3083 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3084 * will not be sent.
3085 *
3086 * Return: true if the ROAMING_ADV can be sent, false otherwise
3087 */
3088static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3089{
3090 struct batadv_tt_roam_node *tt_roam_node;
3091 bool ret = false;
3092
3093 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3094 /* The new tt_req will be issued only if I'm not waiting for a
3095 * reply from the same orig_node yet
3096 */
3097 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3098 if (!batadv_compare_eth(tt_roam_node->addr, client))
3099 continue;
3100
3101 if (batadv_has_timed_out(tt_roam_node->first_time,
3102 BATADV_ROAMING_MAX_TIME))
3103 continue;
3104
3105 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3106 /* Sorry, you roamed too many times! */
3107 goto unlock;
3108 ret = true;
3109 break;
3110 }
3111
3112 if (!ret) {
3113 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
3114 if (!tt_roam_node)
3115 goto unlock;
3116
3117 tt_roam_node->first_time = jiffies;
3118 atomic_set(&tt_roam_node->counter,
3119 BATADV_ROAMING_MAX_COUNT - 1);
3120 ether_addr_copy(tt_roam_node->addr, client);
3121
3122 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3123 ret = true;
3124 }
3125
3126unlock:
3127 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3128 return ret;
3129}
3130
3131/**
3132 * batadv_send_roam_adv - send a roaming advertisement message
3133 * @bat_priv: the bat priv with all the soft interface information
3134 * @client: mac address of the roaming client
3135 * @vid: VLAN identifier
3136 * @orig_node: message destination
3137 *
3138 * Send a ROAMING_ADV message to the node which was previously serving this
3139 * client. This is done to inform the node that from now on all traffic destined
3140 * for this particular roamed client has to be forwarded to the sender of the
3141 * roaming message.
3142 */
3143static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3144 unsigned short vid,
3145 struct batadv_orig_node *orig_node)
3146{
3147 struct batadv_hard_iface *primary_if;
3148 struct batadv_tvlv_roam_adv tvlv_roam;
3149
3150 primary_if = batadv_primary_if_get_selected(bat_priv);
3151 if (!primary_if)
3152 goto out;
3153
3154 /* before going on we have to check whether the client has
3155 * already roamed to us too many times
3156 */
3157 if (!batadv_tt_check_roam_count(bat_priv, client))
3158 goto out;
3159
3160 batadv_dbg(BATADV_DBG_TT, bat_priv,
3161 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3162 orig_node->orig, client, BATADV_PRINT_VID(vid));
3163
3164 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3165
3166 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3167 tvlv_roam.vid = htons(vid);
3168
3169 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3170 orig_node->orig, BATADV_TVLV_ROAM, 1,
3171 &tvlv_roam, sizeof(tvlv_roam));
3172
3173out:
3174 if (primary_if)
3175 batadv_hardif_free_ref(primary_if);
3176}
3177
3178static void batadv_tt_purge(struct work_struct *work)
3179{
3180 struct delayed_work *delayed_work;
3181 struct batadv_priv_tt *priv_tt;
3182 struct batadv_priv *bat_priv;
3183
3184 delayed_work = container_of(work, struct delayed_work, work);
3185 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3186 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3187
3188 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3189 batadv_tt_global_purge(bat_priv);
3190 batadv_tt_req_purge(bat_priv);
3191 batadv_tt_roam_purge(bat_priv);
3192
3193 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3194 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3195}
3196
3197void batadv_tt_free(struct batadv_priv *bat_priv)
3198{
3199 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3200 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3201
3202 cancel_delayed_work_sync(&bat_priv->tt.work);
3203
3204 batadv_tt_local_table_free(bat_priv);
3205 batadv_tt_global_table_free(bat_priv);
3206 batadv_tt_req_list_free(bat_priv);
3207 batadv_tt_changes_list_free(bat_priv);
3208 batadv_tt_roam_list_free(bat_priv);
3209
3210 kfree(bat_priv->tt.last_changeset);
3211}
3212
3213/**
3214 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3215 * table and possibly count them in the TT size
3216 * @bat_priv: the bat priv with all the soft interface information
3217 * @flags: the flag to switch
3218 * @enable: whether to set or unset the flag
3219 * @count: whether to increase the TT size by the number of changed entries
3220 */
3221static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3222 bool enable, bool count)
3223{
3224 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3225 struct batadv_tt_common_entry *tt_common_entry;
3226 u16 changed_num = 0;
3227 struct hlist_head *head;
3228 u32 i;
3229
3230 if (!hash)
3231 return;
3232
3233 for (i = 0; i < hash->size; i++) {
3234 head = &hash->table[i];
3235
3236 rcu_read_lock();
3237 hlist_for_each_entry_rcu(tt_common_entry,
3238 head, hash_entry) {
3239 if (enable) {
3240 if ((tt_common_entry->flags & flags) == flags)
3241 continue;
3242 tt_common_entry->flags |= flags;
3243 } else {
3244 if (!(tt_common_entry->flags & flags))
3245 continue;
3246 tt_common_entry->flags &= ~flags;
3247 }
3248 changed_num++;
3249
3250 if (!count)
3251 continue;
3252
3253 batadv_tt_local_size_inc(bat_priv,
3254 tt_common_entry->vid);
3255 }
3256 rcu_read_unlock();
3257 }
3258}
3259
3260/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3261static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3262{
3263 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3264 struct batadv_tt_common_entry *tt_common;
3265 struct batadv_tt_local_entry *tt_local;
3266 struct batadv_softif_vlan *vlan;
3267 struct hlist_node *node_tmp;
3268 struct hlist_head *head;
3269 spinlock_t *list_lock; /* protects write access to the hash lists */
3270 u32 i;
3271
3272 if (!hash)
3273 return;
3274
3275 for (i = 0; i < hash->size; i++) {
3276 head = &hash->table[i];
3277 list_lock = &hash->list_locks[i];
3278
3279 spin_lock_bh(list_lock);
3280 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3281 hash_entry) {
3282 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3283 continue;
3284
3285 batadv_dbg(BATADV_DBG_TT, bat_priv,
3286 "Deleting local tt entry (%pM, vid: %d): pending\n",
3287 tt_common->addr,
3288 BATADV_PRINT_VID(tt_common->vid));
3289
3290 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3291 hlist_del_rcu(&tt_common->hash_entry);
3292 tt_local = container_of(tt_common,
3293 struct batadv_tt_local_entry,
3294 common);
3295
3296 /* decrease the reference held for this vlan */
3297 vlan = batadv_softif_vlan_get(bat_priv, tt_common->vid);
3298 if (vlan) {
3299 batadv_softif_vlan_free_ref(vlan);
3300 batadv_softif_vlan_free_ref(vlan);
3301 }
3302
3303 batadv_tt_local_entry_free_ref(tt_local);
3304 }
3305 spin_unlock_bh(list_lock);
3306 }
3307}
3308
3309/**
3310 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3311 * which have been queued in the time since the last commit
3312 * @bat_priv: the bat priv with all the soft interface information
3313 *
3314 * Caller must hold tt->commit_lock.
3315 */
3316static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3317{
3318 lockdep_assert_held(&bat_priv->tt.commit_lock);
3319
3320 /* Update multicast addresses in local translation table */
3321 batadv_mcast_mla_update(bat_priv);
3322
3323 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3324 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3325 batadv_tt_tvlv_container_update(bat_priv);
3326 return;
3327 }
3328
3329 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3330
3331 batadv_tt_local_purge_pending_clients(bat_priv);
3332 batadv_tt_local_update_crc(bat_priv);
3333
3334 /* Increment the TTVN only once per OGM interval */
3335 atomic_inc(&bat_priv->tt.vn);
3336 batadv_dbg(BATADV_DBG_TT, bat_priv,
3337 "Local changes committed, updating to ttvn %u\n",
3338 (u8)atomic_read(&bat_priv->tt.vn));
3339
3340 /* reset the sending counter */
3341 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3342 batadv_tt_tvlv_container_update(bat_priv);
3343}
3344
3345/**
3346 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3347 * have been queued in the time since the last commit
3348 * @bat_priv: the bat priv with all the soft interface information
3349 */
3350void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3351{
3352 spin_lock_bh(&bat_priv->tt.commit_lock);
3353 batadv_tt_local_commit_changes_nolock(bat_priv);
3354 spin_unlock_bh(&bat_priv->tt.commit_lock);
3355}
3356
3357bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3358 unsigned short vid)
3359{
3360 struct batadv_tt_local_entry *tt_local_entry = NULL;
3361 struct batadv_tt_global_entry *tt_global_entry = NULL;
3362 struct batadv_softif_vlan *vlan;
3363 bool ret = false;
3364
3365 vlan = batadv_softif_vlan_get(bat_priv, vid);
3366 if (!vlan)
3367 return false;
3368
3369 if (!atomic_read(&vlan->ap_isolation))
3370 goto out;
3371
3372 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3373 if (!tt_local_entry)
3374 goto out;
3375
3376 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3377 if (!tt_global_entry)
3378 goto out;
3379
3380 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3381 goto out;
3382
3383 ret = true;
3384
3385out:
3386 batadv_softif_vlan_free_ref(vlan);
3387 if (tt_global_entry)
3388 batadv_tt_global_entry_free_ref(tt_global_entry);
3389 if (tt_local_entry)
3390 batadv_tt_local_entry_free_ref(tt_local_entry);
3391 return ret;
3392}
3393
3394/**
3395 * batadv_tt_update_orig - update global translation table with new tt
3396 * information received via ogms
3397 * @bat_priv: the bat priv with all the soft interface information
3398 * @orig_node: the orig_node of the ogm
3399 * @tt_buff: pointer to the first tvlv VLAN entry
3400 * @tt_num_vlan: number of tvlv VLAN entries
3401 * @tt_change: pointer to the first entry in the TT buffer
3402 * @tt_num_changes: number of tt changes inside the tt buffer
3403 * @ttvn: translation table version number of this changeset
3404 */
3405static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3406 struct batadv_orig_node *orig_node,
3407 const void *tt_buff, u16 tt_num_vlan,
3408 struct batadv_tvlv_tt_change *tt_change,
3409 u16 tt_num_changes, u8 ttvn)
3410{
3411 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
3412 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3413 bool full_table = true;
3414 bool has_tt_init;
3415
3416 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3417 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3418 &orig_node->capa_initialized);
3419
3420 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3421 * increased by one -> we can apply the attached changes
3422 */
3423 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
3424 /* the OGM could not contain the changes due to their size or
3425 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3426 * times.
3427 * In this case send a tt request
3428 */
3429 if (!tt_num_changes) {
3430 full_table = false;
3431 goto request_table;
3432 }
3433
3434 spin_lock_bh(&orig_node->tt_lock);
3435
3436 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3437 ttvn, tt_change);
3438
3439 /* Even if we received the precomputed crc with the OGM, we
3440 * prefer to recompute it to spot any possible inconsistency
3441 * in the global table
3442 */
3443 batadv_tt_global_update_crc(bat_priv, orig_node);
3444
3445 spin_unlock_bh(&orig_node->tt_lock);
3446
3447 /* The ttvn alone is not enough to guarantee consistency
3448 * because a single value could represent different states
3449 * (due to the wrap around). Thus a node has to check whether
3450 * the resulting table (after applying the changes) is still
3451 * consistent or not. E.g. a node could disconnect while its
3452 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3453 * checking the CRC value is mandatory to detect the
3454 * inconsistency
3455 */
3456 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3457 tt_num_vlan))
3458 goto request_table;
3459 } else {
3460 /* if we missed more than one change or our tables are not
3461 * in sync anymore -> request fresh tt data
3462 */
3463 if (!has_tt_init || ttvn != orig_ttvn ||
3464 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3465 tt_num_vlan)) {
3466request_table:
3467 batadv_dbg(BATADV_DBG_TT, bat_priv,
3468 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3469 orig_node->orig, ttvn, orig_ttvn,
3470 tt_num_changes);
3471 batadv_send_tt_request(bat_priv, orig_node, ttvn,
3472 tt_vlan, tt_num_vlan,
3473 full_table);
3474 return;
3475 }
3476 }
3477}
3478
3479/**
3480 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3481 * @bat_priv: the bat priv with all the soft interface information
3482 * @addr: the mac address of the client to check
3483 * @vid: VLAN identifier
3484 *
3485 * Return: true if we know that the client has moved from its old originator
3486 * to another one. This entry is still kept for consistency purposes and will be
3487 * deleted later by a DEL or because of timeout
3488 */
3489bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
3490 u8 *addr, unsigned short vid)
3491{
3492 struct batadv_tt_global_entry *tt_global_entry;
3493 bool ret = false;
3494
3495 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3496 if (!tt_global_entry)
3497 goto out;
3498
3499 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3500 batadv_tt_global_entry_free_ref(tt_global_entry);
3501out:
3502 return ret;
3503}
3504
3505/**
3506 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3507 * @bat_priv: the bat priv with all the soft interface information
3508 * @addr: the mac address of the local client to query
3509 * @vid: VLAN identifier
3510 *
3511 * Return: true if the local client is known to be roaming (it is not served by
3512 * this node anymore) or not. If yes, the client is still present in the table
3513 * to keep the latter consistent with the node TTVN
3514 */
3515bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
3516 u8 *addr, unsigned short vid)
3517{
3518 struct batadv_tt_local_entry *tt_local_entry;
3519 bool ret = false;
3520
3521 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3522 if (!tt_local_entry)
3523 goto out;
3524
3525 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3526 batadv_tt_local_entry_free_ref(tt_local_entry);
3527out:
3528 return ret;
3529}
3530
3531bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3532 struct batadv_orig_node *orig_node,
3533 const unsigned char *addr,
3534 unsigned short vid)
3535{
3536 bool ret = false;
3537
3538 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
3539 BATADV_TT_CLIENT_TEMP,
3540 atomic_read(&orig_node->last_ttvn)))
3541 goto out;
3542
3543 batadv_dbg(BATADV_DBG_TT, bat_priv,
3544 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3545 addr, BATADV_PRINT_VID(vid), orig_node->orig);
3546 ret = true;
3547out:
3548 return ret;
3549}
3550
3551/**
3552 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3553 * maximum packet size that can be transported through the mesh
3554 * @soft_iface: netdev struct of the mesh interface
3555 *
3556 * Remove entries older than 'timeout' and half timeout if more entries need
3557 * to be removed.
3558 */
3559void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3560{
3561 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3562 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3563 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3564 bool reduced = false;
3565
3566 spin_lock_bh(&bat_priv->tt.commit_lock);
3567
3568 while (true) {
3569 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3570 if (packet_size_max >= table_size)
3571 break;
3572
3573 batadv_tt_local_purge(bat_priv, timeout);
3574 batadv_tt_local_purge_pending_clients(bat_priv);
3575
3576 timeout /= 2;
3577 reduced = true;
3578 net_ratelimited_function(batadv_info, soft_iface,
3579 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3580 packet_size_max);
3581 }
3582
3583 /* commit these changes immediately, to avoid synchronization problem
3584 * with the TTVN
3585 */
3586 if (reduced)
3587 batadv_tt_local_commit_changes_nolock(bat_priv);
3588
3589 spin_unlock_bh(&bat_priv->tt.commit_lock);
3590}
3591
3592/**
3593 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3594 * @bat_priv: the bat priv with all the soft interface information
3595 * @orig: the orig_node of the ogm
3596 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3597 * @tvlv_value: tvlv buffer containing the gateway data
3598 * @tvlv_value_len: tvlv buffer length
3599 */
3600static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3601 struct batadv_orig_node *orig,
3602 u8 flags, void *tvlv_value,
3603 u16 tvlv_value_len)
3604{
3605 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3606 struct batadv_tvlv_tt_change *tt_change;
3607 struct batadv_tvlv_tt_data *tt_data;
3608 u16 num_entries, num_vlan;
3609
3610 if (tvlv_value_len < sizeof(*tt_data))
3611 return;
3612
3613 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3614 tvlv_value_len -= sizeof(*tt_data);
3615
3616 num_vlan = ntohs(tt_data->num_vlan);
3617
3618 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3619 return;
3620
3621 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3622 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3623 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3624
3625 num_entries = batadv_tt_entries(tvlv_value_len);
3626
3627 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3628 num_entries, tt_data->ttvn);
3629}
3630
3631/**
3632 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3633 * container
3634 * @bat_priv: the bat priv with all the soft interface information
3635 * @src: mac address of tt tvlv sender
3636 * @dst: mac address of tt tvlv recipient
3637 * @tvlv_value: tvlv buffer containing the tt data
3638 * @tvlv_value_len: tvlv buffer length
3639 *
3640 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3641 * otherwise.
3642 */
3643static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3644 u8 *src, u8 *dst,
3645 void *tvlv_value,
3646 u16 tvlv_value_len)
3647{
3648 struct batadv_tvlv_tt_data *tt_data;
3649 u16 tt_vlan_len, tt_num_entries;
3650 char tt_flag;
3651 bool ret;
3652
3653 if (tvlv_value_len < sizeof(*tt_data))
3654 return NET_RX_SUCCESS;
3655
3656 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3657 tvlv_value_len -= sizeof(*tt_data);
3658
3659 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3660 tt_vlan_len *= ntohs(tt_data->num_vlan);
3661
3662 if (tvlv_value_len < tt_vlan_len)
3663 return NET_RX_SUCCESS;
3664
3665 tvlv_value_len -= tt_vlan_len;
3666 tt_num_entries = batadv_tt_entries(tvlv_value_len);
3667
3668 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3669 case BATADV_TT_REQUEST:
3670 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3671
3672 /* If this node cannot provide a TT response the tt_request is
3673 * forwarded
3674 */
3675 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3676 if (!ret) {
3677 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3678 tt_flag = 'F';
3679 else
3680 tt_flag = '.';
3681
3682 batadv_dbg(BATADV_DBG_TT, bat_priv,
3683 "Routing TT_REQUEST to %pM [%c]\n",
3684 dst, tt_flag);
3685 /* tvlv API will re-route the packet */
3686 return NET_RX_DROP;
3687 }
3688 break;
3689 case BATADV_TT_RESPONSE:
3690 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3691
3692 if (batadv_is_my_mac(bat_priv, dst)) {
3693 batadv_handle_tt_response(bat_priv, tt_data,
3694 src, tt_num_entries);
3695 return NET_RX_SUCCESS;
3696 }
3697
3698 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3699 tt_flag = 'F';
3700 else
3701 tt_flag = '.';
3702
3703 batadv_dbg(BATADV_DBG_TT, bat_priv,
3704 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3705
3706 /* tvlv API will re-route the packet */
3707 return NET_RX_DROP;
3708 }
3709
3710 return NET_RX_SUCCESS;
3711}
3712
3713/**
3714 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3715 * @bat_priv: the bat priv with all the soft interface information
3716 * @src: mac address of tt tvlv sender
3717 * @dst: mac address of tt tvlv recipient
3718 * @tvlv_value: tvlv buffer containing the tt data
3719 * @tvlv_value_len: tvlv buffer length
3720 *
3721 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3722 * otherwise.
3723 */
3724static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3725 u8 *src, u8 *dst,
3726 void *tvlv_value,
3727 u16 tvlv_value_len)
3728{
3729 struct batadv_tvlv_roam_adv *roaming_adv;
3730 struct batadv_orig_node *orig_node = NULL;
3731
3732 /* If this node is not the intended recipient of the
3733 * roaming advertisement the packet is forwarded
3734 * (the tvlv API will re-route the packet).
3735 */
3736 if (!batadv_is_my_mac(bat_priv, dst))
3737 return NET_RX_DROP;
3738
3739 if (tvlv_value_len < sizeof(*roaming_adv))
3740 goto out;
3741
3742 orig_node = batadv_orig_hash_find(bat_priv, src);
3743 if (!orig_node)
3744 goto out;
3745
3746 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3747 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3748
3749 batadv_dbg(BATADV_DBG_TT, bat_priv,
3750 "Received ROAMING_ADV from %pM (client %pM)\n",
3751 src, roaming_adv->client);
3752
3753 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
3754 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
3755 atomic_read(&orig_node->last_ttvn) + 1);
3756
3757out:
3758 if (orig_node)
3759 batadv_orig_node_free_ref(orig_node);
3760 return NET_RX_SUCCESS;
3761}
3762
3763/**
3764 * batadv_tt_init - initialise the translation table internals
3765 * @bat_priv: the bat priv with all the soft interface information
3766 *
3767 * Return: 0 on success or negative error number in case of failure.
3768 */
3769int batadv_tt_init(struct batadv_priv *bat_priv)
3770{
3771 int ret;
3772
3773 /* synchronized flags must be remote */
3774 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3775
3776 ret = batadv_tt_local_init(bat_priv);
3777 if (ret < 0)
3778 return ret;
3779
3780 ret = batadv_tt_global_init(bat_priv);
3781 if (ret < 0)
3782 return ret;
3783
3784 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
3785 batadv_tt_tvlv_unicast_handler_v1,
3786 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
3787
3788 batadv_tvlv_handler_register(bat_priv, NULL,
3789 batadv_roam_tvlv_unicast_handler_v1,
3790 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3791
3792 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3793 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3794 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3795
3796 return 1;
3797}
3798
3799/**
3800 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3801 * @bat_priv: the bat priv with all the soft interface information
3802 * @addr: the mac address of the client
3803 * @vid: the identifier of the VLAN where this client is connected
3804 *
3805 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
3806 * otherwise
3807 */
3808bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
3809 const u8 *addr, unsigned short vid)
3810{
3811 struct batadv_tt_global_entry *tt;
3812 bool ret;
3813
3814 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3815 if (!tt)
3816 return false;
3817
3818 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3819
3820 batadv_tt_global_entry_free_ref(tt);
3821
3822 return ret;
3823}
This page took 0.041185 seconds and 5 git commands to generate.