mesh_pathtbl.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. /*
  2. * Copyright (c) 2008, 2009 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/random.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/string.h>
  15. #include <net/mac80211.h>
  16. #include "wme.h"
  17. #include "ieee80211_i.h"
  18. #include "mesh.h"
  19. /* There will be initially 2^INIT_PATHS_SIZE_ORDER buckets */
  20. #define INIT_PATHS_SIZE_ORDER 2
  21. /* Keep the mean chain length below this constant */
  22. #define MEAN_CHAIN_LEN 2
  23. #define MPATH_EXPIRED(mpath) ((mpath->flags & MESH_PATH_ACTIVE) && \
  24. time_after(jiffies, mpath->exp_time) && \
  25. !(mpath->flags & MESH_PATH_FIXED))
  26. struct mpath_node {
  27. struct hlist_node list;
  28. struct rcu_head rcu;
  29. /* This indirection allows two different tables to point to the same
  30. * mesh_path structure, useful when resizing
  31. */
  32. struct mesh_path *mpath;
  33. };
  34. static struct mesh_table __rcu *mesh_paths;
  35. static struct mesh_table __rcu *mpp_paths; /* Store paths for MPP&MAP */
  36. int mesh_paths_generation;
  37. /* This lock will have the grow table function as writer and add / delete nodes
  38. * as readers. RCU provides sufficient protection only when reading the table
  39. * (i.e. doing lookups). Adding or adding or removing nodes requires we take
  40. * the read lock or we risk operating on an old table. The write lock is only
  41. * needed when modifying the number of buckets a table.
  42. */
  43. static DEFINE_RWLOCK(pathtbl_resize_lock);
  44. static inline struct mesh_table *resize_dereference_mesh_paths(void)
  45. {
  46. return rcu_dereference_protected(mesh_paths,
  47. lockdep_is_held(&pathtbl_resize_lock));
  48. }
  49. static inline struct mesh_table *resize_dereference_mpp_paths(void)
  50. {
  51. return rcu_dereference_protected(mpp_paths,
  52. lockdep_is_held(&pathtbl_resize_lock));
  53. }
  54. /*
  55. * CAREFUL -- "tbl" must not be an expression,
  56. * in particular not an rcu_dereference(), since
  57. * it's used twice. So it is illegal to do
  58. * for_each_mesh_entry(rcu_dereference(...), ...)
  59. */
  60. #define for_each_mesh_entry(tbl, p, node, i) \
  61. for (i = 0; i <= tbl->hash_mask; i++) \
  62. hlist_for_each_entry_rcu(node, p, &tbl->hash_buckets[i], list)
  63. static struct mesh_table *mesh_table_alloc(int size_order)
  64. {
  65. int i;
  66. struct mesh_table *newtbl;
  67. newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC);
  68. if (!newtbl)
  69. return NULL;
  70. newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) *
  71. (1 << size_order), GFP_ATOMIC);
  72. if (!newtbl->hash_buckets) {
  73. kfree(newtbl);
  74. return NULL;
  75. }
  76. newtbl->hashwlock = kmalloc(sizeof(spinlock_t) *
  77. (1 << size_order), GFP_ATOMIC);
  78. if (!newtbl->hashwlock) {
  79. kfree(newtbl->hash_buckets);
  80. kfree(newtbl);
  81. return NULL;
  82. }
  83. newtbl->size_order = size_order;
  84. newtbl->hash_mask = (1 << size_order) - 1;
  85. atomic_set(&newtbl->entries, 0);
  86. get_random_bytes(&newtbl->hash_rnd,
  87. sizeof(newtbl->hash_rnd));
  88. for (i = 0; i <= newtbl->hash_mask; i++)
  89. spin_lock_init(&newtbl->hashwlock[i]);
  90. spin_lock_init(&newtbl->gates_lock);
  91. return newtbl;
  92. }
  93. static void __mesh_table_free(struct mesh_table *tbl)
  94. {
  95. kfree(tbl->hash_buckets);
  96. kfree(tbl->hashwlock);
  97. kfree(tbl);
  98. }
  99. static void mesh_table_free(struct mesh_table *tbl, bool free_leafs)
  100. {
  101. struct hlist_head *mesh_hash;
  102. struct hlist_node *p, *q;
  103. struct mpath_node *gate;
  104. int i;
  105. mesh_hash = tbl->hash_buckets;
  106. for (i = 0; i <= tbl->hash_mask; i++) {
  107. spin_lock_bh(&tbl->hashwlock[i]);
  108. hlist_for_each_safe(p, q, &mesh_hash[i]) {
  109. tbl->free_node(p, free_leafs);
  110. atomic_dec(&tbl->entries);
  111. }
  112. spin_unlock_bh(&tbl->hashwlock[i]);
  113. }
  114. if (free_leafs) {
  115. spin_lock_bh(&tbl->gates_lock);
  116. hlist_for_each_entry_safe(gate, p, q,
  117. tbl->known_gates, list) {
  118. hlist_del(&gate->list);
  119. kfree(gate);
  120. }
  121. kfree(tbl->known_gates);
  122. spin_unlock_bh(&tbl->gates_lock);
  123. }
  124. __mesh_table_free(tbl);
  125. }
  126. static int mesh_table_grow(struct mesh_table *oldtbl,
  127. struct mesh_table *newtbl)
  128. {
  129. struct hlist_head *oldhash;
  130. struct hlist_node *p, *q;
  131. int i;
  132. if (atomic_read(&oldtbl->entries)
  133. < oldtbl->mean_chain_len * (oldtbl->hash_mask + 1))
  134. return -EAGAIN;
  135. newtbl->free_node = oldtbl->free_node;
  136. newtbl->mean_chain_len = oldtbl->mean_chain_len;
  137. newtbl->copy_node = oldtbl->copy_node;
  138. newtbl->known_gates = oldtbl->known_gates;
  139. atomic_set(&newtbl->entries, atomic_read(&oldtbl->entries));
  140. oldhash = oldtbl->hash_buckets;
  141. for (i = 0; i <= oldtbl->hash_mask; i++)
  142. hlist_for_each(p, &oldhash[i])
  143. if (oldtbl->copy_node(p, newtbl) < 0)
  144. goto errcopy;
  145. return 0;
  146. errcopy:
  147. for (i = 0; i <= newtbl->hash_mask; i++) {
  148. hlist_for_each_safe(p, q, &newtbl->hash_buckets[i])
  149. oldtbl->free_node(p, 0);
  150. }
  151. return -ENOMEM;
  152. }
  153. static u32 mesh_table_hash(u8 *addr, struct ieee80211_sub_if_data *sdata,
  154. struct mesh_table *tbl)
  155. {
  156. /* Use last four bytes of hw addr and interface index as hash index */
  157. return jhash_2words(*(u32 *)(addr+2), sdata->dev->ifindex, tbl->hash_rnd)
  158. & tbl->hash_mask;
  159. }
  160. /**
  161. *
  162. * mesh_path_assign_nexthop - update mesh path next hop
  163. *
  164. * @mpath: mesh path to update
  165. * @sta: next hop to assign
  166. *
  167. * Locking: mpath->state_lock must be held when calling this function
  168. */
  169. void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
  170. {
  171. struct sk_buff *skb;
  172. struct ieee80211_hdr *hdr;
  173. unsigned long flags;
  174. rcu_assign_pointer(mpath->next_hop, sta);
  175. spin_lock_irqsave(&mpath->frame_queue.lock, flags);
  176. skb_queue_walk(&mpath->frame_queue, skb) {
  177. hdr = (struct ieee80211_hdr *) skb->data;
  178. memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
  179. memcpy(hdr->addr2, mpath->sdata->vif.addr, ETH_ALEN);
  180. ieee80211_mps_set_frame_flags(sta->sdata, sta, hdr);
  181. }
  182. spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
  183. }
  184. static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
  185. struct mesh_path *gate_mpath)
  186. {
  187. struct ieee80211_hdr *hdr;
  188. struct ieee80211s_hdr *mshdr;
  189. int mesh_hdrlen, hdrlen;
  190. char *next_hop;
  191. hdr = (struct ieee80211_hdr *) skb->data;
  192. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  193. mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  194. if (!(mshdr->flags & MESH_FLAGS_AE)) {
  195. /* size of the fixed part of the mesh header */
  196. mesh_hdrlen = 6;
  197. /* make room for the two extended addresses */
  198. skb_push(skb, 2 * ETH_ALEN);
  199. memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
  200. hdr = (struct ieee80211_hdr *) skb->data;
  201. /* we preserve the previous mesh header and only add
  202. * the new addreses */
  203. mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  204. mshdr->flags = MESH_FLAGS_AE_A5_A6;
  205. memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN);
  206. memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN);
  207. }
  208. /* update next hop */
  209. hdr = (struct ieee80211_hdr *) skb->data;
  210. rcu_read_lock();
  211. next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
  212. memcpy(hdr->addr1, next_hop, ETH_ALEN);
  213. rcu_read_unlock();
  214. memcpy(hdr->addr2, gate_mpath->sdata->vif.addr, ETH_ALEN);
  215. memcpy(hdr->addr3, dst_addr, ETH_ALEN);
  216. }
  217. /**
  218. *
  219. * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
  220. *
  221. * This function is used to transfer or copy frames from an unresolved mpath to
  222. * a gate mpath. The function also adds the Address Extension field and
  223. * updates the next hop.
  224. *
  225. * If a frame already has an Address Extension field, only the next hop and
  226. * destination addresses are updated.
  227. *
  228. * The gate mpath must be an active mpath with a valid mpath->next_hop.
  229. *
  230. * @mpath: An active mpath the frames will be sent to (i.e. the gate)
  231. * @from_mpath: The failed mpath
  232. * @copy: When true, copy all the frames to the new mpath queue. When false,
  233. * move them.
  234. */
  235. static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
  236. struct mesh_path *from_mpath,
  237. bool copy)
  238. {
  239. struct sk_buff *skb, *fskb, *tmp;
  240. struct sk_buff_head failq;
  241. unsigned long flags;
  242. BUG_ON(gate_mpath == from_mpath);
  243. BUG_ON(!gate_mpath->next_hop);
  244. __skb_queue_head_init(&failq);
  245. spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
  246. skb_queue_splice_init(&from_mpath->frame_queue, &failq);
  247. spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
  248. skb_queue_walk_safe(&failq, fskb, tmp) {
  249. if (skb_queue_len(&gate_mpath->frame_queue) >=
  250. MESH_FRAME_QUEUE_LEN) {
  251. mpath_dbg(gate_mpath->sdata, "mpath queue full!\n");
  252. break;
  253. }
  254. skb = skb_copy(fskb, GFP_ATOMIC);
  255. if (WARN_ON(!skb))
  256. break;
  257. prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
  258. skb_queue_tail(&gate_mpath->frame_queue, skb);
  259. if (copy)
  260. continue;
  261. __skb_unlink(fskb, &failq);
  262. kfree_skb(fskb);
  263. }
  264. mpath_dbg(gate_mpath->sdata, "Mpath queue for gate %pM has %d frames\n",
  265. gate_mpath->dst, skb_queue_len(&gate_mpath->frame_queue));
  266. if (!copy)
  267. return;
  268. spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
  269. skb_queue_splice(&failq, &from_mpath->frame_queue);
  270. spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
  271. }
  272. static struct mesh_path *mpath_lookup(struct mesh_table *tbl, u8 *dst,
  273. struct ieee80211_sub_if_data *sdata)
  274. {
  275. struct mesh_path *mpath;
  276. struct hlist_node *n;
  277. struct hlist_head *bucket;
  278. struct mpath_node *node;
  279. bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
  280. hlist_for_each_entry_rcu(node, n, bucket, list) {
  281. mpath = node->mpath;
  282. if (mpath->sdata == sdata &&
  283. ether_addr_equal(dst, mpath->dst)) {
  284. if (MPATH_EXPIRED(mpath)) {
  285. spin_lock_bh(&mpath->state_lock);
  286. mpath->flags &= ~MESH_PATH_ACTIVE;
  287. spin_unlock_bh(&mpath->state_lock);
  288. }
  289. return mpath;
  290. }
  291. }
  292. return NULL;
  293. }
  294. /**
  295. * mesh_path_lookup - look up a path in the mesh path table
  296. * @dst: hardware address (ETH_ALEN length) of destination
  297. * @sdata: local subif
  298. *
  299. * Returns: pointer to the mesh path structure, or NULL if not found
  300. *
  301. * Locking: must be called within a read rcu section.
  302. */
  303. struct mesh_path *mesh_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
  304. {
  305. return mpath_lookup(rcu_dereference(mesh_paths), dst, sdata);
  306. }
  307. struct mesh_path *mpp_path_lookup(u8 *dst, struct ieee80211_sub_if_data *sdata)
  308. {
  309. return mpath_lookup(rcu_dereference(mpp_paths), dst, sdata);
  310. }
  311. /**
  312. * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
  313. * @idx: index
  314. * @sdata: local subif, or NULL for all entries
  315. *
  316. * Returns: pointer to the mesh path structure, or NULL if not found.
  317. *
  318. * Locking: must be called within a read rcu section.
  319. */
  320. struct mesh_path *mesh_path_lookup_by_idx(int idx, struct ieee80211_sub_if_data *sdata)
  321. {
  322. struct mesh_table *tbl = rcu_dereference(mesh_paths);
  323. struct mpath_node *node;
  324. struct hlist_node *p;
  325. int i;
  326. int j = 0;
  327. for_each_mesh_entry(tbl, p, node, i) {
  328. if (sdata && node->mpath->sdata != sdata)
  329. continue;
  330. if (j++ == idx) {
  331. if (MPATH_EXPIRED(node->mpath)) {
  332. spin_lock_bh(&node->mpath->state_lock);
  333. node->mpath->flags &= ~MESH_PATH_ACTIVE;
  334. spin_unlock_bh(&node->mpath->state_lock);
  335. }
  336. return node->mpath;
  337. }
  338. }
  339. return NULL;
  340. }
  341. /**
  342. * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
  343. * @mpath: gate path to add to table
  344. */
  345. int mesh_path_add_gate(struct mesh_path *mpath)
  346. {
  347. struct mesh_table *tbl;
  348. struct mpath_node *gate, *new_gate;
  349. struct hlist_node *n;
  350. int err;
  351. rcu_read_lock();
  352. tbl = rcu_dereference(mesh_paths);
  353. hlist_for_each_entry_rcu(gate, n, tbl->known_gates, list)
  354. if (gate->mpath == mpath) {
  355. err = -EEXIST;
  356. goto err_rcu;
  357. }
  358. new_gate = kzalloc(sizeof(struct mpath_node), GFP_ATOMIC);
  359. if (!new_gate) {
  360. err = -ENOMEM;
  361. goto err_rcu;
  362. }
  363. mpath->is_gate = true;
  364. mpath->sdata->u.mesh.num_gates++;
  365. new_gate->mpath = mpath;
  366. spin_lock_bh(&tbl->gates_lock);
  367. hlist_add_head_rcu(&new_gate->list, tbl->known_gates);
  368. spin_unlock_bh(&tbl->gates_lock);
  369. rcu_read_unlock();
  370. mpath_dbg(mpath->sdata,
  371. "Mesh path: Recorded new gate: %pM. %d known gates\n",
  372. mpath->dst, mpath->sdata->u.mesh.num_gates);
  373. return 0;
  374. err_rcu:
  375. rcu_read_unlock();
  376. return err;
  377. }
  378. /**
  379. * mesh_gate_del - remove a mesh gate from the list of known gates
  380. * @tbl: table which holds our list of known gates
  381. * @mpath: gate mpath
  382. *
  383. * Returns: 0 on success
  384. *
  385. * Locking: must be called inside rcu_read_lock() section
  386. */
  387. static int mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
  388. {
  389. struct mpath_node *gate;
  390. struct hlist_node *p, *q;
  391. hlist_for_each_entry_safe(gate, p, q, tbl->known_gates, list)
  392. if (gate->mpath == mpath) {
  393. spin_lock_bh(&tbl->gates_lock);
  394. hlist_del_rcu(&gate->list);
  395. kfree_rcu(gate, rcu);
  396. spin_unlock_bh(&tbl->gates_lock);
  397. mpath->sdata->u.mesh.num_gates--;
  398. mpath->is_gate = false;
  399. mpath_dbg(mpath->sdata,
  400. "Mesh path: Deleted gate: %pM. %d known gates\n",
  401. mpath->dst, mpath->sdata->u.mesh.num_gates);
  402. break;
  403. }
  404. return 0;
  405. }
  406. /**
  407. * mesh_gate_num - number of gates known to this interface
  408. * @sdata: subif data
  409. */
  410. int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
  411. {
  412. return sdata->u.mesh.num_gates;
  413. }
  414. /**
  415. * mesh_path_add - allocate and add a new path to the mesh path table
  416. * @addr: destination address of the path (ETH_ALEN length)
  417. * @sdata: local subif
  418. *
  419. * Returns: 0 on success
  420. *
  421. * State: the initial state of the new path is set to 0
  422. */
  423. int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
  424. {
  425. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  426. struct ieee80211_local *local = sdata->local;
  427. struct mesh_table *tbl;
  428. struct mesh_path *mpath, *new_mpath;
  429. struct mpath_node *node, *new_node;
  430. struct hlist_head *bucket;
  431. struct hlist_node *n;
  432. int grow = 0;
  433. int err = 0;
  434. u32 hash_idx;
  435. if (ether_addr_equal(dst, sdata->vif.addr))
  436. /* never add ourselves as neighbours */
  437. return -ENOTSUPP;
  438. if (is_multicast_ether_addr(dst))
  439. return -ENOTSUPP;
  440. if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
  441. return -ENOSPC;
  442. err = -ENOMEM;
  443. new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
  444. if (!new_mpath)
  445. goto err_path_alloc;
  446. new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
  447. if (!new_node)
  448. goto err_node_alloc;
  449. read_lock_bh(&pathtbl_resize_lock);
  450. memcpy(new_mpath->dst, dst, ETH_ALEN);
  451. eth_broadcast_addr(new_mpath->rann_snd_addr);
  452. new_mpath->is_root = false;
  453. new_mpath->sdata = sdata;
  454. new_mpath->flags = 0;
  455. skb_queue_head_init(&new_mpath->frame_queue);
  456. new_node->mpath = new_mpath;
  457. new_mpath->timer.data = (unsigned long) new_mpath;
  458. new_mpath->timer.function = mesh_path_timer;
  459. new_mpath->exp_time = jiffies;
  460. spin_lock_init(&new_mpath->state_lock);
  461. init_timer(&new_mpath->timer);
  462. tbl = resize_dereference_mesh_paths();
  463. hash_idx = mesh_table_hash(dst, sdata, tbl);
  464. bucket = &tbl->hash_buckets[hash_idx];
  465. spin_lock(&tbl->hashwlock[hash_idx]);
  466. err = -EEXIST;
  467. hlist_for_each_entry(node, n, bucket, list) {
  468. mpath = node->mpath;
  469. if (mpath->sdata == sdata &&
  470. ether_addr_equal(dst, mpath->dst))
  471. goto err_exists;
  472. }
  473. hlist_add_head_rcu(&new_node->list, bucket);
  474. if (atomic_inc_return(&tbl->entries) >=
  475. tbl->mean_chain_len * (tbl->hash_mask + 1))
  476. grow = 1;
  477. mesh_paths_generation++;
  478. spin_unlock(&tbl->hashwlock[hash_idx]);
  479. read_unlock_bh(&pathtbl_resize_lock);
  480. if (grow) {
  481. set_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags);
  482. ieee80211_queue_work(&local->hw, &sdata->work);
  483. }
  484. return 0;
  485. err_exists:
  486. spin_unlock(&tbl->hashwlock[hash_idx]);
  487. read_unlock_bh(&pathtbl_resize_lock);
  488. kfree(new_node);
  489. err_node_alloc:
  490. kfree(new_mpath);
  491. err_path_alloc:
  492. atomic_dec(&sdata->u.mesh.mpaths);
  493. return err;
  494. }
  495. static void mesh_table_free_rcu(struct rcu_head *rcu)
  496. {
  497. struct mesh_table *tbl = container_of(rcu, struct mesh_table, rcu_head);
  498. mesh_table_free(tbl, false);
  499. }
  500. void mesh_mpath_table_grow(void)
  501. {
  502. struct mesh_table *oldtbl, *newtbl;
  503. write_lock_bh(&pathtbl_resize_lock);
  504. oldtbl = resize_dereference_mesh_paths();
  505. newtbl = mesh_table_alloc(oldtbl->size_order + 1);
  506. if (!newtbl)
  507. goto out;
  508. if (mesh_table_grow(oldtbl, newtbl) < 0) {
  509. __mesh_table_free(newtbl);
  510. goto out;
  511. }
  512. rcu_assign_pointer(mesh_paths, newtbl);
  513. call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
  514. out:
  515. write_unlock_bh(&pathtbl_resize_lock);
  516. }
  517. void mesh_mpp_table_grow(void)
  518. {
  519. struct mesh_table *oldtbl, *newtbl;
  520. write_lock_bh(&pathtbl_resize_lock);
  521. oldtbl = resize_dereference_mpp_paths();
  522. newtbl = mesh_table_alloc(oldtbl->size_order + 1);
  523. if (!newtbl)
  524. goto out;
  525. if (mesh_table_grow(oldtbl, newtbl) < 0) {
  526. __mesh_table_free(newtbl);
  527. goto out;
  528. }
  529. rcu_assign_pointer(mpp_paths, newtbl);
  530. call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
  531. out:
  532. write_unlock_bh(&pathtbl_resize_lock);
  533. }
  534. int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata)
  535. {
  536. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  537. struct ieee80211_local *local = sdata->local;
  538. struct mesh_table *tbl;
  539. struct mesh_path *mpath, *new_mpath;
  540. struct mpath_node *node, *new_node;
  541. struct hlist_head *bucket;
  542. struct hlist_node *n;
  543. int grow = 0;
  544. int err = 0;
  545. u32 hash_idx;
  546. if (ether_addr_equal(dst, sdata->vif.addr))
  547. /* never add ourselves as neighbours */
  548. return -ENOTSUPP;
  549. if (is_multicast_ether_addr(dst))
  550. return -ENOTSUPP;
  551. err = -ENOMEM;
  552. new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
  553. if (!new_mpath)
  554. goto err_path_alloc;
  555. new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
  556. if (!new_node)
  557. goto err_node_alloc;
  558. read_lock_bh(&pathtbl_resize_lock);
  559. memcpy(new_mpath->dst, dst, ETH_ALEN);
  560. memcpy(new_mpath->mpp, mpp, ETH_ALEN);
  561. new_mpath->sdata = sdata;
  562. new_mpath->flags = 0;
  563. skb_queue_head_init(&new_mpath->frame_queue);
  564. new_node->mpath = new_mpath;
  565. init_timer(&new_mpath->timer);
  566. new_mpath->exp_time = jiffies;
  567. spin_lock_init(&new_mpath->state_lock);
  568. tbl = resize_dereference_mpp_paths();
  569. hash_idx = mesh_table_hash(dst, sdata, tbl);
  570. bucket = &tbl->hash_buckets[hash_idx];
  571. spin_lock(&tbl->hashwlock[hash_idx]);
  572. err = -EEXIST;
  573. hlist_for_each_entry(node, n, bucket, list) {
  574. mpath = node->mpath;
  575. if (mpath->sdata == sdata &&
  576. ether_addr_equal(dst, mpath->dst))
  577. goto err_exists;
  578. }
  579. hlist_add_head_rcu(&new_node->list, bucket);
  580. if (atomic_inc_return(&tbl->entries) >=
  581. tbl->mean_chain_len * (tbl->hash_mask + 1))
  582. grow = 1;
  583. spin_unlock(&tbl->hashwlock[hash_idx]);
  584. read_unlock_bh(&pathtbl_resize_lock);
  585. if (grow) {
  586. set_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags);
  587. ieee80211_queue_work(&local->hw, &sdata->work);
  588. }
  589. return 0;
  590. err_exists:
  591. spin_unlock(&tbl->hashwlock[hash_idx]);
  592. read_unlock_bh(&pathtbl_resize_lock);
  593. kfree(new_node);
  594. err_node_alloc:
  595. kfree(new_mpath);
  596. err_path_alloc:
  597. return err;
  598. }
  599. /**
  600. * mesh_plink_broken - deactivates paths and sends perr when a link breaks
  601. *
  602. * @sta: broken peer link
  603. *
  604. * This function must be called from the rate control algorithm if enough
  605. * delivery errors suggest that a peer link is no longer usable.
  606. */
  607. void mesh_plink_broken(struct sta_info *sta)
  608. {
  609. struct mesh_table *tbl;
  610. static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  611. struct mesh_path *mpath;
  612. struct mpath_node *node;
  613. struct hlist_node *p;
  614. struct ieee80211_sub_if_data *sdata = sta->sdata;
  615. int i;
  616. __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_DEST_UNREACHABLE);
  617. rcu_read_lock();
  618. tbl = rcu_dereference(mesh_paths);
  619. for_each_mesh_entry(tbl, p, node, i) {
  620. mpath = node->mpath;
  621. if (rcu_dereference(mpath->next_hop) == sta &&
  622. mpath->flags & MESH_PATH_ACTIVE &&
  623. !(mpath->flags & MESH_PATH_FIXED)) {
  624. spin_lock_bh(&mpath->state_lock);
  625. mpath->flags &= ~MESH_PATH_ACTIVE;
  626. ++mpath->sn;
  627. spin_unlock_bh(&mpath->state_lock);
  628. mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl,
  629. mpath->dst, cpu_to_le32(mpath->sn),
  630. reason, bcast, sdata);
  631. }
  632. }
  633. rcu_read_unlock();
  634. }
  635. static void mesh_path_node_reclaim(struct rcu_head *rp)
  636. {
  637. struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
  638. struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
  639. del_timer_sync(&node->mpath->timer);
  640. atomic_dec(&sdata->u.mesh.mpaths);
  641. kfree(node->mpath);
  642. kfree(node);
  643. }
  644. /* needs to be called with the corresponding hashwlock taken */
  645. static void __mesh_path_del(struct mesh_table *tbl, struct mpath_node *node)
  646. {
  647. struct mesh_path *mpath;
  648. mpath = node->mpath;
  649. spin_lock(&mpath->state_lock);
  650. mpath->flags |= MESH_PATH_RESOLVING;
  651. if (mpath->is_gate)
  652. mesh_gate_del(tbl, mpath);
  653. hlist_del_rcu(&node->list);
  654. call_rcu(&node->rcu, mesh_path_node_reclaim);
  655. spin_unlock(&mpath->state_lock);
  656. atomic_dec(&tbl->entries);
  657. }
  658. /**
  659. * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
  660. *
  661. * @sta: mesh peer to match
  662. *
  663. * RCU notes: this function is called when a mesh plink transitions from
  664. * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
  665. * allows path creation. This will happen before the sta can be freed (because
  666. * sta_info_destroy() calls this) so any reader in a rcu read block will be
  667. * protected against the plink disappearing.
  668. */
  669. void mesh_path_flush_by_nexthop(struct sta_info *sta)
  670. {
  671. struct mesh_table *tbl;
  672. struct mesh_path *mpath;
  673. struct mpath_node *node;
  674. struct hlist_node *p;
  675. int i;
  676. rcu_read_lock();
  677. read_lock_bh(&pathtbl_resize_lock);
  678. tbl = resize_dereference_mesh_paths();
  679. for_each_mesh_entry(tbl, p, node, i) {
  680. mpath = node->mpath;
  681. if (rcu_dereference(mpath->next_hop) == sta) {
  682. spin_lock(&tbl->hashwlock[i]);
  683. __mesh_path_del(tbl, node);
  684. spin_unlock(&tbl->hashwlock[i]);
  685. }
  686. }
  687. read_unlock_bh(&pathtbl_resize_lock);
  688. rcu_read_unlock();
  689. }
  690. static void table_flush_by_iface(struct mesh_table *tbl,
  691. struct ieee80211_sub_if_data *sdata)
  692. {
  693. struct mesh_path *mpath;
  694. struct mpath_node *node;
  695. struct hlist_node *p;
  696. int i;
  697. WARN_ON(!rcu_read_lock_held());
  698. for_each_mesh_entry(tbl, p, node, i) {
  699. mpath = node->mpath;
  700. if (mpath->sdata != sdata)
  701. continue;
  702. spin_lock_bh(&tbl->hashwlock[i]);
  703. __mesh_path_del(tbl, node);
  704. spin_unlock_bh(&tbl->hashwlock[i]);
  705. }
  706. }
  707. /**
  708. * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
  709. *
  710. * This function deletes both mesh paths as well as mesh portal paths.
  711. *
  712. * @sdata: interface data to match
  713. *
  714. */
  715. void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
  716. {
  717. struct mesh_table *tbl;
  718. rcu_read_lock();
  719. read_lock_bh(&pathtbl_resize_lock);
  720. tbl = resize_dereference_mesh_paths();
  721. table_flush_by_iface(tbl, sdata);
  722. tbl = resize_dereference_mpp_paths();
  723. table_flush_by_iface(tbl, sdata);
  724. read_unlock_bh(&pathtbl_resize_lock);
  725. rcu_read_unlock();
  726. }
  727. /**
  728. * mesh_path_del - delete a mesh path from the table
  729. *
  730. * @addr: dst address (ETH_ALEN length)
  731. * @sdata: local subif
  732. *
  733. * Returns: 0 if successful
  734. */
  735. int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
  736. {
  737. struct mesh_table *tbl;
  738. struct mesh_path *mpath;
  739. struct mpath_node *node;
  740. struct hlist_head *bucket;
  741. struct hlist_node *n;
  742. int hash_idx;
  743. int err = 0;
  744. read_lock_bh(&pathtbl_resize_lock);
  745. tbl = resize_dereference_mesh_paths();
  746. hash_idx = mesh_table_hash(addr, sdata, tbl);
  747. bucket = &tbl->hash_buckets[hash_idx];
  748. spin_lock(&tbl->hashwlock[hash_idx]);
  749. hlist_for_each_entry(node, n, bucket, list) {
  750. mpath = node->mpath;
  751. if (mpath->sdata == sdata &&
  752. ether_addr_equal(addr, mpath->dst)) {
  753. __mesh_path_del(tbl, node);
  754. goto enddel;
  755. }
  756. }
  757. err = -ENXIO;
  758. enddel:
  759. mesh_paths_generation++;
  760. spin_unlock(&tbl->hashwlock[hash_idx]);
  761. read_unlock_bh(&pathtbl_resize_lock);
  762. return err;
  763. }
  764. /**
  765. * mesh_path_tx_pending - sends pending frames in a mesh path queue
  766. *
  767. * @mpath: mesh path to activate
  768. *
  769. * Locking: the state_lock of the mpath structure must NOT be held when calling
  770. * this function.
  771. */
  772. void mesh_path_tx_pending(struct mesh_path *mpath)
  773. {
  774. if (mpath->flags & MESH_PATH_ACTIVE)
  775. ieee80211_add_pending_skbs(mpath->sdata->local,
  776. &mpath->frame_queue);
  777. }
  778. /**
  779. * mesh_path_send_to_gates - sends pending frames to all known mesh gates
  780. *
  781. * @mpath: mesh path whose queue will be emptied
  782. *
  783. * If there is only one gate, the frames are transferred from the failed mpath
  784. * queue to that gate's queue. If there are more than one gates, the frames
  785. * are copied from each gate to the next. After frames are copied, the
  786. * mpath queues are emptied onto the transmission queue.
  787. */
  788. int mesh_path_send_to_gates(struct mesh_path *mpath)
  789. {
  790. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  791. struct hlist_node *n;
  792. struct mesh_table *tbl;
  793. struct mesh_path *from_mpath = mpath;
  794. struct mpath_node *gate = NULL;
  795. bool copy = false;
  796. struct hlist_head *known_gates;
  797. rcu_read_lock();
  798. tbl = rcu_dereference(mesh_paths);
  799. known_gates = tbl->known_gates;
  800. rcu_read_unlock();
  801. if (!known_gates)
  802. return -EHOSTUNREACH;
  803. hlist_for_each_entry_rcu(gate, n, known_gates, list) {
  804. if (gate->mpath->sdata != sdata)
  805. continue;
  806. if (gate->mpath->flags & MESH_PATH_ACTIVE) {
  807. mpath_dbg(sdata, "Forwarding to %pM\n", gate->mpath->dst);
  808. mesh_path_move_to_queue(gate->mpath, from_mpath, copy);
  809. from_mpath = gate->mpath;
  810. copy = true;
  811. } else {
  812. mpath_dbg(sdata,
  813. "Not forwarding %p (flags %#x)\n",
  814. gate->mpath, gate->mpath->flags);
  815. }
  816. }
  817. hlist_for_each_entry_rcu(gate, n, known_gates, list)
  818. if (gate->mpath->sdata == sdata) {
  819. mpath_dbg(sdata, "Sending to %pM\n", gate->mpath->dst);
  820. mesh_path_tx_pending(gate->mpath);
  821. }
  822. return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
  823. }
  824. /**
  825. * mesh_path_discard_frame - discard a frame whose path could not be resolved
  826. *
  827. * @skb: frame to discard
  828. * @sdata: network subif the frame was to be sent through
  829. *
  830. * Locking: the function must me called within a rcu_read_lock region
  831. */
  832. void mesh_path_discard_frame(struct sk_buff *skb,
  833. struct ieee80211_sub_if_data *sdata)
  834. {
  835. kfree_skb(skb);
  836. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  837. }
  838. /**
  839. * mesh_path_flush_pending - free the pending queue of a mesh path
  840. *
  841. * @mpath: mesh path whose queue has to be freed
  842. *
  843. * Locking: the function must me called within a rcu_read_lock region
  844. */
  845. void mesh_path_flush_pending(struct mesh_path *mpath)
  846. {
  847. struct sk_buff *skb;
  848. while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
  849. mesh_path_discard_frame(skb, mpath->sdata);
  850. }
  851. /**
  852. * mesh_path_fix_nexthop - force a specific next hop for a mesh path
  853. *
  854. * @mpath: the mesh path to modify
  855. * @next_hop: the next hop to force
  856. *
  857. * Locking: this function must be called holding mpath->state_lock
  858. */
  859. void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
  860. {
  861. spin_lock_bh(&mpath->state_lock);
  862. mesh_path_assign_nexthop(mpath, next_hop);
  863. mpath->sn = 0xffff;
  864. mpath->metric = 0;
  865. mpath->hop_count = 0;
  866. mpath->exp_time = 0;
  867. mpath->flags |= MESH_PATH_FIXED;
  868. mesh_path_activate(mpath);
  869. spin_unlock_bh(&mpath->state_lock);
  870. mesh_path_tx_pending(mpath);
  871. }
  872. static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
  873. {
  874. struct mesh_path *mpath;
  875. struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
  876. mpath = node->mpath;
  877. hlist_del_rcu(p);
  878. if (free_leafs) {
  879. del_timer_sync(&mpath->timer);
  880. kfree(mpath);
  881. }
  882. kfree(node);
  883. }
  884. static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
  885. {
  886. struct mesh_path *mpath;
  887. struct mpath_node *node, *new_node;
  888. u32 hash_idx;
  889. new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
  890. if (new_node == NULL)
  891. return -ENOMEM;
  892. node = hlist_entry(p, struct mpath_node, list);
  893. mpath = node->mpath;
  894. new_node->mpath = mpath;
  895. hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
  896. hlist_add_head(&new_node->list,
  897. &newtbl->hash_buckets[hash_idx]);
  898. return 0;
  899. }
  900. int mesh_pathtbl_init(void)
  901. {
  902. struct mesh_table *tbl_path, *tbl_mpp;
  903. int ret;
  904. tbl_path = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
  905. if (!tbl_path)
  906. return -ENOMEM;
  907. tbl_path->free_node = &mesh_path_node_free;
  908. tbl_path->copy_node = &mesh_path_node_copy;
  909. tbl_path->mean_chain_len = MEAN_CHAIN_LEN;
  910. tbl_path->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
  911. if (!tbl_path->known_gates) {
  912. ret = -ENOMEM;
  913. goto free_path;
  914. }
  915. INIT_HLIST_HEAD(tbl_path->known_gates);
  916. tbl_mpp = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
  917. if (!tbl_mpp) {
  918. ret = -ENOMEM;
  919. goto free_path;
  920. }
  921. tbl_mpp->free_node = &mesh_path_node_free;
  922. tbl_mpp->copy_node = &mesh_path_node_copy;
  923. tbl_mpp->mean_chain_len = MEAN_CHAIN_LEN;
  924. tbl_mpp->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
  925. if (!tbl_mpp->known_gates) {
  926. ret = -ENOMEM;
  927. goto free_mpp;
  928. }
  929. INIT_HLIST_HEAD(tbl_mpp->known_gates);
  930. /* Need no locking since this is during init */
  931. RCU_INIT_POINTER(mesh_paths, tbl_path);
  932. RCU_INIT_POINTER(mpp_paths, tbl_mpp);
  933. return 0;
  934. free_mpp:
  935. mesh_table_free(tbl_mpp, true);
  936. free_path:
  937. mesh_table_free(tbl_path, true);
  938. return ret;
  939. }
  940. void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
  941. {
  942. struct mesh_table *tbl;
  943. struct mesh_path *mpath;
  944. struct mpath_node *node;
  945. struct hlist_node *p;
  946. int i;
  947. rcu_read_lock();
  948. tbl = rcu_dereference(mesh_paths);
  949. for_each_mesh_entry(tbl, p, node, i) {
  950. if (node->mpath->sdata != sdata)
  951. continue;
  952. mpath = node->mpath;
  953. if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
  954. (!(mpath->flags & MESH_PATH_FIXED)) &&
  955. time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
  956. mesh_path_del(mpath->dst, mpath->sdata);
  957. }
  958. rcu_read_unlock();
  959. }
  960. void mesh_pathtbl_unregister(void)
  961. {
  962. /* no need for locking during exit path */
  963. mesh_table_free(rcu_dereference_protected(mesh_paths, 1), true);
  964. mesh_table_free(rcu_dereference_protected(mpp_paths, 1), true);
  965. }