mesh_pathtbl.c 19 KB

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