mesh_pathtbl.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. static inline bool mpath_expired(struct mesh_path *mpath)
  24. {
  25. return (mpath->flags & MESH_PATH_ACTIVE) &&
  26. time_after(jiffies, mpath->exp_time) &&
  27. !(mpath->flags & MESH_PATH_FIXED);
  28. }
  29. struct mpath_node {
  30. struct hlist_node list;
  31. struct rcu_head rcu;
  32. /* This indirection allows two different tables to point to the same
  33. * mesh_path structure, useful when resizing
  34. */
  35. struct mesh_path *mpath;
  36. };
  37. static struct mesh_table __rcu *mesh_paths;
  38. static struct mesh_table __rcu *mpp_paths; /* Store paths for MPP&MAP */
  39. int mesh_paths_generation;
  40. /* This lock will have the grow table function as writer and add / delete nodes
  41. * as readers. RCU provides sufficient protection only when reading the table
  42. * (i.e. doing lookups). Adding or adding or removing nodes requires we take
  43. * the read lock or we risk operating on an old table. The write lock is only
  44. * needed when modifying the number of buckets a table.
  45. */
  46. static DEFINE_RWLOCK(pathtbl_resize_lock);
  47. static inline struct mesh_table *resize_dereference_mesh_paths(void)
  48. {
  49. return rcu_dereference_protected(mesh_paths,
  50. lockdep_is_held(&pathtbl_resize_lock));
  51. }
  52. static inline struct mesh_table *resize_dereference_mpp_paths(void)
  53. {
  54. return rcu_dereference_protected(mpp_paths,
  55. lockdep_is_held(&pathtbl_resize_lock));
  56. }
  57. /*
  58. * CAREFUL -- "tbl" must not be an expression,
  59. * in particular not an rcu_dereference(), since
  60. * it's used twice. So it is illegal to do
  61. * for_each_mesh_entry(rcu_dereference(...), ...)
  62. */
  63. #define for_each_mesh_entry(tbl, node, i) \
  64. for (i = 0; i <= tbl->hash_mask; i++) \
  65. hlist_for_each_entry_rcu(node, &tbl->hash_buckets[i], list)
  66. static struct mesh_table *mesh_table_alloc(int size_order)
  67. {
  68. int i;
  69. struct mesh_table *newtbl;
  70. newtbl = kmalloc(sizeof(struct mesh_table), GFP_ATOMIC);
  71. if (!newtbl)
  72. return NULL;
  73. newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) *
  74. (1 << size_order), GFP_ATOMIC);
  75. if (!newtbl->hash_buckets) {
  76. kfree(newtbl);
  77. return NULL;
  78. }
  79. newtbl->hashwlock = kmalloc(sizeof(spinlock_t) *
  80. (1 << size_order), GFP_ATOMIC);
  81. if (!newtbl->hashwlock) {
  82. kfree(newtbl->hash_buckets);
  83. kfree(newtbl);
  84. return NULL;
  85. }
  86. newtbl->size_order = size_order;
  87. newtbl->hash_mask = (1 << size_order) - 1;
  88. atomic_set(&newtbl->entries, 0);
  89. get_random_bytes(&newtbl->hash_rnd,
  90. sizeof(newtbl->hash_rnd));
  91. for (i = 0; i <= newtbl->hash_mask; i++)
  92. spin_lock_init(&newtbl->hashwlock[i]);
  93. spin_lock_init(&newtbl->gates_lock);
  94. return newtbl;
  95. }
  96. static void __mesh_table_free(struct mesh_table *tbl)
  97. {
  98. kfree(tbl->hash_buckets);
  99. kfree(tbl->hashwlock);
  100. kfree(tbl);
  101. }
  102. static void mesh_table_free(struct mesh_table *tbl, bool free_leafs)
  103. {
  104. struct hlist_head *mesh_hash;
  105. struct hlist_node *p, *q;
  106. struct mpath_node *gate;
  107. int i;
  108. mesh_hash = tbl->hash_buckets;
  109. for (i = 0; i <= tbl->hash_mask; i++) {
  110. spin_lock_bh(&tbl->hashwlock[i]);
  111. hlist_for_each_safe(p, q, &mesh_hash[i]) {
  112. tbl->free_node(p, free_leafs);
  113. atomic_dec(&tbl->entries);
  114. }
  115. spin_unlock_bh(&tbl->hashwlock[i]);
  116. }
  117. if (free_leafs) {
  118. spin_lock_bh(&tbl->gates_lock);
  119. hlist_for_each_entry_safe(gate, q,
  120. tbl->known_gates, list) {
  121. hlist_del(&gate->list);
  122. kfree(gate);
  123. }
  124. kfree(tbl->known_gates);
  125. spin_unlock_bh(&tbl->gates_lock);
  126. }
  127. __mesh_table_free(tbl);
  128. }
  129. static int mesh_table_grow(struct mesh_table *oldtbl,
  130. struct mesh_table *newtbl)
  131. {
  132. struct hlist_head *oldhash;
  133. struct hlist_node *p, *q;
  134. int i;
  135. if (atomic_read(&oldtbl->entries)
  136. < oldtbl->mean_chain_len * (oldtbl->hash_mask + 1))
  137. return -EAGAIN;
  138. newtbl->free_node = oldtbl->free_node;
  139. newtbl->mean_chain_len = oldtbl->mean_chain_len;
  140. newtbl->copy_node = oldtbl->copy_node;
  141. newtbl->known_gates = oldtbl->known_gates;
  142. atomic_set(&newtbl->entries, atomic_read(&oldtbl->entries));
  143. oldhash = oldtbl->hash_buckets;
  144. for (i = 0; i <= oldtbl->hash_mask; i++)
  145. hlist_for_each(p, &oldhash[i])
  146. if (oldtbl->copy_node(p, newtbl) < 0)
  147. goto errcopy;
  148. return 0;
  149. errcopy:
  150. for (i = 0; i <= newtbl->hash_mask; i++) {
  151. hlist_for_each_safe(p, q, &newtbl->hash_buckets[i])
  152. oldtbl->free_node(p, 0);
  153. }
  154. return -ENOMEM;
  155. }
  156. static u32 mesh_table_hash(const u8 *addr, struct ieee80211_sub_if_data *sdata,
  157. struct mesh_table *tbl)
  158. {
  159. /* Use last four bytes of hw addr and interface index as hash index */
  160. return jhash_2words(*(u32 *)(addr+2), sdata->dev->ifindex,
  161. tbl->hash_rnd) & tbl->hash_mask;
  162. }
  163. /**
  164. *
  165. * mesh_path_assign_nexthop - update mesh path next hop
  166. *
  167. * @mpath: mesh path to update
  168. * @sta: next hop to assign
  169. *
  170. * Locking: mpath->state_lock must be held when calling this function
  171. */
  172. void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
  173. {
  174. struct sk_buff *skb;
  175. struct ieee80211_hdr *hdr;
  176. unsigned long flags;
  177. rcu_assign_pointer(mpath->next_hop, sta);
  178. spin_lock_irqsave(&mpath->frame_queue.lock, flags);
  179. skb_queue_walk(&mpath->frame_queue, skb) {
  180. hdr = (struct ieee80211_hdr *) skb->data;
  181. memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
  182. memcpy(hdr->addr2, mpath->sdata->vif.addr, ETH_ALEN);
  183. ieee80211_mps_set_frame_flags(sta->sdata, sta, hdr);
  184. }
  185. spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
  186. }
  187. static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
  188. struct mesh_path *gate_mpath)
  189. {
  190. struct ieee80211_hdr *hdr;
  191. struct ieee80211s_hdr *mshdr;
  192. int mesh_hdrlen, hdrlen;
  193. char *next_hop;
  194. hdr = (struct ieee80211_hdr *) skb->data;
  195. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  196. mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  197. if (!(mshdr->flags & MESH_FLAGS_AE)) {
  198. /* size of the fixed part of the mesh header */
  199. mesh_hdrlen = 6;
  200. /* make room for the two extended addresses */
  201. skb_push(skb, 2 * ETH_ALEN);
  202. memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
  203. hdr = (struct ieee80211_hdr *) skb->data;
  204. /* we preserve the previous mesh header and only add
  205. * the new addreses */
  206. mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  207. mshdr->flags = MESH_FLAGS_AE_A5_A6;
  208. memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN);
  209. memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN);
  210. }
  211. /* update next hop */
  212. hdr = (struct ieee80211_hdr *) skb->data;
  213. rcu_read_lock();
  214. next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
  215. memcpy(hdr->addr1, next_hop, ETH_ALEN);
  216. rcu_read_unlock();
  217. memcpy(hdr->addr2, gate_mpath->sdata->vif.addr, ETH_ALEN);
  218. memcpy(hdr->addr3, dst_addr, ETH_ALEN);
  219. }
  220. /**
  221. *
  222. * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
  223. *
  224. * This function is used to transfer or copy frames from an unresolved mpath to
  225. * a gate mpath. The function also adds the Address Extension field and
  226. * updates the next hop.
  227. *
  228. * If a frame already has an Address Extension field, only the next hop and
  229. * destination addresses are updated.
  230. *
  231. * The gate mpath must be an active mpath with a valid mpath->next_hop.
  232. *
  233. * @mpath: An active mpath the frames will be sent to (i.e. the gate)
  234. * @from_mpath: The failed mpath
  235. * @copy: When true, copy all the frames to the new mpath queue. When false,
  236. * move them.
  237. */
  238. static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
  239. struct mesh_path *from_mpath,
  240. bool copy)
  241. {
  242. struct sk_buff *skb, *fskb, *tmp;
  243. struct sk_buff_head failq;
  244. unsigned long flags;
  245. BUG_ON(gate_mpath == from_mpath);
  246. BUG_ON(!gate_mpath->next_hop);
  247. __skb_queue_head_init(&failq);
  248. spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
  249. skb_queue_splice_init(&from_mpath->frame_queue, &failq);
  250. spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
  251. skb_queue_walk_safe(&failq, fskb, tmp) {
  252. if (skb_queue_len(&gate_mpath->frame_queue) >=
  253. MESH_FRAME_QUEUE_LEN) {
  254. mpath_dbg(gate_mpath->sdata, "mpath queue full!\n");
  255. break;
  256. }
  257. skb = skb_copy(fskb, GFP_ATOMIC);
  258. if (WARN_ON(!skb))
  259. break;
  260. prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
  261. skb_queue_tail(&gate_mpath->frame_queue, skb);
  262. if (copy)
  263. continue;
  264. __skb_unlink(fskb, &failq);
  265. kfree_skb(fskb);
  266. }
  267. mpath_dbg(gate_mpath->sdata, "Mpath queue for gate %pM has %d frames\n",
  268. gate_mpath->dst, skb_queue_len(&gate_mpath->frame_queue));
  269. if (!copy)
  270. return;
  271. spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
  272. skb_queue_splice(&failq, &from_mpath->frame_queue);
  273. spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
  274. }
  275. static struct mesh_path *mpath_lookup(struct mesh_table *tbl, const u8 *dst,
  276. struct ieee80211_sub_if_data *sdata)
  277. {
  278. struct mesh_path *mpath;
  279. struct hlist_head *bucket;
  280. struct mpath_node *node;
  281. bucket = &tbl->hash_buckets[mesh_table_hash(dst, sdata, tbl)];
  282. hlist_for_each_entry_rcu(node, bucket, list) {
  283. mpath = node->mpath;
  284. if (mpath->sdata == sdata &&
  285. ether_addr_equal(dst, mpath->dst)) {
  286. if (mpath_expired(mpath)) {
  287. spin_lock_bh(&mpath->state_lock);
  288. mpath->flags &= ~MESH_PATH_ACTIVE;
  289. spin_unlock_bh(&mpath->state_lock);
  290. }
  291. return mpath;
  292. }
  293. }
  294. return NULL;
  295. }
  296. /**
  297. * mesh_path_lookup - look up a path in the mesh path table
  298. * @sdata: local subif
  299. * @dst: hardware address (ETH_ALEN length) of destination
  300. *
  301. * Returns: pointer to the mesh path structure, or NULL if not found
  302. *
  303. * Locking: must be called within a read rcu section.
  304. */
  305. struct mesh_path *
  306. mesh_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
  307. {
  308. return mpath_lookup(rcu_dereference(mesh_paths), dst, sdata);
  309. }
  310. struct mesh_path *
  311. mpp_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
  312. {
  313. return mpath_lookup(rcu_dereference(mpp_paths), dst, sdata);
  314. }
  315. /**
  316. * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
  317. * @idx: index
  318. * @sdata: local subif, or NULL for all entries
  319. *
  320. * Returns: pointer to the mesh path structure, or NULL if not found.
  321. *
  322. * Locking: must be called within a read rcu section.
  323. */
  324. struct mesh_path *
  325. mesh_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
  326. {
  327. struct mesh_table *tbl = rcu_dereference(mesh_paths);
  328. struct mpath_node *node;
  329. int i;
  330. int j = 0;
  331. for_each_mesh_entry(tbl, node, i) {
  332. if (sdata && node->mpath->sdata != sdata)
  333. continue;
  334. if (j++ == idx) {
  335. if (mpath_expired(node->mpath)) {
  336. spin_lock_bh(&node->mpath->state_lock);
  337. node->mpath->flags &= ~MESH_PATH_ACTIVE;
  338. spin_unlock_bh(&node->mpath->state_lock);
  339. }
  340. return node->mpath;
  341. }
  342. }
  343. return NULL;
  344. }
  345. /**
  346. * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
  347. * @mpath: gate path to add to table
  348. */
  349. int mesh_path_add_gate(struct mesh_path *mpath)
  350. {
  351. struct mesh_table *tbl;
  352. struct mpath_node *gate, *new_gate;
  353. int err;
  354. rcu_read_lock();
  355. tbl = rcu_dereference(mesh_paths);
  356. hlist_for_each_entry_rcu(gate, tbl->known_gates, list)
  357. if (gate->mpath == mpath) {
  358. err = -EEXIST;
  359. goto err_rcu;
  360. }
  361. new_gate = kzalloc(sizeof(struct mpath_node), GFP_ATOMIC);
  362. if (!new_gate) {
  363. err = -ENOMEM;
  364. goto err_rcu;
  365. }
  366. mpath->is_gate = true;
  367. mpath->sdata->u.mesh.num_gates++;
  368. new_gate->mpath = mpath;
  369. spin_lock_bh(&tbl->gates_lock);
  370. hlist_add_head_rcu(&new_gate->list, tbl->known_gates);
  371. spin_unlock_bh(&tbl->gates_lock);
  372. mpath_dbg(mpath->sdata,
  373. "Mesh path: Recorded new gate: %pM. %d known gates\n",
  374. mpath->dst, mpath->sdata->u.mesh.num_gates);
  375. err = 0;
  376. err_rcu:
  377. rcu_read_unlock();
  378. return err;
  379. }
  380. /**
  381. * mesh_gate_del - remove a mesh gate from the list of known gates
  382. * @tbl: table which holds our list of known gates
  383. * @mpath: gate mpath
  384. *
  385. * Locking: must be called inside rcu_read_lock() section
  386. */
  387. static void mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
  388. {
  389. struct mpath_node *gate;
  390. struct hlist_node *q;
  391. hlist_for_each_entry_safe(gate, q, tbl->known_gates, list) {
  392. if (gate->mpath != mpath)
  393. continue;
  394. spin_lock_bh(&tbl->gates_lock);
  395. hlist_del_rcu(&gate->list);
  396. kfree_rcu(gate, rcu);
  397. spin_unlock_bh(&tbl->gates_lock);
  398. mpath->sdata->u.mesh.num_gates--;
  399. mpath->is_gate = false;
  400. mpath_dbg(mpath->sdata,
  401. "Mesh path: Deleted gate: %pM. %d known gates\n",
  402. mpath->dst, mpath->sdata->u.mesh.num_gates);
  403. break;
  404. }
  405. }
  406. /**
  407. * mesh_gate_num - number of gates known to this interface
  408. * @sdata: subif data
  409. */
  410. int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
  411. {
  412. return sdata->u.mesh.num_gates;
  413. }
  414. /**
  415. * mesh_path_add - allocate and add a new path to the mesh path table
  416. * @dst: destination address of the path (ETH_ALEN length)
  417. * @sdata: local subif
  418. *
  419. * Returns: 0 on success
  420. *
  421. * State: the initial state of the new path is set to 0
  422. */
  423. int mesh_path_add(struct ieee80211_sub_if_data *sdata, const u8 *dst)
  424. {
  425. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  426. struct ieee80211_local *local = sdata->local;
  427. struct mesh_table *tbl;
  428. struct mesh_path *mpath, *new_mpath;
  429. struct mpath_node *node, *new_node;
  430. struct hlist_head *bucket;
  431. 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, 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(struct ieee80211_sub_if_data *sdata,
  534. const u8 *dst, const u8 *mpp)
  535. {
  536. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  537. struct ieee80211_local *local = sdata->local;
  538. struct mesh_table *tbl;
  539. struct mesh_path *mpath, *new_mpath;
  540. struct mpath_node *node, *new_node;
  541. struct hlist_head *bucket;
  542. 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, 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 ieee80211_sub_if_data *sdata = sta->sdata;
  613. int i;
  614. __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_DEST_UNREACHABLE);
  615. rcu_read_lock();
  616. tbl = rcu_dereference(mesh_paths);
  617. for_each_mesh_entry(tbl, node, i) {
  618. mpath = node->mpath;
  619. if (rcu_dereference(mpath->next_hop) == sta &&
  620. mpath->flags & MESH_PATH_ACTIVE &&
  621. !(mpath->flags & MESH_PATH_FIXED)) {
  622. spin_lock_bh(&mpath->state_lock);
  623. mpath->flags &= ~MESH_PATH_ACTIVE;
  624. ++mpath->sn;
  625. spin_unlock_bh(&mpath->state_lock);
  626. mesh_path_error_tx(sdata,
  627. sdata->u.mesh.mshcfg.element_ttl,
  628. mpath->dst, cpu_to_le32(mpath->sn),
  629. reason, bcast);
  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. int i;
  674. rcu_read_lock();
  675. read_lock_bh(&pathtbl_resize_lock);
  676. tbl = resize_dereference_mesh_paths();
  677. for_each_mesh_entry(tbl, node, i) {
  678. mpath = node->mpath;
  679. if (rcu_dereference(mpath->next_hop) == sta) {
  680. spin_lock(&tbl->hashwlock[i]);
  681. __mesh_path_del(tbl, node);
  682. spin_unlock(&tbl->hashwlock[i]);
  683. }
  684. }
  685. read_unlock_bh(&pathtbl_resize_lock);
  686. rcu_read_unlock();
  687. }
  688. static void table_flush_by_iface(struct mesh_table *tbl,
  689. struct ieee80211_sub_if_data *sdata)
  690. {
  691. struct mesh_path *mpath;
  692. struct mpath_node *node;
  693. int i;
  694. WARN_ON(!rcu_read_lock_held());
  695. for_each_mesh_entry(tbl, node, i) {
  696. mpath = node->mpath;
  697. if (mpath->sdata != sdata)
  698. continue;
  699. spin_lock_bh(&tbl->hashwlock[i]);
  700. __mesh_path_del(tbl, node);
  701. spin_unlock_bh(&tbl->hashwlock[i]);
  702. }
  703. }
  704. /**
  705. * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
  706. *
  707. * This function deletes both mesh paths as well as mesh portal paths.
  708. *
  709. * @sdata: interface data to match
  710. *
  711. */
  712. void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
  713. {
  714. struct mesh_table *tbl;
  715. rcu_read_lock();
  716. read_lock_bh(&pathtbl_resize_lock);
  717. tbl = resize_dereference_mesh_paths();
  718. table_flush_by_iface(tbl, sdata);
  719. tbl = resize_dereference_mpp_paths();
  720. table_flush_by_iface(tbl, sdata);
  721. read_unlock_bh(&pathtbl_resize_lock);
  722. rcu_read_unlock();
  723. }
  724. /**
  725. * mesh_path_del - delete a mesh path from the table
  726. *
  727. * @addr: dst address (ETH_ALEN length)
  728. * @sdata: local subif
  729. *
  730. * Returns: 0 if successful
  731. */
  732. int mesh_path_del(struct ieee80211_sub_if_data *sdata, const u8 *addr)
  733. {
  734. struct mesh_table *tbl;
  735. struct mesh_path *mpath;
  736. struct mpath_node *node;
  737. struct hlist_head *bucket;
  738. int hash_idx;
  739. int err = 0;
  740. read_lock_bh(&pathtbl_resize_lock);
  741. tbl = resize_dereference_mesh_paths();
  742. hash_idx = mesh_table_hash(addr, sdata, tbl);
  743. bucket = &tbl->hash_buckets[hash_idx];
  744. spin_lock(&tbl->hashwlock[hash_idx]);
  745. hlist_for_each_entry(node, bucket, list) {
  746. mpath = node->mpath;
  747. if (mpath->sdata == sdata &&
  748. ether_addr_equal(addr, mpath->dst)) {
  749. __mesh_path_del(tbl, node);
  750. goto enddel;
  751. }
  752. }
  753. err = -ENXIO;
  754. enddel:
  755. mesh_paths_generation++;
  756. spin_unlock(&tbl->hashwlock[hash_idx]);
  757. read_unlock_bh(&pathtbl_resize_lock);
  758. return err;
  759. }
  760. /**
  761. * mesh_path_tx_pending - sends pending frames in a mesh path queue
  762. *
  763. * @mpath: mesh path to activate
  764. *
  765. * Locking: the state_lock of the mpath structure must NOT be held when calling
  766. * this function.
  767. */
  768. void mesh_path_tx_pending(struct mesh_path *mpath)
  769. {
  770. if (mpath->flags & MESH_PATH_ACTIVE)
  771. ieee80211_add_pending_skbs(mpath->sdata->local,
  772. &mpath->frame_queue);
  773. }
  774. /**
  775. * mesh_path_send_to_gates - sends pending frames to all known mesh gates
  776. *
  777. * @mpath: mesh path whose queue will be emptied
  778. *
  779. * If there is only one gate, the frames are transferred from the failed mpath
  780. * queue to that gate's queue. If there are more than one gates, the frames
  781. * are copied from each gate to the next. After frames are copied, the
  782. * mpath queues are emptied onto the transmission queue.
  783. */
  784. int mesh_path_send_to_gates(struct mesh_path *mpath)
  785. {
  786. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  787. struct mesh_table *tbl;
  788. struct mesh_path *from_mpath = mpath;
  789. struct mpath_node *gate = NULL;
  790. bool copy = false;
  791. struct hlist_head *known_gates;
  792. rcu_read_lock();
  793. tbl = rcu_dereference(mesh_paths);
  794. known_gates = tbl->known_gates;
  795. rcu_read_unlock();
  796. if (!known_gates)
  797. return -EHOSTUNREACH;
  798. hlist_for_each_entry_rcu(gate, known_gates, list) {
  799. if (gate->mpath->sdata != sdata)
  800. continue;
  801. if (gate->mpath->flags & MESH_PATH_ACTIVE) {
  802. mpath_dbg(sdata, "Forwarding to %pM\n", gate->mpath->dst);
  803. mesh_path_move_to_queue(gate->mpath, from_mpath, copy);
  804. from_mpath = gate->mpath;
  805. copy = true;
  806. } else {
  807. mpath_dbg(sdata,
  808. "Not forwarding %p (flags %#x)\n",
  809. gate->mpath, gate->mpath->flags);
  810. }
  811. }
  812. hlist_for_each_entry_rcu(gate, known_gates, list)
  813. if (gate->mpath->sdata == sdata) {
  814. mpath_dbg(sdata, "Sending to %pM\n", gate->mpath->dst);
  815. mesh_path_tx_pending(gate->mpath);
  816. }
  817. return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
  818. }
  819. /**
  820. * mesh_path_discard_frame - discard a frame whose path could not be resolved
  821. *
  822. * @skb: frame to discard
  823. * @sdata: network subif the frame was to be sent through
  824. *
  825. * Locking: the function must me called within a rcu_read_lock region
  826. */
  827. void mesh_path_discard_frame(struct ieee80211_sub_if_data *sdata,
  828. struct sk_buff *skb)
  829. {
  830. kfree_skb(skb);
  831. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  832. }
  833. /**
  834. * mesh_path_flush_pending - free the pending queue of a mesh path
  835. *
  836. * @mpath: mesh path whose queue has to be freed
  837. *
  838. * Locking: the function must me called within a rcu_read_lock region
  839. */
  840. void mesh_path_flush_pending(struct mesh_path *mpath)
  841. {
  842. struct sk_buff *skb;
  843. while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
  844. mesh_path_discard_frame(mpath->sdata, skb);
  845. }
  846. /**
  847. * mesh_path_fix_nexthop - force a specific next hop for a mesh path
  848. *
  849. * @mpath: the mesh path to modify
  850. * @next_hop: the next hop to force
  851. *
  852. * Locking: this function must be called holding mpath->state_lock
  853. */
  854. void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
  855. {
  856. spin_lock_bh(&mpath->state_lock);
  857. mesh_path_assign_nexthop(mpath, next_hop);
  858. mpath->sn = 0xffff;
  859. mpath->metric = 0;
  860. mpath->hop_count = 0;
  861. mpath->exp_time = 0;
  862. mpath->flags |= MESH_PATH_FIXED;
  863. mesh_path_activate(mpath);
  864. spin_unlock_bh(&mpath->state_lock);
  865. mesh_path_tx_pending(mpath);
  866. }
  867. static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
  868. {
  869. struct mesh_path *mpath;
  870. struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
  871. mpath = node->mpath;
  872. hlist_del_rcu(p);
  873. if (free_leafs) {
  874. del_timer_sync(&mpath->timer);
  875. kfree(mpath);
  876. }
  877. kfree(node);
  878. }
  879. static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
  880. {
  881. struct mesh_path *mpath;
  882. struct mpath_node *node, *new_node;
  883. u32 hash_idx;
  884. new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
  885. if (new_node == NULL)
  886. return -ENOMEM;
  887. node = hlist_entry(p, struct mpath_node, list);
  888. mpath = node->mpath;
  889. new_node->mpath = mpath;
  890. hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
  891. hlist_add_head(&new_node->list,
  892. &newtbl->hash_buckets[hash_idx]);
  893. return 0;
  894. }
  895. int mesh_pathtbl_init(void)
  896. {
  897. struct mesh_table *tbl_path, *tbl_mpp;
  898. int ret;
  899. tbl_path = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
  900. if (!tbl_path)
  901. return -ENOMEM;
  902. tbl_path->free_node = &mesh_path_node_free;
  903. tbl_path->copy_node = &mesh_path_node_copy;
  904. tbl_path->mean_chain_len = MEAN_CHAIN_LEN;
  905. tbl_path->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
  906. if (!tbl_path->known_gates) {
  907. ret = -ENOMEM;
  908. goto free_path;
  909. }
  910. INIT_HLIST_HEAD(tbl_path->known_gates);
  911. tbl_mpp = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
  912. if (!tbl_mpp) {
  913. ret = -ENOMEM;
  914. goto free_path;
  915. }
  916. tbl_mpp->free_node = &mesh_path_node_free;
  917. tbl_mpp->copy_node = &mesh_path_node_copy;
  918. tbl_mpp->mean_chain_len = MEAN_CHAIN_LEN;
  919. tbl_mpp->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
  920. if (!tbl_mpp->known_gates) {
  921. ret = -ENOMEM;
  922. goto free_mpp;
  923. }
  924. INIT_HLIST_HEAD(tbl_mpp->known_gates);
  925. /* Need no locking since this is during init */
  926. RCU_INIT_POINTER(mesh_paths, tbl_path);
  927. RCU_INIT_POINTER(mpp_paths, tbl_mpp);
  928. return 0;
  929. free_mpp:
  930. mesh_table_free(tbl_mpp, true);
  931. free_path:
  932. mesh_table_free(tbl_path, true);
  933. return ret;
  934. }
  935. void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
  936. {
  937. struct mesh_table *tbl;
  938. struct mesh_path *mpath;
  939. struct mpath_node *node;
  940. int i;
  941. rcu_read_lock();
  942. tbl = rcu_dereference(mesh_paths);
  943. for_each_mesh_entry(tbl, node, i) {
  944. if (node->mpath->sdata != sdata)
  945. continue;
  946. mpath = node->mpath;
  947. if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
  948. (!(mpath->flags & MESH_PATH_FIXED)) &&
  949. time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
  950. mesh_path_del(mpath->sdata, mpath->dst);
  951. }
  952. rcu_read_unlock();
  953. }
  954. void mesh_pathtbl_unregister(void)
  955. {
  956. /* no need for locking during exit path */
  957. mesh_table_free(rcu_dereference_protected(mesh_paths, 1), true);
  958. mesh_table_free(rcu_dereference_protected(mpp_paths, 1), true);
  959. }