mesh_pathtbl.c 29 KB

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