1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2008, 2009 open80211s Ltd. 4 * Copyright (C) 2023 Intel Corporation 5 * Author: Luis Carlos Cobo <luisca@cozybit.com> 6 */ 7 8 #include <linux/etherdevice.h> 9 #include <linux/list.h> 10 #include <linux/random.h> 11 #include <linux/slab.h> 12 #include <linux/spinlock.h> 13 #include <linux/string.h> 14 #include <net/mac80211.h> 15 #include "wme.h" 16 #include "ieee80211_i.h" 17 #include "mesh.h" 18 #include <linux/rhashtable.h> 19 20 static void mesh_path_free_rcu(struct mesh_table *tbl, struct mesh_path *mpath); 21 22 static u32 mesh_table_hash(const void *addr, u32 len, u32 seed) 23 { 24 /* Use last four bytes of hw addr as hash index */ 25 return jhash_1word(get_unaligned((u32 *)((u8 *)addr + 2)), seed); 26 } 27 28 static const struct rhashtable_params mesh_rht_params = { 29 .nelem_hint = 2, 30 .automatic_shrinking = true, 31 .key_len = ETH_ALEN, 32 .key_offset = offsetof(struct mesh_path, dst), 33 .head_offset = offsetof(struct mesh_path, rhash), 34 .hashfn = mesh_table_hash, 35 }; 36 37 static const struct rhashtable_params fast_tx_rht_params = { 38 .nelem_hint = 10, 39 .automatic_shrinking = true, 40 .key_len = sizeof_field(struct ieee80211_mesh_fast_tx, key), 41 .key_offset = offsetof(struct ieee80211_mesh_fast_tx, key), 42 .head_offset = offsetof(struct ieee80211_mesh_fast_tx, rhash), 43 .hashfn = mesh_table_hash, 44 }; 45 46 static void __mesh_fast_tx_entry_free(void *ptr, void *tblptr) 47 { 48 struct ieee80211_mesh_fast_tx *entry = ptr; 49 50 kfree_rcu(entry, fast_tx.rcu_head); 51 } 52 53 static void mesh_fast_tx_deinit(struct ieee80211_sub_if_data *sdata) 54 { 55 struct mesh_tx_cache *cache; 56 57 cache = &sdata->u.mesh.tx_cache; 58 rhashtable_free_and_destroy(&cache->rht, 59 __mesh_fast_tx_entry_free, NULL); 60 } 61 62 static void mesh_fast_tx_init(struct ieee80211_sub_if_data *sdata) 63 { 64 struct mesh_tx_cache *cache; 65 66 cache = &sdata->u.mesh.tx_cache; 67 rhashtable_init(&cache->rht, &fast_tx_rht_params); 68 INIT_HLIST_HEAD(&cache->walk_head); 69 spin_lock_init(&cache->walk_lock); 70 } 71 72 static inline bool mpath_expired(struct mesh_path *mpath) 73 { 74 return (mpath->flags & MESH_PATH_ACTIVE) && 75 time_after(jiffies, mpath->exp_time) && 76 !(mpath->flags & MESH_PATH_FIXED); 77 } 78 79 static void mesh_path_rht_free(void *ptr, void *tblptr) 80 { 81 struct mesh_path *mpath = ptr; 82 struct mesh_table *tbl = tblptr; 83 84 mesh_path_free_rcu(tbl, mpath); 85 } 86 87 static void mesh_table_init(struct mesh_table *tbl) 88 { 89 INIT_HLIST_HEAD(&tbl->known_gates); 90 INIT_HLIST_HEAD(&tbl->walk_head); 91 atomic_set(&tbl->entries, 0); 92 spin_lock_init(&tbl->gates_lock); 93 spin_lock_init(&tbl->walk_lock); 94 95 /* rhashtable_init() may fail only in case of wrong 96 * mesh_rht_params 97 */ 98 WARN_ON(rhashtable_init(&tbl->rhead, &mesh_rht_params)); 99 } 100 101 static void mesh_table_free(struct mesh_table *tbl) 102 { 103 rhashtable_free_and_destroy(&tbl->rhead, 104 mesh_path_rht_free, tbl); 105 } 106 107 /** 108 * mesh_path_assign_nexthop - update mesh path next hop 109 * 110 * @mpath: mesh path to update 111 * @sta: next hop to assign 112 * 113 * Locking: mpath->state_lock must be held when calling this function 114 */ 115 void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta) 116 { 117 struct sk_buff *skb; 118 struct ieee80211_hdr *hdr; 119 unsigned long flags; 120 121 rcu_assign_pointer(mpath->next_hop, sta); 122 123 spin_lock_irqsave(&mpath->frame_queue.lock, flags); 124 skb_queue_walk(&mpath->frame_queue, skb) { 125 hdr = (struct ieee80211_hdr *) skb->data; 126 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN); 127 memcpy(hdr->addr2, mpath->sdata->vif.addr, ETH_ALEN); 128 ieee80211_mps_set_frame_flags(sta->sdata, sta, hdr); 129 } 130 131 spin_unlock_irqrestore(&mpath->frame_queue.lock, flags); 132 } 133 134 static void prepare_for_gate(struct sk_buff *skb, char *dst_addr, 135 struct mesh_path *gate_mpath) 136 { 137 struct ieee80211_hdr *hdr; 138 struct ieee80211s_hdr *mshdr; 139 int mesh_hdrlen, hdrlen; 140 char *next_hop; 141 142 hdr = (struct ieee80211_hdr *) skb->data; 143 hdrlen = ieee80211_hdrlen(hdr->frame_control); 144 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen); 145 146 if (!(mshdr->flags & MESH_FLAGS_AE)) { 147 /* size of the fixed part of the mesh header */ 148 mesh_hdrlen = 6; 149 150 /* make room for the two extended addresses */ 151 skb_push(skb, 2 * ETH_ALEN); 152 memmove(skb->data, hdr, hdrlen + mesh_hdrlen); 153 154 hdr = (struct ieee80211_hdr *) skb->data; 155 156 /* we preserve the previous mesh header and only add 157 * the new addresses */ 158 mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen); 159 mshdr->flags = MESH_FLAGS_AE_A5_A6; 160 memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN); 161 memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN); 162 } 163 164 /* update next hop */ 165 hdr = (struct ieee80211_hdr *) skb->data; 166 rcu_read_lock(); 167 next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr; 168 memcpy(hdr->addr1, next_hop, ETH_ALEN); 169 rcu_read_unlock(); 170 memcpy(hdr->addr2, gate_mpath->sdata->vif.addr, ETH_ALEN); 171 memcpy(hdr->addr3, dst_addr, ETH_ALEN); 172 } 173 174 /** 175 * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another 176 * 177 * @gate_mpath: An active mpath the frames will be sent to (i.e. the gate) 178 * @from_mpath: The failed mpath 179 * @copy: When true, copy all the frames to the new mpath queue. When false, 180 * move them. 181 * 182 * This function is used to transfer or copy frames from an unresolved mpath to 183 * a gate mpath. The function also adds the Address Extension field and 184 * updates the next hop. 185 * 186 * If a frame already has an Address Extension field, only the next hop and 187 * destination addresses are updated. 188 * 189 * The gate mpath must be an active mpath with a valid mpath->next_hop. 190 */ 191 static void mesh_path_move_to_queue(struct mesh_path *gate_mpath, 192 struct mesh_path *from_mpath, 193 bool copy) 194 { 195 struct sk_buff *skb, *fskb, *tmp; 196 struct sk_buff_head failq; 197 unsigned long flags; 198 199 if (WARN_ON(gate_mpath == from_mpath)) 200 return; 201 if (WARN_ON(!gate_mpath->next_hop)) 202 return; 203 204 __skb_queue_head_init(&failq); 205 206 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags); 207 skb_queue_splice_init(&from_mpath->frame_queue, &failq); 208 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags); 209 210 skb_queue_walk_safe(&failq, fskb, tmp) { 211 if (skb_queue_len(&gate_mpath->frame_queue) >= 212 MESH_FRAME_QUEUE_LEN) { 213 mpath_dbg(gate_mpath->sdata, "mpath queue full!\n"); 214 break; 215 } 216 217 skb = skb_copy(fskb, GFP_ATOMIC); 218 if (WARN_ON(!skb)) 219 break; 220 221 prepare_for_gate(skb, gate_mpath->dst, gate_mpath); 222 skb_queue_tail(&gate_mpath->frame_queue, skb); 223 224 if (copy) 225 continue; 226 227 __skb_unlink(fskb, &failq); 228 kfree_skb(fskb); 229 } 230 231 mpath_dbg(gate_mpath->sdata, "Mpath queue for gate %pM has %d frames\n", 232 gate_mpath->dst, skb_queue_len(&gate_mpath->frame_queue)); 233 234 if (!copy) 235 return; 236 237 spin_lock_irqsave(&from_mpath->frame_queue.lock, flags); 238 skb_queue_splice(&failq, &from_mpath->frame_queue); 239 spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags); 240 } 241 242 243 static struct mesh_path *mpath_lookup(struct mesh_table *tbl, const u8 *dst, 244 struct ieee80211_sub_if_data *sdata) 245 { 246 struct mesh_path *mpath; 247 248 mpath = rhashtable_lookup(&tbl->rhead, dst, mesh_rht_params); 249 250 if (mpath) { 251 spin_lock_bh(&mpath->state_lock); 252 if (mpath_expired(mpath)) 253 mpath->flags &= ~MESH_PATH_ACTIVE; 254 spin_unlock_bh(&mpath->state_lock); 255 } 256 return mpath; 257 } 258 259 /** 260 * mesh_path_lookup - look up a path in the mesh path table 261 * @sdata: local subif 262 * @dst: hardware address (ETH_ALEN length) of destination 263 * 264 * Returns: pointer to the mesh path structure, or NULL if not found 265 * 266 * Locking: must be called within a read rcu section. 267 */ 268 struct mesh_path * 269 mesh_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst) 270 { 271 return mpath_lookup(&sdata->u.mesh.mesh_paths, dst, sdata); 272 } 273 274 struct mesh_path * 275 mpp_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst) 276 { 277 return mpath_lookup(&sdata->u.mesh.mpp_paths, dst, sdata); 278 } 279 280 static struct mesh_path * 281 __mesh_path_lookup_by_idx(struct mesh_table *tbl, int idx) 282 { 283 int i = 0; 284 struct mesh_path *mpath; 285 286 hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) { 287 if (i++ == idx) 288 break; 289 } 290 291 if (!mpath) 292 return NULL; 293 294 spin_lock_bh(&mpath->state_lock); 295 if (mpath_expired(mpath)) 296 mpath->flags &= ~MESH_PATH_ACTIVE; 297 spin_unlock_bh(&mpath->state_lock); 298 299 return mpath; 300 } 301 302 /** 303 * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index 304 * @sdata: local subif, or NULL for all entries 305 * @idx: index 306 * 307 * Returns: pointer to the mesh path structure, or NULL if not found. 308 * 309 * Locking: must be called within a read rcu section. 310 */ 311 struct mesh_path * 312 mesh_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx) 313 { 314 return __mesh_path_lookup_by_idx(&sdata->u.mesh.mesh_paths, idx); 315 } 316 317 /** 318 * mpp_path_lookup_by_idx - look up a path in the proxy path table by its index 319 * @sdata: local subif, or NULL for all entries 320 * @idx: index 321 * 322 * Returns: pointer to the proxy path structure, or NULL if not found. 323 * 324 * Locking: must be called within a read rcu section. 325 */ 326 struct mesh_path * 327 mpp_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx) 328 { 329 return __mesh_path_lookup_by_idx(&sdata->u.mesh.mpp_paths, idx); 330 } 331 332 /** 333 * mesh_path_add_gate - add the given mpath to a mesh gate to our path table 334 * @mpath: gate path to add to table 335 * 336 * Returns: 0 on success, -EEXIST 337 */ 338 int mesh_path_add_gate(struct mesh_path *mpath) 339 { 340 struct mesh_table *tbl; 341 int err; 342 343 rcu_read_lock(); 344 tbl = &mpath->sdata->u.mesh.mesh_paths; 345 346 spin_lock_bh(&mpath->state_lock); 347 if (mpath->is_gate) { 348 err = -EEXIST; 349 spin_unlock_bh(&mpath->state_lock); 350 goto err_rcu; 351 } 352 mpath->is_gate = true; 353 mpath->sdata->u.mesh.num_gates++; 354 355 spin_lock(&tbl->gates_lock); 356 hlist_add_head_rcu(&mpath->gate_list, &tbl->known_gates); 357 spin_unlock(&tbl->gates_lock); 358 359 spin_unlock_bh(&mpath->state_lock); 360 361 mpath_dbg(mpath->sdata, 362 "Mesh path: Recorded new gate: %pM. %d known gates\n", 363 mpath->dst, mpath->sdata->u.mesh.num_gates); 364 err = 0; 365 err_rcu: 366 rcu_read_unlock(); 367 return err; 368 } 369 370 /** 371 * mesh_gate_del - remove a mesh gate from the list of known gates 372 * @tbl: table which holds our list of known gates 373 * @mpath: gate mpath 374 */ 375 static void mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath) 376 { 377 lockdep_assert_held(&mpath->state_lock); 378 if (!mpath->is_gate) 379 return; 380 381 mpath->is_gate = false; 382 spin_lock_bh(&tbl->gates_lock); 383 hlist_del_rcu(&mpath->gate_list); 384 mpath->sdata->u.mesh.num_gates--; 385 spin_unlock_bh(&tbl->gates_lock); 386 387 mpath_dbg(mpath->sdata, 388 "Mesh path: Deleted gate: %pM. %d known gates\n", 389 mpath->dst, mpath->sdata->u.mesh.num_gates); 390 } 391 392 /** 393 * mesh_gate_num - number of gates known to this interface 394 * @sdata: subif data 395 * 396 * Returns: The number of gates 397 */ 398 int mesh_gate_num(struct ieee80211_sub_if_data *sdata) 399 { 400 return sdata->u.mesh.num_gates; 401 } 402 403 static 404 struct mesh_path *mesh_path_new(struct ieee80211_sub_if_data *sdata, 405 const u8 *dst, gfp_t gfp_flags) 406 { 407 struct mesh_path *new_mpath; 408 409 new_mpath = kzalloc_obj(struct mesh_path, gfp_flags); 410 if (!new_mpath) 411 return NULL; 412 413 memcpy(new_mpath->dst, dst, ETH_ALEN); 414 eth_broadcast_addr(new_mpath->rann_snd_addr); 415 new_mpath->is_root = false; 416 new_mpath->sdata = sdata; 417 new_mpath->flags = 0; 418 skb_queue_head_init(&new_mpath->frame_queue); 419 new_mpath->exp_time = jiffies; 420 spin_lock_init(&new_mpath->state_lock); 421 timer_setup(&new_mpath->timer, mesh_path_timer, 0); 422 423 return new_mpath; 424 } 425 426 static void mesh_fast_tx_entry_free(struct mesh_tx_cache *cache, 427 struct ieee80211_mesh_fast_tx *entry) 428 { 429 hlist_del_rcu(&entry->walk_list); 430 rhashtable_remove_fast(&cache->rht, &entry->rhash, fast_tx_rht_params); 431 kfree_rcu(entry, fast_tx.rcu_head); 432 } 433 434 struct ieee80211_mesh_fast_tx * 435 mesh_fast_tx_get(struct ieee80211_sub_if_data *sdata, 436 struct ieee80211_mesh_fast_tx_key *key) 437 { 438 struct ieee80211_mesh_fast_tx *entry; 439 struct mesh_tx_cache *cache; 440 441 cache = &sdata->u.mesh.tx_cache; 442 entry = rhashtable_lookup(&cache->rht, key, fast_tx_rht_params); 443 if (!entry) 444 return NULL; 445 446 if (!(entry->mpath->flags & MESH_PATH_ACTIVE) || 447 mpath_expired(entry->mpath)) { 448 spin_lock_bh(&cache->walk_lock); 449 entry = rhashtable_lookup(&cache->rht, key, fast_tx_rht_params); 450 if (entry) 451 mesh_fast_tx_entry_free(cache, entry); 452 spin_unlock_bh(&cache->walk_lock); 453 return NULL; 454 } 455 456 mesh_path_refresh(sdata, entry->mpath, NULL); 457 if (entry->mppath) 458 entry->mppath->exp_time = jiffies; 459 entry->timestamp = jiffies; 460 461 return entry; 462 } 463 464 void mesh_fast_tx_cache(struct ieee80211_sub_if_data *sdata, 465 struct sk_buff *skb, struct mesh_path *mpath) 466 { 467 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 468 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 469 struct ieee80211_mesh_fast_tx *entry, *prev; 470 struct ieee80211_mesh_fast_tx build = {}; 471 struct ieee80211s_hdr *meshhdr; 472 struct mesh_tx_cache *cache; 473 struct ieee80211_key *key; 474 struct mesh_path *mppath; 475 struct sta_info *sta; 476 u8 *qc; 477 478 if (sdata->noack_map || 479 !ieee80211_is_data_qos(hdr->frame_control)) 480 return; 481 482 build.fast_tx.hdr_len = ieee80211_hdrlen(hdr->frame_control); 483 meshhdr = (struct ieee80211s_hdr *)(skb->data + build.fast_tx.hdr_len); 484 build.hdrlen = ieee80211_get_mesh_hdrlen(meshhdr); 485 486 cache = &sdata->u.mesh.tx_cache; 487 if (atomic_read(&cache->rht.nelems) >= MESH_FAST_TX_CACHE_MAX_SIZE) 488 return; 489 490 sta = rcu_dereference(mpath->next_hop); 491 if (!sta) 492 return; 493 494 build.key.type = MESH_FAST_TX_TYPE_LOCAL; 495 if ((meshhdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6) { 496 /* This is required to keep the mppath alive */ 497 mppath = mpp_path_lookup(sdata, meshhdr->eaddr1); 498 if (!mppath) 499 return; 500 build.mppath = mppath; 501 if (!ether_addr_equal(meshhdr->eaddr2, sdata->vif.addr)) 502 build.key.type = MESH_FAST_TX_TYPE_PROXIED; 503 } else if (ieee80211_has_a4(hdr->frame_control)) { 504 mppath = mpath; 505 } else { 506 return; 507 } 508 509 if (!ether_addr_equal(hdr->addr4, sdata->vif.addr)) 510 build.key.type = MESH_FAST_TX_TYPE_FORWARDED; 511 512 /* rate limit, in case fast xmit can't be enabled */ 513 if (mppath->fast_tx_check == jiffies) 514 return; 515 516 mppath->fast_tx_check = jiffies; 517 518 /* 519 * Same use of the sta lock as in ieee80211_check_fast_xmit, in order 520 * to protect against concurrent sta key updates. 521 */ 522 spin_lock_bh(&sta->lock); 523 key = rcu_access_pointer(sta->ptk[sta->ptk_idx]); 524 if (!key) 525 key = rcu_access_pointer(sdata->default_unicast_key); 526 build.fast_tx.key = key; 527 528 if (key) { 529 bool gen_iv, iv_spc; 530 531 gen_iv = key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV; 532 iv_spc = key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE; 533 534 if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) || 535 (key->flags & KEY_FLAG_TAINTED)) 536 goto unlock_sta; 537 538 switch (key->conf.cipher) { 539 case WLAN_CIPHER_SUITE_CCMP: 540 case WLAN_CIPHER_SUITE_CCMP_256: 541 if (gen_iv) 542 build.fast_tx.pn_offs = build.fast_tx.hdr_len; 543 if (gen_iv || iv_spc) 544 build.fast_tx.hdr_len += IEEE80211_CCMP_HDR_LEN; 545 break; 546 case WLAN_CIPHER_SUITE_GCMP: 547 case WLAN_CIPHER_SUITE_GCMP_256: 548 if (gen_iv) 549 build.fast_tx.pn_offs = build.fast_tx.hdr_len; 550 if (gen_iv || iv_spc) 551 build.fast_tx.hdr_len += IEEE80211_GCMP_HDR_LEN; 552 break; 553 default: 554 goto unlock_sta; 555 } 556 } 557 558 memcpy(build.key.addr, mppath->dst, ETH_ALEN); 559 build.timestamp = jiffies; 560 build.fast_tx.band = info->band; 561 build.fast_tx.da_offs = offsetof(struct ieee80211_hdr, addr3); 562 build.fast_tx.sa_offs = offsetof(struct ieee80211_hdr, addr4); 563 build.mpath = mpath; 564 memcpy(build.hdr, meshhdr, build.hdrlen); 565 memcpy(build.hdr + build.hdrlen, rfc1042_header, sizeof(rfc1042_header)); 566 build.hdrlen += sizeof(rfc1042_header); 567 memcpy(build.fast_tx.hdr, hdr, build.fast_tx.hdr_len); 568 569 hdr = (struct ieee80211_hdr *)build.fast_tx.hdr; 570 if (build.fast_tx.key) 571 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED); 572 573 qc = ieee80211_get_qos_ctl(hdr); 574 qc[1] |= IEEE80211_QOS_CTL_MESH_CONTROL_PRESENT >> 8; 575 576 entry = kmemdup(&build, sizeof(build), GFP_ATOMIC); 577 if (!entry) 578 goto unlock_sta; 579 580 spin_lock(&cache->walk_lock); 581 if ((READ_ONCE(mpath->flags) & MESH_PATH_DELETED) || 582 (mppath && (READ_ONCE(mppath->flags) & MESH_PATH_DELETED))) { 583 kfree(entry); 584 goto unlock_cache; 585 } 586 587 prev = rhashtable_lookup_get_insert_fast(&cache->rht, 588 &entry->rhash, 589 fast_tx_rht_params); 590 if (IS_ERR(prev)) { 591 kfree(entry); 592 goto unlock_cache; 593 } 594 595 /* 596 * replace any previous entry in the hash table, in case we're 597 * replacing it with a different type (e.g. mpath -> mpp) 598 */ 599 if (unlikely(prev)) { 600 rhashtable_replace_fast(&cache->rht, &prev->rhash, 601 &entry->rhash, fast_tx_rht_params); 602 hlist_del_rcu(&prev->walk_list); 603 kfree_rcu(prev, fast_tx.rcu_head); 604 } 605 606 hlist_add_head(&entry->walk_list, &cache->walk_head); 607 608 unlock_cache: 609 spin_unlock(&cache->walk_lock); 610 unlock_sta: 611 spin_unlock_bh(&sta->lock); 612 } 613 614 void mesh_fast_tx_gc(struct ieee80211_sub_if_data *sdata) 615 { 616 unsigned long timeout = msecs_to_jiffies(MESH_FAST_TX_CACHE_TIMEOUT); 617 struct mesh_tx_cache *cache = &sdata->u.mesh.tx_cache; 618 struct ieee80211_mesh_fast_tx *entry; 619 struct hlist_node *n; 620 621 if (atomic_read(&cache->rht.nelems) < MESH_FAST_TX_CACHE_THRESHOLD_SIZE) 622 return; 623 624 spin_lock_bh(&cache->walk_lock); 625 hlist_for_each_entry_safe(entry, n, &cache->walk_head, walk_list) 626 if (!time_is_after_jiffies(entry->timestamp + timeout)) 627 mesh_fast_tx_entry_free(cache, entry); 628 spin_unlock_bh(&cache->walk_lock); 629 } 630 631 void mesh_fast_tx_flush_mpath(struct mesh_path *mpath) 632 { 633 struct ieee80211_sub_if_data *sdata = mpath->sdata; 634 struct mesh_tx_cache *cache = &sdata->u.mesh.tx_cache; 635 struct ieee80211_mesh_fast_tx *entry; 636 struct hlist_node *n; 637 638 spin_lock_bh(&cache->walk_lock); 639 hlist_for_each_entry_safe(entry, n, &cache->walk_head, walk_list) 640 if (entry->mpath == mpath) 641 mesh_fast_tx_entry_free(cache, entry); 642 spin_unlock_bh(&cache->walk_lock); 643 } 644 645 void mesh_fast_tx_flush_sta(struct ieee80211_sub_if_data *sdata, 646 struct sta_info *sta) 647 { 648 struct mesh_tx_cache *cache = &sdata->u.mesh.tx_cache; 649 struct ieee80211_mesh_fast_tx *entry; 650 struct hlist_node *n; 651 652 spin_lock_bh(&cache->walk_lock); 653 hlist_for_each_entry_safe(entry, n, &cache->walk_head, walk_list) 654 if (rcu_access_pointer(entry->mpath->next_hop) == sta) 655 mesh_fast_tx_entry_free(cache, entry); 656 spin_unlock_bh(&cache->walk_lock); 657 } 658 659 void mesh_fast_tx_flush_addr(struct ieee80211_sub_if_data *sdata, 660 const u8 *addr) 661 { 662 struct mesh_tx_cache *cache = &sdata->u.mesh.tx_cache; 663 struct ieee80211_mesh_fast_tx_key key = {}; 664 struct ieee80211_mesh_fast_tx *entry; 665 int i; 666 667 ether_addr_copy(key.addr, addr); 668 spin_lock_bh(&cache->walk_lock); 669 for (i = 0; i < NUM_MESH_FAST_TX_TYPE; i++) { 670 key.type = i; 671 entry = rhashtable_lookup_fast(&cache->rht, &key, fast_tx_rht_params); 672 if (entry) 673 mesh_fast_tx_entry_free(cache, entry); 674 } 675 spin_unlock_bh(&cache->walk_lock); 676 } 677 678 /** 679 * mesh_path_add - allocate and add a new path to the mesh path table 680 * @sdata: local subif 681 * @dst: destination address of the path (ETH_ALEN length) 682 * 683 * Returns: the new or the existing path on success, an ERR_PTR() on failure 684 * 685 * State: the initial state of the new path is set to 0 686 */ 687 struct mesh_path *mesh_path_add(struct ieee80211_sub_if_data *sdata, 688 const u8 *dst) 689 { 690 struct mesh_table *tbl; 691 struct mesh_path *mpath, *new_mpath; 692 693 if (ether_addr_equal(dst, sdata->vif.addr)) 694 /* never add ourselves as neighbours */ 695 return ERR_PTR(-EOPNOTSUPP); 696 697 if (is_multicast_ether_addr(dst)) 698 return ERR_PTR(-EOPNOTSUPP); 699 700 if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0) 701 return ERR_PTR(-ENOSPC); 702 703 new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC); 704 if (!new_mpath) { 705 atomic_dec(&sdata->u.mesh.mpaths); 706 return ERR_PTR(-ENOMEM); 707 } 708 709 tbl = &sdata->u.mesh.mesh_paths; 710 spin_lock_bh(&tbl->walk_lock); 711 mpath = rhashtable_lookup_get_insert_fast(&tbl->rhead, 712 &new_mpath->rhash, 713 mesh_rht_params); 714 if (!mpath) { 715 hlist_add_head_rcu(&new_mpath->walk_list, &tbl->walk_head); 716 sdata->u.mesh.mesh_paths_generation++; 717 } 718 spin_unlock_bh(&tbl->walk_lock); 719 720 if (mpath) { 721 kfree(new_mpath); 722 atomic_dec(&sdata->u.mesh.mpaths); 723 724 if (IS_ERR(mpath)) 725 return mpath; 726 727 new_mpath = mpath; 728 } 729 730 return new_mpath; 731 } 732 733 int mpp_path_add(struct ieee80211_sub_if_data *sdata, 734 const u8 *dst, const u8 *mpp) 735 { 736 struct mesh_table *tbl; 737 struct mesh_path *new_mpath; 738 int ret; 739 740 if (ether_addr_equal(dst, sdata->vif.addr)) 741 /* never add ourselves as neighbours */ 742 return -EOPNOTSUPP; 743 744 if (is_multicast_ether_addr(dst)) 745 return -EOPNOTSUPP; 746 747 if (!atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS)) 748 return -ENOSPC; 749 750 new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC); 751 752 if (!new_mpath) { 753 atomic_dec(&sdata->u.mesh.mpaths); 754 return -ENOMEM; 755 } 756 757 memcpy(new_mpath->mpp, mpp, ETH_ALEN); 758 tbl = &sdata->u.mesh.mpp_paths; 759 760 spin_lock_bh(&tbl->walk_lock); 761 ret = rhashtable_lookup_insert_fast(&tbl->rhead, 762 &new_mpath->rhash, 763 mesh_rht_params); 764 if (!ret) { 765 hlist_add_head_rcu(&new_mpath->walk_list, &tbl->walk_head); 766 sdata->u.mesh.mpp_paths_generation++; 767 } 768 spin_unlock_bh(&tbl->walk_lock); 769 770 if (ret) { 771 kfree(new_mpath); 772 atomic_dec(&sdata->u.mesh.mpaths); 773 } else { 774 mesh_fast_tx_flush_addr(sdata, dst); 775 } 776 777 return ret; 778 } 779 780 781 /** 782 * mesh_plink_broken - deactivates paths and sends perr when a link breaks 783 * 784 * @sta: broken peer link 785 * 786 * This function must be called from the rate control algorithm if enough 787 * delivery errors suggest that a peer link is no longer usable. 788 */ 789 void mesh_plink_broken(struct sta_info *sta) 790 { 791 struct ieee80211_sub_if_data *sdata = sta->sdata; 792 struct mesh_table *tbl = &sdata->u.mesh.mesh_paths; 793 static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 794 struct mesh_path *mpath; 795 796 rcu_read_lock(); 797 hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) { 798 if (rcu_access_pointer(mpath->next_hop) == sta && 799 mpath->flags & MESH_PATH_ACTIVE && 800 !(mpath->flags & MESH_PATH_FIXED)) { 801 spin_lock_bh(&mpath->state_lock); 802 mpath->flags &= ~MESH_PATH_ACTIVE; 803 ++mpath->sn; 804 spin_unlock_bh(&mpath->state_lock); 805 mesh_path_error_tx(sdata, 806 sdata->u.mesh.mshcfg.element_ttl, 807 mpath->dst, mpath->sn, 808 WLAN_REASON_MESH_PATH_DEST_UNREACHABLE, bcast); 809 } 810 } 811 rcu_read_unlock(); 812 } 813 814 static void mesh_path_free_rcu(struct mesh_table *tbl, 815 struct mesh_path *mpath) 816 { 817 struct ieee80211_sub_if_data *sdata = mpath->sdata; 818 819 spin_lock_bh(&mpath->state_lock); 820 WRITE_ONCE(mpath->flags, 821 mpath->flags | MESH_PATH_RESOLVING | MESH_PATH_DELETED); 822 mesh_gate_del(tbl, mpath); 823 spin_unlock_bh(&mpath->state_lock); 824 timer_shutdown_sync(&mpath->timer); 825 atomic_dec(&sdata->u.mesh.mpaths); 826 atomic_dec(&tbl->entries); 827 mesh_path_flush_pending(mpath); 828 kfree_rcu(mpath, rcu); 829 } 830 831 static void __mesh_path_del(struct mesh_table *tbl, struct mesh_path *mpath) 832 { 833 hlist_del_rcu(&mpath->walk_list); 834 rhashtable_remove_fast(&tbl->rhead, &mpath->rhash, mesh_rht_params); 835 spin_lock_bh(&mpath->state_lock); 836 WRITE_ONCE(mpath->flags, mpath->flags | MESH_PATH_DELETED); 837 spin_unlock_bh(&mpath->state_lock); 838 if (tbl == &mpath->sdata->u.mesh.mpp_paths) { 839 mpath->sdata->u.mesh.mpp_paths_generation++; 840 mesh_fast_tx_flush_addr(mpath->sdata, mpath->dst); 841 } else { 842 mpath->sdata->u.mesh.mesh_paths_generation++; 843 mesh_fast_tx_flush_mpath(mpath); 844 } 845 mesh_path_free_rcu(tbl, mpath); 846 } 847 848 /** 849 * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches 850 * 851 * @sta: mesh peer to match 852 * 853 * RCU notes: this function is called when a mesh plink transitions from 854 * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that 855 * allows path creation. This will happen before the sta can be freed (because 856 * sta_info_destroy() calls this) so any reader in a rcu read block will be 857 * protected against the plink disappearing. 858 */ 859 void mesh_path_flush_by_nexthop(struct sta_info *sta) 860 { 861 struct ieee80211_sub_if_data *sdata = sta->sdata; 862 struct mesh_table *tbl = &sdata->u.mesh.mesh_paths; 863 struct mesh_path *mpath; 864 struct hlist_node *n; 865 866 spin_lock_bh(&tbl->walk_lock); 867 hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) { 868 if (rcu_access_pointer(mpath->next_hop) == sta) 869 __mesh_path_del(tbl, mpath); 870 } 871 spin_unlock_bh(&tbl->walk_lock); 872 } 873 874 static void mpp_flush_by_proxy(struct ieee80211_sub_if_data *sdata, 875 const u8 *proxy) 876 { 877 struct mesh_table *tbl = &sdata->u.mesh.mpp_paths; 878 struct mesh_path *mpath; 879 struct hlist_node *n; 880 881 spin_lock_bh(&tbl->walk_lock); 882 hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) { 883 if (ether_addr_equal(mpath->mpp, proxy)) 884 __mesh_path_del(tbl, mpath); 885 } 886 spin_unlock_bh(&tbl->walk_lock); 887 } 888 889 static void table_flush_by_iface(struct mesh_table *tbl) 890 { 891 struct mesh_path *mpath; 892 struct hlist_node *n; 893 894 spin_lock_bh(&tbl->walk_lock); 895 hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) { 896 __mesh_path_del(tbl, mpath); 897 } 898 spin_unlock_bh(&tbl->walk_lock); 899 } 900 901 /** 902 * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface 903 * 904 * @sdata: interface data to match 905 * 906 * This function deletes both mesh paths as well as mesh portal paths. 907 */ 908 void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata) 909 { 910 table_flush_by_iface(&sdata->u.mesh.mesh_paths); 911 table_flush_by_iface(&sdata->u.mesh.mpp_paths); 912 } 913 914 /** 915 * table_path_del - delete a path from the mesh or mpp table 916 * 917 * @tbl: mesh or mpp path table 918 * @sdata: local subif 919 * @addr: dst address (ETH_ALEN length) 920 * 921 * Returns: 0 if successful 922 */ 923 static int table_path_del(struct mesh_table *tbl, 924 struct ieee80211_sub_if_data *sdata, 925 const u8 *addr) 926 { 927 struct mesh_path *mpath; 928 929 spin_lock_bh(&tbl->walk_lock); 930 mpath = rhashtable_lookup_fast(&tbl->rhead, addr, mesh_rht_params); 931 if (!mpath) { 932 spin_unlock_bh(&tbl->walk_lock); 933 return -ENXIO; 934 } 935 936 __mesh_path_del(tbl, mpath); 937 spin_unlock_bh(&tbl->walk_lock); 938 return 0; 939 } 940 941 942 /** 943 * mesh_path_del - delete a mesh path from the table 944 * 945 * @sdata: local subif 946 * @addr: dst address (ETH_ALEN length) 947 * 948 * Returns: 0 if successful 949 */ 950 int mesh_path_del(struct ieee80211_sub_if_data *sdata, const u8 *addr) 951 { 952 /* flush relevant mpp entries first */ 953 mpp_flush_by_proxy(sdata, addr); 954 955 return table_path_del(&sdata->u.mesh.mesh_paths, sdata, addr); 956 } 957 958 /** 959 * mesh_path_tx_pending - sends pending frames in a mesh path queue 960 * 961 * @mpath: mesh path to activate 962 * 963 * Locking: the state_lock of the mpath structure must NOT be held when calling 964 * this function. 965 */ 966 void mesh_path_tx_pending(struct mesh_path *mpath) 967 { 968 if (mpath->flags & MESH_PATH_ACTIVE) 969 ieee80211_add_pending_skbs(mpath->sdata->local, 970 &mpath->frame_queue); 971 } 972 973 /** 974 * mesh_path_send_to_gates - sends pending frames to all known mesh gates 975 * 976 * @mpath: mesh path whose queue will be emptied 977 * 978 * If there is only one gate, the frames are transferred from the failed mpath 979 * queue to that gate's queue. If there are more than one gates, the frames 980 * are copied from each gate to the next. After frames are copied, the 981 * mpath queues are emptied onto the transmission queue. 982 * 983 * Returns: 0 on success, -EHOSTUNREACH 984 */ 985 int mesh_path_send_to_gates(struct mesh_path *mpath) 986 { 987 struct ieee80211_sub_if_data *sdata = mpath->sdata; 988 struct mesh_table *tbl; 989 struct mesh_path *from_mpath = mpath; 990 struct mesh_path *gate; 991 bool copy = false; 992 993 tbl = &sdata->u.mesh.mesh_paths; 994 995 rcu_read_lock(); 996 hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) { 997 if (gate->flags & MESH_PATH_ACTIVE) { 998 mpath_dbg(sdata, "Forwarding to %pM\n", gate->dst); 999 mesh_path_move_to_queue(gate, from_mpath, copy); 1000 from_mpath = gate; 1001 copy = true; 1002 } else { 1003 mpath_dbg(sdata, 1004 "Not forwarding to %pM (flags %#x)\n", 1005 gate->dst, gate->flags); 1006 } 1007 } 1008 1009 hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) { 1010 mpath_dbg(sdata, "Sending to %pM\n", gate->dst); 1011 mesh_path_tx_pending(gate); 1012 } 1013 rcu_read_unlock(); 1014 1015 return (from_mpath == mpath) ? -EHOSTUNREACH : 0; 1016 } 1017 1018 /** 1019 * mesh_path_discard_frame - discard a frame whose path could not be resolved 1020 * 1021 * @sdata: network subif the frame was to be sent through 1022 * @skb: frame to discard 1023 * 1024 * Locking: the function must me called within a rcu_read_lock region 1025 */ 1026 void mesh_path_discard_frame(struct ieee80211_sub_if_data *sdata, 1027 struct sk_buff *skb) 1028 { 1029 ieee80211_free_txskb(&sdata->local->hw, skb); 1030 sdata->u.mesh.mshstats.dropped_frames_no_route++; 1031 } 1032 1033 /** 1034 * mesh_path_flush_pending - free the pending queue of a mesh path 1035 * 1036 * @mpath: mesh path whose queue has to be freed 1037 * 1038 * Locking: the function must me called within a rcu_read_lock region 1039 */ 1040 void mesh_path_flush_pending(struct mesh_path *mpath) 1041 { 1042 struct ieee80211_sub_if_data *sdata = mpath->sdata; 1043 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; 1044 struct mesh_preq_queue *preq, *tmp; 1045 struct sk_buff *skb; 1046 1047 while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL) 1048 mesh_path_discard_frame(mpath->sdata, skb); 1049 1050 spin_lock_bh(&ifmsh->mesh_preq_queue_lock); 1051 list_for_each_entry_safe(preq, tmp, &ifmsh->preq_queue.list, list) { 1052 if (ether_addr_equal(mpath->dst, preq->dst)) { 1053 list_del(&preq->list); 1054 kfree(preq); 1055 --ifmsh->preq_queue_len; 1056 } 1057 } 1058 spin_unlock_bh(&ifmsh->mesh_preq_queue_lock); 1059 } 1060 1061 /** 1062 * mesh_path_fix_nexthop - force a specific next hop for a mesh path 1063 * 1064 * @mpath: the mesh path to modify 1065 * @next_hop: the next hop to force 1066 * 1067 * Locking: this function must be called holding mpath->state_lock 1068 */ 1069 void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop) 1070 { 1071 spin_lock_bh(&mpath->state_lock); 1072 mesh_path_assign_nexthop(mpath, next_hop); 1073 mpath->sn = 0xffff; 1074 mpath->metric = 0; 1075 mpath->hop_count = 0; 1076 mpath->exp_time = 0; 1077 mpath->flags = MESH_PATH_FIXED | MESH_PATH_SN_VALID; 1078 mesh_path_activate(mpath); 1079 mesh_fast_tx_flush_mpath(mpath); 1080 spin_unlock_bh(&mpath->state_lock); 1081 ewma_mesh_fail_avg_init(&next_hop->mesh->fail_avg); 1082 /* init it at a low value - 0 start is tricky */ 1083 ewma_mesh_fail_avg_add(&next_hop->mesh->fail_avg, 1); 1084 mesh_path_tx_pending(mpath); 1085 } 1086 1087 void mesh_pathtbl_init(struct ieee80211_sub_if_data *sdata) 1088 { 1089 mesh_table_init(&sdata->u.mesh.mesh_paths); 1090 mesh_table_init(&sdata->u.mesh.mpp_paths); 1091 mesh_fast_tx_init(sdata); 1092 } 1093 1094 static 1095 void mesh_path_tbl_expire(struct ieee80211_sub_if_data *sdata, 1096 struct mesh_table *tbl) 1097 { 1098 struct mesh_path *mpath; 1099 struct hlist_node *n; 1100 1101 spin_lock_bh(&tbl->walk_lock); 1102 hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) { 1103 if ((!(mpath->flags & MESH_PATH_RESOLVING)) && 1104 (!(mpath->flags & MESH_PATH_FIXED)) && 1105 time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE)) 1106 __mesh_path_del(tbl, mpath); 1107 } 1108 spin_unlock_bh(&tbl->walk_lock); 1109 } 1110 1111 void mesh_path_expire(struct ieee80211_sub_if_data *sdata) 1112 { 1113 mesh_path_tbl_expire(sdata, &sdata->u.mesh.mesh_paths); 1114 mesh_path_tbl_expire(sdata, &sdata->u.mesh.mpp_paths); 1115 } 1116 1117 void mesh_pathtbl_unregister(struct ieee80211_sub_if_data *sdata) 1118 { 1119 mesh_fast_tx_deinit(sdata); 1120 mesh_table_free(&sdata->u.mesh.mesh_paths); 1121 mesh_table_free(&sdata->u.mesh.mpp_paths); 1122 } 1123