mesh_pathtbl.c 29 KB

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