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