mesh_pathtbl.c 31 KB

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