mesh_pathtbl.c 29 KB

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