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