mesh_pathtbl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. new_mpath = kzalloc(sizeof(struct mesh_path), GFP_KERNEL);
  142. if (!new_mpath) {
  143. atomic_dec(&sdata->u.sta.mpaths);
  144. err = -ENOMEM;
  145. goto endadd2;
  146. }
  147. new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL);
  148. if (!new_node) {
  149. kfree(new_mpath);
  150. atomic_dec(&sdata->u.sta.mpaths);
  151. err = -ENOMEM;
  152. goto endadd2;
  153. }
  154. read_lock(&pathtbl_resize_lock);
  155. memcpy(new_mpath->dst, dst, ETH_ALEN);
  156. new_mpath->dev = dev;
  157. new_mpath->flags = 0;
  158. skb_queue_head_init(&new_mpath->frame_queue);
  159. new_node->mpath = new_mpath;
  160. new_mpath->timer.data = (unsigned long) new_mpath;
  161. new_mpath->timer.function = mesh_path_timer;
  162. new_mpath->exp_time = jiffies;
  163. spin_lock_init(&new_mpath->state_lock);
  164. init_timer(&new_mpath->timer);
  165. hash_idx = mesh_table_hash(dst, dev, mesh_paths);
  166. bucket = &mesh_paths->hash_buckets[hash_idx];
  167. spin_lock(&mesh_paths->hashwlock[hash_idx]);
  168. hlist_for_each_entry(node, n, bucket, list) {
  169. mpath = node->mpath;
  170. if (mpath->dev == dev && memcmp(dst, mpath->dst, ETH_ALEN)
  171. == 0) {
  172. err = -EEXIST;
  173. atomic_dec(&sdata->u.sta.mpaths);
  174. kfree(new_node);
  175. kfree(new_mpath);
  176. goto endadd;
  177. }
  178. }
  179. hlist_add_head_rcu(&new_node->list, bucket);
  180. if (atomic_inc_return(&mesh_paths->entries) >=
  181. mesh_paths->mean_chain_len * (mesh_paths->hash_mask + 1))
  182. grow = 1;
  183. endadd:
  184. spin_unlock(&mesh_paths->hashwlock[hash_idx]);
  185. read_unlock(&pathtbl_resize_lock);
  186. if (!err && grow) {
  187. struct mesh_table *oldtbl, *newtbl;
  188. write_lock(&pathtbl_resize_lock);
  189. oldtbl = mesh_paths;
  190. newtbl = mesh_table_grow(mesh_paths);
  191. if (!newtbl) {
  192. write_unlock(&pathtbl_resize_lock);
  193. return -ENOMEM;
  194. }
  195. rcu_assign_pointer(mesh_paths, newtbl);
  196. write_unlock(&pathtbl_resize_lock);
  197. synchronize_rcu();
  198. mesh_table_free(oldtbl, false);
  199. }
  200. endadd2:
  201. return err;
  202. }
  203. /**
  204. * mesh_plink_broken - deactivates paths and sends perr when a link breaks
  205. *
  206. * @sta: broken peer link
  207. *
  208. * This function must be called from the rate control algorithm if enough
  209. * delivery errors suggest that a peer link is no longer usable.
  210. */
  211. void mesh_plink_broken(struct sta_info *sta)
  212. {
  213. struct mesh_path *mpath;
  214. struct mpath_node *node;
  215. struct hlist_node *p;
  216. struct net_device *dev = sta->sdata->dev;
  217. int i;
  218. rcu_read_lock();
  219. for_each_mesh_entry(mesh_paths, p, node, i) {
  220. mpath = node->mpath;
  221. spin_lock_bh(&mpath->state_lock);
  222. if (mpath->next_hop == sta &&
  223. mpath->flags & MESH_PATH_ACTIVE &&
  224. !(mpath->flags & MESH_PATH_FIXED)) {
  225. mpath->flags &= ~MESH_PATH_ACTIVE;
  226. ++mpath->dsn;
  227. spin_unlock_bh(&mpath->state_lock);
  228. mesh_path_error_tx(mpath->dst,
  229. cpu_to_le32(mpath->dsn),
  230. dev->broadcast, dev);
  231. } else
  232. spin_unlock_bh(&mpath->state_lock);
  233. }
  234. rcu_read_unlock();
  235. }
  236. EXPORT_SYMBOL(mesh_plink_broken);
  237. /**
  238. * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
  239. *
  240. * @sta - mesh peer to match
  241. *
  242. * RCU notes: this function is called when a mesh plink transitions from
  243. * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
  244. * allows path creation. This will happen before the sta can be freed (because
  245. * sta_info_destroy() calls this) so any reader in a rcu read block will be
  246. * protected against the plink disappearing.
  247. */
  248. void mesh_path_flush_by_nexthop(struct sta_info *sta)
  249. {
  250. struct mesh_path *mpath;
  251. struct mpath_node *node;
  252. struct hlist_node *p;
  253. int i;
  254. for_each_mesh_entry(mesh_paths, p, node, i) {
  255. mpath = node->mpath;
  256. if (mpath->next_hop == sta)
  257. mesh_path_del(mpath->dst, mpath->dev);
  258. }
  259. }
  260. void mesh_path_flush(struct net_device *dev)
  261. {
  262. struct mesh_path *mpath;
  263. struct mpath_node *node;
  264. struct hlist_node *p;
  265. int i;
  266. for_each_mesh_entry(mesh_paths, p, node, i) {
  267. mpath = node->mpath;
  268. if (mpath->dev == dev)
  269. mesh_path_del(mpath->dst, mpath->dev);
  270. }
  271. }
  272. static void mesh_path_node_reclaim(struct rcu_head *rp)
  273. {
  274. struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
  275. struct ieee80211_sub_if_data *sdata =
  276. IEEE80211_DEV_TO_SUB_IF(node->mpath->dev);
  277. del_timer_sync(&node->mpath->timer);
  278. atomic_dec(&sdata->u.sta.mpaths);
  279. kfree(node->mpath);
  280. kfree(node);
  281. }
  282. /**
  283. * mesh_path_del - delete a mesh path from the table
  284. *
  285. * @addr: dst address (ETH_ALEN length)
  286. * @dev: local interface
  287. *
  288. * Returns: 0 if succesful
  289. */
  290. int mesh_path_del(u8 *addr, struct net_device *dev)
  291. {
  292. struct mesh_path *mpath;
  293. struct mpath_node *node;
  294. struct hlist_head *bucket;
  295. struct hlist_node *n;
  296. int hash_idx;
  297. int err = 0;
  298. read_lock(&pathtbl_resize_lock);
  299. hash_idx = mesh_table_hash(addr, dev, mesh_paths);
  300. bucket = &mesh_paths->hash_buckets[hash_idx];
  301. spin_lock(&mesh_paths->hashwlock[hash_idx]);
  302. hlist_for_each_entry(node, n, bucket, list) {
  303. mpath = node->mpath;
  304. if (mpath->dev == dev &&
  305. memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
  306. spin_lock_bh(&mpath->state_lock);
  307. mpath->flags |= MESH_PATH_RESOLVING;
  308. hlist_del_rcu(&node->list);
  309. call_rcu(&node->rcu, mesh_path_node_reclaim);
  310. atomic_dec(&mesh_paths->entries);
  311. spin_unlock_bh(&mpath->state_lock);
  312. goto enddel;
  313. }
  314. }
  315. err = -ENXIO;
  316. enddel:
  317. spin_unlock(&mesh_paths->hashwlock[hash_idx]);
  318. read_unlock(&pathtbl_resize_lock);
  319. return err;
  320. }
  321. /**
  322. * mesh_path_tx_pending - sends pending frames in a mesh path queue
  323. *
  324. * @mpath: mesh path to activate
  325. *
  326. * Locking: the state_lock of the mpath structure must NOT be held when calling
  327. * this function.
  328. */
  329. void mesh_path_tx_pending(struct mesh_path *mpath)
  330. {
  331. struct sk_buff *skb;
  332. while ((skb = skb_dequeue(&mpath->frame_queue)) &&
  333. (mpath->flags & MESH_PATH_ACTIVE))
  334. dev_queue_xmit(skb);
  335. }
  336. /**
  337. * mesh_path_discard_frame - discard a frame whose path could not be resolved
  338. *
  339. * @skb: frame to discard
  340. * @dev: network device the frame was to be sent through
  341. *
  342. * If the frame was beign forwarded from another MP, a PERR frame will be sent
  343. * to the precursor.
  344. *
  345. * Locking: the function must me called within a rcu_read_lock region
  346. */
  347. void mesh_path_discard_frame(struct sk_buff *skb, struct net_device *dev)
  348. {
  349. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  350. struct mesh_path *mpath;
  351. u32 dsn = 0;
  352. if (skb->pkt_type == PACKET_OTHERHOST) {
  353. struct ieee80211s_hdr *prev_meshhdr;
  354. int mshhdrlen;
  355. u8 *ra, *da;
  356. prev_meshhdr = ((struct ieee80211s_hdr *)skb->cb);
  357. mshhdrlen = ieee80211_get_mesh_hdrlen(prev_meshhdr);
  358. da = skb->data;
  359. ra = MESH_PREQ(skb);
  360. mpath = mesh_path_lookup(da, dev);
  361. if (mpath)
  362. dsn = ++mpath->dsn;
  363. mesh_path_error_tx(skb->data, cpu_to_le32(dsn), ra, dev);
  364. }
  365. kfree_skb(skb);
  366. sdata->u.sta.mshstats.dropped_frames_no_route++;
  367. }
  368. /**
  369. * mesh_path_flush_pending - free the pending queue of a mesh path
  370. *
  371. * @mpath: mesh path whose queue has to be freed
  372. *
  373. * Locking: the function must me called withing a rcu_read_lock region
  374. */
  375. void mesh_path_flush_pending(struct mesh_path *mpath)
  376. {
  377. struct ieee80211_sub_if_data *sdata;
  378. struct sk_buff *skb;
  379. sdata = IEEE80211_DEV_TO_SUB_IF(mpath->dev);
  380. while ((skb = skb_dequeue(&mpath->frame_queue)) &&
  381. (mpath->flags & MESH_PATH_ACTIVE))
  382. mesh_path_discard_frame(skb, mpath->dev);
  383. }
  384. /**
  385. * mesh_path_fix_nexthop - force a specific next hop for a mesh path
  386. *
  387. * @mpath: the mesh path to modify
  388. * @next_hop: the next hop to force
  389. *
  390. * Locking: this function must be called holding mpath->state_lock
  391. */
  392. void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
  393. {
  394. spin_lock_bh(&mpath->state_lock);
  395. mesh_path_assign_nexthop(mpath, next_hop);
  396. mpath->dsn = 0xffff;
  397. mpath->metric = 0;
  398. mpath->hop_count = 0;
  399. mpath->exp_time = 0;
  400. mpath->flags |= MESH_PATH_FIXED;
  401. mesh_path_activate(mpath);
  402. spin_unlock_bh(&mpath->state_lock);
  403. mesh_path_tx_pending(mpath);
  404. }
  405. static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
  406. {
  407. struct mesh_path *mpath;
  408. struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
  409. mpath = node->mpath;
  410. hlist_del_rcu(p);
  411. synchronize_rcu();
  412. if (free_leafs)
  413. kfree(mpath);
  414. kfree(node);
  415. }
  416. static void mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
  417. {
  418. struct mesh_path *mpath;
  419. struct mpath_node *node, *new_node;
  420. u32 hash_idx;
  421. node = hlist_entry(p, struct mpath_node, list);
  422. mpath = node->mpath;
  423. new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL);
  424. new_node->mpath = mpath;
  425. hash_idx = mesh_table_hash(mpath->dst, mpath->dev, newtbl);
  426. hlist_add_head(&new_node->list,
  427. &newtbl->hash_buckets[hash_idx]);
  428. }
  429. int mesh_pathtbl_init(void)
  430. {
  431. mesh_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
  432. mesh_paths->free_node = &mesh_path_node_free;
  433. mesh_paths->copy_node = &mesh_path_node_copy;
  434. mesh_paths->mean_chain_len = MEAN_CHAIN_LEN;
  435. if (!mesh_paths)
  436. return -ENOMEM;
  437. return 0;
  438. }
  439. void mesh_path_expire(struct net_device *dev)
  440. {
  441. struct mesh_path *mpath;
  442. struct mpath_node *node;
  443. struct hlist_node *p;
  444. int i;
  445. read_lock(&pathtbl_resize_lock);
  446. for_each_mesh_entry(mesh_paths, p, node, i) {
  447. if (node->mpath->dev != dev)
  448. continue;
  449. mpath = node->mpath;
  450. spin_lock_bh(&mpath->state_lock);
  451. if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
  452. (!(mpath->flags & MESH_PATH_FIXED)) &&
  453. time_after(jiffies,
  454. mpath->exp_time + MESH_PATH_EXPIRE)) {
  455. spin_unlock_bh(&mpath->state_lock);
  456. mesh_path_del(mpath->dst, mpath->dev);
  457. } else
  458. spin_unlock_bh(&mpath->state_lock);
  459. }
  460. read_unlock(&pathtbl_resize_lock);
  461. }
  462. void mesh_pathtbl_unregister(void)
  463. {
  464. mesh_table_free(mesh_paths, true);
  465. }