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