mesh_pathtbl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Copyright (c) 2008 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/spinlock.h>
  13. #include <linux/string.h>
  14. #include <net/mac80211.h>
  15. #include "ieee80211_i.h"
  16. #include "mesh.h"
  17. /* There will be initially 2^INIT_PATHS_SIZE_ORDER buckets */
  18. #define INIT_PATHS_SIZE_ORDER 2
  19. /* Keep the mean chain length below this constant */
  20. #define MEAN_CHAIN_LEN 2
  21. #define MPATH_EXPIRED(mpath) ((mpath->flags & MESH_PATH_ACTIVE) && \
  22. time_after(jiffies, mpath->exp_time) && \
  23. !(mpath->flags & MESH_PATH_FIXED))
  24. struct mpath_node {
  25. struct hlist_node list;
  26. struct rcu_head rcu;
  27. /* This indirection allows two different tables to point to the same
  28. * mesh_path structure, useful when resizing
  29. */
  30. struct mesh_path *mpath;
  31. };
  32. static struct mesh_table *mesh_paths;
  33. /* This lock will have the grow table function as writer and add / delete nodes
  34. * as readers. When reading the table (i.e. doing lookups) we are well protected
  35. * by RCU
  36. */
  37. static DEFINE_RWLOCK(pathtbl_resize_lock);
  38. /**
  39. *
  40. * mesh_path_assign_nexthop - update mesh path next hop
  41. *
  42. * @mpath: mesh path to update
  43. * @sta: next hop to assign
  44. *
  45. * Locking: mpath->state_lock must be held when calling this function
  46. */
  47. void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
  48. {
  49. rcu_assign_pointer(mpath->next_hop, sta);
  50. }
  51. /**
  52. * mesh_path_lookup - look up a path in the mesh path table
  53. * @dst: hardware address (ETH_ALEN length) of destination
  54. * @sdata: local subif
  55. *
  56. * Returns: pointer to the mesh path structure, or NULL if not found
  57. *
  58. * Locking: must be called within a read rcu section.
  59. */
  60. struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
  61. {
  62. struct mesh_path *mpath;
  63. struct hlist_node *n;
  64. struct hlist_head *bucket;
  65. struct mesh_table *tbl;
  66. struct mpath_node *node;
  67. tbl = rcu_dereference(mesh_paths);
  68. bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
  69. hlist_for_each_entry_rcu(node, n, bucket, list) {
  70. mpath = node->mpath;
  71. if (mpath->sdata == sdata &&
  72. memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
  73. if (MPATH_EXPIRED(mpath)) {
  74. spin_lock_bh(&mpath->state_lock);
  75. if (MPATH_EXPIRED(mpath))
  76. mpath->flags &= ~MESH_PATH_ACTIVE;
  77. spin_unlock_bh(&mpath->state_lock);
  78. }
  79. return mpath;
  80. }
  81. }
  82. return NULL;
  83. }
  84. /**
  85. * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
  86. * @idx: index
  87. * @sdata: local subif, or NULL for all entries
  88. *
  89. * Returns: pointer to the mesh path structure, or NULL if not found.
  90. *
  91. * Locking: must be called within a read rcu section.
  92. */
  93. struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
  94. {
  95. struct mpath_node *node;
  96. struct hlist_node *p;
  97. int i;
  98. int j = 0;
  99. for_each_mesh_entry(mesh_paths, p, node, i) {
  100. if (sdata && node->mpath->sdata != sdata)
  101. continue;
  102. if (j++ == idx) {
  103. if (MPATH_EXPIRED(node->mpath)) {
  104. spin_lock_bh(&node->mpath->state_lock);
  105. if (MPATH_EXPIRED(node->mpath))
  106. node->mpath->flags &= ~MESH_PATH_ACTIVE;
  107. spin_unlock_bh(&node->mpath->state_lock);
  108. }
  109. return node->mpath;
  110. }
  111. }
  112. return NULL;
  113. }
  114. /**
  115. * mesh_path_add - allocate and add a new path to the mesh path table
  116. * @addr: destination address of the path (ETH_ALEN length)
  117. * @sdata: local subif
  118. *
  119. * Returns: 0 on sucess
  120. *
  121. * State: the initial state of the new path is set to 0
  122. */
  123. int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
  124. {
  125. struct mesh_path *mpath, *new_mpath;
  126. struct mpath_node *node, *new_node;
  127. struct hlist_head *bucket;
  128. struct hlist_node *n;
  129. int grow = 0;
  130. int err = 0;
  131. u32 hash_idx;
  132. if (memcmp(dst, sdata->dev->dev_addr, ETH_ALEN) == 0)
  133. /* never add ourselves as neighbours */
  134. return -ENOTSUPP;
  135. if (is_multicast_ether_addr(dst))
  136. return -ENOTSUPP;
  137. if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
  138. return -ENOSPC;
  139. err = -ENOMEM;
  140. new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL);
  141. if (!new_mpath)
  142. goto err_path_alloc;
  143. new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL);
  144. if (!new_node)
  145. goto err_node_alloc;
  146. read_lock(&pathtbl_resize_lock);
  147. memcpy(new_mpath->dst, dst, ETH_ALEN);
  148. new_mpath->sdata = sdata;
  149. new_mpath->flags = 0;
  150. skb_queue_head_init(&new_mpath->frame_queue);
  151. new_node->mpath = new_mpath;
  152. new_mpath->timer.data = (unsigned long) new_mpath;
  153. new_mpath->timer.function = mesh_path_timer;
  154. new_mpath->exp_time = jiffies;
  155. spin_lock_init(&new_mpath->state_lock);
  156. init_timer(&new_mpath->timer);
  157. hash_idx = mesh_table_hash(dst, sdata, mesh_paths);
  158. bucket = &mesh_paths->hash_buckets[hash_idx];
  159. spin_lock(&mesh_paths->hashwlock[hash_idx]);
  160. err = -EEXIST;
  161. hlist_for_each_entry(node, n, bucket, list) {
  162. mpath = node->mpath;
  163. if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
  164. goto err_exists;
  165. }
  166. hlist_add_head_rcu(&new_node->list, bucket);
  167. if (atomic_inc_return(&mesh_paths->entries) >=
  168. mesh_paths->mean_chain_len * (mesh_paths->hash_mask + 1))
  169. grow = 1;
  170. spin_unlock(&mesh_paths->hashwlock[hash_idx]);
  171. read_unlock(&pathtbl_resize_lock);
  172. if (grow) {
  173. struct mesh_table *oldtbl, *newtbl;
  174. write_lock(&pathtbl_resize_lock);
  175. oldtbl = mesh_paths;
  176. newtbl = mesh_table_grow(mesh_paths);
  177. if (!newtbl) {
  178. write_unlock(&pathtbl_resize_lock);
  179. return 0;
  180. }
  181. rcu_assign_pointer(mesh_paths, newtbl);
  182. write_unlock(&pathtbl_resize_lock);
  183. synchronize_rcu();
  184. mesh_table_free(oldtbl, false);
  185. }
  186. return 0;
  187. err_exists:
  188. spin_unlock(&mesh_paths->hashwlock[hash_idx]);
  189. read_unlock(&pathtbl_resize_lock);
  190. kfree(new_node);
  191. err_node_alloc:
  192. kfree(new_mpath);
  193. err_path_alloc:
  194. atomic_dec(&sdata->u.mesh.mpaths);
  195. return err;
  196. }
  197. /**
  198. * mesh_plink_broken - deactivates paths and sends perr when a link breaks
  199. *
  200. * @sta: broken peer link
  201. *
  202. * This function must be called from the rate control algorithm if enough
  203. * delivery errors suggest that a peer link is no longer usable.
  204. */
  205. void mesh_plink_broken(struct sta_info *sta)
  206. {
  207. struct mesh_path *mpath;
  208. struct mpath_node *node;
  209. struct hlist_node *p;
  210. struct ieee80211_sub_if_data *sdata = sta->sdata;
  211. int i;
  212. rcu_read_lock();
  213. for_each_mesh_entry(mesh_paths, p, node, i) {
  214. mpath = node->mpath;
  215. spin_lock_bh(&mpath->state_lock);
  216. if (mpath->next_hop == sta &&
  217. mpath->flags & MESH_PATH_ACTIVE &&
  218. !(mpath->flags & MESH_PATH_FIXED)) {
  219. mpath->flags &= ~MESH_PATH_ACTIVE;
  220. ++mpath->dsn;
  221. spin_unlock_bh(&mpath->state_lock);
  222. mesh_path_error_tx(mpath->dst,
  223. cpu_to_le32(mpath->dsn),
  224. sdata->dev->broadcast, sdata);
  225. } else
  226. spin_unlock_bh(&mpath->state_lock);
  227. }
  228. rcu_read_unlock();
  229. }
  230. /**
  231. * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
  232. *
  233. * @sta - mesh peer to match
  234. *
  235. * RCU notes: this function is called when a mesh plink transitions from
  236. * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
  237. * allows path creation. This will happen before the sta can be freed (because
  238. * sta_info_destroy() calls this) so any reader in a rcu read block will be
  239. * protected against the plink disappearing.
  240. */
  241. void mesh_path_flush_by_nexthop(struct sta_info *sta)
  242. {
  243. struct mesh_path *mpath;
  244. struct mpath_node *node;
  245. struct hlist_node *p;
  246. int i;
  247. for_each_mesh_entry(mesh_paths, p, node, i) {
  248. mpath = node->mpath;
  249. if (mpath->next_hop == sta)
  250. mesh_path_del(mpath->dst, mpath->sdata);
  251. }
  252. }
  253. void mesh_path_flush(struct ieee80211_sub_if_data *sdata)
  254. {
  255. struct mesh_path *mpath;
  256. struct mpath_node *node;
  257. struct hlist_node *p;
  258. int i;
  259. for_each_mesh_entry(mesh_paths, p, node, i) {
  260. mpath = node->mpath;
  261. if (mpath->sdata == sdata)
  262. mesh_path_del(mpath->dst, mpath->sdata);
  263. }
  264. }
  265. static void mesh_path_node_reclaim(struct rcu_head *rp)
  266. {
  267. struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
  268. struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
  269. del_timer_sync(&node->mpath->timer);
  270. atomic_dec(&sdata->u.mesh.mpaths);
  271. kfree(node->mpath);
  272. kfree(node);
  273. }
  274. /**
  275. * mesh_path_del - delete a mesh path from the table
  276. *
  277. * @addr: dst address (ETH_ALEN length)
  278. * @sdata: local subif
  279. *
  280. * Returns: 0 if succesful
  281. */
  282. int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
  283. {
  284. struct mesh_path *mpath;
  285. struct mpath_node *node;
  286. struct hlist_head *bucket;
  287. struct hlist_node *n;
  288. int hash_idx;
  289. int err = 0;
  290. read_lock(&pathtbl_resize_lock);
  291. hash_idx = mesh_table_hash(addr, sdata, mesh_paths);
  292. bucket = &mesh_paths->hash_buckets[hash_idx];
  293. spin_lock(&mesh_paths->hashwlock[hash_idx]);
  294. hlist_for_each_entry(node, n, bucket, list) {
  295. mpath = node->mpath;
  296. if (mpath->sdata == sdata &&
  297. memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
  298. spin_lock_bh(&mpath->state_lock);
  299. mpath->flags |= MESH_PATH_RESOLVING;
  300. hlist_del_rcu(&node->list);
  301. call_rcu(&node->rcu, mesh_path_node_reclaim);
  302. atomic_dec(&mesh_paths->entries);
  303. spin_unlock_bh(&mpath->state_lock);
  304. goto enddel;
  305. }
  306. }
  307. err = -ENXIO;
  308. enddel:
  309. spin_unlock(&mesh_paths->hashwlock[hash_idx]);
  310. read_unlock(&pathtbl_resize_lock);
  311. return err;
  312. }
  313. /**
  314. * mesh_path_tx_pending - sends pending frames in a mesh path queue
  315. *
  316. * @mpath: mesh path to activate
  317. *
  318. * Locking: the state_lock of the mpath structure must NOT be held when calling
  319. * this function.
  320. */
  321. void mesh_path_tx_pending(struct mesh_path *mpath)
  322. {
  323. struct sk_buff *skb;
  324. while ((skb = skb_dequeue(&mpath->frame_queue)) &&
  325. (mpath->flags & MESH_PATH_ACTIVE))
  326. dev_queue_xmit(skb);
  327. }
  328. /**
  329. * mesh_path_discard_frame - discard a frame whose path could not be resolved
  330. *
  331. * @skb: frame to discard
  332. * @sdata: network subif the frame was to be sent through
  333. *
  334. * If the frame was beign forwarded from another MP, a PERR frame will be sent
  335. * to the precursor.
  336. *
  337. * Locking: the function must me called within a rcu_read_lock region
  338. */
  339. void mesh_path_discard_frame(struct sk_buff *skb,
  340. struct ieee80211_sub_if_data *sdata)
  341. {
  342. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  343. struct mesh_path *mpath;
  344. u32 dsn = 0;
  345. if (memcmp(hdr->addr4, sdata->dev->dev_addr, ETH_ALEN) != 0) {
  346. u8 *ra, *da;
  347. da = hdr->addr3;
  348. ra = hdr->addr2;
  349. mpath = mesh_path_lookup(da, sdata);
  350. if (mpath)
  351. dsn = ++mpath->dsn;
  352. mesh_path_error_tx(skb->data, cpu_to_le32(dsn), ra, sdata);
  353. }
  354. kfree_skb(skb);
  355. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  356. }
  357. /**
  358. * mesh_path_flush_pending - free the pending queue of a mesh path
  359. *
  360. * @mpath: mesh path whose queue has to be freed
  361. *
  362. * Locking: the function must me called withing a rcu_read_lock region
  363. */
  364. void mesh_path_flush_pending(struct mesh_path *mpath)
  365. {
  366. struct sk_buff *skb;
  367. while ((skb = skb_dequeue(&mpath->frame_queue)) &&
  368. (mpath->flags & MESH_PATH_ACTIVE))
  369. mesh_path_discard_frame(skb, mpath->sdata);
  370. }
  371. /**
  372. * mesh_path_fix_nexthop - force a specific next hop for a mesh path
  373. *
  374. * @mpath: the mesh path to modify
  375. * @next_hop: the next hop to force
  376. *
  377. * Locking: this function must be called holding mpath->state_lock
  378. */
  379. void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
  380. {
  381. spin_lock_bh(&mpath->state_lock);
  382. mesh_path_assign_nexthop(mpath, next_hop);
  383. mpath->dsn = 0xffff;
  384. mpath->metric = 0;
  385. mpath->hop_count = 0;
  386. mpath->exp_time = 0;
  387. mpath->flags |= MESH_PATH_FIXED;
  388. mesh_path_activate(mpath);
  389. spin_unlock_bh(&mpath->state_lock);
  390. mesh_path_tx_pending(mpath);
  391. }
  392. static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
  393. {
  394. struct mesh_path *mpath;
  395. struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
  396. mpath = node->mpath;
  397. hlist_del_rcu(p);
  398. if (free_leafs)
  399. kfree(mpath);
  400. kfree(node);
  401. }
  402. static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
  403. {
  404. struct mesh_path *mpath;
  405. struct mpath_node *node, *new_node;
  406. u32 hash_idx;
  407. new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
  408. if (new_node == NULL)
  409. return -ENOMEM;
  410. node = hlist_entry(p, struct mpath_node, list);
  411. mpath = node->mpath;
  412. new_node->mpath = mpath;
  413. hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
  414. hlist_add_head(&new_node->list,
  415. &newtbl->hash_buckets[hash_idx]);
  416. return 0;
  417. }
  418. int mesh_pathtbl_init(void)
  419. {
  420. mesh_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
  421. mesh_paths->free_node = &mesh_path_node_free;
  422. mesh_paths->copy_node = &mesh_path_node_copy;
  423. mesh_paths->mean_chain_len = MEAN_CHAIN_LEN;
  424. if (!mesh_paths)
  425. return -ENOMEM;
  426. return 0;
  427. }
  428. void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
  429. {
  430. struct mesh_path *mpath;
  431. struct mpath_node *node;
  432. struct hlist_node *p;
  433. int i;
  434. read_lock(&pathtbl_resize_lock);
  435. for_each_mesh_entry(mesh_paths, p, node, i) {
  436. if (node->mpath->sdata != sdata)
  437. continue;
  438. mpath = node->mpath;
  439. spin_lock_bh(&mpath->state_lock);
  440. if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
  441. (!(mpath->flags & MESH_PATH_FIXED)) &&
  442. time_after(jiffies,
  443. mpath->exp_time + MESH_PATH_EXPIRE)) {
  444. spin_unlock_bh(&mpath->state_lock);
  445. mesh_path_del(mpath->dst, mpath->sdata);
  446. } else
  447. spin_unlock_bh(&mpath->state_lock);
  448. }
  449. read_unlock(&pathtbl_resize_lock);
  450. }
  451. void mesh_pathtbl_unregister(void)
  452. {
  453. mesh_table_free(mesh_paths, true);
  454. }