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