mesh_pathtbl.c 13 KB

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