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