mesh_pathtbl.c 30 KB

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