mesh_pathtbl.c 29 KB

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