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. __sta_info_get(sta);
  51. if (mpath->next_hop)
  52. sta_info_put(mpath->next_hop);
  53. mpath->next_hop = sta;
  54. }
  55. /**
  56. * mesh_path_lookup - look up a path in the mesh path table
  57. * @dst: hardware address (ETH_ALEN length) of destination
  58. * @dev: local interface
  59. *
  60. * Returns: pointer to the mesh path structure, or NULL if not found
  61. *
  62. * Locking: must be called within a read rcu section.
  63. */
  64. struct mesh_path *mesh_path_lookup(u8 *dst, struct net_device *dev)
  65. {
  66. struct mesh_path *mpath;
  67. struct hlist_node *n;
  68. struct hlist_head *bucket;
  69. struct mesh_table *tbl;
  70. struct mpath_node *node;
  71. tbl = rcu_dereference(mesh_paths);
  72. bucket = &tbl->hash_buckets[mesh_table_hash(dst, dev, tbl)];
  73. hlist_for_each_entry_rcu(node, n, bucket, list) {
  74. mpath = node->mpath;
  75. if (mpath->dev == dev &&
  76. memcmp(dst, mpath->dst, ETH_ALEN) == 0) {
  77. if (MPATH_EXPIRED(mpath)) {
  78. spin_lock_bh(&mpath->state_lock);
  79. if (MPATH_EXPIRED(mpath))
  80. mpath->flags &= ~MESH_PATH_ACTIVE;
  81. spin_unlock_bh(&mpath->state_lock);
  82. }
  83. return mpath;
  84. }
  85. }
  86. return NULL;
  87. }
  88. /**
  89. * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
  90. * @idx: index
  91. * @dev: local interface
  92. *
  93. * Returns: pointer to the mesh path structure, or NULL if not found.
  94. *
  95. * Locking: must be called within a read rcu section.
  96. */
  97. struct mesh_path *mesh_path_lookup_by_idx(int idx, struct net_device *dev)
  98. {
  99. struct mpath_node *node;
  100. struct hlist_node *p;
  101. int i;
  102. int j = 0;
  103. for_each_mesh_entry(mesh_paths, p, node, i)
  104. if (j++ == idx) {
  105. if (MPATH_EXPIRED(node->mpath)) {
  106. spin_lock_bh(&node->mpath->state_lock);
  107. if (MPATH_EXPIRED(node->mpath))
  108. node->mpath->flags &= ~MESH_PATH_ACTIVE;
  109. spin_unlock_bh(&node->mpath->state_lock);
  110. }
  111. return node->mpath;
  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->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 ESTAB
  237. * to any other state, since ESTAB state is the only one that allows path
  238. * creation. This will happen before the sta can be freed (since we hold
  239. * a reference to it) so any reader in a rcu read block will be protected
  240. * against the plink dissapearing.
  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. if (node->mpath->next_hop)
  272. sta_info_put(node->mpath->next_hop);
  273. atomic_dec(&sdata->u.sta.mpaths);
  274. kfree(node->mpath);
  275. kfree(node);
  276. }
  277. /**
  278. * mesh_path_del - delete a mesh path from the table
  279. *
  280. * @addr: dst address (ETH_ALEN length)
  281. * @dev: local interface
  282. *
  283. * Returns: 0 if succesful
  284. *
  285. * State: if the path is being resolved, the deletion will be postponed until
  286. * the path resolution completes or times out.
  287. */
  288. int mesh_path_del(u8 *addr, struct net_device *dev)
  289. {
  290. struct mesh_path *mpath;
  291. struct mpath_node *node;
  292. struct hlist_head *bucket;
  293. struct hlist_node *n;
  294. int hash_idx;
  295. int err = 0;
  296. read_lock(&pathtbl_resize_lock);
  297. hash_idx = mesh_table_hash(addr, dev, mesh_paths);
  298. bucket = &mesh_paths->hash_buckets[hash_idx];
  299. spin_lock(&mesh_paths->hashwlock[hash_idx]);
  300. hlist_for_each_entry(node, n, bucket, list) {
  301. mpath = node->mpath;
  302. if (mpath->dev == dev &&
  303. memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
  304. spin_lock_bh(&mpath->state_lock);
  305. if (mpath->flags & MESH_PATH_RESOLVING) {
  306. mpath->flags |= MESH_PATH_DELETE;
  307. } else {
  308. mpath->flags |= MESH_PATH_RESOLVING;
  309. hlist_del_rcu(&node->list);
  310. call_rcu(&node->rcu, mesh_path_node_reclaim);
  311. atomic_dec(&mesh_paths->entries);
  312. }
  313. spin_unlock_bh(&mpath->state_lock);
  314. goto enddel;
  315. }
  316. }
  317. err = -ENXIO;
  318. enddel:
  319. spin_unlock(&mesh_paths->hashwlock[hash_idx]);
  320. read_unlock(&pathtbl_resize_lock);
  321. return err;
  322. }
  323. /**
  324. * mesh_path_tx_pending - sends pending frames in a mesh path queue
  325. *
  326. * @mpath: mesh path to activate
  327. *
  328. * Locking: the state_lock of the mpath structure must NOT be held when calling
  329. * this function.
  330. */
  331. void mesh_path_tx_pending(struct mesh_path *mpath)
  332. {
  333. struct sk_buff *skb;
  334. while ((skb = skb_dequeue(&mpath->frame_queue)) &&
  335. (mpath->flags & MESH_PATH_ACTIVE))
  336. dev_queue_xmit(skb);
  337. }
  338. /**
  339. * mesh_path_discard_frame - discard a frame whose path could not be resolved
  340. *
  341. * @skb: frame to discard
  342. * @dev: network device the frame was to be sent through
  343. *
  344. * If the frame was beign forwarded from another MP, a PERR frame will be sent
  345. * to the precursor.
  346. *
  347. * Locking: the function must me called within a rcu_read_lock region
  348. */
  349. void mesh_path_discard_frame(struct sk_buff *skb, struct net_device *dev)
  350. {
  351. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  352. struct mesh_path *mpath;
  353. u32 dsn = 0;
  354. if (skb->pkt_type == PACKET_OTHERHOST) {
  355. struct ieee80211s_hdr *prev_meshhdr;
  356. int mshhdrlen;
  357. u8 *ra, *da;
  358. prev_meshhdr = ((struct ieee80211s_hdr *)skb->cb);
  359. mshhdrlen = ieee80211_get_mesh_hdrlen(prev_meshhdr);
  360. da = skb->data;
  361. ra = MESH_PREQ(skb);
  362. mpath = mesh_path_lookup(da, dev);
  363. if (mpath)
  364. dsn = ++mpath->dsn;
  365. mesh_path_error_tx(skb->data, cpu_to_le32(dsn), ra, dev);
  366. }
  367. kfree_skb(skb);
  368. sdata->u.sta.mshstats.dropped_frames_no_route++;
  369. }
  370. /**
  371. * mesh_path_flush_pending - free the pending queue of a mesh path
  372. *
  373. * @mpath: mesh path whose queue has to be freed
  374. *
  375. * Locking: the function must me called withing a rcu_read_lock region
  376. */
  377. void mesh_path_flush_pending(struct mesh_path *mpath)
  378. {
  379. struct ieee80211_sub_if_data *sdata;
  380. struct sk_buff *skb;
  381. sdata = IEEE80211_DEV_TO_SUB_IF(mpath->dev);
  382. while ((skb = skb_dequeue(&mpath->frame_queue)) &&
  383. (mpath->flags & MESH_PATH_ACTIVE))
  384. mesh_path_discard_frame(skb, mpath->dev);
  385. }
  386. /**
  387. * mesh_path_fix_nexthop - force a specific next hop for a mesh path
  388. *
  389. * @mpath: the mesh path to modify
  390. * @next_hop: the next hop to force
  391. *
  392. * Locking: this function must be called holding mpath->state_lock
  393. */
  394. void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
  395. {
  396. spin_lock_bh(&mpath->state_lock);
  397. mesh_path_assign_nexthop(mpath, next_hop);
  398. mpath->dsn = 0xffff;
  399. mpath->metric = 0;
  400. mpath->hop_count = 0;
  401. mpath->exp_time = 0;
  402. mpath->flags |= MESH_PATH_FIXED;
  403. mesh_path_activate(mpath);
  404. spin_unlock_bh(&mpath->state_lock);
  405. mesh_path_tx_pending(mpath);
  406. }
  407. static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
  408. {
  409. struct mesh_path *mpath;
  410. struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
  411. mpath = node->mpath;
  412. hlist_del_rcu(p);
  413. synchronize_rcu();
  414. if (free_leafs)
  415. kfree(mpath);
  416. kfree(node);
  417. }
  418. static void mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
  419. {
  420. struct mesh_path *mpath;
  421. struct mpath_node *node, *new_node;
  422. u32 hash_idx;
  423. node = hlist_entry(p, struct mpath_node, list);
  424. mpath = node->mpath;
  425. new_node = kmalloc(sizeof(struct mpath_node), GFP_KERNEL);
  426. new_node->mpath = mpath;
  427. hash_idx = mesh_table_hash(mpath->dst, mpath->dev, newtbl);
  428. hlist_add_head(&new_node->list,
  429. &newtbl->hash_buckets[hash_idx]);
  430. }
  431. int mesh_pathtbl_init(void)
  432. {
  433. mesh_paths = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
  434. mesh_paths->free_node = &mesh_path_node_free;
  435. mesh_paths->copy_node = &mesh_path_node_copy;
  436. mesh_paths->mean_chain_len = MEAN_CHAIN_LEN;
  437. if (!mesh_paths)
  438. return -ENOMEM;
  439. return 0;
  440. }
  441. void mesh_path_expire(struct net_device *dev)
  442. {
  443. struct mesh_path *mpath;
  444. struct mpath_node *node;
  445. struct hlist_node *p;
  446. int i;
  447. read_lock(&pathtbl_resize_lock);
  448. for_each_mesh_entry(mesh_paths, p, node, i) {
  449. if (node->mpath->dev != dev)
  450. continue;
  451. mpath = node->mpath;
  452. spin_lock_bh(&mpath->state_lock);
  453. if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
  454. (!(mpath->flags & MESH_PATH_FIXED)) &&
  455. time_after(jiffies,
  456. mpath->exp_time + MESH_PATH_EXPIRE)) {
  457. spin_unlock_bh(&mpath->state_lock);
  458. mesh_path_del(mpath->dst, mpath->dev);
  459. } else
  460. spin_unlock_bh(&mpath->state_lock);
  461. }
  462. read_unlock(&pathtbl_resize_lock);
  463. }
  464. void mesh_pathtbl_unregister(void)
  465. {
  466. mesh_table_free(mesh_paths, true);
  467. }