mesh_pathtbl.c 30 KB

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