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