mesh_pathtbl.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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. static void mesh_gate_node_reclaim(struct rcu_head *rp)
  351. {
  352. struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
  353. kfree(node);
  354. }
  355. /**
  356. * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
  357. * @mpath: gate path to add to table
  358. */
  359. int mesh_path_add_gate(struct mesh_path *mpath)
  360. {
  361. struct mesh_table *tbl;
  362. struct mpath_node *gate, *new_gate;
  363. struct hlist_node *n;
  364. int err;
  365. rcu_read_lock();
  366. tbl = rcu_dereference(mesh_paths);
  367. hlist_for_each_entry_rcu(gate, n, tbl->known_gates, list)
  368. if (gate->mpath == mpath) {
  369. err = -EEXIST;
  370. goto err_rcu;
  371. }
  372. new_gate = kzalloc(sizeof(struct mpath_node), GFP_ATOMIC);
  373. if (!new_gate) {
  374. err = -ENOMEM;
  375. goto err_rcu;
  376. }
  377. mpath->is_gate = true;
  378. mpath->sdata->u.mesh.num_gates++;
  379. new_gate->mpath = mpath;
  380. spin_lock_bh(&tbl->gates_lock);
  381. hlist_add_head_rcu(&new_gate->list, tbl->known_gates);
  382. spin_unlock_bh(&tbl->gates_lock);
  383. rcu_read_unlock();
  384. mpath_dbg("Mesh path (%s): Recorded new gate: %pM. %d known gates\n",
  385. mpath->sdata->name, mpath->dst,
  386. mpath->sdata->u.mesh.num_gates);
  387. return 0;
  388. err_rcu:
  389. rcu_read_unlock();
  390. return err;
  391. }
  392. /**
  393. * mesh_gate_del - remove a mesh gate from the list of known gates
  394. * @tbl: table which holds our list of known gates
  395. * @mpath: gate mpath
  396. *
  397. * Returns: 0 on success
  398. *
  399. * Locking: must be called inside rcu_read_lock() section
  400. */
  401. static int mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
  402. {
  403. struct mpath_node *gate;
  404. struct hlist_node *p, *q;
  405. hlist_for_each_entry_safe(gate, p, q, tbl->known_gates, list)
  406. if (gate->mpath == mpath) {
  407. spin_lock_bh(&tbl->gates_lock);
  408. hlist_del_rcu(&gate->list);
  409. call_rcu(&gate->rcu, mesh_gate_node_reclaim);
  410. spin_unlock_bh(&tbl->gates_lock);
  411. mpath->sdata->u.mesh.num_gates--;
  412. mpath->is_gate = false;
  413. mpath_dbg("Mesh path (%s): Deleted gate: %pM. "
  414. "%d known gates\n", mpath->sdata->name,
  415. mpath->dst, mpath->sdata->u.mesh.num_gates);
  416. break;
  417. }
  418. return 0;
  419. }
  420. /**
  421. * mesh_gate_num - number of gates known to this interface
  422. * @sdata: subif data
  423. */
  424. int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
  425. {
  426. return sdata->u.mesh.num_gates;
  427. }
  428. /**
  429. * mesh_path_add - allocate and add a new path to the mesh path table
  430. * @addr: destination address of the path (ETH_ALEN length)
  431. * @sdata: local subif
  432. *
  433. * Returns: 0 on success
  434. *
  435. * State: the initial state of the new path is set to 0
  436. */
  437. int mesh_path_add(u8 *dst, struct ieee80211_sub_if_data *sdata)
  438. {
  439. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  440. struct ieee80211_local *local = sdata->local;
  441. struct mesh_table *tbl;
  442. struct mesh_path *mpath, *new_mpath;
  443. struct mpath_node *node, *new_node;
  444. struct hlist_head *bucket;
  445. struct hlist_node *n;
  446. int grow = 0;
  447. int err = 0;
  448. u32 hash_idx;
  449. if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
  450. /* never add ourselves as neighbours */
  451. return -ENOTSUPP;
  452. if (is_multicast_ether_addr(dst))
  453. return -ENOTSUPP;
  454. if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
  455. return -ENOSPC;
  456. err = -ENOMEM;
  457. new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
  458. if (!new_mpath)
  459. goto err_path_alloc;
  460. new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
  461. if (!new_node)
  462. goto err_node_alloc;
  463. read_lock_bh(&pathtbl_resize_lock);
  464. memcpy(new_mpath->dst, dst, ETH_ALEN);
  465. new_mpath->sdata = sdata;
  466. new_mpath->flags = 0;
  467. skb_queue_head_init(&new_mpath->frame_queue);
  468. new_node->mpath = new_mpath;
  469. new_mpath->timer.data = (unsigned long) new_mpath;
  470. new_mpath->timer.function = mesh_path_timer;
  471. new_mpath->exp_time = jiffies;
  472. spin_lock_init(&new_mpath->state_lock);
  473. init_timer(&new_mpath->timer);
  474. tbl = resize_dereference_mesh_paths();
  475. hash_idx = mesh_table_hash(dst, sdata, tbl);
  476. bucket = &tbl->hash_buckets[hash_idx];
  477. spin_lock_bh(&tbl->hashwlock[hash_idx]);
  478. err = -EEXIST;
  479. hlist_for_each_entry(node, n, bucket, list) {
  480. mpath = node->mpath;
  481. if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
  482. goto err_exists;
  483. }
  484. hlist_add_head_rcu(&new_node->list, bucket);
  485. if (atomic_inc_return(&tbl->entries) >=
  486. tbl->mean_chain_len * (tbl->hash_mask + 1))
  487. grow = 1;
  488. mesh_paths_generation++;
  489. spin_unlock_bh(&tbl->hashwlock[hash_idx]);
  490. read_unlock_bh(&pathtbl_resize_lock);
  491. if (grow) {
  492. set_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags);
  493. ieee80211_queue_work(&local->hw, &sdata->work);
  494. }
  495. return 0;
  496. err_exists:
  497. spin_unlock_bh(&tbl->hashwlock[hash_idx]);
  498. read_unlock_bh(&pathtbl_resize_lock);
  499. kfree(new_node);
  500. err_node_alloc:
  501. kfree(new_mpath);
  502. err_path_alloc:
  503. atomic_dec(&sdata->u.mesh.mpaths);
  504. return err;
  505. }
  506. static void mesh_table_free_rcu(struct rcu_head *rcu)
  507. {
  508. struct mesh_table *tbl = container_of(rcu, struct mesh_table, rcu_head);
  509. mesh_table_free(tbl, false);
  510. }
  511. void mesh_mpath_table_grow(void)
  512. {
  513. struct mesh_table *oldtbl, *newtbl;
  514. write_lock_bh(&pathtbl_resize_lock);
  515. oldtbl = resize_dereference_mesh_paths();
  516. newtbl = mesh_table_alloc(oldtbl->size_order + 1);
  517. if (!newtbl)
  518. goto out;
  519. if (mesh_table_grow(oldtbl, newtbl) < 0) {
  520. __mesh_table_free(newtbl);
  521. goto out;
  522. }
  523. rcu_assign_pointer(mesh_paths, newtbl);
  524. call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
  525. out:
  526. write_unlock_bh(&pathtbl_resize_lock);
  527. }
  528. void mesh_mpp_table_grow(void)
  529. {
  530. struct mesh_table *oldtbl, *newtbl;
  531. write_lock_bh(&pathtbl_resize_lock);
  532. oldtbl = resize_dereference_mpp_paths();
  533. newtbl = mesh_table_alloc(oldtbl->size_order + 1);
  534. if (!newtbl)
  535. goto out;
  536. if (mesh_table_grow(oldtbl, newtbl) < 0) {
  537. __mesh_table_free(newtbl);
  538. goto out;
  539. }
  540. rcu_assign_pointer(mpp_paths, newtbl);
  541. call_rcu(&oldtbl->rcu_head, mesh_table_free_rcu);
  542. out:
  543. write_unlock_bh(&pathtbl_resize_lock);
  544. }
  545. int mpp_path_add(u8 *dst, u8 *mpp, struct ieee80211_sub_if_data *sdata)
  546. {
  547. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  548. struct ieee80211_local *local = sdata->local;
  549. struct mesh_table *tbl;
  550. struct mesh_path *mpath, *new_mpath;
  551. struct mpath_node *node, *new_node;
  552. struct hlist_head *bucket;
  553. struct hlist_node *n;
  554. int grow = 0;
  555. int err = 0;
  556. u32 hash_idx;
  557. if (memcmp(dst, sdata->vif.addr, ETH_ALEN) == 0)
  558. /* never add ourselves as neighbours */
  559. return -ENOTSUPP;
  560. if (is_multicast_ether_addr(dst))
  561. return -ENOTSUPP;
  562. err = -ENOMEM;
  563. new_mpath = kzalloc(sizeof(struct mesh_path), GFP_ATOMIC);
  564. if (!new_mpath)
  565. goto err_path_alloc;
  566. new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
  567. if (!new_node)
  568. goto err_node_alloc;
  569. read_lock_bh(&pathtbl_resize_lock);
  570. memcpy(new_mpath->dst, dst, ETH_ALEN);
  571. memcpy(new_mpath->mpp, mpp, ETH_ALEN);
  572. new_mpath->sdata = sdata;
  573. new_mpath->flags = 0;
  574. skb_queue_head_init(&new_mpath->frame_queue);
  575. new_node->mpath = new_mpath;
  576. init_timer(&new_mpath->timer);
  577. new_mpath->exp_time = jiffies;
  578. spin_lock_init(&new_mpath->state_lock);
  579. tbl = resize_dereference_mpp_paths();
  580. hash_idx = mesh_table_hash(dst, sdata, tbl);
  581. bucket = &tbl->hash_buckets[hash_idx];
  582. spin_lock_bh(&tbl->hashwlock[hash_idx]);
  583. err = -EEXIST;
  584. hlist_for_each_entry(node, n, bucket, list) {
  585. mpath = node->mpath;
  586. if (mpath->sdata == sdata && memcmp(dst, mpath->dst, ETH_ALEN) == 0)
  587. goto err_exists;
  588. }
  589. hlist_add_head_rcu(&new_node->list, bucket);
  590. if (atomic_inc_return(&tbl->entries) >=
  591. tbl->mean_chain_len * (tbl->hash_mask + 1))
  592. grow = 1;
  593. spin_unlock_bh(&tbl->hashwlock[hash_idx]);
  594. read_unlock_bh(&pathtbl_resize_lock);
  595. if (grow) {
  596. set_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags);
  597. ieee80211_queue_work(&local->hw, &sdata->work);
  598. }
  599. return 0;
  600. err_exists:
  601. spin_unlock_bh(&tbl->hashwlock[hash_idx]);
  602. read_unlock_bh(&pathtbl_resize_lock);
  603. kfree(new_node);
  604. err_node_alloc:
  605. kfree(new_mpath);
  606. err_path_alloc:
  607. return err;
  608. }
  609. /**
  610. * mesh_plink_broken - deactivates paths and sends perr when a link breaks
  611. *
  612. * @sta: broken peer link
  613. *
  614. * This function must be called from the rate control algorithm if enough
  615. * delivery errors suggest that a peer link is no longer usable.
  616. */
  617. void mesh_plink_broken(struct sta_info *sta)
  618. {
  619. struct mesh_table *tbl;
  620. static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  621. struct mesh_path *mpath;
  622. struct mpath_node *node;
  623. struct hlist_node *p;
  624. struct ieee80211_sub_if_data *sdata = sta->sdata;
  625. int i;
  626. __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_DEST_UNREACHABLE);
  627. rcu_read_lock();
  628. tbl = rcu_dereference(mesh_paths);
  629. for_each_mesh_entry(tbl, p, node, i) {
  630. mpath = node->mpath;
  631. if (rcu_dereference(mpath->next_hop) == sta &&
  632. mpath->flags & MESH_PATH_ACTIVE &&
  633. !(mpath->flags & MESH_PATH_FIXED)) {
  634. spin_lock_bh(&mpath->state_lock);
  635. mpath->flags &= ~MESH_PATH_ACTIVE;
  636. ++mpath->sn;
  637. spin_unlock_bh(&mpath->state_lock);
  638. mesh_path_error_tx(sdata->u.mesh.mshcfg.element_ttl,
  639. mpath->dst, cpu_to_le32(mpath->sn),
  640. reason, bcast, sdata);
  641. }
  642. }
  643. rcu_read_unlock();
  644. }
  645. static void mesh_path_node_reclaim(struct rcu_head *rp)
  646. {
  647. struct mpath_node *node = container_of(rp, struct mpath_node, rcu);
  648. struct ieee80211_sub_if_data *sdata = node->mpath->sdata;
  649. del_timer_sync(&node->mpath->timer);
  650. atomic_dec(&sdata->u.mesh.mpaths);
  651. kfree(node->mpath);
  652. kfree(node);
  653. }
  654. /* needs to be called with the corresponding hashwlock taken */
  655. static void __mesh_path_del(struct mesh_table *tbl, struct mpath_node *node)
  656. {
  657. struct mesh_path *mpath;
  658. mpath = node->mpath;
  659. spin_lock(&mpath->state_lock);
  660. mpath->flags |= MESH_PATH_RESOLVING;
  661. if (mpath->is_gate)
  662. mesh_gate_del(tbl, mpath);
  663. hlist_del_rcu(&node->list);
  664. call_rcu(&node->rcu, mesh_path_node_reclaim);
  665. spin_unlock(&mpath->state_lock);
  666. atomic_dec(&tbl->entries);
  667. }
  668. /**
  669. * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
  670. *
  671. * @sta - mesh peer to match
  672. *
  673. * RCU notes: this function is called when a mesh plink transitions from
  674. * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
  675. * allows path creation. This will happen before the sta can be freed (because
  676. * sta_info_destroy() calls this) so any reader in a rcu read block will be
  677. * protected against the plink disappearing.
  678. */
  679. void mesh_path_flush_by_nexthop(struct sta_info *sta)
  680. {
  681. struct mesh_table *tbl;
  682. struct mesh_path *mpath;
  683. struct mpath_node *node;
  684. struct hlist_node *p;
  685. int i;
  686. rcu_read_lock();
  687. read_lock_bh(&pathtbl_resize_lock);
  688. tbl = resize_dereference_mesh_paths();
  689. for_each_mesh_entry(tbl, p, node, i) {
  690. mpath = node->mpath;
  691. if (rcu_dereference(mpath->next_hop) == sta) {
  692. spin_lock_bh(&tbl->hashwlock[i]);
  693. __mesh_path_del(tbl, node);
  694. spin_unlock_bh(&tbl->hashwlock[i]);
  695. }
  696. }
  697. read_unlock_bh(&pathtbl_resize_lock);
  698. rcu_read_unlock();
  699. }
  700. static void table_flush_by_iface(struct mesh_table *tbl,
  701. struct ieee80211_sub_if_data *sdata)
  702. {
  703. struct mesh_path *mpath;
  704. struct mpath_node *node;
  705. struct hlist_node *p;
  706. int i;
  707. WARN_ON(!rcu_read_lock_held());
  708. for_each_mesh_entry(tbl, p, node, i) {
  709. mpath = node->mpath;
  710. if (mpath->sdata != sdata)
  711. continue;
  712. spin_lock_bh(&tbl->hashwlock[i]);
  713. __mesh_path_del(tbl, node);
  714. spin_unlock_bh(&tbl->hashwlock[i]);
  715. }
  716. }
  717. /**
  718. * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
  719. *
  720. * This function deletes both mesh paths as well as mesh portal paths.
  721. *
  722. * @sdata - interface data to match
  723. *
  724. */
  725. void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
  726. {
  727. struct mesh_table *tbl;
  728. rcu_read_lock();
  729. read_lock_bh(&pathtbl_resize_lock);
  730. tbl = resize_dereference_mesh_paths();
  731. table_flush_by_iface(tbl, sdata);
  732. tbl = resize_dereference_mpp_paths();
  733. table_flush_by_iface(tbl, sdata);
  734. read_unlock_bh(&pathtbl_resize_lock);
  735. rcu_read_unlock();
  736. }
  737. /**
  738. * mesh_path_del - delete a mesh path from the table
  739. *
  740. * @addr: dst address (ETH_ALEN length)
  741. * @sdata: local subif
  742. *
  743. * Returns: 0 if successful
  744. */
  745. int mesh_path_del(u8 *addr, struct ieee80211_sub_if_data *sdata)
  746. {
  747. struct mesh_table *tbl;
  748. struct mesh_path *mpath;
  749. struct mpath_node *node;
  750. struct hlist_head *bucket;
  751. struct hlist_node *n;
  752. int hash_idx;
  753. int err = 0;
  754. read_lock_bh(&pathtbl_resize_lock);
  755. tbl = resize_dereference_mesh_paths();
  756. hash_idx = mesh_table_hash(addr, sdata, tbl);
  757. bucket = &tbl->hash_buckets[hash_idx];
  758. spin_lock_bh(&tbl->hashwlock[hash_idx]);
  759. hlist_for_each_entry(node, n, bucket, list) {
  760. mpath = node->mpath;
  761. if (mpath->sdata == sdata &&
  762. memcmp(addr, mpath->dst, ETH_ALEN) == 0) {
  763. __mesh_path_del(tbl, node);
  764. goto enddel;
  765. }
  766. }
  767. err = -ENXIO;
  768. enddel:
  769. mesh_paths_generation++;
  770. spin_unlock_bh(&tbl->hashwlock[hash_idx]);
  771. read_unlock_bh(&pathtbl_resize_lock);
  772. return err;
  773. }
  774. /**
  775. * mesh_path_tx_pending - sends pending frames in a mesh path queue
  776. *
  777. * @mpath: mesh path to activate
  778. *
  779. * Locking: the state_lock of the mpath structure must NOT be held when calling
  780. * this function.
  781. */
  782. void mesh_path_tx_pending(struct mesh_path *mpath)
  783. {
  784. if (mpath->flags & MESH_PATH_ACTIVE)
  785. ieee80211_add_pending_skbs(mpath->sdata->local,
  786. &mpath->frame_queue);
  787. }
  788. /**
  789. * mesh_path_send_to_gates - sends pending frames to all known mesh gates
  790. *
  791. * @mpath: mesh path whose queue will be emptied
  792. *
  793. * If there is only one gate, the frames are transferred from the failed mpath
  794. * queue to that gate's queue. If there are more than one gates, the frames
  795. * are copied from each gate to the next. After frames are copied, the
  796. * mpath queues are emptied onto the transmission queue.
  797. */
  798. int mesh_path_send_to_gates(struct mesh_path *mpath)
  799. {
  800. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  801. struct hlist_node *n;
  802. struct mesh_table *tbl;
  803. struct mesh_path *from_mpath = mpath;
  804. struct mpath_node *gate = NULL;
  805. bool copy = false;
  806. struct hlist_head *known_gates;
  807. rcu_read_lock();
  808. tbl = rcu_dereference(mesh_paths);
  809. known_gates = tbl->known_gates;
  810. rcu_read_unlock();
  811. if (!known_gates)
  812. return -EHOSTUNREACH;
  813. hlist_for_each_entry_rcu(gate, n, known_gates, list) {
  814. if (gate->mpath->sdata != sdata)
  815. continue;
  816. if (gate->mpath->flags & MESH_PATH_ACTIVE) {
  817. mpath_dbg("Forwarding to %pM\n", gate->mpath->dst);
  818. mesh_path_move_to_queue(gate->mpath, from_mpath, copy);
  819. from_mpath = gate->mpath;
  820. copy = true;
  821. } else {
  822. mpath_dbg("Not forwarding %p\n", gate->mpath);
  823. mpath_dbg("flags %x\n", gate->mpath->flags);
  824. }
  825. }
  826. hlist_for_each_entry_rcu(gate, n, known_gates, list)
  827. if (gate->mpath->sdata == sdata) {
  828. mpath_dbg("Sending to %pM\n", gate->mpath->dst);
  829. mesh_path_tx_pending(gate->mpath);
  830. }
  831. return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
  832. }
  833. /**
  834. * mesh_path_discard_frame - discard a frame whose path could not be resolved
  835. *
  836. * @skb: frame to discard
  837. * @sdata: network subif the frame was to be sent through
  838. *
  839. * Locking: the function must me called within a rcu_read_lock region
  840. */
  841. void mesh_path_discard_frame(struct sk_buff *skb,
  842. struct ieee80211_sub_if_data *sdata)
  843. {
  844. kfree_skb(skb);
  845. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  846. }
  847. /**
  848. * mesh_path_flush_pending - free the pending queue of a mesh path
  849. *
  850. * @mpath: mesh path whose queue has to be freed
  851. *
  852. * Locking: the function must me called within a rcu_read_lock region
  853. */
  854. void mesh_path_flush_pending(struct mesh_path *mpath)
  855. {
  856. struct sk_buff *skb;
  857. while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
  858. mesh_path_discard_frame(skb, mpath->sdata);
  859. }
  860. /**
  861. * mesh_path_fix_nexthop - force a specific next hop for a mesh path
  862. *
  863. * @mpath: the mesh path to modify
  864. * @next_hop: the next hop to force
  865. *
  866. * Locking: this function must be called holding mpath->state_lock
  867. */
  868. void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
  869. {
  870. spin_lock_bh(&mpath->state_lock);
  871. mesh_path_assign_nexthop(mpath, next_hop);
  872. mpath->sn = 0xffff;
  873. mpath->metric = 0;
  874. mpath->hop_count = 0;
  875. mpath->exp_time = 0;
  876. mpath->flags |= MESH_PATH_FIXED;
  877. mesh_path_activate(mpath);
  878. spin_unlock_bh(&mpath->state_lock);
  879. mesh_path_tx_pending(mpath);
  880. }
  881. static void mesh_path_node_free(struct hlist_node *p, bool free_leafs)
  882. {
  883. struct mesh_path *mpath;
  884. struct mpath_node *node = hlist_entry(p, struct mpath_node, list);
  885. mpath = node->mpath;
  886. hlist_del_rcu(p);
  887. if (free_leafs) {
  888. del_timer_sync(&mpath->timer);
  889. kfree(mpath);
  890. }
  891. kfree(node);
  892. }
  893. static int mesh_path_node_copy(struct hlist_node *p, struct mesh_table *newtbl)
  894. {
  895. struct mesh_path *mpath;
  896. struct mpath_node *node, *new_node;
  897. u32 hash_idx;
  898. new_node = kmalloc(sizeof(struct mpath_node), GFP_ATOMIC);
  899. if (new_node == NULL)
  900. return -ENOMEM;
  901. node = hlist_entry(p, struct mpath_node, list);
  902. mpath = node->mpath;
  903. new_node->mpath = mpath;
  904. hash_idx = mesh_table_hash(mpath->dst, mpath->sdata, newtbl);
  905. hlist_add_head(&new_node->list,
  906. &newtbl->hash_buckets[hash_idx]);
  907. return 0;
  908. }
  909. int mesh_pathtbl_init(void)
  910. {
  911. struct mesh_table *tbl_path, *tbl_mpp;
  912. int ret;
  913. tbl_path = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
  914. if (!tbl_path)
  915. return -ENOMEM;
  916. tbl_path->free_node = &mesh_path_node_free;
  917. tbl_path->copy_node = &mesh_path_node_copy;
  918. tbl_path->mean_chain_len = MEAN_CHAIN_LEN;
  919. tbl_path->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
  920. if (!tbl_path->known_gates) {
  921. ret = -ENOMEM;
  922. goto free_path;
  923. }
  924. INIT_HLIST_HEAD(tbl_path->known_gates);
  925. tbl_mpp = mesh_table_alloc(INIT_PATHS_SIZE_ORDER);
  926. if (!tbl_mpp) {
  927. ret = -ENOMEM;
  928. goto free_path;
  929. }
  930. tbl_mpp->free_node = &mesh_path_node_free;
  931. tbl_mpp->copy_node = &mesh_path_node_copy;
  932. tbl_mpp->mean_chain_len = MEAN_CHAIN_LEN;
  933. tbl_mpp->known_gates = kzalloc(sizeof(struct hlist_head), GFP_ATOMIC);
  934. if (!tbl_mpp->known_gates) {
  935. ret = -ENOMEM;
  936. goto free_mpp;
  937. }
  938. INIT_HLIST_HEAD(tbl_mpp->known_gates);
  939. /* Need no locking since this is during init */
  940. RCU_INIT_POINTER(mesh_paths, tbl_path);
  941. RCU_INIT_POINTER(mpp_paths, tbl_mpp);
  942. return 0;
  943. free_mpp:
  944. mesh_table_free(tbl_mpp, true);
  945. free_path:
  946. mesh_table_free(tbl_path, true);
  947. return ret;
  948. }
  949. void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
  950. {
  951. struct mesh_table *tbl;
  952. struct mesh_path *mpath;
  953. struct mpath_node *node;
  954. struct hlist_node *p;
  955. int i;
  956. rcu_read_lock();
  957. tbl = rcu_dereference(mesh_paths);
  958. for_each_mesh_entry(tbl, p, node, i) {
  959. if (node->mpath->sdata != sdata)
  960. continue;
  961. mpath = node->mpath;
  962. if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
  963. (!(mpath->flags & MESH_PATH_FIXED)) &&
  964. time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
  965. mesh_path_del(mpath->dst, mpath->sdata);
  966. }
  967. rcu_read_unlock();
  968. }
  969. void mesh_pathtbl_unregister(void)
  970. {
  971. /* no need for locking during exit path */
  972. mesh_table_free(rcu_dereference_protected(mesh_paths, 1), true);
  973. mesh_table_free(rcu_dereference_protected(mpp_paths, 1), true);
  974. }