0001
0002
0003
0004
0005
0006
0007 #include "translation-table.h"
0008 #include "main.h"
0009
0010 #include <linux/atomic.h>
0011 #include <linux/bitops.h>
0012 #include <linux/build_bug.h>
0013 #include <linux/byteorder/generic.h>
0014 #include <linux/cache.h>
0015 #include <linux/compiler.h>
0016 #include <linux/container_of.h>
0017 #include <linux/crc32c.h>
0018 #include <linux/errno.h>
0019 #include <linux/etherdevice.h>
0020 #include <linux/gfp.h>
0021 #include <linux/if_ether.h>
0022 #include <linux/init.h>
0023 #include <linux/jhash.h>
0024 #include <linux/jiffies.h>
0025 #include <linux/kref.h>
0026 #include <linux/list.h>
0027 #include <linux/lockdep.h>
0028 #include <linux/net.h>
0029 #include <linux/netdevice.h>
0030 #include <linux/netlink.h>
0031 #include <linux/rculist.h>
0032 #include <linux/rcupdate.h>
0033 #include <linux/skbuff.h>
0034 #include <linux/slab.h>
0035 #include <linux/spinlock.h>
0036 #include <linux/stddef.h>
0037 #include <linux/string.h>
0038 #include <linux/workqueue.h>
0039 #include <net/genetlink.h>
0040 #include <net/netlink.h>
0041 #include <net/sock.h>
0042 #include <uapi/linux/batadv_packet.h>
0043 #include <uapi/linux/batman_adv.h>
0044
0045 #include "bridge_loop_avoidance.h"
0046 #include "hard-interface.h"
0047 #include "hash.h"
0048 #include "log.h"
0049 #include "netlink.h"
0050 #include "originator.h"
0051 #include "soft-interface.h"
0052 #include "tvlv.h"
0053
0054 static struct kmem_cache *batadv_tl_cache __read_mostly;
0055 static struct kmem_cache *batadv_tg_cache __read_mostly;
0056 static struct kmem_cache *batadv_tt_orig_cache __read_mostly;
0057 static struct kmem_cache *batadv_tt_change_cache __read_mostly;
0058 static struct kmem_cache *batadv_tt_req_cache __read_mostly;
0059 static struct kmem_cache *batadv_tt_roam_cache __read_mostly;
0060
0061
0062 static struct lock_class_key batadv_tt_local_hash_lock_class_key;
0063 static struct lock_class_key batadv_tt_global_hash_lock_class_key;
0064
0065 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
0066 unsigned short vid,
0067 struct batadv_orig_node *orig_node);
0068 static void batadv_tt_purge(struct work_struct *work);
0069 static void
0070 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
0071 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
0072 struct batadv_orig_node *orig_node,
0073 const unsigned char *addr,
0074 unsigned short vid, const char *message,
0075 bool roaming);
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086 static bool batadv_compare_tt(const struct hlist_node *node, const void *data2)
0087 {
0088 const void *data1 = container_of(node, struct batadv_tt_common_entry,
0089 hash_entry);
0090 const struct batadv_tt_common_entry *tt1 = data1;
0091 const struct batadv_tt_common_entry *tt2 = data2;
0092
0093 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
0094 }
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104 static inline u32 batadv_choose_tt(const void *data, u32 size)
0105 {
0106 const struct batadv_tt_common_entry *tt;
0107 u32 hash = 0;
0108
0109 tt = data;
0110 hash = jhash(&tt->addr, ETH_ALEN, hash);
0111 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
0112
0113 return hash % size;
0114 }
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125 static struct batadv_tt_common_entry *
0126 batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
0127 unsigned short vid)
0128 {
0129 struct hlist_head *head;
0130 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
0131 u32 index;
0132
0133 if (!hash)
0134 return NULL;
0135
0136 ether_addr_copy(to_search.addr, addr);
0137 to_search.vid = vid;
0138
0139 index = batadv_choose_tt(&to_search, hash->size);
0140 head = &hash->table[index];
0141
0142 rcu_read_lock();
0143 hlist_for_each_entry_rcu(tt, head, hash_entry) {
0144 if (!batadv_compare_eth(tt, addr))
0145 continue;
0146
0147 if (tt->vid != vid)
0148 continue;
0149
0150 if (!kref_get_unless_zero(&tt->refcount))
0151 continue;
0152
0153 tt_tmp = tt;
0154 break;
0155 }
0156 rcu_read_unlock();
0157
0158 return tt_tmp;
0159 }
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170 static struct batadv_tt_local_entry *
0171 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
0172 unsigned short vid)
0173 {
0174 struct batadv_tt_common_entry *tt_common_entry;
0175 struct batadv_tt_local_entry *tt_local_entry = NULL;
0176
0177 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
0178 vid);
0179 if (tt_common_entry)
0180 tt_local_entry = container_of(tt_common_entry,
0181 struct batadv_tt_local_entry,
0182 common);
0183 return tt_local_entry;
0184 }
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195 struct batadv_tt_global_entry *
0196 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
0197 unsigned short vid)
0198 {
0199 struct batadv_tt_common_entry *tt_common_entry;
0200 struct batadv_tt_global_entry *tt_global_entry = NULL;
0201
0202 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
0203 vid);
0204 if (tt_common_entry)
0205 tt_global_entry = container_of(tt_common_entry,
0206 struct batadv_tt_global_entry,
0207 common);
0208 return tt_global_entry;
0209 }
0210
0211
0212
0213
0214
0215 static void batadv_tt_local_entry_free_rcu(struct rcu_head *rcu)
0216 {
0217 struct batadv_tt_local_entry *tt_local_entry;
0218
0219 tt_local_entry = container_of(rcu, struct batadv_tt_local_entry,
0220 common.rcu);
0221
0222 kmem_cache_free(batadv_tl_cache, tt_local_entry);
0223 }
0224
0225
0226
0227
0228
0229
0230 static void batadv_tt_local_entry_release(struct kref *ref)
0231 {
0232 struct batadv_tt_local_entry *tt_local_entry;
0233
0234 tt_local_entry = container_of(ref, struct batadv_tt_local_entry,
0235 common.refcount);
0236
0237 batadv_softif_vlan_put(tt_local_entry->vlan);
0238
0239 call_rcu(&tt_local_entry->common.rcu, batadv_tt_local_entry_free_rcu);
0240 }
0241
0242
0243
0244
0245
0246
0247 static void
0248 batadv_tt_local_entry_put(struct batadv_tt_local_entry *tt_local_entry)
0249 {
0250 if (!tt_local_entry)
0251 return;
0252
0253 kref_put(&tt_local_entry->common.refcount,
0254 batadv_tt_local_entry_release);
0255 }
0256
0257
0258
0259
0260
0261 static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
0262 {
0263 struct batadv_tt_global_entry *tt_global_entry;
0264
0265 tt_global_entry = container_of(rcu, struct batadv_tt_global_entry,
0266 common.rcu);
0267
0268 kmem_cache_free(batadv_tg_cache, tt_global_entry);
0269 }
0270
0271
0272
0273
0274
0275
0276 void batadv_tt_global_entry_release(struct kref *ref)
0277 {
0278 struct batadv_tt_global_entry *tt_global_entry;
0279
0280 tt_global_entry = container_of(ref, struct batadv_tt_global_entry,
0281 common.refcount);
0282
0283 batadv_tt_global_del_orig_list(tt_global_entry);
0284
0285 call_rcu(&tt_global_entry->common.rcu, batadv_tt_global_entry_free_rcu);
0286 }
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297 int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
0298 const u8 *addr, unsigned short vid)
0299 {
0300 struct batadv_tt_global_entry *tt_global_entry;
0301 int count;
0302
0303 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
0304 if (!tt_global_entry)
0305 return 0;
0306
0307 count = atomic_read(&tt_global_entry->orig_list_count);
0308 batadv_tt_global_entry_put(tt_global_entry);
0309
0310 return count;
0311 }
0312
0313
0314
0315
0316
0317
0318
0319
0320 static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
0321 unsigned short vid, int v)
0322 {
0323 struct batadv_softif_vlan *vlan;
0324
0325 vlan = batadv_softif_vlan_get(bat_priv, vid);
0326 if (!vlan)
0327 return;
0328
0329 atomic_add(v, &vlan->tt.num_entries);
0330
0331 batadv_softif_vlan_put(vlan);
0332 }
0333
0334
0335
0336
0337
0338
0339
0340 static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
0341 unsigned short vid)
0342 {
0343 batadv_tt_local_size_mod(bat_priv, vid, 1);
0344 }
0345
0346
0347
0348
0349
0350
0351
0352 static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
0353 unsigned short vid)
0354 {
0355 batadv_tt_local_size_mod(bat_priv, vid, -1);
0356 }
0357
0358
0359
0360
0361
0362
0363
0364
0365 static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
0366 unsigned short vid, int v)
0367 {
0368 struct batadv_orig_node_vlan *vlan;
0369
0370 vlan = batadv_orig_node_vlan_new(orig_node, vid);
0371 if (!vlan)
0372 return;
0373
0374 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
0375 spin_lock_bh(&orig_node->vlan_list_lock);
0376 if (!hlist_unhashed(&vlan->list)) {
0377 hlist_del_init_rcu(&vlan->list);
0378 batadv_orig_node_vlan_put(vlan);
0379 }
0380 spin_unlock_bh(&orig_node->vlan_list_lock);
0381 }
0382
0383 batadv_orig_node_vlan_put(vlan);
0384 }
0385
0386
0387
0388
0389
0390
0391
0392 static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
0393 unsigned short vid)
0394 {
0395 batadv_tt_global_size_mod(orig_node, vid, 1);
0396 }
0397
0398
0399
0400
0401
0402
0403
0404 static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
0405 unsigned short vid)
0406 {
0407 batadv_tt_global_size_mod(orig_node, vid, -1);
0408 }
0409
0410
0411
0412
0413
0414 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
0415 {
0416 struct batadv_tt_orig_list_entry *orig_entry;
0417
0418 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
0419
0420 kmem_cache_free(batadv_tt_orig_cache, orig_entry);
0421 }
0422
0423
0424
0425
0426
0427
0428 static void batadv_tt_orig_list_entry_release(struct kref *ref)
0429 {
0430 struct batadv_tt_orig_list_entry *orig_entry;
0431
0432 orig_entry = container_of(ref, struct batadv_tt_orig_list_entry,
0433 refcount);
0434
0435 batadv_orig_node_put(orig_entry->orig_node);
0436 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
0437 }
0438
0439
0440
0441
0442
0443
0444 static void
0445 batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry *orig_entry)
0446 {
0447 if (!orig_entry)
0448 return;
0449
0450 kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
0451 }
0452
0453
0454
0455
0456
0457
0458
0459 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
0460 struct batadv_tt_local_entry *tt_local_entry,
0461 u8 event_flags)
0462 {
0463 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
0464 struct batadv_tt_common_entry *common = &tt_local_entry->common;
0465 u8 flags = common->flags | event_flags;
0466 bool event_removed = false;
0467 bool del_op_requested, del_op_entry;
0468
0469 tt_change_node = kmem_cache_alloc(batadv_tt_change_cache, GFP_ATOMIC);
0470 if (!tt_change_node)
0471 return;
0472
0473 tt_change_node->change.flags = flags;
0474 memset(tt_change_node->change.reserved, 0,
0475 sizeof(tt_change_node->change.reserved));
0476 ether_addr_copy(tt_change_node->change.addr, common->addr);
0477 tt_change_node->change.vid = htons(common->vid);
0478
0479 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
0480
0481
0482 spin_lock_bh(&bat_priv->tt.changes_list_lock);
0483 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
0484 list) {
0485 if (!batadv_compare_eth(entry->change.addr, common->addr))
0486 continue;
0487
0488
0489
0490
0491
0492
0493
0494
0495 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
0496 if (!del_op_requested && del_op_entry)
0497 goto del;
0498 if (del_op_requested && !del_op_entry)
0499 goto del;
0500
0501
0502
0503
0504 if (!del_op_requested && !del_op_entry)
0505 entry->change.flags = flags;
0506
0507 continue;
0508 del:
0509 list_del(&entry->list);
0510 kmem_cache_free(batadv_tt_change_cache, entry);
0511 kmem_cache_free(batadv_tt_change_cache, tt_change_node);
0512 event_removed = true;
0513 goto unlock;
0514 }
0515
0516
0517 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
0518
0519 unlock:
0520 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
0521
0522 if (event_removed)
0523 atomic_dec(&bat_priv->tt.local_changes);
0524 else
0525 atomic_inc(&bat_priv->tt.local_changes);
0526 }
0527
0528
0529
0530
0531
0532
0533
0534 static int batadv_tt_len(int changes_num)
0535 {
0536 return changes_num * sizeof(struct batadv_tvlv_tt_change);
0537 }
0538
0539
0540
0541
0542
0543
0544
0545 static u16 batadv_tt_entries(u16 tt_len)
0546 {
0547 return tt_len / batadv_tt_len(1);
0548 }
0549
0550
0551
0552
0553
0554
0555
0556
0557 static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
0558 {
0559 u16 num_vlan = 0;
0560 u16 tt_local_entries = 0;
0561 struct batadv_softif_vlan *vlan;
0562 int hdr_size;
0563
0564 rcu_read_lock();
0565 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
0566 num_vlan++;
0567 tt_local_entries += atomic_read(&vlan->tt.num_entries);
0568 }
0569 rcu_read_unlock();
0570
0571
0572 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
0573 hdr_size += sizeof(struct batadv_tvlv_hdr);
0574 hdr_size += sizeof(struct batadv_tvlv_tt_data);
0575 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
0576
0577 return hdr_size + batadv_tt_len(tt_local_entries);
0578 }
0579
0580 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
0581 {
0582 if (bat_priv->tt.local_hash)
0583 return 0;
0584
0585 bat_priv->tt.local_hash = batadv_hash_new(1024);
0586
0587 if (!bat_priv->tt.local_hash)
0588 return -ENOMEM;
0589
0590 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
0591 &batadv_tt_local_hash_lock_class_key);
0592
0593 return 0;
0594 }
0595
0596 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
0597 struct batadv_tt_global_entry *tt_global,
0598 const char *message)
0599 {
0600 struct batadv_tt_global_entry *tt_removed_entry;
0601 struct hlist_node *tt_removed_node;
0602
0603 batadv_dbg(BATADV_DBG_TT, bat_priv,
0604 "Deleting global tt entry %pM (vid: %d): %s\n",
0605 tt_global->common.addr,
0606 batadv_print_vid(tt_global->common.vid), message);
0607
0608 tt_removed_node = batadv_hash_remove(bat_priv->tt.global_hash,
0609 batadv_compare_tt,
0610 batadv_choose_tt,
0611 &tt_global->common);
0612 if (!tt_removed_node)
0613 return;
0614
0615
0616 tt_removed_entry = hlist_entry(tt_removed_node,
0617 struct batadv_tt_global_entry,
0618 common.hash_entry);
0619 batadv_tt_global_entry_put(tt_removed_entry);
0620 }
0621
0622
0623
0624
0625
0626
0627
0628
0629
0630
0631
0632
0633
0634
0635 bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
0636 unsigned short vid, int ifindex, u32 mark)
0637 {
0638 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
0639 struct batadv_tt_local_entry *tt_local;
0640 struct batadv_tt_global_entry *tt_global = NULL;
0641 struct net *net = dev_net(soft_iface);
0642 struct batadv_softif_vlan *vlan;
0643 struct net_device *in_dev = NULL;
0644 struct batadv_hard_iface *in_hardif = NULL;
0645 struct hlist_head *head;
0646 struct batadv_tt_orig_list_entry *orig_entry;
0647 int hash_added, table_size, packet_size_max;
0648 bool ret = false;
0649 bool roamed_back = false;
0650 u8 remote_flags;
0651 u32 match_mark;
0652
0653 if (ifindex != BATADV_NULL_IFINDEX)
0654 in_dev = dev_get_by_index(net, ifindex);
0655
0656 if (in_dev)
0657 in_hardif = batadv_hardif_get_by_netdev(in_dev);
0658
0659 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
0660
0661 if (!is_multicast_ether_addr(addr))
0662 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
0663
0664 if (tt_local) {
0665 tt_local->last_seen = jiffies;
0666 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
0667 batadv_dbg(BATADV_DBG_TT, bat_priv,
0668 "Re-adding pending client %pM (vid: %d)\n",
0669 addr, batadv_print_vid(vid));
0670
0671
0672
0673
0674
0675 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
0676 goto add_event;
0677 }
0678
0679 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
0680 batadv_dbg(BATADV_DBG_TT, bat_priv,
0681 "Roaming client %pM (vid: %d) came back to its original location\n",
0682 addr, batadv_print_vid(vid));
0683
0684
0685
0686
0687
0688 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
0689 roamed_back = true;
0690 }
0691 goto check_roaming;
0692 }
0693
0694
0695 table_size = batadv_tt_local_table_transmit_size(bat_priv);
0696 table_size += batadv_tt_len(1);
0697 packet_size_max = atomic_read(&bat_priv->packet_size_max);
0698 if (table_size > packet_size_max) {
0699 net_ratelimited_function(batadv_info, soft_iface,
0700 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
0701 table_size, packet_size_max, addr);
0702 goto out;
0703 }
0704
0705 tt_local = kmem_cache_alloc(batadv_tl_cache, GFP_ATOMIC);
0706 if (!tt_local)
0707 goto out;
0708
0709
0710 vlan = batadv_softif_vlan_get(bat_priv, vid);
0711 if (!vlan) {
0712 net_ratelimited_function(batadv_info, soft_iface,
0713 "adding TT local entry %pM to non-existent VLAN %d\n",
0714 addr, batadv_print_vid(vid));
0715 kmem_cache_free(batadv_tl_cache, tt_local);
0716 tt_local = NULL;
0717 goto out;
0718 }
0719
0720 batadv_dbg(BATADV_DBG_TT, bat_priv,
0721 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
0722 addr, batadv_print_vid(vid),
0723 (u8)atomic_read(&bat_priv->tt.vn));
0724
0725 ether_addr_copy(tt_local->common.addr, addr);
0726
0727
0728
0729
0730 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
0731 tt_local->common.vid = vid;
0732 if (batadv_is_wifi_hardif(in_hardif))
0733 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
0734 kref_init(&tt_local->common.refcount);
0735 tt_local->last_seen = jiffies;
0736 tt_local->common.added_at = tt_local->last_seen;
0737 tt_local->vlan = vlan;
0738
0739
0740
0741
0742 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
0743 is_multicast_ether_addr(addr))
0744 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
0745
0746 kref_get(&tt_local->common.refcount);
0747 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
0748 batadv_choose_tt, &tt_local->common,
0749 &tt_local->common.hash_entry);
0750
0751 if (unlikely(hash_added != 0)) {
0752
0753 batadv_tt_local_entry_put(tt_local);
0754 goto out;
0755 }
0756
0757 add_event:
0758 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
0759
0760 check_roaming:
0761
0762
0763
0764 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
0765
0766 head = &tt_global->orig_list;
0767 rcu_read_lock();
0768 hlist_for_each_entry_rcu(orig_entry, head, list) {
0769 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
0770 tt_global->common.vid,
0771 orig_entry->orig_node);
0772 }
0773 rcu_read_unlock();
0774 if (roamed_back) {
0775 batadv_tt_global_free(bat_priv, tt_global,
0776 "Roaming canceled");
0777 tt_global = NULL;
0778 } else {
0779
0780
0781
0782 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
0783 tt_global->roam_at = jiffies;
0784 }
0785 }
0786
0787
0788
0789
0790 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
0791
0792 if (batadv_is_wifi_hardif(in_hardif))
0793 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
0794 else
0795 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
0796
0797
0798
0799
0800
0801 match_mark = (mark & bat_priv->isolation_mark_mask);
0802 if (bat_priv->isolation_mark_mask &&
0803 match_mark == bat_priv->isolation_mark)
0804 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
0805 else
0806 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
0807
0808
0809
0810
0811 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
0812 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
0813
0814 ret = true;
0815 out:
0816 batadv_hardif_put(in_hardif);
0817 dev_put(in_dev);
0818 batadv_tt_local_entry_put(tt_local);
0819 batadv_tt_global_entry_put(tt_global);
0820 return ret;
0821 }
0822
0823
0824
0825
0826
0827
0828
0829
0830
0831
0832
0833
0834
0835
0836
0837
0838
0839
0840 static u16
0841 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
0842 struct batadv_tvlv_tt_data **tt_data,
0843 struct batadv_tvlv_tt_change **tt_change,
0844 s32 *tt_len)
0845 {
0846 u16 num_vlan = 0;
0847 u16 num_entries = 0;
0848 u16 change_offset;
0849 u16 tvlv_len;
0850 struct batadv_tvlv_tt_vlan_data *tt_vlan;
0851 struct batadv_orig_node_vlan *vlan;
0852 u8 *tt_change_ptr;
0853
0854 spin_lock_bh(&orig_node->vlan_list_lock);
0855 hlist_for_each_entry(vlan, &orig_node->vlan_list, list) {
0856 num_vlan++;
0857 num_entries += atomic_read(&vlan->tt.num_entries);
0858 }
0859
0860 change_offset = sizeof(**tt_data);
0861 change_offset += num_vlan * sizeof(*tt_vlan);
0862
0863
0864 if (*tt_len < 0)
0865 *tt_len = batadv_tt_len(num_entries);
0866
0867 tvlv_len = *tt_len;
0868 tvlv_len += change_offset;
0869
0870 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
0871 if (!*tt_data) {
0872 *tt_len = 0;
0873 goto out;
0874 }
0875
0876 (*tt_data)->flags = BATADV_NO_FLAGS;
0877 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
0878 (*tt_data)->num_vlan = htons(num_vlan);
0879
0880 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
0881 hlist_for_each_entry(vlan, &orig_node->vlan_list, list) {
0882 tt_vlan->vid = htons(vlan->vid);
0883 tt_vlan->crc = htonl(vlan->tt.crc);
0884 tt_vlan->reserved = 0;
0885
0886 tt_vlan++;
0887 }
0888
0889 tt_change_ptr = (u8 *)*tt_data + change_offset;
0890 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
0891
0892 out:
0893 spin_unlock_bh(&orig_node->vlan_list_lock);
0894 return tvlv_len;
0895 }
0896
0897
0898
0899
0900
0901
0902
0903
0904
0905
0906
0907
0908
0909
0910
0911
0912
0913
0914
0915 static u16
0916 batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
0917 struct batadv_tvlv_tt_data **tt_data,
0918 struct batadv_tvlv_tt_change **tt_change,
0919 s32 *tt_len)
0920 {
0921 struct batadv_tvlv_tt_vlan_data *tt_vlan;
0922 struct batadv_softif_vlan *vlan;
0923 u16 num_vlan = 0;
0924 u16 vlan_entries = 0;
0925 u16 total_entries = 0;
0926 u16 tvlv_len;
0927 u8 *tt_change_ptr;
0928 int change_offset;
0929
0930 spin_lock_bh(&bat_priv->softif_vlan_list_lock);
0931 hlist_for_each_entry(vlan, &bat_priv->softif_vlan_list, list) {
0932 vlan_entries = atomic_read(&vlan->tt.num_entries);
0933 if (vlan_entries < 1)
0934 continue;
0935
0936 num_vlan++;
0937 total_entries += vlan_entries;
0938 }
0939
0940 change_offset = sizeof(**tt_data);
0941 change_offset += num_vlan * sizeof(*tt_vlan);
0942
0943
0944 if (*tt_len < 0)
0945 *tt_len = batadv_tt_len(total_entries);
0946
0947 tvlv_len = *tt_len;
0948 tvlv_len += change_offset;
0949
0950 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
0951 if (!*tt_data) {
0952 tvlv_len = 0;
0953 goto out;
0954 }
0955
0956 (*tt_data)->flags = BATADV_NO_FLAGS;
0957 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
0958 (*tt_data)->num_vlan = htons(num_vlan);
0959
0960 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
0961 hlist_for_each_entry(vlan, &bat_priv->softif_vlan_list, list) {
0962 vlan_entries = atomic_read(&vlan->tt.num_entries);
0963 if (vlan_entries < 1)
0964 continue;
0965
0966 tt_vlan->vid = htons(vlan->vid);
0967 tt_vlan->crc = htonl(vlan->tt.crc);
0968 tt_vlan->reserved = 0;
0969
0970 tt_vlan++;
0971 }
0972
0973 tt_change_ptr = (u8 *)*tt_data + change_offset;
0974 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
0975
0976 out:
0977 spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
0978 return tvlv_len;
0979 }
0980
0981
0982
0983
0984
0985
0986 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
0987 {
0988 struct batadv_tt_change_node *entry, *safe;
0989 struct batadv_tvlv_tt_data *tt_data;
0990 struct batadv_tvlv_tt_change *tt_change;
0991 int tt_diff_len, tt_change_len = 0;
0992 int tt_diff_entries_num = 0;
0993 int tt_diff_entries_count = 0;
0994 u16 tvlv_len;
0995
0996 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
0997 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
0998
0999
1000
1001
1002 if (tt_diff_len > bat_priv->soft_iface->mtu)
1003 tt_diff_len = 0;
1004
1005 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
1006 &tt_change, &tt_diff_len);
1007 if (!tvlv_len)
1008 return;
1009
1010 tt_data->flags = BATADV_TT_OGM_DIFF;
1011
1012 if (tt_diff_len == 0)
1013 goto container_register;
1014
1015 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1016 atomic_set(&bat_priv->tt.local_changes, 0);
1017
1018 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1019 list) {
1020 if (tt_diff_entries_count < tt_diff_entries_num) {
1021 memcpy(tt_change + tt_diff_entries_count,
1022 &entry->change,
1023 sizeof(struct batadv_tvlv_tt_change));
1024 tt_diff_entries_count++;
1025 }
1026 list_del(&entry->list);
1027 kmem_cache_free(batadv_tt_change_cache, entry);
1028 }
1029 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1030
1031
1032 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1033 kfree(bat_priv->tt.last_changeset);
1034 bat_priv->tt.last_changeset_len = 0;
1035 bat_priv->tt.last_changeset = NULL;
1036 tt_change_len = batadv_tt_len(tt_diff_entries_count);
1037
1038 if (tt_diff_entries_count > 0) {
1039
1040
1041
1042 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
1043 if (bat_priv->tt.last_changeset) {
1044 memcpy(bat_priv->tt.last_changeset,
1045 tt_change, tt_change_len);
1046 bat_priv->tt.last_changeset_len = tt_diff_len;
1047 }
1048 }
1049 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1050
1051 container_register:
1052 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
1053 tvlv_len);
1054 kfree(tt_data);
1055 }
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067 static int
1068 batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid,
1069 struct netlink_callback *cb,
1070 struct batadv_priv *bat_priv,
1071 struct batadv_tt_common_entry *common)
1072 {
1073 void *hdr;
1074 struct batadv_softif_vlan *vlan;
1075 struct batadv_tt_local_entry *local;
1076 unsigned int last_seen_msecs;
1077 u32 crc;
1078
1079 local = container_of(common, struct batadv_tt_local_entry, common);
1080 last_seen_msecs = jiffies_to_msecs(jiffies - local->last_seen);
1081
1082 vlan = batadv_softif_vlan_get(bat_priv, common->vid);
1083 if (!vlan)
1084 return 0;
1085
1086 crc = vlan->tt.crc;
1087
1088 batadv_softif_vlan_put(vlan);
1089
1090 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
1091 &batadv_netlink_family, NLM_F_MULTI,
1092 BATADV_CMD_GET_TRANSTABLE_LOCAL);
1093 if (!hdr)
1094 return -ENOBUFS;
1095
1096 genl_dump_check_consistent(cb, hdr);
1097
1098 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
1099 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
1100 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
1101 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, common->flags))
1102 goto nla_put_failure;
1103
1104 if (!(common->flags & BATADV_TT_CLIENT_NOPURGE) &&
1105 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, last_seen_msecs))
1106 goto nla_put_failure;
1107
1108 genlmsg_end(msg, hdr);
1109 return 0;
1110
1111 nla_put_failure:
1112 genlmsg_cancel(msg, hdr);
1113 return -EMSGSIZE;
1114 }
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128 static int
1129 batadv_tt_local_dump_bucket(struct sk_buff *msg, u32 portid,
1130 struct netlink_callback *cb,
1131 struct batadv_priv *bat_priv,
1132 struct batadv_hashtable *hash, unsigned int bucket,
1133 int *idx_s)
1134 {
1135 struct batadv_tt_common_entry *common;
1136 int idx = 0;
1137
1138 spin_lock_bh(&hash->list_locks[bucket]);
1139 cb->seq = atomic_read(&hash->generation) << 1 | 1;
1140
1141 hlist_for_each_entry(common, &hash->table[bucket], hash_entry) {
1142 if (idx++ < *idx_s)
1143 continue;
1144
1145 if (batadv_tt_local_dump_entry(msg, portid, cb, bat_priv,
1146 common)) {
1147 spin_unlock_bh(&hash->list_locks[bucket]);
1148 *idx_s = idx - 1;
1149 return -EMSGSIZE;
1150 }
1151 }
1152 spin_unlock_bh(&hash->list_locks[bucket]);
1153
1154 *idx_s = 0;
1155 return 0;
1156 }
1157
1158
1159
1160
1161
1162
1163
1164
1165 int batadv_tt_local_dump(struct sk_buff *msg, struct netlink_callback *cb)
1166 {
1167 struct net *net = sock_net(cb->skb->sk);
1168 struct net_device *soft_iface;
1169 struct batadv_priv *bat_priv;
1170 struct batadv_hard_iface *primary_if = NULL;
1171 struct batadv_hashtable *hash;
1172 int ret;
1173 int ifindex;
1174 int bucket = cb->args[0];
1175 int idx = cb->args[1];
1176 int portid = NETLINK_CB(cb->skb).portid;
1177
1178 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1179 if (!ifindex)
1180 return -EINVAL;
1181
1182 soft_iface = dev_get_by_index(net, ifindex);
1183 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1184 ret = -ENODEV;
1185 goto out;
1186 }
1187
1188 bat_priv = netdev_priv(soft_iface);
1189
1190 primary_if = batadv_primary_if_get_selected(bat_priv);
1191 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1192 ret = -ENOENT;
1193 goto out;
1194 }
1195
1196 hash = bat_priv->tt.local_hash;
1197
1198 while (bucket < hash->size) {
1199 if (batadv_tt_local_dump_bucket(msg, portid, cb, bat_priv,
1200 hash, bucket, &idx))
1201 break;
1202
1203 bucket++;
1204 }
1205
1206 ret = msg->len;
1207
1208 out:
1209 batadv_hardif_put(primary_if);
1210 dev_put(soft_iface);
1211
1212 cb->args[0] = bucket;
1213 cb->args[1] = idx;
1214
1215 return ret;
1216 }
1217
1218 static void
1219 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1220 struct batadv_tt_local_entry *tt_local_entry,
1221 u16 flags, const char *message)
1222 {
1223 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
1224
1225
1226
1227
1228
1229 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
1230
1231 batadv_dbg(BATADV_DBG_TT, bat_priv,
1232 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1233 tt_local_entry->common.addr,
1234 batadv_print_vid(tt_local_entry->common.vid), message);
1235 }
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247 u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1248 unsigned short vid, const char *message,
1249 bool roaming)
1250 {
1251 struct batadv_tt_local_entry *tt_removed_entry;
1252 struct batadv_tt_local_entry *tt_local_entry;
1253 u16 flags, curr_flags = BATADV_NO_FLAGS;
1254 struct hlist_node *tt_removed_node;
1255
1256 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1257 if (!tt_local_entry)
1258 goto out;
1259
1260 curr_flags = tt_local_entry->common.flags;
1261
1262 flags = BATADV_TT_CLIENT_DEL;
1263
1264
1265
1266
1267 if (roaming) {
1268 flags |= BATADV_TT_CLIENT_ROAM;
1269
1270 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1271 }
1272
1273 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1274 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1275 message);
1276 goto out;
1277 }
1278
1279
1280
1281 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1282
1283 tt_removed_node = batadv_hash_remove(bat_priv->tt.local_hash,
1284 batadv_compare_tt,
1285 batadv_choose_tt,
1286 &tt_local_entry->common);
1287 if (!tt_removed_node)
1288 goto out;
1289
1290
1291 tt_removed_entry = hlist_entry(tt_removed_node,
1292 struct batadv_tt_local_entry,
1293 common.hash_entry);
1294 batadv_tt_local_entry_put(tt_removed_entry);
1295
1296 out:
1297 batadv_tt_local_entry_put(tt_local_entry);
1298
1299 return curr_flags;
1300 }
1301
1302
1303
1304
1305
1306
1307
1308
1309 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1310 struct hlist_head *head,
1311 int timeout)
1312 {
1313 struct batadv_tt_local_entry *tt_local_entry;
1314 struct batadv_tt_common_entry *tt_common_entry;
1315 struct hlist_node *node_tmp;
1316
1317 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1318 hash_entry) {
1319 tt_local_entry = container_of(tt_common_entry,
1320 struct batadv_tt_local_entry,
1321 common);
1322 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1323 continue;
1324
1325
1326 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1327 continue;
1328
1329 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1330 continue;
1331
1332 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1333 BATADV_TT_CLIENT_DEL, "timed out");
1334 }
1335 }
1336
1337
1338
1339
1340
1341
1342
1343 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1344 int timeout)
1345 {
1346 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1347 struct hlist_head *head;
1348 spinlock_t *list_lock;
1349 u32 i;
1350
1351 for (i = 0; i < hash->size; i++) {
1352 head = &hash->table[i];
1353 list_lock = &hash->list_locks[i];
1354
1355 spin_lock_bh(list_lock);
1356 batadv_tt_local_purge_list(bat_priv, head, timeout);
1357 spin_unlock_bh(list_lock);
1358 }
1359 }
1360
1361 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1362 {
1363 struct batadv_hashtable *hash;
1364 spinlock_t *list_lock;
1365 struct batadv_tt_common_entry *tt_common_entry;
1366 struct batadv_tt_local_entry *tt_local;
1367 struct hlist_node *node_tmp;
1368 struct hlist_head *head;
1369 u32 i;
1370
1371 if (!bat_priv->tt.local_hash)
1372 return;
1373
1374 hash = bat_priv->tt.local_hash;
1375
1376 for (i = 0; i < hash->size; i++) {
1377 head = &hash->table[i];
1378 list_lock = &hash->list_locks[i];
1379
1380 spin_lock_bh(list_lock);
1381 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1382 head, hash_entry) {
1383 hlist_del_rcu(&tt_common_entry->hash_entry);
1384 tt_local = container_of(tt_common_entry,
1385 struct batadv_tt_local_entry,
1386 common);
1387
1388 batadv_tt_local_entry_put(tt_local);
1389 }
1390 spin_unlock_bh(list_lock);
1391 }
1392
1393 batadv_hash_destroy(hash);
1394
1395 bat_priv->tt.local_hash = NULL;
1396 }
1397
1398 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1399 {
1400 if (bat_priv->tt.global_hash)
1401 return 0;
1402
1403 bat_priv->tt.global_hash = batadv_hash_new(1024);
1404
1405 if (!bat_priv->tt.global_hash)
1406 return -ENOMEM;
1407
1408 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1409 &batadv_tt_global_hash_lock_class_key);
1410
1411 return 0;
1412 }
1413
1414 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1415 {
1416 struct batadv_tt_change_node *entry, *safe;
1417
1418 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1419
1420 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1421 list) {
1422 list_del(&entry->list);
1423 kmem_cache_free(batadv_tt_change_cache, entry);
1424 }
1425
1426 atomic_set(&bat_priv->tt.local_changes, 0);
1427 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1428 }
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441 static struct batadv_tt_orig_list_entry *
1442 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1443 const struct batadv_orig_node *orig_node)
1444 {
1445 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1446 const struct hlist_head *head;
1447
1448 rcu_read_lock();
1449 head = &entry->orig_list;
1450 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1451 if (tmp_orig_entry->orig_node != orig_node)
1452 continue;
1453 if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
1454 continue;
1455
1456 orig_entry = tmp_orig_entry;
1457 break;
1458 }
1459 rcu_read_unlock();
1460
1461 return orig_entry;
1462 }
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476 static bool
1477 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1478 const struct batadv_orig_node *orig_node,
1479 u8 *flags)
1480 {
1481 struct batadv_tt_orig_list_entry *orig_entry;
1482 bool found = false;
1483
1484 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1485 if (orig_entry) {
1486 found = true;
1487
1488 if (flags)
1489 *flags = orig_entry->flags;
1490
1491 batadv_tt_orig_list_entry_put(orig_entry);
1492 }
1493
1494 return found;
1495 }
1496
1497
1498
1499
1500
1501
1502
1503
1504 static void
1505 batadv_tt_global_sync_flags(struct batadv_tt_global_entry *tt_global)
1506 {
1507 struct batadv_tt_orig_list_entry *orig_entry;
1508 const struct hlist_head *head;
1509 u16 flags = BATADV_NO_FLAGS;
1510
1511 rcu_read_lock();
1512 head = &tt_global->orig_list;
1513 hlist_for_each_entry_rcu(orig_entry, head, list)
1514 flags |= orig_entry->flags;
1515 rcu_read_unlock();
1516
1517 flags |= tt_global->common.flags & (~BATADV_TT_SYNC_MASK);
1518 tt_global->common.flags = flags;
1519 }
1520
1521
1522
1523
1524
1525
1526
1527
1528 static void
1529 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1530 struct batadv_orig_node *orig_node, int ttvn,
1531 u8 flags)
1532 {
1533 struct batadv_tt_orig_list_entry *orig_entry;
1534
1535 spin_lock_bh(&tt_global->list_lock);
1536
1537 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1538 if (orig_entry) {
1539
1540
1541
1542 orig_entry->ttvn = ttvn;
1543 orig_entry->flags = flags;
1544 goto sync_flags;
1545 }
1546
1547 orig_entry = kmem_cache_zalloc(batadv_tt_orig_cache, GFP_ATOMIC);
1548 if (!orig_entry)
1549 goto out;
1550
1551 INIT_HLIST_NODE(&orig_entry->list);
1552 kref_get(&orig_node->refcount);
1553 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1554 orig_entry->orig_node = orig_node;
1555 orig_entry->ttvn = ttvn;
1556 orig_entry->flags = flags;
1557 kref_init(&orig_entry->refcount);
1558
1559 kref_get(&orig_entry->refcount);
1560 hlist_add_head_rcu(&orig_entry->list,
1561 &tt_global->orig_list);
1562 atomic_inc(&tt_global->orig_list_count);
1563
1564 sync_flags:
1565 batadv_tt_global_sync_flags(tt_global);
1566 out:
1567 batadv_tt_orig_list_entry_put(orig_entry);
1568
1569 spin_unlock_bh(&tt_global->list_lock);
1570 }
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1592 struct batadv_orig_node *orig_node,
1593 const unsigned char *tt_addr,
1594 unsigned short vid, u16 flags, u8 ttvn)
1595 {
1596 struct batadv_tt_global_entry *tt_global_entry;
1597 struct batadv_tt_local_entry *tt_local_entry;
1598 bool ret = false;
1599 int hash_added;
1600 struct batadv_tt_common_entry *common;
1601 u16 local_flags;
1602
1603
1604 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1605 return true;
1606
1607 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1608 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1609
1610
1611
1612
1613
1614 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1615 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1616 goto out;
1617
1618 if (!tt_global_entry) {
1619 tt_global_entry = kmem_cache_zalloc(batadv_tg_cache,
1620 GFP_ATOMIC);
1621 if (!tt_global_entry)
1622 goto out;
1623
1624 common = &tt_global_entry->common;
1625 ether_addr_copy(common->addr, tt_addr);
1626 common->vid = vid;
1627
1628 if (!is_multicast_ether_addr(common->addr))
1629 common->flags = flags & (~BATADV_TT_SYNC_MASK);
1630
1631 tt_global_entry->roam_at = 0;
1632
1633
1634
1635
1636 if (flags & BATADV_TT_CLIENT_ROAM)
1637 tt_global_entry->roam_at = jiffies;
1638 kref_init(&common->refcount);
1639 common->added_at = jiffies;
1640
1641 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1642 atomic_set(&tt_global_entry->orig_list_count, 0);
1643 spin_lock_init(&tt_global_entry->list_lock);
1644
1645 kref_get(&common->refcount);
1646 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1647 batadv_compare_tt,
1648 batadv_choose_tt, common,
1649 &common->hash_entry);
1650
1651 if (unlikely(hash_added != 0)) {
1652
1653 batadv_tt_global_entry_put(tt_global_entry);
1654 goto out_remove;
1655 }
1656 } else {
1657 common = &tt_global_entry->common;
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668 if (flags & BATADV_TT_CLIENT_TEMP) {
1669 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1670 goto out;
1671 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1672 orig_node, NULL))
1673 goto out_remove;
1674 batadv_tt_global_del_orig_list(tt_global_entry);
1675 goto add_orig_entry;
1676 }
1677
1678
1679
1680
1681
1682
1683
1684 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1685 batadv_tt_global_del_orig_list(tt_global_entry);
1686 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1687 }
1688
1689
1690
1691
1692
1693 if (!is_multicast_ether_addr(common->addr))
1694 common->flags |= flags & (~BATADV_TT_SYNC_MASK);
1695
1696
1697
1698
1699
1700
1701
1702
1703 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1704 batadv_tt_global_del_orig_list(tt_global_entry);
1705 common->flags &= ~BATADV_TT_CLIENT_ROAM;
1706 tt_global_entry->roam_at = 0;
1707 }
1708 }
1709 add_orig_entry:
1710
1711 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn,
1712 flags & BATADV_TT_SYNC_MASK);
1713
1714 batadv_dbg(BATADV_DBG_TT, bat_priv,
1715 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1716 common->addr, batadv_print_vid(common->vid),
1717 orig_node->orig);
1718 ret = true;
1719
1720 out_remove:
1721
1722
1723
1724 if (is_multicast_ether_addr(tt_addr))
1725 goto out;
1726
1727
1728 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1729 "global tt received",
1730 flags & BATADV_TT_CLIENT_ROAM);
1731 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1732
1733 if (!(flags & BATADV_TT_CLIENT_ROAM))
1734
1735
1736
1737 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1738
1739 out:
1740 batadv_tt_global_entry_put(tt_global_entry);
1741 batadv_tt_local_entry_put(tt_local_entry);
1742 return ret;
1743 }
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753 static struct batadv_tt_orig_list_entry *
1754 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1755 struct batadv_tt_global_entry *tt_global_entry)
1756 {
1757 struct batadv_neigh_node *router, *best_router = NULL;
1758 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1759 struct hlist_head *head;
1760 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1761
1762 head = &tt_global_entry->orig_list;
1763 hlist_for_each_entry_rcu(orig_entry, head, list) {
1764 router = batadv_orig_router_get(orig_entry->orig_node,
1765 BATADV_IF_DEFAULT);
1766 if (!router)
1767 continue;
1768
1769 if (best_router &&
1770 bao->neigh.cmp(router, BATADV_IF_DEFAULT, best_router,
1771 BATADV_IF_DEFAULT) <= 0) {
1772 batadv_neigh_node_put(router);
1773 continue;
1774 }
1775
1776
1777 batadv_neigh_node_put(best_router);
1778
1779 best_entry = orig_entry;
1780 best_router = router;
1781 }
1782
1783 batadv_neigh_node_put(best_router);
1784
1785 return best_entry;
1786 }
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799 static int
1800 batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1801 struct batadv_tt_common_entry *common,
1802 struct batadv_tt_orig_list_entry *orig,
1803 bool best)
1804 {
1805 u16 flags = (common->flags & (~BATADV_TT_SYNC_MASK)) | orig->flags;
1806 void *hdr;
1807 struct batadv_orig_node_vlan *vlan;
1808 u8 last_ttvn;
1809 u32 crc;
1810
1811 vlan = batadv_orig_node_vlan_get(orig->orig_node,
1812 common->vid);
1813 if (!vlan)
1814 return 0;
1815
1816 crc = vlan->tt.crc;
1817
1818 batadv_orig_node_vlan_put(vlan);
1819
1820 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1821 NLM_F_MULTI,
1822 BATADV_CMD_GET_TRANSTABLE_GLOBAL);
1823 if (!hdr)
1824 return -ENOBUFS;
1825
1826 last_ttvn = atomic_read(&orig->orig_node->last_ttvn);
1827
1828 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
1829 nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1830 orig->orig_node->orig) ||
1831 nla_put_u8(msg, BATADV_ATTR_TT_TTVN, orig->ttvn) ||
1832 nla_put_u8(msg, BATADV_ATTR_TT_LAST_TTVN, last_ttvn) ||
1833 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
1834 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
1835 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, flags))
1836 goto nla_put_failure;
1837
1838 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
1839 goto nla_put_failure;
1840
1841 genlmsg_end(msg, hdr);
1842 return 0;
1843
1844 nla_put_failure:
1845 genlmsg_cancel(msg, hdr);
1846 return -EMSGSIZE;
1847 }
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862 static int
1863 batadv_tt_global_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1864 struct batadv_priv *bat_priv,
1865 struct batadv_tt_common_entry *common, int *sub_s)
1866 {
1867 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1868 struct batadv_tt_global_entry *global;
1869 struct hlist_head *head;
1870 int sub = 0;
1871 bool best;
1872
1873 global = container_of(common, struct batadv_tt_global_entry, common);
1874 best_entry = batadv_transtable_best_orig(bat_priv, global);
1875 head = &global->orig_list;
1876
1877 hlist_for_each_entry_rcu(orig_entry, head, list) {
1878 if (sub++ < *sub_s)
1879 continue;
1880
1881 best = (orig_entry == best_entry);
1882
1883 if (batadv_tt_global_dump_subentry(msg, portid, seq, common,
1884 orig_entry, best)) {
1885 *sub_s = sub - 1;
1886 return -EMSGSIZE;
1887 }
1888 }
1889
1890 *sub_s = 0;
1891 return 0;
1892 }
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906 static int
1907 batadv_tt_global_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
1908 struct batadv_priv *bat_priv,
1909 struct hlist_head *head, int *idx_s, int *sub)
1910 {
1911 struct batadv_tt_common_entry *common;
1912 int idx = 0;
1913
1914 rcu_read_lock();
1915 hlist_for_each_entry_rcu(common, head, hash_entry) {
1916 if (idx++ < *idx_s)
1917 continue;
1918
1919 if (batadv_tt_global_dump_entry(msg, portid, seq, bat_priv,
1920 common, sub)) {
1921 rcu_read_unlock();
1922 *idx_s = idx - 1;
1923 return -EMSGSIZE;
1924 }
1925 }
1926 rcu_read_unlock();
1927
1928 *idx_s = 0;
1929 *sub = 0;
1930 return 0;
1931 }
1932
1933
1934
1935
1936
1937
1938
1939
1940 int batadv_tt_global_dump(struct sk_buff *msg, struct netlink_callback *cb)
1941 {
1942 struct net *net = sock_net(cb->skb->sk);
1943 struct net_device *soft_iface;
1944 struct batadv_priv *bat_priv;
1945 struct batadv_hard_iface *primary_if = NULL;
1946 struct batadv_hashtable *hash;
1947 struct hlist_head *head;
1948 int ret;
1949 int ifindex;
1950 int bucket = cb->args[0];
1951 int idx = cb->args[1];
1952 int sub = cb->args[2];
1953 int portid = NETLINK_CB(cb->skb).portid;
1954
1955 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1956 if (!ifindex)
1957 return -EINVAL;
1958
1959 soft_iface = dev_get_by_index(net, ifindex);
1960 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1961 ret = -ENODEV;
1962 goto out;
1963 }
1964
1965 bat_priv = netdev_priv(soft_iface);
1966
1967 primary_if = batadv_primary_if_get_selected(bat_priv);
1968 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1969 ret = -ENOENT;
1970 goto out;
1971 }
1972
1973 hash = bat_priv->tt.global_hash;
1974
1975 while (bucket < hash->size) {
1976 head = &hash->table[bucket];
1977
1978 if (batadv_tt_global_dump_bucket(msg, portid,
1979 cb->nlh->nlmsg_seq, bat_priv,
1980 head, &idx, &sub))
1981 break;
1982
1983 bucket++;
1984 }
1985
1986 ret = msg->len;
1987
1988 out:
1989 batadv_hardif_put(primary_if);
1990 dev_put(soft_iface);
1991
1992 cb->args[0] = bucket;
1993 cb->args[1] = idx;
1994 cb->args[2] = sub;
1995
1996 return ret;
1997 }
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010 static void
2011 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
2012 struct batadv_tt_orig_list_entry *orig_entry)
2013 {
2014 lockdep_assert_held(&tt_global_entry->list_lock);
2015
2016 batadv_tt_global_size_dec(orig_entry->orig_node,
2017 tt_global_entry->common.vid);
2018 atomic_dec(&tt_global_entry->orig_list_count);
2019
2020
2021
2022 hlist_del_rcu(&orig_entry->list);
2023 batadv_tt_orig_list_entry_put(orig_entry);
2024 }
2025
2026
2027 static void
2028 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
2029 {
2030 struct hlist_head *head;
2031 struct hlist_node *safe;
2032 struct batadv_tt_orig_list_entry *orig_entry;
2033
2034 spin_lock_bh(&tt_global_entry->list_lock);
2035 head = &tt_global_entry->orig_list;
2036 hlist_for_each_entry_safe(orig_entry, safe, head, list)
2037 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
2038 spin_unlock_bh(&tt_global_entry->list_lock);
2039 }
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051 static void
2052 batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
2053 struct batadv_tt_global_entry *tt_global_entry,
2054 struct batadv_orig_node *orig_node,
2055 const char *message)
2056 {
2057 struct hlist_head *head;
2058 struct hlist_node *safe;
2059 struct batadv_tt_orig_list_entry *orig_entry;
2060 unsigned short vid;
2061
2062 spin_lock_bh(&tt_global_entry->list_lock);
2063 head = &tt_global_entry->orig_list;
2064 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
2065 if (orig_entry->orig_node == orig_node) {
2066 vid = tt_global_entry->common.vid;
2067 batadv_dbg(BATADV_DBG_TT, bat_priv,
2068 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
2069 orig_node->orig,
2070 tt_global_entry->common.addr,
2071 batadv_print_vid(vid), message);
2072 _batadv_tt_global_del_orig_entry(tt_global_entry,
2073 orig_entry);
2074 }
2075 }
2076 spin_unlock_bh(&tt_global_entry->list_lock);
2077 }
2078
2079
2080
2081
2082
2083 static void
2084 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
2085 struct batadv_tt_global_entry *tt_global_entry,
2086 struct batadv_orig_node *orig_node,
2087 const char *message)
2088 {
2089 bool last_entry = true;
2090 struct hlist_head *head;
2091 struct batadv_tt_orig_list_entry *orig_entry;
2092
2093
2094
2095
2096
2097 rcu_read_lock();
2098 head = &tt_global_entry->orig_list;
2099 hlist_for_each_entry_rcu(orig_entry, head, list) {
2100 if (orig_entry->orig_node != orig_node) {
2101 last_entry = false;
2102 break;
2103 }
2104 }
2105 rcu_read_unlock();
2106
2107 if (last_entry) {
2108
2109 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
2110 tt_global_entry->roam_at = jiffies;
2111 } else {
2112
2113
2114
2115 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2116 orig_node, message);
2117 }
2118 }
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
2131 struct batadv_orig_node *orig_node,
2132 const unsigned char *addr, unsigned short vid,
2133 const char *message, bool roaming)
2134 {
2135 struct batadv_tt_global_entry *tt_global_entry;
2136 struct batadv_tt_local_entry *local_entry = NULL;
2137
2138 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2139 if (!tt_global_entry)
2140 goto out;
2141
2142 if (!roaming) {
2143 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2144 orig_node, message);
2145
2146 if (hlist_empty(&tt_global_entry->orig_list))
2147 batadv_tt_global_free(bat_priv, tt_global_entry,
2148 message);
2149
2150 goto out;
2151 }
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166 local_entry = batadv_tt_local_hash_find(bat_priv,
2167 tt_global_entry->common.addr,
2168 vid);
2169 if (local_entry) {
2170
2171 batadv_tt_global_del_orig_list(tt_global_entry);
2172 batadv_tt_global_free(bat_priv, tt_global_entry, message);
2173 } else {
2174
2175 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
2176 orig_node, message);
2177 }
2178
2179 out:
2180 batadv_tt_global_entry_put(tt_global_entry);
2181 batadv_tt_local_entry_put(local_entry);
2182 }
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
2194 struct batadv_orig_node *orig_node,
2195 s32 match_vid,
2196 const char *message)
2197 {
2198 struct batadv_tt_global_entry *tt_global;
2199 struct batadv_tt_common_entry *tt_common_entry;
2200 u32 i;
2201 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2202 struct hlist_node *safe;
2203 struct hlist_head *head;
2204 spinlock_t *list_lock;
2205 unsigned short vid;
2206
2207 if (!hash)
2208 return;
2209
2210 for (i = 0; i < hash->size; i++) {
2211 head = &hash->table[i];
2212 list_lock = &hash->list_locks[i];
2213
2214 spin_lock_bh(list_lock);
2215 hlist_for_each_entry_safe(tt_common_entry, safe,
2216 head, hash_entry) {
2217
2218 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
2219 continue;
2220
2221 tt_global = container_of(tt_common_entry,
2222 struct batadv_tt_global_entry,
2223 common);
2224
2225 batadv_tt_global_del_orig_node(bat_priv, tt_global,
2226 orig_node, message);
2227
2228 if (hlist_empty(&tt_global->orig_list)) {
2229 vid = tt_global->common.vid;
2230 batadv_dbg(BATADV_DBG_TT, bat_priv,
2231 "Deleting global tt entry %pM (vid: %d): %s\n",
2232 tt_global->common.addr,
2233 batadv_print_vid(vid), message);
2234 hlist_del_rcu(&tt_common_entry->hash_entry);
2235 batadv_tt_global_entry_put(tt_global);
2236 }
2237 }
2238 spin_unlock_bh(list_lock);
2239 }
2240 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2241 }
2242
2243 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
2244 char **msg)
2245 {
2246 bool purge = false;
2247 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
2248 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
2249
2250 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
2251 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
2252 purge = true;
2253 *msg = "Roaming timeout\n";
2254 }
2255
2256 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
2257 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
2258 purge = true;
2259 *msg = "Temporary client timeout\n";
2260 }
2261
2262 return purge;
2263 }
2264
2265 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
2266 {
2267 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2268 struct hlist_head *head;
2269 struct hlist_node *node_tmp;
2270 spinlock_t *list_lock;
2271 u32 i;
2272 char *msg = NULL;
2273 struct batadv_tt_common_entry *tt_common;
2274 struct batadv_tt_global_entry *tt_global;
2275
2276 for (i = 0; i < hash->size; i++) {
2277 head = &hash->table[i];
2278 list_lock = &hash->list_locks[i];
2279
2280 spin_lock_bh(list_lock);
2281 hlist_for_each_entry_safe(tt_common, node_tmp, head,
2282 hash_entry) {
2283 tt_global = container_of(tt_common,
2284 struct batadv_tt_global_entry,
2285 common);
2286
2287 if (!batadv_tt_global_to_purge(tt_global, &msg))
2288 continue;
2289
2290 batadv_dbg(BATADV_DBG_TT, bat_priv,
2291 "Deleting global tt entry %pM (vid: %d): %s\n",
2292 tt_global->common.addr,
2293 batadv_print_vid(tt_global->common.vid),
2294 msg);
2295
2296 hlist_del_rcu(&tt_common->hash_entry);
2297
2298 batadv_tt_global_entry_put(tt_global);
2299 }
2300 spin_unlock_bh(list_lock);
2301 }
2302 }
2303
2304 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
2305 {
2306 struct batadv_hashtable *hash;
2307 spinlock_t *list_lock;
2308 struct batadv_tt_common_entry *tt_common_entry;
2309 struct batadv_tt_global_entry *tt_global;
2310 struct hlist_node *node_tmp;
2311 struct hlist_head *head;
2312 u32 i;
2313
2314 if (!bat_priv->tt.global_hash)
2315 return;
2316
2317 hash = bat_priv->tt.global_hash;
2318
2319 for (i = 0; i < hash->size; i++) {
2320 head = &hash->table[i];
2321 list_lock = &hash->list_locks[i];
2322
2323 spin_lock_bh(list_lock);
2324 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
2325 head, hash_entry) {
2326 hlist_del_rcu(&tt_common_entry->hash_entry);
2327 tt_global = container_of(tt_common_entry,
2328 struct batadv_tt_global_entry,
2329 common);
2330 batadv_tt_global_entry_put(tt_global);
2331 }
2332 spin_unlock_bh(list_lock);
2333 }
2334
2335 batadv_hash_destroy(hash);
2336
2337 bat_priv->tt.global_hash = NULL;
2338 }
2339
2340 static bool
2341 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2342 struct batadv_tt_global_entry *tt_global_entry)
2343 {
2344 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2345 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2346 return true;
2347
2348
2349 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2350 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2351 return true;
2352
2353 return false;
2354 }
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2371 const u8 *src,
2372 const u8 *addr,
2373 unsigned short vid)
2374 {
2375 struct batadv_tt_local_entry *tt_local_entry = NULL;
2376 struct batadv_tt_global_entry *tt_global_entry = NULL;
2377 struct batadv_orig_node *orig_node = NULL;
2378 struct batadv_tt_orig_list_entry *best_entry;
2379
2380 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2381 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2382 if (!tt_local_entry ||
2383 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2384 goto out;
2385 }
2386
2387 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2388 if (!tt_global_entry)
2389 goto out;
2390
2391
2392
2393
2394 if (tt_local_entry &&
2395 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2396 goto out;
2397
2398 rcu_read_lock();
2399 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2400
2401 if (best_entry)
2402 orig_node = best_entry->orig_node;
2403 if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
2404 orig_node = NULL;
2405 rcu_read_unlock();
2406
2407 out:
2408 batadv_tt_global_entry_put(tt_global_entry);
2409 batadv_tt_local_entry_put(tt_local_entry);
2410
2411 return orig_node;
2412 }
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438 static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2439 struct batadv_orig_node *orig_node,
2440 unsigned short vid)
2441 {
2442 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2443 struct batadv_tt_orig_list_entry *tt_orig;
2444 struct batadv_tt_common_entry *tt_common;
2445 struct batadv_tt_global_entry *tt_global;
2446 struct hlist_head *head;
2447 u32 i, crc_tmp, crc = 0;
2448 u8 flags;
2449 __be16 tmp_vid;
2450
2451 for (i = 0; i < hash->size; i++) {
2452 head = &hash->table[i];
2453
2454 rcu_read_lock();
2455 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2456 tt_global = container_of(tt_common,
2457 struct batadv_tt_global_entry,
2458 common);
2459
2460
2461
2462 if (tt_common->vid != vid)
2463 continue;
2464
2465
2466
2467
2468
2469
2470 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2471 continue;
2472
2473
2474
2475
2476 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2477 continue;
2478
2479
2480
2481
2482 tt_orig = batadv_tt_global_orig_entry_find(tt_global,
2483 orig_node);
2484 if (!tt_orig)
2485 continue;
2486
2487
2488
2489
2490 tmp_vid = htons(tt_common->vid);
2491 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2492
2493
2494
2495
2496 flags = tt_orig->flags;
2497 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2498
2499 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2500
2501 batadv_tt_orig_list_entry_put(tt_orig);
2502 }
2503 rcu_read_unlock();
2504 }
2505
2506 return crc;
2507 }
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519 static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2520 unsigned short vid)
2521 {
2522 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2523 struct batadv_tt_common_entry *tt_common;
2524 struct hlist_head *head;
2525 u32 i, crc_tmp, crc = 0;
2526 u8 flags;
2527 __be16 tmp_vid;
2528
2529 for (i = 0; i < hash->size; i++) {
2530 head = &hash->table[i];
2531
2532 rcu_read_lock();
2533 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2534
2535
2536
2537 if (tt_common->vid != vid)
2538 continue;
2539
2540
2541
2542
2543 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2544 continue;
2545
2546
2547
2548
2549 tmp_vid = htons(tt_common->vid);
2550 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2551
2552
2553
2554
2555 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2556 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2557
2558 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2559 }
2560 rcu_read_unlock();
2561 }
2562
2563 return crc;
2564 }
2565
2566
2567
2568
2569
2570 static void batadv_tt_req_node_release(struct kref *ref)
2571 {
2572 struct batadv_tt_req_node *tt_req_node;
2573
2574 tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
2575
2576 kmem_cache_free(batadv_tt_req_cache, tt_req_node);
2577 }
2578
2579
2580
2581
2582
2583
2584 static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
2585 {
2586 if (!tt_req_node)
2587 return;
2588
2589 kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
2590 }
2591
2592 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2593 {
2594 struct batadv_tt_req_node *node;
2595 struct hlist_node *safe;
2596
2597 spin_lock_bh(&bat_priv->tt.req_list_lock);
2598
2599 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2600 hlist_del_init(&node->list);
2601 batadv_tt_req_node_put(node);
2602 }
2603
2604 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2605 }
2606
2607 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2608 struct batadv_orig_node *orig_node,
2609 const void *tt_buff,
2610 u16 tt_buff_len)
2611 {
2612
2613
2614
2615 spin_lock_bh(&orig_node->tt_buff_lock);
2616 if (tt_buff_len > 0) {
2617 kfree(orig_node->tt_buff);
2618 orig_node->tt_buff_len = 0;
2619 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2620 if (orig_node->tt_buff) {
2621 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2622 orig_node->tt_buff_len = tt_buff_len;
2623 }
2624 }
2625 spin_unlock_bh(&orig_node->tt_buff_lock);
2626 }
2627
2628 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2629 {
2630 struct batadv_tt_req_node *node;
2631 struct hlist_node *safe;
2632
2633 spin_lock_bh(&bat_priv->tt.req_list_lock);
2634 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2635 if (batadv_has_timed_out(node->issued_at,
2636 BATADV_TT_REQUEST_TIMEOUT)) {
2637 hlist_del_init(&node->list);
2638 batadv_tt_req_node_put(node);
2639 }
2640 }
2641 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2642 }
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652 static struct batadv_tt_req_node *
2653 batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2654 struct batadv_orig_node *orig_node)
2655 {
2656 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2657
2658 spin_lock_bh(&bat_priv->tt.req_list_lock);
2659 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2660 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2661 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2662 BATADV_TT_REQUEST_TIMEOUT))
2663 goto unlock;
2664 }
2665
2666 tt_req_node = kmem_cache_alloc(batadv_tt_req_cache, GFP_ATOMIC);
2667 if (!tt_req_node)
2668 goto unlock;
2669
2670 kref_init(&tt_req_node->refcount);
2671 ether_addr_copy(tt_req_node->addr, orig_node->orig);
2672 tt_req_node->issued_at = jiffies;
2673
2674 kref_get(&tt_req_node->refcount);
2675 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2676 unlock:
2677 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2678 return tt_req_node;
2679 }
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692 static bool batadv_tt_local_valid(const void *entry_ptr,
2693 const void *data_ptr,
2694 u8 *flags)
2695 {
2696 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2697
2698 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2699 return false;
2700
2701 if (flags)
2702 *flags = tt_common_entry->flags;
2703
2704 return true;
2705 }
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719 static bool batadv_tt_global_valid(const void *entry_ptr,
2720 const void *data_ptr,
2721 u8 *flags)
2722 {
2723 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2724 const struct batadv_tt_global_entry *tt_global_entry;
2725 const struct batadv_orig_node *orig_node = data_ptr;
2726
2727 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2728 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2729 return false;
2730
2731 tt_global_entry = container_of(tt_common_entry,
2732 struct batadv_tt_global_entry,
2733 common);
2734
2735 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node,
2736 flags);
2737 }
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2753 struct batadv_hashtable *hash,
2754 void *tvlv_buff, u16 tt_len,
2755 bool (*valid_cb)(const void *,
2756 const void *,
2757 u8 *flags),
2758 void *cb_data)
2759 {
2760 struct batadv_tt_common_entry *tt_common_entry;
2761 struct batadv_tvlv_tt_change *tt_change;
2762 struct hlist_head *head;
2763 u16 tt_tot, tt_num_entries = 0;
2764 u8 flags;
2765 bool ret;
2766 u32 i;
2767
2768 tt_tot = batadv_tt_entries(tt_len);
2769 tt_change = tvlv_buff;
2770
2771 if (!valid_cb)
2772 return;
2773
2774 rcu_read_lock();
2775 for (i = 0; i < hash->size; i++) {
2776 head = &hash->table[i];
2777
2778 hlist_for_each_entry_rcu(tt_common_entry,
2779 head, hash_entry) {
2780 if (tt_tot == tt_num_entries)
2781 break;
2782
2783 ret = valid_cb(tt_common_entry, cb_data, &flags);
2784 if (!ret)
2785 continue;
2786
2787 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
2788 tt_change->flags = flags;
2789 tt_change->vid = htons(tt_common_entry->vid);
2790 memset(tt_change->reserved, 0,
2791 sizeof(tt_change->reserved));
2792
2793 tt_num_entries++;
2794 tt_change++;
2795 }
2796 }
2797 rcu_read_unlock();
2798 }
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2810 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2811 u16 num_vlan)
2812 {
2813 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2814 struct batadv_orig_node_vlan *vlan;
2815 int i, orig_num_vlan;
2816 u32 crc;
2817
2818
2819 for (i = 0; i < num_vlan; i++) {
2820 tt_vlan_tmp = tt_vlan + i;
2821
2822
2823
2824
2825 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2826 orig_node->orig,
2827 ntohs(tt_vlan_tmp->vid)))
2828 continue;
2829
2830 vlan = batadv_orig_node_vlan_get(orig_node,
2831 ntohs(tt_vlan_tmp->vid));
2832 if (!vlan)
2833 return false;
2834
2835 crc = vlan->tt.crc;
2836 batadv_orig_node_vlan_put(vlan);
2837
2838 if (crc != ntohl(tt_vlan_tmp->crc))
2839 return false;
2840 }
2841
2842
2843
2844
2845 rcu_read_lock();
2846 orig_num_vlan = 0;
2847 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
2848 orig_num_vlan++;
2849 rcu_read_unlock();
2850
2851 if (orig_num_vlan > num_vlan)
2852 return false;
2853
2854 return true;
2855 }
2856
2857
2858
2859
2860
2861 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2862 {
2863 struct batadv_softif_vlan *vlan;
2864
2865
2866 rcu_read_lock();
2867 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2868 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2869 }
2870 rcu_read_unlock();
2871 }
2872
2873
2874
2875
2876
2877
2878 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2879 struct batadv_orig_node *orig_node)
2880 {
2881 struct batadv_orig_node_vlan *vlan;
2882 u32 crc;
2883
2884
2885 rcu_read_lock();
2886 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2887
2888
2889
2890 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2891 vlan->vid))
2892 continue;
2893
2894 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2895 vlan->tt.crc = crc;
2896 }
2897 rcu_read_unlock();
2898 }
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912 static bool batadv_send_tt_request(struct batadv_priv *bat_priv,
2913 struct batadv_orig_node *dst_orig_node,
2914 u8 ttvn,
2915 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2916 u16 num_vlan, bool full_table)
2917 {
2918 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2919 struct batadv_tt_req_node *tt_req_node = NULL;
2920 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2921 struct batadv_hard_iface *primary_if;
2922 bool ret = false;
2923 int i, size;
2924
2925 primary_if = batadv_primary_if_get_selected(bat_priv);
2926 if (!primary_if)
2927 goto out;
2928
2929
2930
2931
2932 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
2933 if (!tt_req_node)
2934 goto out;
2935
2936 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2937 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2938 if (!tvlv_tt_data)
2939 goto out;
2940
2941 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2942 tvlv_tt_data->ttvn = ttvn;
2943 tvlv_tt_data->num_vlan = htons(num_vlan);
2944
2945
2946
2947
2948 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2949 for (i = 0; i < num_vlan; i++) {
2950 tt_vlan_req->vid = tt_vlan->vid;
2951 tt_vlan_req->crc = tt_vlan->crc;
2952
2953 tt_vlan_req++;
2954 tt_vlan++;
2955 }
2956
2957 if (full_table)
2958 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2959
2960 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
2961 dst_orig_node->orig, full_table ? 'F' : '.');
2962
2963 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
2964 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2965 dst_orig_node->orig, BATADV_TVLV_TT, 1,
2966 tvlv_tt_data, size);
2967 ret = true;
2968
2969 out:
2970 batadv_hardif_put(primary_if);
2971
2972 if (ret && tt_req_node) {
2973 spin_lock_bh(&bat_priv->tt.req_list_lock);
2974 if (!hlist_unhashed(&tt_req_node->list)) {
2975 hlist_del_init(&tt_req_node->list);
2976 batadv_tt_req_node_put(tt_req_node);
2977 }
2978 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2979 }
2980
2981 batadv_tt_req_node_put(tt_req_node);
2982
2983 kfree(tvlv_tt_data);
2984 return ret;
2985 }
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2998 struct batadv_tvlv_tt_data *tt_data,
2999 u8 *req_src, u8 *req_dst)
3000 {
3001 struct batadv_orig_node *req_dst_orig_node;
3002 struct batadv_orig_node *res_dst_orig_node = NULL;
3003 struct batadv_tvlv_tt_change *tt_change;
3004 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3005 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3006 bool ret = false, full_table;
3007 u8 orig_ttvn, req_ttvn;
3008 u16 tvlv_len;
3009 s32 tt_len;
3010
3011 batadv_dbg(BATADV_DBG_TT, bat_priv,
3012 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
3013 req_src, tt_data->ttvn, req_dst,
3014 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3015
3016
3017 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
3018 if (!req_dst_orig_node)
3019 goto out;
3020
3021 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
3022 if (!res_dst_orig_node)
3023 goto out;
3024
3025 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
3026 req_ttvn = tt_data->ttvn;
3027
3028 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3029
3030 if (orig_ttvn != req_ttvn ||
3031 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
3032 ntohs(tt_data->num_vlan)))
3033 goto out;
3034
3035
3036 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
3037 !req_dst_orig_node->tt_buff)
3038 full_table = true;
3039 else
3040 full_table = false;
3041
3042
3043
3044
3045 if (!full_table) {
3046 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
3047 tt_len = req_dst_orig_node->tt_buff_len;
3048
3049 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3050 &tvlv_tt_data,
3051 &tt_change,
3052 &tt_len);
3053 if (!tt_len)
3054 goto unlock;
3055
3056
3057 memcpy(tt_change, req_dst_orig_node->tt_buff,
3058 req_dst_orig_node->tt_buff_len);
3059 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3060 } else {
3061
3062
3063
3064 tt_len = -1;
3065 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3066 &tvlv_tt_data,
3067 &tt_change,
3068 &tt_len);
3069 if (!tt_len)
3070 goto out;
3071
3072
3073 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
3074 tt_change, tt_len,
3075 batadv_tt_global_valid,
3076 req_dst_orig_node);
3077 }
3078
3079
3080 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
3081 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
3082 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
3083 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
3084 res_dst_orig_node->orig);
3085 goto out;
3086 }
3087
3088 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3089 tvlv_tt_data->ttvn = req_ttvn;
3090
3091 if (full_table)
3092 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3093
3094 batadv_dbg(BATADV_DBG_TT, bat_priv,
3095 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
3096 res_dst_orig_node->orig, req_dst_orig_node->orig,
3097 full_table ? 'F' : '.', req_ttvn);
3098
3099 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3100
3101 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
3102 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3103 tvlv_len);
3104
3105 ret = true;
3106 goto out;
3107
3108 unlock:
3109 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3110
3111 out:
3112 batadv_orig_node_put(res_dst_orig_node);
3113 batadv_orig_node_put(req_dst_orig_node);
3114 kfree(tvlv_tt_data);
3115 return ret;
3116 }
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
3128 struct batadv_tvlv_tt_data *tt_data,
3129 u8 *req_src)
3130 {
3131 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3132 struct batadv_hard_iface *primary_if = NULL;
3133 struct batadv_tvlv_tt_change *tt_change;
3134 struct batadv_orig_node *orig_node;
3135 u8 my_ttvn, req_ttvn;
3136 u16 tvlv_len;
3137 bool full_table;
3138 s32 tt_len;
3139
3140 batadv_dbg(BATADV_DBG_TT, bat_priv,
3141 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
3142 req_src, tt_data->ttvn,
3143 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3144
3145 spin_lock_bh(&bat_priv->tt.commit_lock);
3146
3147 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3148 req_ttvn = tt_data->ttvn;
3149
3150 orig_node = batadv_orig_hash_find(bat_priv, req_src);
3151 if (!orig_node)
3152 goto out;
3153
3154 primary_if = batadv_primary_if_get_selected(bat_priv);
3155 if (!primary_if)
3156 goto out;
3157
3158
3159
3160
3161 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
3162 !bat_priv->tt.last_changeset)
3163 full_table = true;
3164 else
3165 full_table = false;
3166
3167
3168
3169
3170 if (!full_table) {
3171 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
3172
3173 tt_len = bat_priv->tt.last_changeset_len;
3174 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3175 &tvlv_tt_data,
3176 &tt_change,
3177 &tt_len);
3178 if (!tt_len || !tvlv_len)
3179 goto unlock;
3180
3181
3182 memcpy(tt_change, bat_priv->tt.last_changeset,
3183 bat_priv->tt.last_changeset_len);
3184 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3185 } else {
3186 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3187
3188
3189
3190
3191 tt_len = -1;
3192 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3193 &tvlv_tt_data,
3194 &tt_change,
3195 &tt_len);
3196 if (!tt_len || !tvlv_len)
3197 goto out;
3198
3199
3200 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
3201 tt_change, tt_len,
3202 batadv_tt_local_valid, NULL);
3203 }
3204
3205 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3206 tvlv_tt_data->ttvn = req_ttvn;
3207
3208 if (full_table)
3209 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3210
3211 batadv_dbg(BATADV_DBG_TT, bat_priv,
3212 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
3213 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
3214
3215 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3216
3217 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3218 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3219 tvlv_len);
3220
3221 goto out;
3222
3223 unlock:
3224 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3225 out:
3226 spin_unlock_bh(&bat_priv->tt.commit_lock);
3227 batadv_orig_node_put(orig_node);
3228 batadv_hardif_put(primary_if);
3229 kfree(tvlv_tt_data);
3230
3231 return true;
3232 }
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
3244 struct batadv_tvlv_tt_data *tt_data,
3245 u8 *req_src, u8 *req_dst)
3246 {
3247 if (batadv_is_my_mac(bat_priv, req_dst))
3248 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
3249 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
3250 req_dst);
3251 }
3252
3253 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
3254 struct batadv_orig_node *orig_node,
3255 struct batadv_tvlv_tt_change *tt_change,
3256 u16 tt_num_changes, u8 ttvn)
3257 {
3258 int i;
3259 int roams;
3260
3261 for (i = 0; i < tt_num_changes; i++) {
3262 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
3263 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
3264 batadv_tt_global_del(bat_priv, orig_node,
3265 (tt_change + i)->addr,
3266 ntohs((tt_change + i)->vid),
3267 "tt removed by changes",
3268 roams);
3269 } else {
3270 if (!batadv_tt_global_add(bat_priv, orig_node,
3271 (tt_change + i)->addr,
3272 ntohs((tt_change + i)->vid),
3273 (tt_change + i)->flags, ttvn))
3274
3275
3276
3277
3278
3279
3280 return;
3281 }
3282 }
3283 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
3284 }
3285
3286 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
3287 struct batadv_tvlv_tt_change *tt_change,
3288 u8 ttvn, u8 *resp_src,
3289 u16 num_entries)
3290 {
3291 struct batadv_orig_node *orig_node;
3292
3293 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3294 if (!orig_node)
3295 goto out;
3296
3297
3298 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
3299 "Received full table");
3300
3301 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
3302 ttvn);
3303
3304 spin_lock_bh(&orig_node->tt_buff_lock);
3305 kfree(orig_node->tt_buff);
3306 orig_node->tt_buff_len = 0;
3307 orig_node->tt_buff = NULL;
3308 spin_unlock_bh(&orig_node->tt_buff_lock);
3309
3310 atomic_set(&orig_node->last_ttvn, ttvn);
3311
3312 out:
3313 batadv_orig_node_put(orig_node);
3314 }
3315
3316 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3317 struct batadv_orig_node *orig_node,
3318 u16 tt_num_changes, u8 ttvn,
3319 struct batadv_tvlv_tt_change *tt_change)
3320 {
3321 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3322 tt_num_changes, ttvn);
3323
3324 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3325 batadv_tt_len(tt_num_changes));
3326 atomic_set(&orig_node->last_ttvn, ttvn);
3327 }
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337 bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
3338 unsigned short vid)
3339 {
3340 struct batadv_tt_local_entry *tt_local_entry;
3341 bool ret = false;
3342
3343 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3344 if (!tt_local_entry)
3345 goto out;
3346
3347
3348
3349 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3350 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
3351 goto out;
3352 ret = true;
3353 out:
3354 batadv_tt_local_entry_put(tt_local_entry);
3355 return ret;
3356 }
3357
3358
3359
3360
3361
3362
3363
3364
3365 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3366 struct batadv_tvlv_tt_data *tt_data,
3367 u8 *resp_src, u16 num_entries)
3368 {
3369 struct batadv_tt_req_node *node;
3370 struct hlist_node *safe;
3371 struct batadv_orig_node *orig_node = NULL;
3372 struct batadv_tvlv_tt_change *tt_change;
3373 u8 *tvlv_ptr = (u8 *)tt_data;
3374 u16 change_offset;
3375
3376 batadv_dbg(BATADV_DBG_TT, bat_priv,
3377 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3378 resp_src, tt_data->ttvn, num_entries,
3379 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3380
3381 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3382 if (!orig_node)
3383 goto out;
3384
3385 spin_lock_bh(&orig_node->tt_lock);
3386
3387 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3388 change_offset *= ntohs(tt_data->num_vlan);
3389 change_offset += sizeof(*tt_data);
3390 tvlv_ptr += change_offset;
3391
3392 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
3393 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
3394 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3395 resp_src, num_entries);
3396 } else {
3397 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3398 tt_data->ttvn, tt_change);
3399 }
3400
3401
3402 batadv_tt_global_update_crc(bat_priv, orig_node);
3403
3404 spin_unlock_bh(&orig_node->tt_lock);
3405
3406
3407 spin_lock_bh(&bat_priv->tt.req_list_lock);
3408 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
3409 if (!batadv_compare_eth(node->addr, resp_src))
3410 continue;
3411 hlist_del_init(&node->list);
3412 batadv_tt_req_node_put(node);
3413 }
3414
3415 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3416 out:
3417 batadv_orig_node_put(orig_node);
3418 }
3419
3420 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3421 {
3422 struct batadv_tt_roam_node *node, *safe;
3423
3424 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3425
3426 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3427 list_del(&node->list);
3428 kmem_cache_free(batadv_tt_roam_cache, node);
3429 }
3430
3431 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3432 }
3433
3434 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3435 {
3436 struct batadv_tt_roam_node *node, *safe;
3437
3438 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3439 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3440 if (!batadv_has_timed_out(node->first_time,
3441 BATADV_ROAMING_MAX_TIME))
3442 continue;
3443
3444 list_del(&node->list);
3445 kmem_cache_free(batadv_tt_roam_cache, node);
3446 }
3447 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3448 }
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3462 {
3463 struct batadv_tt_roam_node *tt_roam_node;
3464 bool ret = false;
3465
3466 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3467
3468
3469
3470 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3471 if (!batadv_compare_eth(tt_roam_node->addr, client))
3472 continue;
3473
3474 if (batadv_has_timed_out(tt_roam_node->first_time,
3475 BATADV_ROAMING_MAX_TIME))
3476 continue;
3477
3478 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3479
3480 goto unlock;
3481 ret = true;
3482 break;
3483 }
3484
3485 if (!ret) {
3486 tt_roam_node = kmem_cache_alloc(batadv_tt_roam_cache,
3487 GFP_ATOMIC);
3488 if (!tt_roam_node)
3489 goto unlock;
3490
3491 tt_roam_node->first_time = jiffies;
3492 atomic_set(&tt_roam_node->counter,
3493 BATADV_ROAMING_MAX_COUNT - 1);
3494 ether_addr_copy(tt_roam_node->addr, client);
3495
3496 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3497 ret = true;
3498 }
3499
3500 unlock:
3501 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3502 return ret;
3503 }
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3518 unsigned short vid,
3519 struct batadv_orig_node *orig_node)
3520 {
3521 struct batadv_hard_iface *primary_if;
3522 struct batadv_tvlv_roam_adv tvlv_roam;
3523
3524 primary_if = batadv_primary_if_get_selected(bat_priv);
3525 if (!primary_if)
3526 goto out;
3527
3528
3529
3530
3531 if (!batadv_tt_check_roam_count(bat_priv, client))
3532 goto out;
3533
3534 batadv_dbg(BATADV_DBG_TT, bat_priv,
3535 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3536 orig_node->orig, client, batadv_print_vid(vid));
3537
3538 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3539
3540 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3541 tvlv_roam.vid = htons(vid);
3542
3543 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3544 orig_node->orig, BATADV_TVLV_ROAM, 1,
3545 &tvlv_roam, sizeof(tvlv_roam));
3546
3547 out:
3548 batadv_hardif_put(primary_if);
3549 }
3550
3551 static void batadv_tt_purge(struct work_struct *work)
3552 {
3553 struct delayed_work *delayed_work;
3554 struct batadv_priv_tt *priv_tt;
3555 struct batadv_priv *bat_priv;
3556
3557 delayed_work = to_delayed_work(work);
3558 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3559 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3560
3561 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3562 batadv_tt_global_purge(bat_priv);
3563 batadv_tt_req_purge(bat_priv);
3564 batadv_tt_roam_purge(bat_priv);
3565
3566 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3567 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3568 }
3569
3570
3571
3572
3573
3574 void batadv_tt_free(struct batadv_priv *bat_priv)
3575 {
3576 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_ROAM, 1);
3577
3578 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3579 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3580
3581 cancel_delayed_work_sync(&bat_priv->tt.work);
3582
3583 batadv_tt_local_table_free(bat_priv);
3584 batadv_tt_global_table_free(bat_priv);
3585 batadv_tt_req_list_free(bat_priv);
3586 batadv_tt_changes_list_free(bat_priv);
3587 batadv_tt_roam_list_free(bat_priv);
3588
3589 kfree(bat_priv->tt.last_changeset);
3590 }
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3601 bool enable, bool count)
3602 {
3603 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3604 struct batadv_tt_common_entry *tt_common_entry;
3605 struct hlist_head *head;
3606 u32 i;
3607
3608 if (!hash)
3609 return;
3610
3611 for (i = 0; i < hash->size; i++) {
3612 head = &hash->table[i];
3613
3614 rcu_read_lock();
3615 hlist_for_each_entry_rcu(tt_common_entry,
3616 head, hash_entry) {
3617 if (enable) {
3618 if ((tt_common_entry->flags & flags) == flags)
3619 continue;
3620 tt_common_entry->flags |= flags;
3621 } else {
3622 if (!(tt_common_entry->flags & flags))
3623 continue;
3624 tt_common_entry->flags &= ~flags;
3625 }
3626
3627 if (!count)
3628 continue;
3629
3630 batadv_tt_local_size_inc(bat_priv,
3631 tt_common_entry->vid);
3632 }
3633 rcu_read_unlock();
3634 }
3635 }
3636
3637
3638 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3639 {
3640 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3641 struct batadv_tt_common_entry *tt_common;
3642 struct batadv_tt_local_entry *tt_local;
3643 struct hlist_node *node_tmp;
3644 struct hlist_head *head;
3645 spinlock_t *list_lock;
3646 u32 i;
3647
3648 if (!hash)
3649 return;
3650
3651 for (i = 0; i < hash->size; i++) {
3652 head = &hash->table[i];
3653 list_lock = &hash->list_locks[i];
3654
3655 spin_lock_bh(list_lock);
3656 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3657 hash_entry) {
3658 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3659 continue;
3660
3661 batadv_dbg(BATADV_DBG_TT, bat_priv,
3662 "Deleting local tt entry (%pM, vid: %d): pending\n",
3663 tt_common->addr,
3664 batadv_print_vid(tt_common->vid));
3665
3666 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3667 hlist_del_rcu(&tt_common->hash_entry);
3668 tt_local = container_of(tt_common,
3669 struct batadv_tt_local_entry,
3670 common);
3671
3672 batadv_tt_local_entry_put(tt_local);
3673 }
3674 spin_unlock_bh(list_lock);
3675 }
3676 }
3677
3678
3679
3680
3681
3682
3683
3684
3685 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3686 {
3687 lockdep_assert_held(&bat_priv->tt.commit_lock);
3688
3689 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3690 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3691 batadv_tt_tvlv_container_update(bat_priv);
3692 return;
3693 }
3694
3695 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3696
3697 batadv_tt_local_purge_pending_clients(bat_priv);
3698 batadv_tt_local_update_crc(bat_priv);
3699
3700
3701 atomic_inc(&bat_priv->tt.vn);
3702 batadv_dbg(BATADV_DBG_TT, bat_priv,
3703 "Local changes committed, updating to ttvn %u\n",
3704 (u8)atomic_read(&bat_priv->tt.vn));
3705
3706
3707 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3708 batadv_tt_tvlv_container_update(bat_priv);
3709 }
3710
3711
3712
3713
3714
3715
3716 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3717 {
3718 spin_lock_bh(&bat_priv->tt.commit_lock);
3719 batadv_tt_local_commit_changes_nolock(bat_priv);
3720 spin_unlock_bh(&bat_priv->tt.commit_lock);
3721 }
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3733 unsigned short vid)
3734 {
3735 struct batadv_tt_local_entry *tt_local_entry;
3736 struct batadv_tt_global_entry *tt_global_entry;
3737 struct batadv_softif_vlan *vlan;
3738 bool ret = false;
3739
3740 vlan = batadv_softif_vlan_get(bat_priv, vid);
3741 if (!vlan)
3742 return false;
3743
3744 if (!atomic_read(&vlan->ap_isolation))
3745 goto vlan_put;
3746
3747 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3748 if (!tt_local_entry)
3749 goto vlan_put;
3750
3751 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3752 if (!tt_global_entry)
3753 goto local_entry_put;
3754
3755 if (_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3756 ret = true;
3757
3758 batadv_tt_global_entry_put(tt_global_entry);
3759 local_entry_put:
3760 batadv_tt_local_entry_put(tt_local_entry);
3761 vlan_put:
3762 batadv_softif_vlan_put(vlan);
3763 return ret;
3764 }
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3778 struct batadv_orig_node *orig_node,
3779 const void *tt_buff, u16 tt_num_vlan,
3780 struct batadv_tvlv_tt_change *tt_change,
3781 u16 tt_num_changes, u8 ttvn)
3782 {
3783 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
3784 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3785 bool full_table = true;
3786 bool has_tt_init;
3787
3788 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3789 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3790 &orig_node->capa_initialized);
3791
3792
3793
3794
3795 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
3796
3797
3798
3799
3800
3801 if (!tt_num_changes) {
3802 full_table = false;
3803 goto request_table;
3804 }
3805
3806 spin_lock_bh(&orig_node->tt_lock);
3807
3808 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3809 ttvn, tt_change);
3810
3811
3812
3813
3814
3815 batadv_tt_global_update_crc(bat_priv, orig_node);
3816
3817 spin_unlock_bh(&orig_node->tt_lock);
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3829 tt_num_vlan))
3830 goto request_table;
3831 } else {
3832
3833
3834
3835 if (!has_tt_init || ttvn != orig_ttvn ||
3836 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3837 tt_num_vlan)) {
3838 request_table:
3839 batadv_dbg(BATADV_DBG_TT, bat_priv,
3840 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3841 orig_node->orig, ttvn, orig_ttvn,
3842 tt_num_changes);
3843 batadv_send_tt_request(bat_priv, orig_node, ttvn,
3844 tt_vlan, tt_num_vlan,
3845 full_table);
3846 return;
3847 }
3848 }
3849 }
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
3862 u8 *addr, unsigned short vid)
3863 {
3864 struct batadv_tt_global_entry *tt_global_entry;
3865 bool ret = false;
3866
3867 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3868 if (!tt_global_entry)
3869 goto out;
3870
3871 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3872 batadv_tt_global_entry_put(tt_global_entry);
3873 out:
3874 return ret;
3875 }
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
3888 u8 *addr, unsigned short vid)
3889 {
3890 struct batadv_tt_local_entry *tt_local_entry;
3891 bool ret = false;
3892
3893 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3894 if (!tt_local_entry)
3895 goto out;
3896
3897 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3898 batadv_tt_local_entry_put(tt_local_entry);
3899 out:
3900 return ret;
3901 }
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3913 struct batadv_orig_node *orig_node,
3914 const unsigned char *addr,
3915 unsigned short vid)
3916 {
3917
3918
3919
3920 if (batadv_bla_is_loopdetect_mac(addr))
3921 return false;
3922
3923 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
3924 BATADV_TT_CLIENT_TEMP,
3925 atomic_read(&orig_node->last_ttvn)))
3926 return false;
3927
3928 batadv_dbg(BATADV_DBG_TT, bat_priv,
3929 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3930 addr, batadv_print_vid(vid), orig_node->orig);
3931
3932 return true;
3933 }
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3944 {
3945 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3946 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3947 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3948 bool reduced = false;
3949
3950 spin_lock_bh(&bat_priv->tt.commit_lock);
3951
3952 while (true) {
3953 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3954 if (packet_size_max >= table_size)
3955 break;
3956
3957 batadv_tt_local_purge(bat_priv, timeout);
3958 batadv_tt_local_purge_pending_clients(bat_priv);
3959
3960 timeout /= 2;
3961 reduced = true;
3962 net_ratelimited_function(batadv_info, soft_iface,
3963 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3964 packet_size_max);
3965 }
3966
3967
3968
3969
3970 if (reduced)
3971 batadv_tt_local_commit_changes_nolock(bat_priv);
3972
3973 spin_unlock_bh(&bat_priv->tt.commit_lock);
3974 }
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3985 struct batadv_orig_node *orig,
3986 u8 flags, void *tvlv_value,
3987 u16 tvlv_value_len)
3988 {
3989 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3990 struct batadv_tvlv_tt_change *tt_change;
3991 struct batadv_tvlv_tt_data *tt_data;
3992 u16 num_entries, num_vlan;
3993
3994 if (tvlv_value_len < sizeof(*tt_data))
3995 return;
3996
3997 tt_data = tvlv_value;
3998 tvlv_value_len -= sizeof(*tt_data);
3999
4000 num_vlan = ntohs(tt_data->num_vlan);
4001
4002 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
4003 return;
4004
4005 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
4006 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
4007 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
4008
4009 num_entries = batadv_tt_entries(tvlv_value_len);
4010
4011 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
4012 num_entries, tt_data->ttvn);
4013 }
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
4028 u8 *src, u8 *dst,
4029 void *tvlv_value,
4030 u16 tvlv_value_len)
4031 {
4032 struct batadv_tvlv_tt_data *tt_data;
4033 u16 tt_vlan_len, tt_num_entries;
4034 char tt_flag;
4035 bool ret;
4036
4037 if (tvlv_value_len < sizeof(*tt_data))
4038 return NET_RX_SUCCESS;
4039
4040 tt_data = tvlv_value;
4041 tvlv_value_len -= sizeof(*tt_data);
4042
4043 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
4044 tt_vlan_len *= ntohs(tt_data->num_vlan);
4045
4046 if (tvlv_value_len < tt_vlan_len)
4047 return NET_RX_SUCCESS;
4048
4049 tvlv_value_len -= tt_vlan_len;
4050 tt_num_entries = batadv_tt_entries(tvlv_value_len);
4051
4052 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
4053 case BATADV_TT_REQUEST:
4054 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
4055
4056
4057
4058
4059 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
4060 if (!ret) {
4061 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4062 tt_flag = 'F';
4063 else
4064 tt_flag = '.';
4065
4066 batadv_dbg(BATADV_DBG_TT, bat_priv,
4067 "Routing TT_REQUEST to %pM [%c]\n",
4068 dst, tt_flag);
4069
4070 return NET_RX_DROP;
4071 }
4072 break;
4073 case BATADV_TT_RESPONSE:
4074 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
4075
4076 if (batadv_is_my_mac(bat_priv, dst)) {
4077 batadv_handle_tt_response(bat_priv, tt_data,
4078 src, tt_num_entries);
4079 return NET_RX_SUCCESS;
4080 }
4081
4082 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4083 tt_flag = 'F';
4084 else
4085 tt_flag = '.';
4086
4087 batadv_dbg(BATADV_DBG_TT, bat_priv,
4088 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
4089
4090
4091 return NET_RX_DROP;
4092 }
4093
4094 return NET_RX_SUCCESS;
4095 }
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
4110 u8 *src, u8 *dst,
4111 void *tvlv_value,
4112 u16 tvlv_value_len)
4113 {
4114 struct batadv_tvlv_roam_adv *roaming_adv;
4115 struct batadv_orig_node *orig_node = NULL;
4116
4117
4118
4119
4120
4121 if (!batadv_is_my_mac(bat_priv, dst))
4122 return NET_RX_DROP;
4123
4124 if (tvlv_value_len < sizeof(*roaming_adv))
4125 goto out;
4126
4127 orig_node = batadv_orig_hash_find(bat_priv, src);
4128 if (!orig_node)
4129 goto out;
4130
4131 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
4132 roaming_adv = tvlv_value;
4133
4134 batadv_dbg(BATADV_DBG_TT, bat_priv,
4135 "Received ROAMING_ADV from %pM (client %pM)\n",
4136 src, roaming_adv->client);
4137
4138 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
4139 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
4140 atomic_read(&orig_node->last_ttvn) + 1);
4141
4142 out:
4143 batadv_orig_node_put(orig_node);
4144 return NET_RX_SUCCESS;
4145 }
4146
4147
4148
4149
4150
4151
4152
4153 int batadv_tt_init(struct batadv_priv *bat_priv)
4154 {
4155 int ret;
4156
4157
4158 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
4159
4160 ret = batadv_tt_local_init(bat_priv);
4161 if (ret < 0)
4162 return ret;
4163
4164 ret = batadv_tt_global_init(bat_priv);
4165 if (ret < 0) {
4166 batadv_tt_local_table_free(bat_priv);
4167 return ret;
4168 }
4169
4170 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
4171 batadv_tt_tvlv_unicast_handler_v1,
4172 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
4173
4174 batadv_tvlv_handler_register(bat_priv, NULL,
4175 batadv_roam_tvlv_unicast_handler_v1,
4176 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
4177
4178 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
4179 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
4180 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
4181
4182 return 1;
4183 }
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194 bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
4195 const u8 *addr, unsigned short vid)
4196 {
4197 struct batadv_tt_global_entry *tt;
4198 bool ret;
4199
4200 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
4201 if (!tt)
4202 return false;
4203
4204 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
4205
4206 batadv_tt_global_entry_put(tt);
4207
4208 return ret;
4209 }
4210
4211
4212
4213
4214
4215
4216 int __init batadv_tt_cache_init(void)
4217 {
4218 size_t tl_size = sizeof(struct batadv_tt_local_entry);
4219 size_t tg_size = sizeof(struct batadv_tt_global_entry);
4220 size_t tt_orig_size = sizeof(struct batadv_tt_orig_list_entry);
4221 size_t tt_change_size = sizeof(struct batadv_tt_change_node);
4222 size_t tt_req_size = sizeof(struct batadv_tt_req_node);
4223 size_t tt_roam_size = sizeof(struct batadv_tt_roam_node);
4224
4225 batadv_tl_cache = kmem_cache_create("batadv_tl_cache", tl_size, 0,
4226 SLAB_HWCACHE_ALIGN, NULL);
4227 if (!batadv_tl_cache)
4228 return -ENOMEM;
4229
4230 batadv_tg_cache = kmem_cache_create("batadv_tg_cache", tg_size, 0,
4231 SLAB_HWCACHE_ALIGN, NULL);
4232 if (!batadv_tg_cache)
4233 goto err_tt_tl_destroy;
4234
4235 batadv_tt_orig_cache = kmem_cache_create("batadv_tt_orig_cache",
4236 tt_orig_size, 0,
4237 SLAB_HWCACHE_ALIGN, NULL);
4238 if (!batadv_tt_orig_cache)
4239 goto err_tt_tg_destroy;
4240
4241 batadv_tt_change_cache = kmem_cache_create("batadv_tt_change_cache",
4242 tt_change_size, 0,
4243 SLAB_HWCACHE_ALIGN, NULL);
4244 if (!batadv_tt_change_cache)
4245 goto err_tt_orig_destroy;
4246
4247 batadv_tt_req_cache = kmem_cache_create("batadv_tt_req_cache",
4248 tt_req_size, 0,
4249 SLAB_HWCACHE_ALIGN, NULL);
4250 if (!batadv_tt_req_cache)
4251 goto err_tt_change_destroy;
4252
4253 batadv_tt_roam_cache = kmem_cache_create("batadv_tt_roam_cache",
4254 tt_roam_size, 0,
4255 SLAB_HWCACHE_ALIGN, NULL);
4256 if (!batadv_tt_roam_cache)
4257 goto err_tt_req_destroy;
4258
4259 return 0;
4260
4261 err_tt_req_destroy:
4262 kmem_cache_destroy(batadv_tt_req_cache);
4263 batadv_tt_req_cache = NULL;
4264 err_tt_change_destroy:
4265 kmem_cache_destroy(batadv_tt_change_cache);
4266 batadv_tt_change_cache = NULL;
4267 err_tt_orig_destroy:
4268 kmem_cache_destroy(batadv_tt_orig_cache);
4269 batadv_tt_orig_cache = NULL;
4270 err_tt_tg_destroy:
4271 kmem_cache_destroy(batadv_tg_cache);
4272 batadv_tg_cache = NULL;
4273 err_tt_tl_destroy:
4274 kmem_cache_destroy(batadv_tl_cache);
4275 batadv_tl_cache = NULL;
4276
4277 return -ENOMEM;
4278 }
4279
4280
4281
4282
4283 void batadv_tt_cache_destroy(void)
4284 {
4285 kmem_cache_destroy(batadv_tl_cache);
4286 kmem_cache_destroy(batadv_tg_cache);
4287 kmem_cache_destroy(batadv_tt_orig_cache);
4288 kmem_cache_destroy(batadv_tt_change_cache);
4289 kmem_cache_destroy(batadv_tt_req_cache);
4290 kmem_cache_destroy(batadv_tt_roam_cache);
4291 }