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