mesh_pathtbl.c 29 KB

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