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