mesh_pathtbl.c 19 KB

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