mesh_pathtbl.c 23 KB

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