mesh_pathtbl.c 29 KB

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