mesh_pathtbl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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 "ieee80211_i.h"
  17. #include "mesh.h"
  18. #ifdef CONFIG_MAC80211_VERBOSE_MPATH_DEBUG
  19. #define mpath_dbg(fmt, args...) printk(KERN_DEBUG fmt, ##args)
  20. #else
  21. #define mpath_dbg(fmt, args...) do { (void)(0); } while (0)
  22. #endif
  23. /* There will be initially 2^INIT_PATHS_SIZE_ORDER buckets */
  24. #define INIT_PATHS_SIZE_ORDER 2
  25. /* Keep the mean chain length below this constant */
  26. #define MEAN_CHAIN_LEN 2
  27. #define MPATH_EXPIRED(mpath) ((mpath->flags & MESH_PATH_ACTIVE) && \
  28. time_after(jiffies, mpath->exp_time) && \
  29. !(mpath->flags & MESH_PATH_FIXED))
  30. struct mpath_node {
  31. struct hlist_node list;
  32. struct rcu_head rcu;
  33. /* This indirection allows two different tables to point to the same
  34. * mesh_path structure, useful when resizing
  35. */
  36. struct mesh_path *mpath;
  37. };
  38. static struct mesh_table __rcu *mesh_paths;
  39. static struct mesh_table __rcu *mpp_paths; /* Store paths for MPP&MAP */
  40. int mesh_paths_generation;
  41. /* This lock will have the grow table function as writer and add / delete nodes
  42. * as readers. When reading the table (i.e. doing lookups) we are well protected
  43. * by RCU
  44. */
  45. static DEFINE_RWLOCK(pathtbl_resize_lock);
  46. static inline struct mesh_table *resize_dereference_mesh_paths(void)
  47. {
  48. return rcu_dereference_protected(mesh_paths,
  49. lockdep_is_held(&pathtbl_resize_lock));
  50. }
  51. static inline struct mesh_table *resize_dereference_mpp_paths(void)
  52. {
  53. return rcu_dereference_protected(mpp_paths,
  54. lockdep_is_held(&pathtbl_resize_lock));
  55. }
  56. /*
  57. * CAREFUL -- "tbl" must not be an expression,
  58. * in particular not an rcu_dereference(), since
  59. * it's used twice. So it is illegal to do
  60. * for_each_mesh_entry(rcu_dereference(...), ...)
  61. */
  62. #define for_each_mesh_entry(tbl, p, node, i) \
  63. for (i = 0; i <= tbl->hash_mask; i++) \
  64. hlist_for_each_entry_rcu(node, p, &tbl->hash_buckets[i], list)
  65. static struct mesh_table *mesh_table_alloc(int size_order)
  66. {
  67. int i;
  68. struct mesh_table *newtbl;
  69. newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC);
  70. if (!newtbl)
  71. return NULL;
  72. newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) *
  73. (1 << size_order), GFP_ATOMIC);
  74. if (!newtbl->hash_buckets) {
  75. kfree(newtbl);
  76. return NULL;
  77. }
  78. newtbl->hashwlock = kmalloc(sizeof(spinlock_t) *
  79. (1 << size_order), GFP_ATOMIC);
  80. if (!newtbl->hashwlock) {
  81. kfree(newtbl->hash_buckets);
  82. kfree(newtbl);
  83. return NULL;
  84. }
  85. newtbl->size_order = size_order;
  86. newtbl->hash_mask = (1 << size_order) - 1;
  87. atomic_set(&newtbl->entries, 0);
  88. get_random_bytes(&newtbl->hash_rnd,
  89. sizeof(newtbl->hash_rnd));
  90. for (i = 0; i <= newtbl->hash_mask; i++)
  91. spin_lock_init(&newtbl->hashwlock[i]);
  92. return newtbl;
  93. }
  94. static void __mesh_table_free(struct mesh_table *tbl)
  95. {
  96. kfree(tbl->hash_buckets);
  97. kfree(tbl->hashwlock);
  98. kfree(tbl);
  99. }
  100. static void mesh_table_free(struct mesh_table *tbl, bool free_leafs)
  101. {
  102. struct hlist_head *mesh_hash;
  103. struct hlist_node *p, *q;
  104. int i;
  105. mesh_hash = tbl->hash_buckets;
  106. for (i = 0; i <= tbl->hash_mask; i++) {
  107. spin_lock_bh(&tbl->hashwlock[i]);
  108. hlist_for_each_safe(p, q, &mesh_hash[i]) {
  109. tbl->free_node(p, free_leafs);
  110. atomic_dec(&tbl->entries);
  111. }
  112. spin_unlock_bh(&tbl->hashwlock[i]);
  113. }
  114. __mesh_table_free(tbl);
  115. }
  116. static int mesh_table_grow(struct mesh_table *oldtbl,
  117. struct mesh_table *newtbl)
  118. {
  119. struct hlist_head *oldhash;
  120. struct hlist_node *p, *q;
  121. int i;
  122. if (atomic_read(&oldtbl->entries)
  123. < oldtbl->mean_chain_len * (oldtbl->hash_mask + 1))
  124. return -EAGAIN;
  125. newtbl->free_node = oldtbl->free_node;
  126. newtbl->mean_chain_len = oldtbl->mean_chain_len;
  127. newtbl->copy_node = oldtbl->copy_node;
  128. atomic_set(&newtbl->entries, atomic_read(&oldtbl->entries));
  129. oldhash = oldtbl->hash_buckets;
  130. for (i = 0; i <= oldtbl->hash_mask; i++)
  131. hlist_for_each(p, &oldhash[i])
  132. if (oldtbl->copy_node(p, newtbl) < 0)
  133. goto errcopy;
  134. return 0;
  135. errcopy:
  136. for (i = 0; i <= newtbl->hash_mask; i++) {
  137. hlist_for_each_safe(p, q, &newtbl->hash_buckets[i])
  138. oldtbl->free_node(p, 0);
  139. }
  140. return -ENOMEM;
  141. }
  142. static u32 mesh_table_hash(u8 *addr, struct ieee80211_sub_if_data *sdata,
  143. struct mesh_table *tbl)
  144. {
  145. /* Use last four bytes of hw addr and interface index as hash index */
  146. return jhash_2words(*(u32 *)(addr+2), sdata->dev->ifindex, tbl->hash_rnd)
  147. & tbl->hash_mask;
  148. }
  149. /**
  150. *
  151. * mesh_path_assign_nexthop - update mesh path next hop
  152. *
  153. * @mpath: mesh path to update
  154. * @sta: next hop to assign
  155. *
  156. * Locking: mpath->state_lock must be held when calling this function
  157. */
  158. void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
  159. {
  160. struct sk_buff *skb;
  161. struct ieee80211_hdr *hdr;
  162. struct sk_buff_head tmpq;
  163. unsigned long flags;
  164. rcu_assign_pointer(mpath->next_hop, sta);
  165. __skb_queue_head_init(&tmpq);
  166. spin_lock_irqsave(&mpath->frame_queue.lock, flags);
  167. while ((skb = __skb_dequeue(&mpath->frame_queue)) != NULL) {
  168. hdr = (struct ieee80211_hdr *) skb->data;
  169. memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
  170. __skb_queue_tail(&tmpq, skb);
  171. }
  172. skb_queue_splice(&tmpq, &mpath->frame_queue);
  173. spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
  174. }
  175. /**
  176. * mesh_path_lookup - look up a path in the mesh path table
  177. * @dst: hardware address (ETH_ALEN length) of destination
  178. * @sdata: local subif
  179. *
  180. * Returns: pointer to the mesh path structure, or NULL if not found
  181. *
  182. * Locking: must be called within a read rcu section.
  183. */
  184. struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
  185. {
  186. struct mesh_path *mpath;
  187. struct hlist_node *n;
  188. struct hlist_head *bucket;
  189. struct mesh_table *tbl;
  190. struct mpath_node *node;
  191. tbl = rcu_dereference(mesh_paths);
  192. bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
  193. hlist_for_each_entry_rcu(node, n, bucket, list) {
  194. mpath = node->mpath;
  195. if (mpath->sdata == sdata &&
  196. memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
  197. if (MPATH_EXPIRED(mpath)) {
  198. spin_lock_bh(&mpath->state_lock);
  199. if (MPATH_EXPIRED(mpath))
  200. mpath->flags &= ~MESH_PATH_ACTIVE;
  201. spin_unlock_bh(&mpath->state_lock);
  202. }
  203. return mpath;
  204. }
  205. }
  206. return NULL;
  207. }
  208. struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
  209. {
  210. struct mesh_path *mpath;
  211. struct hlist_node *n;
  212. struct hlist_head *bucket;
  213. struct mesh_table *tbl;
  214. struct mpath_node *node;
  215. tbl = rcu_dereference(mpp_paths);
  216. bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
  217. hlist_for_each_entry_rcu(node, n, bucket, list) {
  218. mpath = node->mpath;
  219. if (mpath->sdata == sdata &&
  220. memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
  221. if (MPATH_EXPIRED(mpath)) {
  222. spin_lock_bh(&mpath->state_lock);
  223. if (MPATH_EXPIRED(mpath))
  224. mpath->flags &= ~MESH_PATH_ACTIVE;
  225. spin_unlock_bh(&mpath->state_lock);
  226. }
  227. return mpath;
  228. }
  229. }
  230. return NULL;
  231. }
  232. /**
  233. * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
  234. * @idx: index
  235. * @sdata: local subif, or NULL for all entries
  236. *
  237. * Returns: pointer to the mesh path structure, or NULL if not found.
  238. *
  239. * Locking: must be called within a read rcu section.
  240. */
  241. struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
  242. {
  243. struct mesh_table *tbl = rcu_dereference(mesh_paths);
  244. struct mpath_node *node;
  245. struct hlist_node *p;
  246. int i;
  247. int j = 0;
  248. for_each_mesh_entry(tbl, p, node, i) {
  249. if (sdata && node->mpath->sdata != sdata)
  250. continue;
  251. if (j++ == idx) {
  252. if (MPATH_EXPIRED(node->mpath)) {
  253. spin_lock_bh(&node->mpath->state_lock);
  254. if (MPATH_EXPIRED(node->mpath))
  255. node->mpath->flags &= ~MESH_PATH_ACTIVE;
  256. spin_unlock_bh(&node->mpath->state_lock);
  257. }
  258. return node->mpath;
  259. }
  260. }
  261. return NULL;
  262. }
  263. /**
  264. * mesh_path_add - allocate and add a new path to the mesh path table
  265. * @addr: destination address of the path (ETH_ALEN length)
  266. * @sdata: local subif
  267. *
  268. * Returns: 0 on success
  269. *
  270. * State: the initial state of the new path is set to 0
  271. */
  272. int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
  273. {
  274. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  275. struct ieee80211_local *local = sdata->local;
  276. struct mesh_table *tbl;
  277. struct mesh_path *mpath, *new_mpath;
  278. struct mpath_node *node, *new_node;
  279. struct hlist_head *bucket;
  280. struct hlist_node *n;
  281. int grow = 0;
  282. int err = 0;
  283. u32 hash_idx;
  284. if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
  285. /* never add ourselves as neighbours */
  286. return -ENOTSUPP;
  287. if (is_multicast_ether_addr(dst))
  288. return -ENOTSUPP;
  289. if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
  290. return -ENOSPC;
  291. err = -ENOMEM;
  292. new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
  293. if (!new_mpath)
  294. goto err_path_alloc;
  295. new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
  296. if (!new_node)
  297. goto err_node_alloc;
  298. read_lock_bh(&pathtbl_resize_lock);
  299. memcpy(new_mpath->dst, dst, ETH_ALEN);
  300. new_mpath->sdata = sdata;
  301. new_mpath->flags = 0;
  302. skb_queue_head_init(&new_mpath->frame_queue);
  303. new_node->mpath = new_mpath;
  304. new_mpath->timer.data = (unsigned long) new_mpath;
  305. new_mpath->timer.function = mesh_path_timer;
  306. new_mpath->exp_time = jiffies;
  307. spin_lock_init(&new_mpath->state_lock);
  308. init_timer(&new_mpath->timer);
  309. tbl = resize_dereference_mesh_paths();
  310. hash_idx = mesh_table_hash(dst, sdata, tbl);
  311. bucket = &tbl->hash_buckets[hash_idx];
  312. spin_lock_bh(&tbl->hashwlock[hash_idx]);
  313. err = -EEXIST;
  314. hlist_for_each_entry(node, n, bucket, list) {
  315. mpath = node->mpath;
  316. if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
  317. goto err_exists;
  318. }
  319. hlist_add_head_rcu(&new_node->list, bucket);
  320. if (atomic_inc_return(&tbl->entries) >=
  321. tbl->mean_chain_len * (tbl->hash_mask + 1))
  322. grow = 1;
  323. mesh_paths_generation++;
  324. spin_unlock_bh(&tbl->hashwlock[hash_idx]);
  325. read_unlock_bh(&pathtbl_resize_lock);
  326. if (grow) {
  327. set_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags);
  328. ieee80211_queue_work(&local->hw, &sdata->work);
  329. }
  330. return 0;
  331. err_exists:
  332. spin_unlock_bh(&tbl->hashwlock[hash_idx]);
  333. read_unlock_bh(&pathtbl_resize_lock);
  334. kfree(new_node);
  335. err_node_alloc:
  336. kfree(new_mpath);
  337. err_path_alloc:
  338. atomic_dec(&sdata->u.mesh.mpaths);
  339. return err;
  340. }
  341. static void mesh_table_free_rcu(struct rcu_head *rcu)
  342. {
  343. struct mesh_table *tbl = container_of(rcu, struct mesh_table, rcu_head);
  344. mesh_table_free(tbl, false);
  345. }
  346. void mesh_mpath_table_grow(void)
  347. {
  348. struct mesh_table *oldtbl, *newtbl;
  349. write_lock_bh(&pathtbl_resize_lock);
  350. oldtbl = resize_dereference_mesh_paths();
  351. newtbl = mesh_table_alloc(oldtbl->size_order + 1);
  352. if (!newtbl)
  353. goto out;
  354. if (mesh_table_grow(oldtbl, newtbl) < 0) {
  355. __mesh_table_free(newtbl);
  356. goto out;
  357. }
  358. rcu_assign_pointer(mesh_paths, newtbl);
  359. call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
  360. out:
  361. write_unlock_bh(&pathtbl_resize_lock);
  362. }
  363. void mesh_mpp_table_grow(void)
  364. {
  365. struct mesh_table *oldtbl, *newtbl;
  366. write_lock_bh(&pathtbl_resize_lock);
  367. oldtbl = resize_dereference_mpp_paths();
  368. newtbl = mesh_table_alloc(oldtbl->size_order + 1);
  369. if (!newtbl)
  370. goto out;
  371. if (mesh_table_grow(oldtbl, newtbl) < 0) {
  372. __mesh_table_free(newtbl);
  373. goto out;
  374. }
  375. rcu_assign_pointer(mpp_paths, newtbl);
  376. call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
  377. out:
  378. write_unlock_bh(&pathtbl_resize_lock);
  379. }
  380. int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata)
  381. {
  382. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  383. struct ieee80211_local *local = sdata->local;
  384. struct mesh_table *tbl;
  385. struct mesh_path *mpath, *new_mpath;
  386. struct mpath_node *node, *new_node;
  387. struct hlist_head *bucket;
  388. struct hlist_node *n;
  389. int grow = 0;
  390. int err = 0;
  391. u32 hash_idx;
  392. if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
  393. /* never add ourselves as neighbours */
  394. return -ENOTSUPP;
  395. if (is_multicast_ether_addr(dst))
  396. return -ENOTSUPP;
  397. err = -ENOMEM;
  398. new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
  399. if (!new_mpath)
  400. goto err_path_alloc;
  401. new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
  402. if (!new_node)
  403. goto err_node_alloc;
  404. read_lock_bh(&pathtbl_resize_lock);
  405. memcpy(new_mpath->dst, dst, ETH_ALEN);
  406. memcpy(new_mpath->mpp, mpp, ETH_ALEN);
  407. new_mpath->sdata = sdata;
  408. new_mpath->flags = 0;
  409. skb_queue_head_init(&new_mpath->frame_queue);
  410. new_node->mpath = new_mpath;
  411. new_mpath->exp_time = jiffies;
  412. spin_lock_init(&new_mpath->state_lock);
  413. tbl = resize_dereference_mpp_paths();
  414. hash_idx = mesh_table_hash(dst, sdata, tbl);
  415. bucket = &tbl->hash_buckets[hash_idx];
  416. spin_lock_bh(&tbl->hashwlock[hash_idx]);
  417. err = -EEXIST;
  418. hlist_for_each_entry(node, n, bucket, list) {
  419. mpath = node->mpath;
  420. if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
  421. goto err_exists;
  422. }
  423. hlist_add_head_rcu(&new_node->list, bucket);
  424. if (atomic_inc_return(&tbl->entries) >=
  425. tbl->mean_chain_len * (tbl->hash_mask + 1))
  426. grow = 1;
  427. spin_unlock_bh(&tbl->hashwlock[hash_idx]);
  428. read_unlock_bh(&pathtbl_resize_lock);
  429. if (grow) {
  430. set_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags);
  431. ieee80211_queue_work(&local->hw, &sdata->work);
  432. }
  433. return 0;
  434. err_exists:
  435. spin_unlock_bh(&tbl->hashwlock[hash_idx]);
  436. read_unlock_bh(&pathtbl_resize_lock);
  437. kfree(new_node);
  438. err_node_alloc:
  439. kfree(new_mpath);
  440. err_path_alloc:
  441. return err;
  442. }
  443. /**
  444. * mesh_plink_broken - deactivates paths and sends perr when a link breaks
  445. *
  446. * @sta: broken peer link
  447. *
  448. * This function must be called from the rate control algorithm if enough
  449. * delivery errors suggest that a peer link is no longer usable.
  450. */
  451. void mesh_plink_broken(struct sta_info *sta)
  452. {
  453. struct mesh_table *tbl;
  454. static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  455. struct mesh_path *mpath;
  456. struct mpath_node *node;
  457. struct hlist_node *p;
  458. struct ieee80211_sub_if_data *sdata = sta->sdata;
  459. int i;
  460. __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_DEST_UNREACHABLE);
  461. rcu_read_lock();
  462. tbl = rcu_dereference(mesh_paths);
  463. for_each_mesh_entry(tbl, p, node, i) {
  464. mpath = node->mpath;
  465. spin_lock_bh(&mpath->state_lock);
  466. if (rcu_dereference(mpath->next_hop) == sta &&
  467. mpath->flags & MESH_PATH_ACTIVE &&
  468. !(mpath->flags & MESH_PATH_FIXED)) {
  469. mpath->flags &= ~MESH_PATH_ACTIVE;
  470. ++mpath->sn;
  471. spin_unlock_bh(&mpath->state_lock);
  472. mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl,
  473. mpath->dst, cpu_to_le32(mpath->sn),
  474. reason, bcast, sdata);
  475. } else
  476. spin_unlock_bh(&mpath->state_lock);
  477. }
  478. rcu_read_unlock();
  479. }
  480. /**
  481. * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
  482. *
  483. * @sta - mesh peer to match
  484. *
  485. * RCU notes: this function is called when a mesh plink transitions from
  486. * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
  487. * allows path creation. This will happen before the sta can be freed (because
  488. * sta_info_destroy() calls this) so any reader in a rcu read block will be
  489. * protected against the plink disappearing.
  490. */
  491. void mesh_path_flush_by_nexthop(struct sta_info *sta)
  492. {
  493. struct mesh_table *tbl;
  494. struct mesh_path *mpath;
  495. struct mpath_node *node;
  496. struct hlist_node *p;
  497. int i;
  498. rcu_read_lock();
  499. tbl = rcu_dereference(mesh_paths);
  500. for_each_mesh_entry(tbl, p, node, i) {
  501. mpath = node->mpath;
  502. if (rcu_dereference(mpath->next_hop) == sta)
  503. mesh_path_del(mpath->dst, mpath->sdata);
  504. }
  505. rcu_read_unlock();
  506. }
  507. void mesh_path_flush(struct ieee80211_sub_if_data *sdata)
  508. {
  509. struct mesh_table *tbl;
  510. struct mesh_path *mpath;
  511. struct mpath_node *node;
  512. struct hlist_node *p;
  513. int i;
  514. rcu_read_lock();
  515. tbl = rcu_dereference(mesh_paths);
  516. for_each_mesh_entry(tbl, p, node, i) {
  517. mpath = node->mpath;
  518. if (mpath->sdata == sdata)
  519. mesh_path_del(mpath->dst, mpath->sdata);
  520. }
  521. rcu_read_unlock();
  522. }
  523. static void mesh_path_node_reclaim(struct rcu_head *rp)
  524. {
  525. struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
  526. struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
  527. if (node->mpath->timer.function)
  528. del_timer_sync(&node->mpath->timer);
  529. atomic_dec(&sdata->u.mesh.mpaths);
  530. kfree(node->mpath);
  531. kfree(node);
  532. }
  533. /**
  534. * mesh_path_del - delete a mesh path from the table
  535. *
  536. * @addr: dst address (ETH_ALEN length)
  537. * @sdata: local subif
  538. *
  539. * Returns: 0 if successful
  540. */
  541. int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
  542. {
  543. struct mesh_table *tbl;
  544. struct mesh_path *mpath;
  545. struct mpath_node *node;
  546. struct hlist_head *bucket;
  547. struct hlist_node *n;
  548. int hash_idx;
  549. int err = 0;
  550. read_lock_bh(&pathtbl_resize_lock);
  551. tbl = resize_dereference_mesh_paths();
  552. hash_idx = mesh_table_hash(addr, sdata, tbl);
  553. bucket = &tbl->hash_buckets[hash_idx];
  554. spin_lock_bh(&tbl->hashwlock[hash_idx]);
  555. hlist_for_each_entry(node, n, bucket, list) {
  556. mpath = node->mpath;
  557. if (mpath->sdata == sdata &&
  558. memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
  559. spin_lock_bh(&mpath->state_lock);
  560. mpath->flags |= MESH_PATH_RESOLVING;
  561. hlist_del_rcu(&node->list);
  562. call_rcu(&node->rcu, mesh_path_node_reclaim);
  563. atomic_dec(&tbl->entries);
  564. spin_unlock_bh(&mpath->state_lock);
  565. goto enddel;
  566. }
  567. }
  568. err = -ENXIO;
  569. enddel:
  570. mesh_paths_generation++;
  571. spin_unlock_bh(&tbl->hashwlock[hash_idx]);
  572. read_unlock_bh(&pathtbl_resize_lock);
  573. return err;
  574. }
  575. /**
  576. * mesh_path_tx_pending - sends pending frames in a mesh path queue
  577. *
  578. * @mpath: mesh path to activate
  579. *
  580. * Locking: the state_lock of the mpath structure must NOT be held when calling
  581. * this function.
  582. */
  583. void mesh_path_tx_pending(struct mesh_path *mpath)
  584. {
  585. if (mpath->flags & MESH_PATH_ACTIVE)
  586. ieee80211_add_pending_skbs(mpath->sdata->local,
  587. &mpath->frame_queue);
  588. }
  589. /**
  590. * mesh_path_discard_frame - discard a frame whose path could not be resolved
  591. *
  592. * @skb: frame to discard
  593. * @sdata: network subif the frame was to be sent through
  594. *
  595. * If the frame was being forwarded from another MP, a PERR frame will be sent
  596. * to the precursor. The precursor's address (i.e. the previous hop) was saved
  597. * in addr1 of the frame-to-be-forwarded, and would only be overwritten once
  598. * the destination is successfully resolved.
  599. *
  600. * Locking: the function must me called within a rcu_read_lock region
  601. */
  602. void mesh_path_discard_frame(struct sk_buff *skb,
  603. struct ieee80211_sub_if_data *sdata)
  604. {
  605. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  606. struct mesh_path *mpath;
  607. u32 sn = 0;
  608. __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_NOFORWARD);
  609. if (memcmp(hdr->addr4, sdata->vif.addr, ETH_ALEN) != 0) {
  610. u8 *ra, *da;
  611. da = hdr->addr3;
  612. ra = hdr->addr1;
  613. mpath = mesh_path_lookup(da, sdata);
  614. if (mpath)
  615. sn = ++mpath->sn;
  616. mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl, skb->data,
  617. cpu_to_le32(sn), reason, ra, sdata);
  618. }
  619. kfree_skb(skb);
  620. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  621. }
  622. /**
  623. * mesh_path_flush_pending - free the pending queue of a mesh path
  624. *
  625. * @mpath: mesh path whose queue has to be freed
  626. *
  627. * Locking: the function must me called within a rcu_read_lock region
  628. */
  629. void mesh_path_flush_pending(struct mesh_path *mpath)
  630. {
  631. struct sk_buff *skb;
  632. while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
  633. mesh_path_discard_frame(skb, mpath->sdata);
  634. }
  635. /**
  636. * mesh_path_fix_nexthop - force a specific next hop for a mesh path
  637. *
  638. * @mpath: the mesh path to modify
  639. * @next_hop: the next hop to force
  640. *
  641. * Locking: this function must be called holding mpath->state_lock
  642. */
  643. void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
  644. {
  645. spin_lock_bh(&mpath->state_lock);
  646. mesh_path_assign_nexthop(mpath, next_hop);
  647. mpath->sn = 0xffff;
  648. mpath->metric = 0;
  649. mpath->hop_count = 0;
  650. mpath->exp_time = 0;
  651. mpath->flags |= MESH_PATH_FIXED;
  652. mesh_path_activate(mpath);
  653. spin_unlock_bh(&mpath->state_lock);
  654. mesh_path_tx_pending(mpath);
  655. }
  656. static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
  657. {
  658. struct mesh_path *mpath;
  659. struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
  660. mpath = node->mpath;
  661. hlist_del_rcu(p);
  662. if (free_leafs) {
  663. if (mpath->timer.function)
  664. del_timer_sync(&mpath->timer);
  665. kfree(mpath);
  666. }
  667. kfree(node);
  668. }
  669. static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
  670. {
  671. struct mesh_path *mpath;
  672. struct mpath_node *node, *new_node;
  673. u32 hash_idx;
  674. new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
  675. if (new_node == NULL)
  676. return -ENOMEM;
  677. node = hlist_entry(p, struct mpath_node, list);
  678. mpath = node->mpath;
  679. new_node->mpath = mpath;
  680. hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
  681. hlist_add_head(&new_node->list,
  682. &newtbl->hash_buckets[hash_idx]);
  683. return 0;
  684. }
  685. int mesh_pathtbl_init(void)
  686. {
  687. struct mesh_table *tbl_path, *tbl_mpp;
  688. tbl_path = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
  689. if (!tbl_path)
  690. return -ENOMEM;
  691. tbl_path->free_node = &mesh_path_node_free;
  692. tbl_path->copy_node = &mesh_path_node_copy;
  693. tbl_path->mean_chain_len = MEAN_CHAIN_LEN;
  694. tbl_mpp = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
  695. if (!tbl_mpp) {
  696. mesh_table_free(tbl_path, true);
  697. return -ENOMEM;
  698. }
  699. tbl_mpp->free_node = &mesh_path_node_free;
  700. tbl_mpp->copy_node = &mesh_path_node_copy;
  701. tbl_mpp->mean_chain_len = MEAN_CHAIN_LEN;
  702. /* Need no locking since this is during init */
  703. RCU_INIT_POINTER(mesh_paths, tbl_path);
  704. RCU_INIT_POINTER(mpp_paths, tbl_mpp);
  705. return 0;
  706. }
  707. void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
  708. {
  709. struct mesh_table *tbl;
  710. struct mesh_path *mpath;
  711. struct mpath_node *node;
  712. struct hlist_node *p;
  713. int i;
  714. rcu_read_lock();
  715. tbl = rcu_dereference(mesh_paths);
  716. for_each_mesh_entry(tbl, p, node, i) {
  717. if (node->mpath->sdata != sdata)
  718. continue;
  719. mpath = node->mpath;
  720. spin_lock_bh(&mpath->state_lock);
  721. if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
  722. (!(mpath->flags & MESH_PATH_FIXED)) &&
  723. time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE)) {
  724. spin_unlock_bh(&mpath->state_lock);
  725. mesh_path_del(mpath->dst, mpath->sdata);
  726. } else
  727. spin_unlock_bh(&mpath->state_lock);
  728. }
  729. rcu_read_unlock();
  730. }
  731. void mesh_pathtbl_unregister(void)
  732. {
  733. /* no need for locking during exit path */
  734. mesh_table_free(rcu_dereference_raw(mesh_paths), true);
  735. mesh_table_free(rcu_dereference_raw(mpp_paths), true);
  736. }