mesh_pathtbl.c 13 KB

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