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