mesh_pathtbl.c 20 KB

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