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