mesh_pathtbl.c 22 KB

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