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