mesh_pathtbl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2008, 2009 open80211s Ltd.
  4. * Author: Luis Carlos Cobo <[email protected]>
  5. */
  6. #include <linux/etherdevice.h>
  7. #include <linux/list.h>
  8. #include <linux/random.h>
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/string.h>
  12. #include <net/mac80211.h>
  13. #include "wme.h"
  14. #include "ieee80211_i.h"
  15. #include "mesh.h"
  16. static void mesh_path_free_rcu(struct mesh_table *tbl, struct mesh_path *mpath);
  17. static u32 mesh_table_hash(const void *addr, u32 len, u32 seed)
  18. {
  19. /* Use last four bytes of hw addr as hash index */
  20. return jhash_1word(__get_unaligned_cpu32((u8 *)addr + 2), seed);
  21. }
  22. static const struct rhashtable_params mesh_rht_params = {
  23. .nelem_hint = 2,
  24. .automatic_shrinking = true,
  25. .key_len = ETH_ALEN,
  26. .key_offset = offsetof(struct mesh_path, dst),
  27. .head_offset = offsetof(struct mesh_path, rhash),
  28. .hashfn = mesh_table_hash,
  29. };
  30. static inline bool mpath_expired(struct mesh_path *mpath)
  31. {
  32. return (mpath->flags & MESH_PATH_ACTIVE) &&
  33. time_after(jiffies, mpath->exp_time) &&
  34. !(mpath->flags & MESH_PATH_FIXED);
  35. }
  36. static void mesh_path_rht_free(void *ptr, void *tblptr)
  37. {
  38. struct mesh_path *mpath = ptr;
  39. struct mesh_table *tbl = tblptr;
  40. mesh_path_free_rcu(tbl, mpath);
  41. }
  42. static void mesh_table_init(struct mesh_table *tbl)
  43. {
  44. INIT_HLIST_HEAD(&tbl->known_gates);
  45. INIT_HLIST_HEAD(&tbl->walk_head);
  46. atomic_set(&tbl->entries, 0);
  47. spin_lock_init(&tbl->gates_lock);
  48. spin_lock_init(&tbl->walk_lock);
  49. /* rhashtable_init() may fail only in case of wrong
  50. * mesh_rht_params
  51. */
  52. WARN_ON(rhashtable_init(&tbl->rhead, &mesh_rht_params));
  53. }
  54. static void mesh_table_free(struct mesh_table *tbl)
  55. {
  56. rhashtable_free_and_destroy(&tbl->rhead,
  57. mesh_path_rht_free, tbl);
  58. }
  59. /**
  60. * mesh_path_assign_nexthop - update mesh path next hop
  61. *
  62. * @mpath: mesh path to update
  63. * @sta: next hop to assign
  64. *
  65. * Locking: mpath->state_lock must be held when calling this function
  66. */
  67. void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
  68. {
  69. struct sk_buff *skb;
  70. struct ieee80211_hdr *hdr;
  71. unsigned long flags;
  72. rcu_assign_pointer(mpath->next_hop, sta);
  73. spin_lock_irqsave(&mpath->frame_queue.lock, flags);
  74. skb_queue_walk(&mpath->frame_queue, skb) {
  75. hdr = (struct ieee80211_hdr *) skb->data;
  76. memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
  77. memcpy(hdr->addr2, mpath->sdata->vif.addr, ETH_ALEN);
  78. ieee80211_mps_set_frame_flags(sta->sdata, sta, hdr);
  79. }
  80. spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
  81. }
  82. static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
  83. struct mesh_path *gate_mpath)
  84. {
  85. struct ieee80211_hdr *hdr;
  86. struct ieee80211s_hdr *mshdr;
  87. int mesh_hdrlen, hdrlen;
  88. char *next_hop;
  89. hdr = (struct ieee80211_hdr *) skb->data;
  90. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  91. mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  92. if (!(mshdr->flags & MESH_FLAGS_AE)) {
  93. /* size of the fixed part of the mesh header */
  94. mesh_hdrlen = 6;
  95. /* make room for the two extended addresses */
  96. skb_push(skb, 2 * ETH_ALEN);
  97. memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
  98. hdr = (struct ieee80211_hdr *) skb->data;
  99. /* we preserve the previous mesh header and only add
  100. * the new addresses */
  101. mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  102. mshdr->flags = MESH_FLAGS_AE_A5_A6;
  103. memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN);
  104. memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN);
  105. }
  106. /* update next hop */
  107. hdr = (struct ieee80211_hdr *) skb->data;
  108. rcu_read_lock();
  109. next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
  110. memcpy(hdr->addr1, next_hop, ETH_ALEN);
  111. rcu_read_unlock();
  112. memcpy(hdr->addr2, gate_mpath->sdata->vif.addr, ETH_ALEN);
  113. memcpy(hdr->addr3, dst_addr, ETH_ALEN);
  114. }
  115. /**
  116. * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
  117. *
  118. * This function is used to transfer or copy frames from an unresolved mpath to
  119. * a gate mpath. The function also adds the Address Extension field and
  120. * updates the next hop.
  121. *
  122. * If a frame already has an Address Extension field, only the next hop and
  123. * destination addresses are updated.
  124. *
  125. * The gate mpath must be an active mpath with a valid mpath->next_hop.
  126. *
  127. * @gate_mpath: An active mpath the frames will be sent to (i.e. the gate)
  128. * @from_mpath: The failed mpath
  129. * @copy: When true, copy all the frames to the new mpath queue. When false,
  130. * move them.
  131. */
  132. static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
  133. struct mesh_path *from_mpath,
  134. bool copy)
  135. {
  136. struct sk_buff *skb, *fskb, *tmp;
  137. struct sk_buff_head failq;
  138. unsigned long flags;
  139. if (WARN_ON(gate_mpath == from_mpath))
  140. return;
  141. if (WARN_ON(!gate_mpath->next_hop))
  142. return;
  143. __skb_queue_head_init(&failq);
  144. spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
  145. skb_queue_splice_init(&from_mpath->frame_queue, &failq);
  146. spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
  147. skb_queue_walk_safe(&failq, fskb, tmp) {
  148. if (skb_queue_len(&gate_mpath->frame_queue) >=
  149. MESH_FRAME_QUEUE_LEN) {
  150. mpath_dbg(gate_mpath->sdata, "mpath queue full!\n");
  151. break;
  152. }
  153. skb = skb_copy(fskb, GFP_ATOMIC);
  154. if (WARN_ON(!skb))
  155. break;
  156. prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
  157. skb_queue_tail(&gate_mpath->frame_queue, skb);
  158. if (copy)
  159. continue;
  160. __skb_unlink(fskb, &failq);
  161. kfree_skb(fskb);
  162. }
  163. mpath_dbg(gate_mpath->sdata, "Mpath queue for gate %pM has %d frames\n",
  164. gate_mpath->dst, skb_queue_len(&gate_mpath->frame_queue));
  165. if (!copy)
  166. return;
  167. spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
  168. skb_queue_splice(&failq, &from_mpath->frame_queue);
  169. spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
  170. }
  171. static struct mesh_path *mpath_lookup(struct mesh_table *tbl, const u8 *dst,
  172. struct ieee80211_sub_if_data *sdata)
  173. {
  174. struct mesh_path *mpath;
  175. mpath = rhashtable_lookup(&tbl->rhead, dst, mesh_rht_params);
  176. if (mpath && mpath_expired(mpath)) {
  177. spin_lock_bh(&mpath->state_lock);
  178. mpath->flags &= ~MESH_PATH_ACTIVE;
  179. spin_unlock_bh(&mpath->state_lock);
  180. }
  181. return mpath;
  182. }
  183. /**
  184. * mesh_path_lookup - look up a path in the mesh path table
  185. * @sdata: local subif
  186. * @dst: hardware address (ETH_ALEN length) of destination
  187. *
  188. * Returns: pointer to the mesh path structure, or NULL if not found
  189. *
  190. * Locking: must be called within a read rcu section.
  191. */
  192. struct mesh_path *
  193. mesh_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
  194. {
  195. return mpath_lookup(&sdata->u.mesh.mesh_paths, dst, sdata);
  196. }
  197. struct mesh_path *
  198. mpp_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
  199. {
  200. return mpath_lookup(&sdata->u.mesh.mpp_paths, dst, sdata);
  201. }
  202. static struct mesh_path *
  203. __mesh_path_lookup_by_idx(struct mesh_table *tbl, int idx)
  204. {
  205. int i = 0;
  206. struct mesh_path *mpath;
  207. hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) {
  208. if (i++ == idx)
  209. break;
  210. }
  211. if (!mpath)
  212. return NULL;
  213. if (mpath_expired(mpath)) {
  214. spin_lock_bh(&mpath->state_lock);
  215. mpath->flags &= ~MESH_PATH_ACTIVE;
  216. spin_unlock_bh(&mpath->state_lock);
  217. }
  218. return mpath;
  219. }
  220. /**
  221. * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
  222. * @idx: index
  223. * @sdata: local subif, or NULL for all entries
  224. *
  225. * Returns: pointer to the mesh path structure, or NULL if not found.
  226. *
  227. * Locking: must be called within a read rcu section.
  228. */
  229. struct mesh_path *
  230. mesh_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
  231. {
  232. return __mesh_path_lookup_by_idx(&sdata->u.mesh.mesh_paths, idx);
  233. }
  234. /**
  235. * mpp_path_lookup_by_idx - look up a path in the proxy path table by its index
  236. * @idx: index
  237. * @sdata: local subif, or NULL for all entries
  238. *
  239. * Returns: pointer to the proxy path structure, or NULL if not found.
  240. *
  241. * Locking: must be called within a read rcu section.
  242. */
  243. struct mesh_path *
  244. mpp_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
  245. {
  246. return __mesh_path_lookup_by_idx(&sdata->u.mesh.mpp_paths, idx);
  247. }
  248. /**
  249. * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
  250. * @mpath: gate path to add to table
  251. */
  252. int mesh_path_add_gate(struct mesh_path *mpath)
  253. {
  254. struct mesh_table *tbl;
  255. int err;
  256. rcu_read_lock();
  257. tbl = &mpath->sdata->u.mesh.mesh_paths;
  258. spin_lock_bh(&mpath->state_lock);
  259. if (mpath->is_gate) {
  260. err = -EEXIST;
  261. spin_unlock_bh(&mpath->state_lock);
  262. goto err_rcu;
  263. }
  264. mpath->is_gate = true;
  265. mpath->sdata->u.mesh.num_gates++;
  266. spin_lock(&tbl->gates_lock);
  267. hlist_add_head_rcu(&mpath->gate_list, &tbl->known_gates);
  268. spin_unlock(&tbl->gates_lock);
  269. spin_unlock_bh(&mpath->state_lock);
  270. mpath_dbg(mpath->sdata,
  271. "Mesh path: Recorded new gate: %pM. %d known gates\n",
  272. mpath->dst, mpath->sdata->u.mesh.num_gates);
  273. err = 0;
  274. err_rcu:
  275. rcu_read_unlock();
  276. return err;
  277. }
  278. /**
  279. * mesh_gate_del - remove a mesh gate from the list of known gates
  280. * @tbl: table which holds our list of known gates
  281. * @mpath: gate mpath
  282. */
  283. static void mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
  284. {
  285. lockdep_assert_held(&mpath->state_lock);
  286. if (!mpath->is_gate)
  287. return;
  288. mpath->is_gate = false;
  289. spin_lock_bh(&tbl->gates_lock);
  290. hlist_del_rcu(&mpath->gate_list);
  291. mpath->sdata->u.mesh.num_gates--;
  292. spin_unlock_bh(&tbl->gates_lock);
  293. mpath_dbg(mpath->sdata,
  294. "Mesh path: Deleted gate: %pM. %d known gates\n",
  295. mpath->dst, mpath->sdata->u.mesh.num_gates);
  296. }
  297. /**
  298. * mesh_gate_num - number of gates known to this interface
  299. * @sdata: subif data
  300. */
  301. int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
  302. {
  303. return sdata->u.mesh.num_gates;
  304. }
  305. static
  306. struct mesh_path *mesh_path_new(struct ieee80211_sub_if_data *sdata,
  307. const u8 *dst, gfp_t gfp_flags)
  308. {
  309. struct mesh_path *new_mpath;
  310. new_mpath = kzalloc(sizeof(struct mesh_path), gfp_flags);
  311. if (!new_mpath)
  312. return NULL;
  313. memcpy(new_mpath->dst, dst, ETH_ALEN);
  314. eth_broadcast_addr(new_mpath->rann_snd_addr);
  315. new_mpath->is_root = false;
  316. new_mpath->sdata = sdata;
  317. new_mpath->flags = 0;
  318. skb_queue_head_init(&new_mpath->frame_queue);
  319. new_mpath->exp_time = jiffies;
  320. spin_lock_init(&new_mpath->state_lock);
  321. timer_setup(&new_mpath->timer, mesh_path_timer, 0);
  322. return new_mpath;
  323. }
  324. /**
  325. * mesh_path_add - allocate and add a new path to the mesh path table
  326. * @dst: destination address of the path (ETH_ALEN length)
  327. * @sdata: local subif
  328. *
  329. * Returns: 0 on success
  330. *
  331. * State: the initial state of the new path is set to 0
  332. */
  333. struct mesh_path *mesh_path_add(struct ieee80211_sub_if_data *sdata,
  334. const u8 *dst)
  335. {
  336. struct mesh_table *tbl;
  337. struct mesh_path *mpath, *new_mpath;
  338. if (ether_addr_equal(dst, sdata->vif.addr))
  339. /* never add ourselves as neighbours */
  340. return ERR_PTR(-ENOTSUPP);
  341. if (is_multicast_ether_addr(dst))
  342. return ERR_PTR(-ENOTSUPP);
  343. if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
  344. return ERR_PTR(-ENOSPC);
  345. new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
  346. if (!new_mpath)
  347. return ERR_PTR(-ENOMEM);
  348. tbl = &sdata->u.mesh.mesh_paths;
  349. spin_lock_bh(&tbl->walk_lock);
  350. mpath = rhashtable_lookup_get_insert_fast(&tbl->rhead,
  351. &new_mpath->rhash,
  352. mesh_rht_params);
  353. if (!mpath)
  354. hlist_add_head(&new_mpath->walk_list, &tbl->walk_head);
  355. spin_unlock_bh(&tbl->walk_lock);
  356. if (mpath) {
  357. kfree(new_mpath);
  358. if (IS_ERR(mpath))
  359. return mpath;
  360. new_mpath = mpath;
  361. }
  362. sdata->u.mesh.mesh_paths_generation++;
  363. return new_mpath;
  364. }
  365. int mpp_path_add(struct ieee80211_sub_if_data *sdata,
  366. const u8 *dst, const u8 *mpp)
  367. {
  368. struct mesh_table *tbl;
  369. struct mesh_path *new_mpath;
  370. int ret;
  371. if (ether_addr_equal(dst, sdata->vif.addr))
  372. /* never add ourselves as neighbours */
  373. return -ENOTSUPP;
  374. if (is_multicast_ether_addr(dst))
  375. return -ENOTSUPP;
  376. new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
  377. if (!new_mpath)
  378. return -ENOMEM;
  379. memcpy(new_mpath->mpp, mpp, ETH_ALEN);
  380. tbl = &sdata->u.mesh.mpp_paths;
  381. spin_lock_bh(&tbl->walk_lock);
  382. ret = rhashtable_lookup_insert_fast(&tbl->rhead,
  383. &new_mpath->rhash,
  384. mesh_rht_params);
  385. if (!ret)
  386. hlist_add_head_rcu(&new_mpath->walk_list, &tbl->walk_head);
  387. spin_unlock_bh(&tbl->walk_lock);
  388. if (ret)
  389. kfree(new_mpath);
  390. sdata->u.mesh.mpp_paths_generation++;
  391. return ret;
  392. }
  393. /**
  394. * mesh_plink_broken - deactivates paths and sends perr when a link breaks
  395. *
  396. * @sta: broken peer link
  397. *
  398. * This function must be called from the rate control algorithm if enough
  399. * delivery errors suggest that a peer link is no longer usable.
  400. */
  401. void mesh_plink_broken(struct sta_info *sta)
  402. {
  403. struct ieee80211_sub_if_data *sdata = sta->sdata;
  404. struct mesh_table *tbl = &sdata->u.mesh.mesh_paths;
  405. static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  406. struct mesh_path *mpath;
  407. rcu_read_lock();
  408. hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) {
  409. if (rcu_access_pointer(mpath->next_hop) == sta &&
  410. mpath->flags & MESH_PATH_ACTIVE &&
  411. !(mpath->flags & MESH_PATH_FIXED)) {
  412. spin_lock_bh(&mpath->state_lock);
  413. mpath->flags &= ~MESH_PATH_ACTIVE;
  414. ++mpath->sn;
  415. spin_unlock_bh(&mpath->state_lock);
  416. mesh_path_error_tx(sdata,
  417. sdata->u.mesh.mshcfg.element_ttl,
  418. mpath->dst, mpath->sn,
  419. WLAN_REASON_MESH_PATH_DEST_UNREACHABLE, bcast);
  420. }
  421. }
  422. rcu_read_unlock();
  423. }
  424. static void mesh_path_free_rcu(struct mesh_table *tbl,
  425. struct mesh_path *mpath)
  426. {
  427. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  428. spin_lock_bh(&mpath->state_lock);
  429. mpath->flags |= MESH_PATH_RESOLVING | MESH_PATH_DELETED;
  430. mesh_gate_del(tbl, mpath);
  431. spin_unlock_bh(&mpath->state_lock);
  432. del_timer_sync(&mpath->timer);
  433. atomic_dec(&sdata->u.mesh.mpaths);
  434. atomic_dec(&tbl->entries);
  435. mesh_path_flush_pending(mpath);
  436. kfree_rcu(mpath, rcu);
  437. }
  438. static void __mesh_path_del(struct mesh_table *tbl, struct mesh_path *mpath)
  439. {
  440. hlist_del_rcu(&mpath->walk_list);
  441. rhashtable_remove_fast(&tbl->rhead, &mpath->rhash, mesh_rht_params);
  442. mesh_path_free_rcu(tbl, mpath);
  443. }
  444. /**
  445. * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
  446. *
  447. * @sta: mesh peer to match
  448. *
  449. * RCU notes: this function is called when a mesh plink transitions from
  450. * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
  451. * allows path creation. This will happen before the sta can be freed (because
  452. * sta_info_destroy() calls this) so any reader in a rcu read block will be
  453. * protected against the plink disappearing.
  454. */
  455. void mesh_path_flush_by_nexthop(struct sta_info *sta)
  456. {
  457. struct ieee80211_sub_if_data *sdata = sta->sdata;
  458. struct mesh_table *tbl = &sdata->u.mesh.mesh_paths;
  459. struct mesh_path *mpath;
  460. struct hlist_node *n;
  461. spin_lock_bh(&tbl->walk_lock);
  462. hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
  463. if (rcu_access_pointer(mpath->next_hop) == sta)
  464. __mesh_path_del(tbl, mpath);
  465. }
  466. spin_unlock_bh(&tbl->walk_lock);
  467. }
  468. static void mpp_flush_by_proxy(struct ieee80211_sub_if_data *sdata,
  469. const u8 *proxy)
  470. {
  471. struct mesh_table *tbl = &sdata->u.mesh.mpp_paths;
  472. struct mesh_path *mpath;
  473. struct hlist_node *n;
  474. spin_lock_bh(&tbl->walk_lock);
  475. hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
  476. if (ether_addr_equal(mpath->mpp, proxy))
  477. __mesh_path_del(tbl, mpath);
  478. }
  479. spin_unlock_bh(&tbl->walk_lock);
  480. }
  481. static void table_flush_by_iface(struct mesh_table *tbl)
  482. {
  483. struct mesh_path *mpath;
  484. struct hlist_node *n;
  485. spin_lock_bh(&tbl->walk_lock);
  486. hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
  487. __mesh_path_del(tbl, mpath);
  488. }
  489. spin_unlock_bh(&tbl->walk_lock);
  490. }
  491. /**
  492. * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
  493. *
  494. * This function deletes both mesh paths as well as mesh portal paths.
  495. *
  496. * @sdata: interface data to match
  497. *
  498. */
  499. void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
  500. {
  501. table_flush_by_iface(&sdata->u.mesh.mesh_paths);
  502. table_flush_by_iface(&sdata->u.mesh.mpp_paths);
  503. }
  504. /**
  505. * table_path_del - delete a path from the mesh or mpp table
  506. *
  507. * @tbl: mesh or mpp path table
  508. * @sdata: local subif
  509. * @addr: dst address (ETH_ALEN length)
  510. *
  511. * Returns: 0 if successful
  512. */
  513. static int table_path_del(struct mesh_table *tbl,
  514. struct ieee80211_sub_if_data *sdata,
  515. const u8 *addr)
  516. {
  517. struct mesh_path *mpath;
  518. spin_lock_bh(&tbl->walk_lock);
  519. mpath = rhashtable_lookup_fast(&tbl->rhead, addr, mesh_rht_params);
  520. if (!mpath) {
  521. spin_unlock_bh(&tbl->walk_lock);
  522. return -ENXIO;
  523. }
  524. __mesh_path_del(tbl, mpath);
  525. spin_unlock_bh(&tbl->walk_lock);
  526. return 0;
  527. }
  528. /**
  529. * mesh_path_del - delete a mesh path from the table
  530. *
  531. * @addr: dst address (ETH_ALEN length)
  532. * @sdata: local subif
  533. *
  534. * Returns: 0 if successful
  535. */
  536. int mesh_path_del(struct ieee80211_sub_if_data *sdata, const u8 *addr)
  537. {
  538. int err;
  539. /* flush relevant mpp entries first */
  540. mpp_flush_by_proxy(sdata, addr);
  541. err = table_path_del(&sdata->u.mesh.mesh_paths, sdata, addr);
  542. sdata->u.mesh.mesh_paths_generation++;
  543. return err;
  544. }
  545. /**
  546. * mesh_path_tx_pending - sends pending frames in a mesh path queue
  547. *
  548. * @mpath: mesh path to activate
  549. *
  550. * Locking: the state_lock of the mpath structure must NOT be held when calling
  551. * this function.
  552. */
  553. void mesh_path_tx_pending(struct mesh_path *mpath)
  554. {
  555. if (mpath->flags & MESH_PATH_ACTIVE)
  556. ieee80211_add_pending_skbs(mpath->sdata->local,
  557. &mpath->frame_queue);
  558. }
  559. /**
  560. * mesh_path_send_to_gates - sends pending frames to all known mesh gates
  561. *
  562. * @mpath: mesh path whose queue will be emptied
  563. *
  564. * If there is only one gate, the frames are transferred from the failed mpath
  565. * queue to that gate's queue. If there are more than one gates, the frames
  566. * are copied from each gate to the next. After frames are copied, the
  567. * mpath queues are emptied onto the transmission queue.
  568. */
  569. int mesh_path_send_to_gates(struct mesh_path *mpath)
  570. {
  571. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  572. struct mesh_table *tbl;
  573. struct mesh_path *from_mpath = mpath;
  574. struct mesh_path *gate;
  575. bool copy = false;
  576. tbl = &sdata->u.mesh.mesh_paths;
  577. rcu_read_lock();
  578. hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) {
  579. if (gate->flags & MESH_PATH_ACTIVE) {
  580. mpath_dbg(sdata, "Forwarding to %pM\n", gate->dst);
  581. mesh_path_move_to_queue(gate, from_mpath, copy);
  582. from_mpath = gate;
  583. copy = true;
  584. } else {
  585. mpath_dbg(sdata,
  586. "Not forwarding to %pM (flags %#x)\n",
  587. gate->dst, gate->flags);
  588. }
  589. }
  590. hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) {
  591. mpath_dbg(sdata, "Sending to %pM\n", gate->dst);
  592. mesh_path_tx_pending(gate);
  593. }
  594. rcu_read_unlock();
  595. return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
  596. }
  597. /**
  598. * mesh_path_discard_frame - discard a frame whose path could not be resolved
  599. *
  600. * @skb: frame to discard
  601. * @sdata: network subif the frame was to be sent through
  602. *
  603. * Locking: the function must me called within a rcu_read_lock region
  604. */
  605. void mesh_path_discard_frame(struct ieee80211_sub_if_data *sdata,
  606. struct sk_buff *skb)
  607. {
  608. ieee80211_free_txskb(&sdata->local->hw, skb);
  609. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  610. }
  611. /**
  612. * mesh_path_flush_pending - free the pending queue of a mesh path
  613. *
  614. * @mpath: mesh path whose queue has to be freed
  615. *
  616. * Locking: the function must me called within a rcu_read_lock region
  617. */
  618. void mesh_path_flush_pending(struct mesh_path *mpath)
  619. {
  620. struct sk_buff *skb;
  621. while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
  622. mesh_path_discard_frame(mpath->sdata, skb);
  623. }
  624. /**
  625. * mesh_path_fix_nexthop - force a specific next hop for a mesh path
  626. *
  627. * @mpath: the mesh path to modify
  628. * @next_hop: the next hop to force
  629. *
  630. * Locking: this function must be called holding mpath->state_lock
  631. */
  632. void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
  633. {
  634. spin_lock_bh(&mpath->state_lock);
  635. mesh_path_assign_nexthop(mpath, next_hop);
  636. mpath->sn = 0xffff;
  637. mpath->metric = 0;
  638. mpath->hop_count = 0;
  639. mpath->exp_time = 0;
  640. mpath->flags = MESH_PATH_FIXED | MESH_PATH_SN_VALID;
  641. mesh_path_activate(mpath);
  642. spin_unlock_bh(&mpath->state_lock);
  643. ewma_mesh_fail_avg_init(&next_hop->mesh->fail_avg);
  644. /* init it at a low value - 0 start is tricky */
  645. ewma_mesh_fail_avg_add(&next_hop->mesh->fail_avg, 1);
  646. mesh_path_tx_pending(mpath);
  647. }
  648. void mesh_pathtbl_init(struct ieee80211_sub_if_data *sdata)
  649. {
  650. mesh_table_init(&sdata->u.mesh.mesh_paths);
  651. mesh_table_init(&sdata->u.mesh.mpp_paths);
  652. }
  653. static
  654. void mesh_path_tbl_expire(struct ieee80211_sub_if_data *sdata,
  655. struct mesh_table *tbl)
  656. {
  657. struct mesh_path *mpath;
  658. struct hlist_node *n;
  659. spin_lock_bh(&tbl->walk_lock);
  660. hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
  661. if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
  662. (!(mpath->flags & MESH_PATH_FIXED)) &&
  663. time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
  664. __mesh_path_del(tbl, mpath);
  665. }
  666. spin_unlock_bh(&tbl->walk_lock);
  667. }
  668. void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
  669. {
  670. mesh_path_tbl_expire(sdata, &sdata->u.mesh.mesh_paths);
  671. mesh_path_tbl_expire(sdata, &sdata->u.mesh.mpp_paths);
  672. }
  673. void mesh_pathtbl_unregister(struct ieee80211_sub_if_data *sdata)
  674. {
  675. mesh_table_free(&sdata->u.mesh.mesh_paths);
  676. mesh_table_free(&sdata->u.mesh.mpp_paths);
  677. }