mesh_pathtbl.c 13 KB

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