mesh_pathtbl.c 30 KB

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