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