mesh_pathtbl.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2008, 2009 open80211s Ltd.
  4. * Copyright (C) 2023 Intel Corporation
  5. * Author: Luis Carlos Cobo <luisca@cozybit.com>
  6. */
  7. #include <linux/etherdevice.h>
  8. #include <linux/list.h>
  9. #include <linux/random.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/string.h>
  13. #include <net/mac80211.h>
  14. #include "wme.h"
  15. #include "ieee80211_i.h"
  16. #include "mesh.h"
  17. #include <linux/rhashtable.h>
  18. static void mesh_path_free_rcu(struct mesh_table *tbl, struct mesh_path *mpath);
  19. static u32 mesh_table_hash(const void *addr, u32 len, u32 seed)
  20. {
  21. /* Use last four bytes of hw addr as hash index */
  22. return jhash_1word(__get_unaligned_cpu32((u8 *)addr + 2), seed);
  23. }
  24. static const struct rhashtable_params mesh_rht_params = {
  25. .nelem_hint = 2,
  26. .automatic_shrinking = true,
  27. .key_len = ETH_ALEN,
  28. .key_offset = offsetof(struct mesh_path, dst),
  29. .head_offset = offsetof(struct mesh_path, rhash),
  30. .hashfn = mesh_table_hash,
  31. };
  32. static const struct rhashtable_params fast_tx_rht_params = {
  33. .nelem_hint = 10,
  34. .automatic_shrinking = true,
  35. .key_len = sizeof_field(struct ieee80211_mesh_fast_tx, key),
  36. .key_offset = offsetof(struct ieee80211_mesh_fast_tx, key),
  37. .head_offset = offsetof(struct ieee80211_mesh_fast_tx, rhash),
  38. .hashfn = mesh_table_hash,
  39. };
  40. static void __mesh_fast_tx_entry_free(void *ptr, void *tblptr)
  41. {
  42. struct ieee80211_mesh_fast_tx *entry = ptr;
  43. kfree_rcu(entry, fast_tx.rcu_head);
  44. }
  45. static void mesh_fast_tx_deinit(struct ieee80211_sub_if_data *sdata)
  46. {
  47. struct mesh_tx_cache *cache;
  48. cache = &sdata->u.mesh.tx_cache;
  49. rhashtable_free_and_destroy(&cache->rht,
  50. __mesh_fast_tx_entry_free, NULL);
  51. }
  52. static void mesh_fast_tx_init(struct ieee80211_sub_if_data *sdata)
  53. {
  54. struct mesh_tx_cache *cache;
  55. cache = &sdata->u.mesh.tx_cache;
  56. rhashtable_init(&cache->rht, &fast_tx_rht_params);
  57. INIT_HLIST_HEAD(&cache->walk_head);
  58. spin_lock_init(&cache->walk_lock);
  59. }
  60. static inline bool mpath_expired(struct mesh_path *mpath)
  61. {
  62. return (mpath->flags & MESH_PATH_ACTIVE) &&
  63. time_after(jiffies, mpath->exp_time) &&
  64. !(mpath->flags & MESH_PATH_FIXED);
  65. }
  66. static void mesh_path_rht_free(void *ptr, void *tblptr)
  67. {
  68. struct mesh_path *mpath = ptr;
  69. struct mesh_table *tbl = tblptr;
  70. mesh_path_free_rcu(tbl, mpath);
  71. }
  72. static void mesh_table_init(struct mesh_table *tbl)
  73. {
  74. INIT_HLIST_HEAD(&tbl->known_gates);
  75. INIT_HLIST_HEAD(&tbl->walk_head);
  76. atomic_set(&tbl->entries, 0);
  77. spin_lock_init(&tbl->gates_lock);
  78. spin_lock_init(&tbl->walk_lock);
  79. /* rhashtable_init() may fail only in case of wrong
  80. * mesh_rht_params
  81. */
  82. WARN_ON(rhashtable_init(&tbl->rhead, &mesh_rht_params));
  83. }
  84. static void mesh_table_free(struct mesh_table *tbl)
  85. {
  86. rhashtable_free_and_destroy(&tbl->rhead,
  87. mesh_path_rht_free, tbl);
  88. }
  89. /**
  90. * mesh_path_assign_nexthop - update mesh path next hop
  91. *
  92. * @mpath: mesh path to update
  93. * @sta: next hop to assign
  94. *
  95. * Locking: mpath->state_lock must be held when calling this function
  96. */
  97. void mesh_path_assign_nexthop(struct mesh_path *mpath, struct sta_info *sta)
  98. {
  99. struct sk_buff *skb;
  100. struct ieee80211_hdr *hdr;
  101. unsigned long flags;
  102. rcu_assign_pointer(mpath->next_hop, sta);
  103. spin_lock_irqsave(&mpath->frame_queue.lock, flags);
  104. skb_queue_walk(&mpath->frame_queue, skb) {
  105. hdr = (struct ieee80211_hdr *) skb->data;
  106. memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
  107. memcpy(hdr->addr2, mpath->sdata->vif.addr, ETH_ALEN);
  108. ieee80211_mps_set_frame_flags(sta->sdata, sta, hdr);
  109. }
  110. spin_unlock_irqrestore(&mpath->frame_queue.lock, flags);
  111. }
  112. static void prepare_for_gate(struct sk_buff *skb, char *dst_addr,
  113. struct mesh_path *gate_mpath)
  114. {
  115. struct ieee80211_hdr *hdr;
  116. struct ieee80211s_hdr *mshdr;
  117. int mesh_hdrlen, hdrlen;
  118. char *next_hop;
  119. hdr = (struct ieee80211_hdr *) skb->data;
  120. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  121. mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  122. if (!(mshdr->flags & MESH_FLAGS_AE)) {
  123. /* size of the fixed part of the mesh header */
  124. mesh_hdrlen = 6;
  125. /* make room for the two extended addresses */
  126. skb_push(skb, 2 * ETH_ALEN);
  127. memmove(skb->data, hdr, hdrlen + mesh_hdrlen);
  128. hdr = (struct ieee80211_hdr *) skb->data;
  129. /* we preserve the previous mesh header and only add
  130. * the new addresses */
  131. mshdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  132. mshdr->flags = MESH_FLAGS_AE_A5_A6;
  133. memcpy(mshdr->eaddr1, hdr->addr3, ETH_ALEN);
  134. memcpy(mshdr->eaddr2, hdr->addr4, ETH_ALEN);
  135. }
  136. /* update next hop */
  137. hdr = (struct ieee80211_hdr *) skb->data;
  138. rcu_read_lock();
  139. next_hop = rcu_dereference(gate_mpath->next_hop)->sta.addr;
  140. memcpy(hdr->addr1, next_hop, ETH_ALEN);
  141. rcu_read_unlock();
  142. memcpy(hdr->addr2, gate_mpath->sdata->vif.addr, ETH_ALEN);
  143. memcpy(hdr->addr3, dst_addr, ETH_ALEN);
  144. }
  145. /**
  146. * mesh_path_move_to_queue - Move or copy frames from one mpath queue to another
  147. *
  148. * @gate_mpath: An active mpath the frames will be sent to (i.e. the gate)
  149. * @from_mpath: The failed mpath
  150. * @copy: When true, copy all the frames to the new mpath queue. When false,
  151. * move them.
  152. *
  153. * This function is used to transfer or copy frames from an unresolved mpath to
  154. * a gate mpath. The function also adds the Address Extension field and
  155. * updates the next hop.
  156. *
  157. * If a frame already has an Address Extension field, only the next hop and
  158. * destination addresses are updated.
  159. *
  160. * The gate mpath must be an active mpath with a valid mpath->next_hop.
  161. */
  162. static void mesh_path_move_to_queue(struct mesh_path *gate_mpath,
  163. struct mesh_path *from_mpath,
  164. bool copy)
  165. {
  166. struct sk_buff *skb, *fskb, *tmp;
  167. struct sk_buff_head failq;
  168. unsigned long flags;
  169. if (WARN_ON(gate_mpath == from_mpath))
  170. return;
  171. if (WARN_ON(!gate_mpath->next_hop))
  172. return;
  173. __skb_queue_head_init(&failq);
  174. spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
  175. skb_queue_splice_init(&from_mpath->frame_queue, &failq);
  176. spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
  177. skb_queue_walk_safe(&failq, fskb, tmp) {
  178. if (skb_queue_len(&gate_mpath->frame_queue) >=
  179. MESH_FRAME_QUEUE_LEN) {
  180. mpath_dbg(gate_mpath->sdata, "mpath queue full!\n");
  181. break;
  182. }
  183. skb = skb_copy(fskb, GFP_ATOMIC);
  184. if (WARN_ON(!skb))
  185. break;
  186. prepare_for_gate(skb, gate_mpath->dst, gate_mpath);
  187. skb_queue_tail(&gate_mpath->frame_queue, skb);
  188. if (copy)
  189. continue;
  190. __skb_unlink(fskb, &failq);
  191. kfree_skb(fskb);
  192. }
  193. mpath_dbg(gate_mpath->sdata, "Mpath queue for gate %pM has %d frames\n",
  194. gate_mpath->dst, skb_queue_len(&gate_mpath->frame_queue));
  195. if (!copy)
  196. return;
  197. spin_lock_irqsave(&from_mpath->frame_queue.lock, flags);
  198. skb_queue_splice(&failq, &from_mpath->frame_queue);
  199. spin_unlock_irqrestore(&from_mpath->frame_queue.lock, flags);
  200. }
  201. static struct mesh_path *mpath_lookup(struct mesh_table *tbl, const u8 *dst,
  202. struct ieee80211_sub_if_data *sdata)
  203. {
  204. struct mesh_path *mpath;
  205. mpath = rhashtable_lookup(&tbl->rhead, dst, mesh_rht_params);
  206. if (mpath && mpath_expired(mpath)) {
  207. spin_lock_bh(&mpath->state_lock);
  208. mpath->flags &= ~MESH_PATH_ACTIVE;
  209. spin_unlock_bh(&mpath->state_lock);
  210. }
  211. return mpath;
  212. }
  213. /**
  214. * mesh_path_lookup - look up a path in the mesh path table
  215. * @sdata: local subif
  216. * @dst: hardware address (ETH_ALEN length) of destination
  217. *
  218. * Returns: pointer to the mesh path structure, or NULL if not found
  219. *
  220. * Locking: must be called within a read rcu section.
  221. */
  222. struct mesh_path *
  223. mesh_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
  224. {
  225. return mpath_lookup(&sdata->u.mesh.mesh_paths, dst, sdata);
  226. }
  227. struct mesh_path *
  228. mpp_path_lookup(struct ieee80211_sub_if_data *sdata, const u8 *dst)
  229. {
  230. return mpath_lookup(&sdata->u.mesh.mpp_paths, dst, sdata);
  231. }
  232. static struct mesh_path *
  233. __mesh_path_lookup_by_idx(struct mesh_table *tbl, int idx)
  234. {
  235. int i = 0;
  236. struct mesh_path *mpath;
  237. hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) {
  238. if (i++ == idx)
  239. break;
  240. }
  241. if (!mpath)
  242. return NULL;
  243. if (mpath_expired(mpath)) {
  244. spin_lock_bh(&mpath->state_lock);
  245. mpath->flags &= ~MESH_PATH_ACTIVE;
  246. spin_unlock_bh(&mpath->state_lock);
  247. }
  248. return mpath;
  249. }
  250. /**
  251. * mesh_path_lookup_by_idx - look up a path in the mesh path table by its index
  252. * @idx: index
  253. * @sdata: local subif, or NULL for all entries
  254. *
  255. * Returns: pointer to the mesh path structure, or NULL if not found.
  256. *
  257. * Locking: must be called within a read rcu section.
  258. */
  259. struct mesh_path *
  260. mesh_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
  261. {
  262. return __mesh_path_lookup_by_idx(&sdata->u.mesh.mesh_paths, idx);
  263. }
  264. /**
  265. * mpp_path_lookup_by_idx - look up a path in the proxy path table by its index
  266. * @idx: index
  267. * @sdata: local subif, or NULL for all entries
  268. *
  269. * Returns: pointer to the proxy path structure, or NULL if not found.
  270. *
  271. * Locking: must be called within a read rcu section.
  272. */
  273. struct mesh_path *
  274. mpp_path_lookup_by_idx(struct ieee80211_sub_if_data *sdata, int idx)
  275. {
  276. return __mesh_path_lookup_by_idx(&sdata->u.mesh.mpp_paths, idx);
  277. }
  278. /**
  279. * mesh_path_add_gate - add the given mpath to a mesh gate to our path table
  280. * @mpath: gate path to add to table
  281. *
  282. * Returns: 0 on success, -EEXIST
  283. */
  284. int mesh_path_add_gate(struct mesh_path *mpath)
  285. {
  286. struct mesh_table *tbl;
  287. int err;
  288. rcu_read_lock();
  289. tbl = &mpath->sdata->u.mesh.mesh_paths;
  290. spin_lock_bh(&mpath->state_lock);
  291. if (mpath->is_gate) {
  292. err = -EEXIST;
  293. spin_unlock_bh(&mpath->state_lock);
  294. goto err_rcu;
  295. }
  296. mpath->is_gate = true;
  297. mpath->sdata->u.mesh.num_gates++;
  298. spin_lock(&tbl->gates_lock);
  299. hlist_add_head_rcu(&mpath->gate_list, &tbl->known_gates);
  300. spin_unlock(&tbl->gates_lock);
  301. spin_unlock_bh(&mpath->state_lock);
  302. mpath_dbg(mpath->sdata,
  303. "Mesh path: Recorded new gate: %pM. %d known gates\n",
  304. mpath->dst, mpath->sdata->u.mesh.num_gates);
  305. err = 0;
  306. err_rcu:
  307. rcu_read_unlock();
  308. return err;
  309. }
  310. /**
  311. * mesh_gate_del - remove a mesh gate from the list of known gates
  312. * @tbl: table which holds our list of known gates
  313. * @mpath: gate mpath
  314. */
  315. static void mesh_gate_del(struct mesh_table *tbl, struct mesh_path *mpath)
  316. {
  317. lockdep_assert_held(&mpath->state_lock);
  318. if (!mpath->is_gate)
  319. return;
  320. mpath->is_gate = false;
  321. spin_lock_bh(&tbl->gates_lock);
  322. hlist_del_rcu(&mpath->gate_list);
  323. mpath->sdata->u.mesh.num_gates--;
  324. spin_unlock_bh(&tbl->gates_lock);
  325. mpath_dbg(mpath->sdata,
  326. "Mesh path: Deleted gate: %pM. %d known gates\n",
  327. mpath->dst, mpath->sdata->u.mesh.num_gates);
  328. }
  329. /**
  330. * mesh_gate_num - number of gates known to this interface
  331. * @sdata: subif data
  332. *
  333. * Returns: The number of gates
  334. */
  335. int mesh_gate_num(struct ieee80211_sub_if_data *sdata)
  336. {
  337. return sdata->u.mesh.num_gates;
  338. }
  339. static
  340. struct mesh_path *mesh_path_new(struct ieee80211_sub_if_data *sdata,
  341. const u8 *dst, gfp_t gfp_flags)
  342. {
  343. struct mesh_path *new_mpath;
  344. new_mpath = kzalloc(sizeof(struct mesh_path), gfp_flags);
  345. if (!new_mpath)
  346. return NULL;
  347. memcpy(new_mpath->dst, dst, ETH_ALEN);
  348. eth_broadcast_addr(new_mpath->rann_snd_addr);
  349. new_mpath->is_root = false;
  350. new_mpath->sdata = sdata;
  351. new_mpath->flags = 0;
  352. skb_queue_head_init(&new_mpath->frame_queue);
  353. new_mpath->exp_time = jiffies;
  354. spin_lock_init(&new_mpath->state_lock);
  355. timer_setup(&new_mpath->timer, mesh_path_timer, 0);
  356. return new_mpath;
  357. }
  358. static void mesh_fast_tx_entry_free(struct mesh_tx_cache *cache,
  359. struct ieee80211_mesh_fast_tx *entry)
  360. {
  361. hlist_del_rcu(&entry->walk_list);
  362. rhashtable_remove_fast(&cache->rht, &entry->rhash, fast_tx_rht_params);
  363. kfree_rcu(entry, fast_tx.rcu_head);
  364. }
  365. struct ieee80211_mesh_fast_tx *
  366. mesh_fast_tx_get(struct ieee80211_sub_if_data *sdata,
  367. struct ieee80211_mesh_fast_tx_key *key)
  368. {
  369. struct ieee80211_mesh_fast_tx *entry;
  370. struct mesh_tx_cache *cache;
  371. cache = &sdata->u.mesh.tx_cache;
  372. entry = rhashtable_lookup(&cache->rht, key, fast_tx_rht_params);
  373. if (!entry)
  374. return NULL;
  375. if (!(entry->mpath->flags & MESH_PATH_ACTIVE) ||
  376. mpath_expired(entry->mpath)) {
  377. spin_lock_bh(&cache->walk_lock);
  378. entry = rhashtable_lookup(&cache->rht, key, fast_tx_rht_params);
  379. if (entry)
  380. mesh_fast_tx_entry_free(cache, entry);
  381. spin_unlock_bh(&cache->walk_lock);
  382. return NULL;
  383. }
  384. mesh_path_refresh(sdata, entry->mpath, NULL);
  385. if (entry->mppath)
  386. entry->mppath->exp_time = jiffies;
  387. entry->timestamp = jiffies;
  388. return entry;
  389. }
  390. void mesh_fast_tx_cache(struct ieee80211_sub_if_data *sdata,
  391. struct sk_buff *skb, struct mesh_path *mpath)
  392. {
  393. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  394. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  395. struct ieee80211_mesh_fast_tx *entry, *prev;
  396. struct ieee80211_mesh_fast_tx build = {};
  397. struct ieee80211s_hdr *meshhdr;
  398. struct mesh_tx_cache *cache;
  399. struct ieee80211_key *key;
  400. struct mesh_path *mppath;
  401. struct sta_info *sta;
  402. u8 *qc;
  403. if (sdata->noack_map ||
  404. !ieee80211_is_data_qos(hdr->frame_control))
  405. return;
  406. build.fast_tx.hdr_len = ieee80211_hdrlen(hdr->frame_control);
  407. meshhdr = (struct ieee80211s_hdr *)(skb->data + build.fast_tx.hdr_len);
  408. build.hdrlen = ieee80211_get_mesh_hdrlen(meshhdr);
  409. cache = &sdata->u.mesh.tx_cache;
  410. if (atomic_read(&cache->rht.nelems) >= MESH_FAST_TX_CACHE_MAX_SIZE)
  411. return;
  412. sta = rcu_dereference(mpath->next_hop);
  413. if (!sta)
  414. return;
  415. build.key.type = MESH_FAST_TX_TYPE_LOCAL;
  416. if ((meshhdr->flags & MESH_FLAGS_AE) == MESH_FLAGS_AE_A5_A6) {
  417. /* This is required to keep the mppath alive */
  418. mppath = mpp_path_lookup(sdata, meshhdr->eaddr1);
  419. if (!mppath)
  420. return;
  421. build.mppath = mppath;
  422. if (!ether_addr_equal(meshhdr->eaddr2, sdata->vif.addr))
  423. build.key.type = MESH_FAST_TX_TYPE_PROXIED;
  424. } else if (ieee80211_has_a4(hdr->frame_control)) {
  425. mppath = mpath;
  426. } else {
  427. return;
  428. }
  429. if (!ether_addr_equal(hdr->addr4, sdata->vif.addr))
  430. build.key.type = MESH_FAST_TX_TYPE_FORWARDED;
  431. /* rate limit, in case fast xmit can't be enabled */
  432. if (mppath->fast_tx_check == jiffies)
  433. return;
  434. mppath->fast_tx_check = jiffies;
  435. /*
  436. * Same use of the sta lock as in ieee80211_check_fast_xmit, in order
  437. * to protect against concurrent sta key updates.
  438. */
  439. spin_lock_bh(&sta->lock);
  440. key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
  441. if (!key)
  442. key = rcu_access_pointer(sdata->default_unicast_key);
  443. build.fast_tx.key = key;
  444. if (key) {
  445. bool gen_iv, iv_spc;
  446. gen_iv = key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
  447. iv_spc = key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
  448. if (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) ||
  449. (key->flags & KEY_FLAG_TAINTED))
  450. goto unlock_sta;
  451. switch (key->conf.cipher) {
  452. case WLAN_CIPHER_SUITE_CCMP:
  453. case WLAN_CIPHER_SUITE_CCMP_256:
  454. if (gen_iv)
  455. build.fast_tx.pn_offs = build.fast_tx.hdr_len;
  456. if (gen_iv || iv_spc)
  457. build.fast_tx.hdr_len += IEEE80211_CCMP_HDR_LEN;
  458. break;
  459. case WLAN_CIPHER_SUITE_GCMP:
  460. case WLAN_CIPHER_SUITE_GCMP_256:
  461. if (gen_iv)
  462. build.fast_tx.pn_offs = build.fast_tx.hdr_len;
  463. if (gen_iv || iv_spc)
  464. build.fast_tx.hdr_len += IEEE80211_GCMP_HDR_LEN;
  465. break;
  466. default:
  467. goto unlock_sta;
  468. }
  469. }
  470. memcpy(build.key.addr, mppath->dst, ETH_ALEN);
  471. build.timestamp = jiffies;
  472. build.fast_tx.band = info->band;
  473. build.fast_tx.da_offs = offsetof(struct ieee80211_hdr, addr3);
  474. build.fast_tx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
  475. build.mpath = mpath;
  476. memcpy(build.hdr, meshhdr, build.hdrlen);
  477. memcpy(build.hdr + build.hdrlen, rfc1042_header, sizeof(rfc1042_header));
  478. build.hdrlen += sizeof(rfc1042_header);
  479. memcpy(build.fast_tx.hdr, hdr, build.fast_tx.hdr_len);
  480. hdr = (struct ieee80211_hdr *)build.fast_tx.hdr;
  481. if (build.fast_tx.key)
  482. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  483. qc = ieee80211_get_qos_ctl(hdr);
  484. qc[1] |= IEEE80211_QOS_CTL_MESH_CONTROL_PRESENT >> 8;
  485. entry = kmemdup(&build, sizeof(build), GFP_ATOMIC);
  486. if (!entry)
  487. goto unlock_sta;
  488. spin_lock(&cache->walk_lock);
  489. prev = rhashtable_lookup_get_insert_fast(&cache->rht,
  490. &entry->rhash,
  491. fast_tx_rht_params);
  492. if (IS_ERR(prev)) {
  493. kfree(entry);
  494. goto unlock_cache;
  495. }
  496. /*
  497. * replace any previous entry in the hash table, in case we're
  498. * replacing it with a different type (e.g. mpath -> mpp)
  499. */
  500. if (unlikely(prev)) {
  501. rhashtable_replace_fast(&cache->rht, &prev->rhash,
  502. &entry->rhash, fast_tx_rht_params);
  503. hlist_del_rcu(&prev->walk_list);
  504. kfree_rcu(prev, fast_tx.rcu_head);
  505. }
  506. hlist_add_head(&entry->walk_list, &cache->walk_head);
  507. unlock_cache:
  508. spin_unlock(&cache->walk_lock);
  509. unlock_sta:
  510. spin_unlock_bh(&sta->lock);
  511. }
  512. void mesh_fast_tx_gc(struct ieee80211_sub_if_data *sdata)
  513. {
  514. unsigned long timeout = msecs_to_jiffies(MESH_FAST_TX_CACHE_TIMEOUT);
  515. struct mesh_tx_cache *cache = &sdata->u.mesh.tx_cache;
  516. struct ieee80211_mesh_fast_tx *entry;
  517. struct hlist_node *n;
  518. if (atomic_read(&cache->rht.nelems) < MESH_FAST_TX_CACHE_THRESHOLD_SIZE)
  519. return;
  520. spin_lock_bh(&cache->walk_lock);
  521. hlist_for_each_entry_safe(entry, n, &cache->walk_head, walk_list)
  522. if (!time_is_after_jiffies(entry->timestamp + timeout))
  523. mesh_fast_tx_entry_free(cache, entry);
  524. spin_unlock_bh(&cache->walk_lock);
  525. }
  526. void mesh_fast_tx_flush_mpath(struct mesh_path *mpath)
  527. {
  528. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  529. struct mesh_tx_cache *cache = &sdata->u.mesh.tx_cache;
  530. struct ieee80211_mesh_fast_tx *entry;
  531. struct hlist_node *n;
  532. spin_lock_bh(&cache->walk_lock);
  533. hlist_for_each_entry_safe(entry, n, &cache->walk_head, walk_list)
  534. if (entry->mpath == mpath)
  535. mesh_fast_tx_entry_free(cache, entry);
  536. spin_unlock_bh(&cache->walk_lock);
  537. }
  538. void mesh_fast_tx_flush_sta(struct ieee80211_sub_if_data *sdata,
  539. struct sta_info *sta)
  540. {
  541. struct mesh_tx_cache *cache = &sdata->u.mesh.tx_cache;
  542. struct ieee80211_mesh_fast_tx *entry;
  543. struct hlist_node *n;
  544. spin_lock_bh(&cache->walk_lock);
  545. hlist_for_each_entry_safe(entry, n, &cache->walk_head, walk_list)
  546. if (rcu_access_pointer(entry->mpath->next_hop) == sta)
  547. mesh_fast_tx_entry_free(cache, entry);
  548. spin_unlock_bh(&cache->walk_lock);
  549. }
  550. void mesh_fast_tx_flush_addr(struct ieee80211_sub_if_data *sdata,
  551. const u8 *addr)
  552. {
  553. struct mesh_tx_cache *cache = &sdata->u.mesh.tx_cache;
  554. struct ieee80211_mesh_fast_tx_key key = {};
  555. struct ieee80211_mesh_fast_tx *entry;
  556. int i;
  557. ether_addr_copy(key.addr, addr);
  558. spin_lock_bh(&cache->walk_lock);
  559. for (i = 0; i < NUM_MESH_FAST_TX_TYPE; i++) {
  560. key.type = i;
  561. entry = rhashtable_lookup_fast(&cache->rht, &key, fast_tx_rht_params);
  562. if (entry)
  563. mesh_fast_tx_entry_free(cache, entry);
  564. }
  565. spin_unlock_bh(&cache->walk_lock);
  566. }
  567. /**
  568. * mesh_path_add - allocate and add a new path to the mesh path table
  569. * @dst: destination address of the path (ETH_ALEN length)
  570. * @sdata: local subif
  571. *
  572. * Returns: 0 on success
  573. *
  574. * State: the initial state of the new path is set to 0
  575. */
  576. struct mesh_path *mesh_path_add(struct ieee80211_sub_if_data *sdata,
  577. const u8 *dst)
  578. {
  579. struct mesh_table *tbl;
  580. struct mesh_path *mpath, *new_mpath;
  581. if (ether_addr_equal(dst, sdata->vif.addr))
  582. /* never add ourselves as neighbours */
  583. return ERR_PTR(-EOPNOTSUPP);
  584. if (is_multicast_ether_addr(dst))
  585. return ERR_PTR(-EOPNOTSUPP);
  586. if (atomic_add_unless(&sdata->u.mesh.mpaths, 1, MESH_MAX_MPATHS) == 0)
  587. return ERR_PTR(-ENOSPC);
  588. new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
  589. if (!new_mpath)
  590. return ERR_PTR(-ENOMEM);
  591. tbl = &sdata->u.mesh.mesh_paths;
  592. spin_lock_bh(&tbl->walk_lock);
  593. mpath = rhashtable_lookup_get_insert_fast(&tbl->rhead,
  594. &new_mpath->rhash,
  595. mesh_rht_params);
  596. if (!mpath)
  597. hlist_add_head(&new_mpath->walk_list, &tbl->walk_head);
  598. spin_unlock_bh(&tbl->walk_lock);
  599. if (mpath) {
  600. kfree(new_mpath);
  601. if (IS_ERR(mpath))
  602. return mpath;
  603. new_mpath = mpath;
  604. }
  605. sdata->u.mesh.mesh_paths_generation++;
  606. return new_mpath;
  607. }
  608. int mpp_path_add(struct ieee80211_sub_if_data *sdata,
  609. const u8 *dst, const u8 *mpp)
  610. {
  611. struct mesh_table *tbl;
  612. struct mesh_path *new_mpath;
  613. int ret;
  614. if (ether_addr_equal(dst, sdata->vif.addr))
  615. /* never add ourselves as neighbours */
  616. return -EOPNOTSUPP;
  617. if (is_multicast_ether_addr(dst))
  618. return -EOPNOTSUPP;
  619. new_mpath = mesh_path_new(sdata, dst, GFP_ATOMIC);
  620. if (!new_mpath)
  621. return -ENOMEM;
  622. memcpy(new_mpath->mpp, mpp, ETH_ALEN);
  623. tbl = &sdata->u.mesh.mpp_paths;
  624. spin_lock_bh(&tbl->walk_lock);
  625. ret = rhashtable_lookup_insert_fast(&tbl->rhead,
  626. &new_mpath->rhash,
  627. mesh_rht_params);
  628. if (!ret)
  629. hlist_add_head_rcu(&new_mpath->walk_list, &tbl->walk_head);
  630. spin_unlock_bh(&tbl->walk_lock);
  631. if (ret)
  632. kfree(new_mpath);
  633. else
  634. mesh_fast_tx_flush_addr(sdata, dst);
  635. sdata->u.mesh.mpp_paths_generation++;
  636. return ret;
  637. }
  638. /**
  639. * mesh_plink_broken - deactivates paths and sends perr when a link breaks
  640. *
  641. * @sta: broken peer link
  642. *
  643. * This function must be called from the rate control algorithm if enough
  644. * delivery errors suggest that a peer link is no longer usable.
  645. */
  646. void mesh_plink_broken(struct sta_info *sta)
  647. {
  648. struct ieee80211_sub_if_data *sdata = sta->sdata;
  649. struct mesh_table *tbl = &sdata->u.mesh.mesh_paths;
  650. static const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  651. struct mesh_path *mpath;
  652. rcu_read_lock();
  653. hlist_for_each_entry_rcu(mpath, &tbl->walk_head, walk_list) {
  654. if (rcu_access_pointer(mpath->next_hop) == sta &&
  655. mpath->flags & MESH_PATH_ACTIVE &&
  656. !(mpath->flags & MESH_PATH_FIXED)) {
  657. spin_lock_bh(&mpath->state_lock);
  658. mpath->flags &= ~MESH_PATH_ACTIVE;
  659. ++mpath->sn;
  660. spin_unlock_bh(&mpath->state_lock);
  661. mesh_path_error_tx(sdata,
  662. sdata->u.mesh.mshcfg.element_ttl,
  663. mpath->dst, mpath->sn,
  664. WLAN_REASON_MESH_PATH_DEST_UNREACHABLE, bcast);
  665. }
  666. }
  667. rcu_read_unlock();
  668. }
  669. static void mesh_path_free_rcu(struct mesh_table *tbl,
  670. struct mesh_path *mpath)
  671. {
  672. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  673. spin_lock_bh(&mpath->state_lock);
  674. mpath->flags |= MESH_PATH_RESOLVING | MESH_PATH_DELETED;
  675. mesh_gate_del(tbl, mpath);
  676. spin_unlock_bh(&mpath->state_lock);
  677. timer_shutdown_sync(&mpath->timer);
  678. atomic_dec(&sdata->u.mesh.mpaths);
  679. atomic_dec(&tbl->entries);
  680. mesh_path_flush_pending(mpath);
  681. kfree_rcu(mpath, rcu);
  682. }
  683. static void __mesh_path_del(struct mesh_table *tbl, struct mesh_path *mpath)
  684. {
  685. hlist_del_rcu(&mpath->walk_list);
  686. rhashtable_remove_fast(&tbl->rhead, &mpath->rhash, mesh_rht_params);
  687. if (tbl == &mpath->sdata->u.mesh.mpp_paths)
  688. mesh_fast_tx_flush_addr(mpath->sdata, mpath->dst);
  689. else
  690. mesh_fast_tx_flush_mpath(mpath);
  691. mesh_path_free_rcu(tbl, mpath);
  692. }
  693. /**
  694. * mesh_path_flush_by_nexthop - Deletes mesh paths if their next hop matches
  695. *
  696. * @sta: mesh peer to match
  697. *
  698. * RCU notes: this function is called when a mesh plink transitions from
  699. * PLINK_ESTAB to any other state, since PLINK_ESTAB state is the only one that
  700. * allows path creation. This will happen before the sta can be freed (because
  701. * sta_info_destroy() calls this) so any reader in a rcu read block will be
  702. * protected against the plink disappearing.
  703. */
  704. void mesh_path_flush_by_nexthop(struct sta_info *sta)
  705. {
  706. struct ieee80211_sub_if_data *sdata = sta->sdata;
  707. struct mesh_table *tbl = &sdata->u.mesh.mesh_paths;
  708. struct mesh_path *mpath;
  709. struct hlist_node *n;
  710. spin_lock_bh(&tbl->walk_lock);
  711. hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
  712. if (rcu_access_pointer(mpath->next_hop) == sta)
  713. __mesh_path_del(tbl, mpath);
  714. }
  715. spin_unlock_bh(&tbl->walk_lock);
  716. }
  717. static void mpp_flush_by_proxy(struct ieee80211_sub_if_data *sdata,
  718. const u8 *proxy)
  719. {
  720. struct mesh_table *tbl = &sdata->u.mesh.mpp_paths;
  721. struct mesh_path *mpath;
  722. struct hlist_node *n;
  723. spin_lock_bh(&tbl->walk_lock);
  724. hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
  725. if (ether_addr_equal(mpath->mpp, proxy))
  726. __mesh_path_del(tbl, mpath);
  727. }
  728. spin_unlock_bh(&tbl->walk_lock);
  729. }
  730. static void table_flush_by_iface(struct mesh_table *tbl)
  731. {
  732. struct mesh_path *mpath;
  733. struct hlist_node *n;
  734. spin_lock_bh(&tbl->walk_lock);
  735. hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
  736. __mesh_path_del(tbl, mpath);
  737. }
  738. spin_unlock_bh(&tbl->walk_lock);
  739. }
  740. /**
  741. * mesh_path_flush_by_iface - Deletes all mesh paths associated with a given iface
  742. *
  743. * @sdata: interface data to match
  744. *
  745. * This function deletes both mesh paths as well as mesh portal paths.
  746. */
  747. void mesh_path_flush_by_iface(struct ieee80211_sub_if_data *sdata)
  748. {
  749. table_flush_by_iface(&sdata->u.mesh.mesh_paths);
  750. table_flush_by_iface(&sdata->u.mesh.mpp_paths);
  751. }
  752. /**
  753. * table_path_del - delete a path from the mesh or mpp table
  754. *
  755. * @tbl: mesh or mpp path table
  756. * @sdata: local subif
  757. * @addr: dst address (ETH_ALEN length)
  758. *
  759. * Returns: 0 if successful
  760. */
  761. static int table_path_del(struct mesh_table *tbl,
  762. struct ieee80211_sub_if_data *sdata,
  763. const u8 *addr)
  764. {
  765. struct mesh_path *mpath;
  766. spin_lock_bh(&tbl->walk_lock);
  767. mpath = rhashtable_lookup_fast(&tbl->rhead, addr, mesh_rht_params);
  768. if (!mpath) {
  769. spin_unlock_bh(&tbl->walk_lock);
  770. return -ENXIO;
  771. }
  772. __mesh_path_del(tbl, mpath);
  773. spin_unlock_bh(&tbl->walk_lock);
  774. return 0;
  775. }
  776. /**
  777. * mesh_path_del - delete a mesh path from the table
  778. *
  779. * @addr: dst address (ETH_ALEN length)
  780. * @sdata: local subif
  781. *
  782. * Returns: 0 if successful
  783. */
  784. int mesh_path_del(struct ieee80211_sub_if_data *sdata, const u8 *addr)
  785. {
  786. int err;
  787. /* flush relevant mpp entries first */
  788. mpp_flush_by_proxy(sdata, addr);
  789. err = table_path_del(&sdata->u.mesh.mesh_paths, sdata, addr);
  790. sdata->u.mesh.mesh_paths_generation++;
  791. return err;
  792. }
  793. /**
  794. * mesh_path_tx_pending - sends pending frames in a mesh path queue
  795. *
  796. * @mpath: mesh path to activate
  797. *
  798. * Locking: the state_lock of the mpath structure must NOT be held when calling
  799. * this function.
  800. */
  801. void mesh_path_tx_pending(struct mesh_path *mpath)
  802. {
  803. if (mpath->flags & MESH_PATH_ACTIVE)
  804. ieee80211_add_pending_skbs(mpath->sdata->local,
  805. &mpath->frame_queue);
  806. }
  807. /**
  808. * mesh_path_send_to_gates - sends pending frames to all known mesh gates
  809. *
  810. * @mpath: mesh path whose queue will be emptied
  811. *
  812. * If there is only one gate, the frames are transferred from the failed mpath
  813. * queue to that gate's queue. If there are more than one gates, the frames
  814. * are copied from each gate to the next. After frames are copied, the
  815. * mpath queues are emptied onto the transmission queue.
  816. *
  817. * Returns: 0 on success, -EHOSTUNREACH
  818. */
  819. int mesh_path_send_to_gates(struct mesh_path *mpath)
  820. {
  821. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  822. struct mesh_table *tbl;
  823. struct mesh_path *from_mpath = mpath;
  824. struct mesh_path *gate;
  825. bool copy = false;
  826. tbl = &sdata->u.mesh.mesh_paths;
  827. rcu_read_lock();
  828. hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) {
  829. if (gate->flags & MESH_PATH_ACTIVE) {
  830. mpath_dbg(sdata, "Forwarding to %pM\n", gate->dst);
  831. mesh_path_move_to_queue(gate, from_mpath, copy);
  832. from_mpath = gate;
  833. copy = true;
  834. } else {
  835. mpath_dbg(sdata,
  836. "Not forwarding to %pM (flags %#x)\n",
  837. gate->dst, gate->flags);
  838. }
  839. }
  840. hlist_for_each_entry_rcu(gate, &tbl->known_gates, gate_list) {
  841. mpath_dbg(sdata, "Sending to %pM\n", gate->dst);
  842. mesh_path_tx_pending(gate);
  843. }
  844. rcu_read_unlock();
  845. return (from_mpath == mpath) ? -EHOSTUNREACH : 0;
  846. }
  847. /**
  848. * mesh_path_discard_frame - discard a frame whose path could not be resolved
  849. *
  850. * @skb: frame to discard
  851. * @sdata: network subif the frame was to be sent through
  852. *
  853. * Locking: the function must me called within a rcu_read_lock region
  854. */
  855. void mesh_path_discard_frame(struct ieee80211_sub_if_data *sdata,
  856. struct sk_buff *skb)
  857. {
  858. ieee80211_free_txskb(&sdata->local->hw, skb);
  859. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  860. }
  861. /**
  862. * mesh_path_flush_pending - free the pending queue of a mesh path
  863. *
  864. * @mpath: mesh path whose queue has to be freed
  865. *
  866. * Locking: the function must me called within a rcu_read_lock region
  867. */
  868. void mesh_path_flush_pending(struct mesh_path *mpath)
  869. {
  870. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  871. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  872. struct mesh_preq_queue *preq, *tmp;
  873. struct sk_buff *skb;
  874. while ((skb = skb_dequeue(&mpath->frame_queue)) != NULL)
  875. mesh_path_discard_frame(mpath->sdata, skb);
  876. spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
  877. list_for_each_entry_safe(preq, tmp, &ifmsh->preq_queue.list, list) {
  878. if (ether_addr_equal(mpath->dst, preq->dst)) {
  879. list_del(&preq->list);
  880. kfree(preq);
  881. --ifmsh->preq_queue_len;
  882. }
  883. }
  884. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  885. }
  886. /**
  887. * mesh_path_fix_nexthop - force a specific next hop for a mesh path
  888. *
  889. * @mpath: the mesh path to modify
  890. * @next_hop: the next hop to force
  891. *
  892. * Locking: this function must be called holding mpath->state_lock
  893. */
  894. void mesh_path_fix_nexthop(struct mesh_path *mpath, struct sta_info *next_hop)
  895. {
  896. spin_lock_bh(&mpath->state_lock);
  897. mesh_path_assign_nexthop(mpath, next_hop);
  898. mpath->sn = 0xffff;
  899. mpath->metric = 0;
  900. mpath->hop_count = 0;
  901. mpath->exp_time = 0;
  902. mpath->flags = MESH_PATH_FIXED | MESH_PATH_SN_VALID;
  903. mesh_path_activate(mpath);
  904. mesh_fast_tx_flush_mpath(mpath);
  905. spin_unlock_bh(&mpath->state_lock);
  906. ewma_mesh_fail_avg_init(&next_hop->mesh->fail_avg);
  907. /* init it at a low value - 0 start is tricky */
  908. ewma_mesh_fail_avg_add(&next_hop->mesh->fail_avg, 1);
  909. mesh_path_tx_pending(mpath);
  910. }
  911. void mesh_pathtbl_init(struct ieee80211_sub_if_data *sdata)
  912. {
  913. mesh_table_init(&sdata->u.mesh.mesh_paths);
  914. mesh_table_init(&sdata->u.mesh.mpp_paths);
  915. mesh_fast_tx_init(sdata);
  916. }
  917. static
  918. void mesh_path_tbl_expire(struct ieee80211_sub_if_data *sdata,
  919. struct mesh_table *tbl)
  920. {
  921. struct mesh_path *mpath;
  922. struct hlist_node *n;
  923. spin_lock_bh(&tbl->walk_lock);
  924. hlist_for_each_entry_safe(mpath, n, &tbl->walk_head, walk_list) {
  925. if ((!(mpath->flags & MESH_PATH_RESOLVING)) &&
  926. (!(mpath->flags & MESH_PATH_FIXED)) &&
  927. time_after(jiffies, mpath->exp_time + MESH_PATH_EXPIRE))
  928. __mesh_path_del(tbl, mpath);
  929. }
  930. spin_unlock_bh(&tbl->walk_lock);
  931. }
  932. void mesh_path_expire(struct ieee80211_sub_if_data *sdata)
  933. {
  934. mesh_path_tbl_expire(sdata, &sdata->u.mesh.mesh_paths);
  935. mesh_path_tbl_expire(sdata, &sdata->u.mesh.mpp_paths);
  936. }
  937. void mesh_pathtbl_unregister(struct ieee80211_sub_if_data *sdata)
  938. {
  939. mesh_fast_tx_deinit(sdata);
  940. mesh_table_free(&sdata->u.mesh.mesh_paths);
  941. mesh_table_free(&sdata->u.mesh.mpp_paths);
  942. }