mesh_pathtbl.c 29 KB

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