mesh_pathtbl.c 21 KB

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