mesh_hwmp.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2008, 2009 open80211s Ltd.
  4. * Copyright (C) 2019, 2021-2023 Intel Corporation
  5. * Author: Luis Carlos Cobo <luisca@cozybit.com>
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/etherdevice.h>
  9. #include <linux/unaligned.h>
  10. #include "wme.h"
  11. #include "mesh.h"
  12. #define TEST_FRAME_LEN 8192
  13. #define MAX_METRIC 0xffffffff
  14. #define ARITH_SHIFT 8
  15. #define LINK_FAIL_THRESH 95
  16. #define MAX_PREQ_QUEUE_LEN 64
  17. static void mesh_queue_preq(struct mesh_path *, u8);
  18. static inline u32 u32_field_get(const u8 *preq_elem, int offset, bool ae)
  19. {
  20. if (ae)
  21. offset += 6;
  22. return get_unaligned_le32(preq_elem + offset);
  23. }
  24. static inline u16 u16_field_get(const u8 *preq_elem, int offset, bool ae)
  25. {
  26. if (ae)
  27. offset += 6;
  28. return get_unaligned_le16(preq_elem + offset);
  29. }
  30. /* HWMP IE processing macros */
  31. #define AE_F (1<<6)
  32. #define AE_F_SET(x) (*x & AE_F)
  33. #define PREQ_IE_FLAGS(x) (*(x))
  34. #define PREQ_IE_HOPCOUNT(x) (*(x + 1))
  35. #define PREQ_IE_TTL(x) (*(x + 2))
  36. #define PREQ_IE_PREQ_ID(x) u32_field_get(x, 3, 0)
  37. #define PREQ_IE_ORIG_ADDR(x) (x + 7)
  38. #define PREQ_IE_ORIG_SN(x) u32_field_get(x, 13, 0)
  39. #define PREQ_IE_LIFETIME(x) u32_field_get(x, 17, AE_F_SET(x))
  40. #define PREQ_IE_METRIC(x) u32_field_get(x, 21, AE_F_SET(x))
  41. #define PREQ_IE_TARGET_F(x) (*(AE_F_SET(x) ? x + 32 : x + 26))
  42. #define PREQ_IE_TARGET_ADDR(x) (AE_F_SET(x) ? x + 33 : x + 27)
  43. #define PREQ_IE_TARGET_SN(x) u32_field_get(x, 33, AE_F_SET(x))
  44. #define PREP_IE_FLAGS(x) PREQ_IE_FLAGS(x)
  45. #define PREP_IE_HOPCOUNT(x) PREQ_IE_HOPCOUNT(x)
  46. #define PREP_IE_TTL(x) PREQ_IE_TTL(x)
  47. #define PREP_IE_ORIG_ADDR(x) (AE_F_SET(x) ? x + 27 : x + 21)
  48. #define PREP_IE_ORIG_SN(x) u32_field_get(x, 27, AE_F_SET(x))
  49. #define PREP_IE_LIFETIME(x) u32_field_get(x, 13, AE_F_SET(x))
  50. #define PREP_IE_METRIC(x) u32_field_get(x, 17, AE_F_SET(x))
  51. #define PREP_IE_TARGET_ADDR(x) (x + 3)
  52. #define PREP_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
  53. #define PERR_IE_TTL(x) (*(x))
  54. #define PERR_IE_TARGET_FLAGS(x) (*(x + 2))
  55. #define PERR_IE_TARGET_ADDR(x) (x + 3)
  56. #define PERR_IE_TARGET_SN(x) u32_field_get(x, 9, 0)
  57. #define PERR_IE_TARGET_RCODE(x) u16_field_get(x, 13, 0)
  58. #define MSEC_TO_TU(x) (x*1000/1024)
  59. #define SN_GT(x, y) ((s32)(y - x) < 0)
  60. #define SN_LT(x, y) ((s32)(x - y) < 0)
  61. #define MAX_SANE_SN_DELTA 32
  62. static inline u32 SN_DELTA(u32 x, u32 y)
  63. {
  64. return x >= y ? x - y : y - x;
  65. }
  66. #define net_traversal_jiffies(s) \
  67. msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime)
  68. #define default_lifetime(s) \
  69. MSEC_TO_TU(s->u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout)
  70. #define min_preq_int_jiff(s) \
  71. (msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval))
  72. #define max_preq_retries(s) (s->u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries)
  73. #define disc_timeout_jiff(s) \
  74. msecs_to_jiffies(sdata->u.mesh.mshcfg.min_discovery_timeout)
  75. #define root_path_confirmation_jiffies(s) \
  76. msecs_to_jiffies(sdata->u.mesh.mshcfg.dot11MeshHWMPconfirmationInterval)
  77. enum mpath_frame_type {
  78. MPATH_PREQ = 0,
  79. MPATH_PREP,
  80. MPATH_PERR,
  81. MPATH_RANN
  82. };
  83. static const u8 broadcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  84. static int mesh_path_sel_frame_tx(enum mpath_frame_type action, u8 flags,
  85. const u8 *orig_addr, u32 orig_sn,
  86. u8 target_flags, const u8 *target,
  87. u32 target_sn, const u8 *da,
  88. u8 hop_count, u8 ttl,
  89. u32 lifetime, u32 metric, u32 preq_id,
  90. struct ieee80211_sub_if_data *sdata)
  91. {
  92. struct ieee80211_local *local = sdata->local;
  93. struct sk_buff *skb;
  94. struct ieee80211_mgmt *mgmt;
  95. u8 *pos, ie_len;
  96. int hdr_len = offsetofend(struct ieee80211_mgmt,
  97. u.action.u.mesh_action);
  98. skb = dev_alloc_skb(local->tx_headroom +
  99. hdr_len +
  100. 2 + 37); /* max HWMP IE */
  101. if (!skb)
  102. return -1;
  103. skb_reserve(skb, local->tx_headroom);
  104. mgmt = skb_put_zero(skb, hdr_len);
  105. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  106. IEEE80211_STYPE_ACTION);
  107. memcpy(mgmt->da, da, ETH_ALEN);
  108. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  109. /* BSSID == SA */
  110. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  111. mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
  112. mgmt->u.action.u.mesh_action.action_code =
  113. WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
  114. switch (action) {
  115. case MPATH_PREQ:
  116. mhwmp_dbg(sdata, "sending PREQ to %pM\n", target);
  117. ie_len = 37;
  118. pos = skb_put(skb, 2 + ie_len);
  119. *pos++ = WLAN_EID_PREQ;
  120. break;
  121. case MPATH_PREP:
  122. mhwmp_dbg(sdata, "sending PREP to %pM\n", orig_addr);
  123. ie_len = 31;
  124. pos = skb_put(skb, 2 + ie_len);
  125. *pos++ = WLAN_EID_PREP;
  126. break;
  127. case MPATH_RANN:
  128. mhwmp_dbg(sdata, "sending RANN from %pM\n", orig_addr);
  129. ie_len = sizeof(struct ieee80211_rann_ie);
  130. pos = skb_put(skb, 2 + ie_len);
  131. *pos++ = WLAN_EID_RANN;
  132. break;
  133. default:
  134. kfree_skb(skb);
  135. return -EOPNOTSUPP;
  136. }
  137. *pos++ = ie_len;
  138. *pos++ = flags;
  139. *pos++ = hop_count;
  140. *pos++ = ttl;
  141. if (action == MPATH_PREP) {
  142. memcpy(pos, target, ETH_ALEN);
  143. pos += ETH_ALEN;
  144. put_unaligned_le32(target_sn, pos);
  145. pos += 4;
  146. } else {
  147. if (action == MPATH_PREQ) {
  148. put_unaligned_le32(preq_id, pos);
  149. pos += 4;
  150. }
  151. memcpy(pos, orig_addr, ETH_ALEN);
  152. pos += ETH_ALEN;
  153. put_unaligned_le32(orig_sn, pos);
  154. pos += 4;
  155. }
  156. put_unaligned_le32(lifetime, pos); /* interval for RANN */
  157. pos += 4;
  158. put_unaligned_le32(metric, pos);
  159. pos += 4;
  160. if (action == MPATH_PREQ) {
  161. *pos++ = 1; /* destination count */
  162. *pos++ = target_flags;
  163. memcpy(pos, target, ETH_ALEN);
  164. pos += ETH_ALEN;
  165. put_unaligned_le32(target_sn, pos);
  166. pos += 4;
  167. } else if (action == MPATH_PREP) {
  168. memcpy(pos, orig_addr, ETH_ALEN);
  169. pos += ETH_ALEN;
  170. put_unaligned_le32(orig_sn, pos);
  171. pos += 4;
  172. }
  173. ieee80211_tx_skb(sdata, skb);
  174. return 0;
  175. }
  176. /* Headroom is not adjusted. Caller should ensure that skb has sufficient
  177. * headroom in case the frame is encrypted. */
  178. static void prepare_frame_for_deferred_tx(struct ieee80211_sub_if_data *sdata,
  179. struct sk_buff *skb)
  180. {
  181. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  182. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  183. skb_reset_mac_header(skb);
  184. skb_reset_network_header(skb);
  185. skb_reset_transport_header(skb);
  186. /* Send all internal mgmt frames on VO. Accordingly set TID to 7. */
  187. skb_set_queue_mapping(skb, IEEE80211_AC_VO);
  188. skb->priority = 7;
  189. info->control.vif = &sdata->vif;
  190. info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
  191. ieee80211_set_qos_hdr(sdata, skb);
  192. ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
  193. }
  194. /**
  195. * mesh_path_error_tx - Sends a PERR mesh management frame
  196. *
  197. * @ttl: allowed remaining hops
  198. * @target: broken destination
  199. * @target_sn: SN of the broken destination
  200. * @target_rcode: reason code for this PERR
  201. * @ra: node this frame is addressed to
  202. * @sdata: local mesh subif
  203. *
  204. * Note: This function may be called with driver locks taken that the driver
  205. * also acquires in the TX path. To avoid a deadlock we don't transmit the
  206. * frame directly but add it to the pending queue instead.
  207. *
  208. * Returns: 0 on success
  209. */
  210. int mesh_path_error_tx(struct ieee80211_sub_if_data *sdata,
  211. u8 ttl, const u8 *target, u32 target_sn,
  212. u16 target_rcode, const u8 *ra)
  213. {
  214. struct ieee80211_local *local = sdata->local;
  215. struct sk_buff *skb;
  216. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  217. struct ieee80211_mgmt *mgmt;
  218. u8 *pos, ie_len;
  219. int hdr_len = offsetofend(struct ieee80211_mgmt,
  220. u.action.u.mesh_action);
  221. if (time_before(jiffies, ifmsh->next_perr))
  222. return -EAGAIN;
  223. skb = dev_alloc_skb(local->tx_headroom +
  224. IEEE80211_ENCRYPT_HEADROOM +
  225. IEEE80211_ENCRYPT_TAILROOM +
  226. hdr_len +
  227. 2 + 15 /* PERR IE */);
  228. if (!skb)
  229. return -1;
  230. skb_reserve(skb, local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM);
  231. mgmt = skb_put_zero(skb, hdr_len);
  232. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  233. IEEE80211_STYPE_ACTION);
  234. memcpy(mgmt->da, ra, ETH_ALEN);
  235. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  236. /* BSSID == SA */
  237. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  238. mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
  239. mgmt->u.action.u.mesh_action.action_code =
  240. WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
  241. ie_len = 15;
  242. pos = skb_put(skb, 2 + ie_len);
  243. *pos++ = WLAN_EID_PERR;
  244. *pos++ = ie_len;
  245. /* ttl */
  246. *pos++ = ttl;
  247. /* number of destinations */
  248. *pos++ = 1;
  249. /* Flags field has AE bit only as defined in
  250. * sec 8.4.2.117 IEEE802.11-2012
  251. */
  252. *pos = 0;
  253. pos++;
  254. memcpy(pos, target, ETH_ALEN);
  255. pos += ETH_ALEN;
  256. put_unaligned_le32(target_sn, pos);
  257. pos += 4;
  258. put_unaligned_le16(target_rcode, pos);
  259. /* see note in function header */
  260. prepare_frame_for_deferred_tx(sdata, skb);
  261. ifmsh->next_perr = TU_TO_EXP_TIME(
  262. ifmsh->mshcfg.dot11MeshHWMPperrMinInterval);
  263. ieee80211_add_pending_skb(local, skb);
  264. return 0;
  265. }
  266. void ieee80211s_update_metric(struct ieee80211_local *local,
  267. struct sta_info *sta,
  268. struct ieee80211_tx_status *st)
  269. {
  270. struct ieee80211_tx_info *txinfo = st->info;
  271. int failed;
  272. struct rate_info rinfo;
  273. failed = !(txinfo->flags & IEEE80211_TX_STAT_ACK);
  274. /* moving average, scaled to 100.
  275. * feed failure as 100 and success as 0
  276. */
  277. ewma_mesh_fail_avg_add(&sta->mesh->fail_avg, failed * 100);
  278. if (ewma_mesh_fail_avg_read(&sta->mesh->fail_avg) >
  279. LINK_FAIL_THRESH)
  280. mesh_plink_broken(sta);
  281. /* use rate info set by the driver directly if present */
  282. if (st->n_rates)
  283. rinfo = sta->deflink.tx_stats.last_rate_info;
  284. else
  285. sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate, &rinfo);
  286. ewma_mesh_tx_rate_avg_add(&sta->mesh->tx_rate_avg,
  287. cfg80211_calculate_bitrate(&rinfo));
  288. }
  289. u32 airtime_link_metric_get(struct ieee80211_local *local,
  290. struct sta_info *sta)
  291. {
  292. /* This should be adjusted for each device */
  293. int device_constant = 1 << ARITH_SHIFT;
  294. int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT;
  295. int s_unit = 1 << ARITH_SHIFT;
  296. int rate, err;
  297. u32 tx_time, estimated_retx;
  298. u64 result;
  299. unsigned long fail_avg =
  300. ewma_mesh_fail_avg_read(&sta->mesh->fail_avg);
  301. if (sta->mesh->plink_state != NL80211_PLINK_ESTAB)
  302. return MAX_METRIC;
  303. /* Try to get rate based on HW/SW RC algorithm.
  304. * Rate is returned in units of Kbps, correct this
  305. * to comply with airtime calculation units
  306. * Round up in case we get rate < 100Kbps
  307. */
  308. rate = DIV_ROUND_UP(sta_get_expected_throughput(sta), 100);
  309. if (rate) {
  310. err = 0;
  311. } else {
  312. if (fail_avg > LINK_FAIL_THRESH)
  313. return MAX_METRIC;
  314. rate = ewma_mesh_tx_rate_avg_read(&sta->mesh->tx_rate_avg);
  315. if (WARN_ON(!rate))
  316. return MAX_METRIC;
  317. err = (fail_avg << ARITH_SHIFT) / 100;
  318. }
  319. /* bitrate is in units of 100 Kbps, while we need rate in units of
  320. * 1Mbps. This will be corrected on tx_time computation.
  321. */
  322. tx_time = (device_constant + 10 * test_frame_len / rate);
  323. estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err));
  324. result = ((u64)tx_time * estimated_retx) >> (2 * ARITH_SHIFT);
  325. return (u32)result;
  326. }
  327. /* Check that the first metric is at least 10% better than the second one */
  328. static bool is_metric_better(u32 x, u32 y)
  329. {
  330. return (x < y) && (x < (y - x / 10));
  331. }
  332. /**
  333. * hwmp_route_info_get - Update routing info to originator and transmitter
  334. *
  335. * @sdata: local mesh subif
  336. * @mgmt: mesh management frame
  337. * @hwmp_ie: hwmp information element (PREP or PREQ)
  338. * @action: type of hwmp ie
  339. *
  340. * This function updates the path routing information to the originator and the
  341. * transmitter of a HWMP PREQ or PREP frame.
  342. *
  343. * Returns: metric to frame originator or 0 if the frame should not be further
  344. * processed
  345. *
  346. * Notes: this function is the only place (besides user-provided info) where
  347. * path routing information is updated.
  348. */
  349. static u32 hwmp_route_info_get(struct ieee80211_sub_if_data *sdata,
  350. struct ieee80211_mgmt *mgmt,
  351. const u8 *hwmp_ie, enum mpath_frame_type action)
  352. {
  353. struct ieee80211_local *local = sdata->local;
  354. struct mesh_path *mpath;
  355. struct sta_info *sta;
  356. bool fresh_info;
  357. const u8 *orig_addr, *ta;
  358. u32 orig_sn, orig_metric;
  359. unsigned long orig_lifetime, exp_time;
  360. u32 last_hop_metric, new_metric;
  361. bool flush_mpath = false;
  362. bool process = true;
  363. u8 hopcount;
  364. rcu_read_lock();
  365. sta = sta_info_get(sdata, mgmt->sa);
  366. if (!sta) {
  367. rcu_read_unlock();
  368. return 0;
  369. }
  370. last_hop_metric = airtime_link_metric_get(local, sta);
  371. /* Update and check originator routing info */
  372. fresh_info = true;
  373. switch (action) {
  374. case MPATH_PREQ:
  375. orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie);
  376. orig_sn = PREQ_IE_ORIG_SN(hwmp_ie);
  377. orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie);
  378. orig_metric = PREQ_IE_METRIC(hwmp_ie);
  379. hopcount = PREQ_IE_HOPCOUNT(hwmp_ie) + 1;
  380. break;
  381. case MPATH_PREP:
  382. /* Originator here refers to the MP that was the target in the
  383. * Path Request. We divert from the nomenclature in the draft
  384. * so that we can easily use a single function to gather path
  385. * information from both PREQ and PREP frames.
  386. */
  387. orig_addr = PREP_IE_TARGET_ADDR(hwmp_ie);
  388. orig_sn = PREP_IE_TARGET_SN(hwmp_ie);
  389. orig_lifetime = PREP_IE_LIFETIME(hwmp_ie);
  390. orig_metric = PREP_IE_METRIC(hwmp_ie);
  391. hopcount = PREP_IE_HOPCOUNT(hwmp_ie) + 1;
  392. break;
  393. default:
  394. rcu_read_unlock();
  395. return 0;
  396. }
  397. new_metric = orig_metric + last_hop_metric;
  398. if (new_metric < orig_metric)
  399. new_metric = MAX_METRIC;
  400. exp_time = TU_TO_EXP_TIME(orig_lifetime);
  401. if (ether_addr_equal(orig_addr, sdata->vif.addr)) {
  402. /* This MP is the originator, we are not interested in this
  403. * frame, except for updating transmitter's path info.
  404. */
  405. process = false;
  406. fresh_info = false;
  407. } else {
  408. mpath = mesh_path_lookup(sdata, orig_addr);
  409. if (mpath) {
  410. spin_lock_bh(&mpath->state_lock);
  411. if (mpath->flags & MESH_PATH_FIXED)
  412. fresh_info = false;
  413. else if ((mpath->flags & MESH_PATH_ACTIVE) &&
  414. (mpath->flags & MESH_PATH_SN_VALID)) {
  415. if (SN_GT(mpath->sn, orig_sn) ||
  416. (mpath->sn == orig_sn &&
  417. (rcu_access_pointer(mpath->next_hop) !=
  418. sta ?
  419. !is_metric_better(new_metric, mpath->metric) :
  420. new_metric >= mpath->metric))) {
  421. process = false;
  422. fresh_info = false;
  423. }
  424. } else if (!(mpath->flags & MESH_PATH_ACTIVE)) {
  425. bool have_sn, newer_sn, bounced;
  426. have_sn = mpath->flags & MESH_PATH_SN_VALID;
  427. newer_sn = have_sn && SN_GT(orig_sn, mpath->sn);
  428. bounced = have_sn &&
  429. (SN_DELTA(orig_sn, mpath->sn) >
  430. MAX_SANE_SN_DELTA);
  431. if (!have_sn || newer_sn) {
  432. /* if SN is newer than what we had
  433. * then we can take it */;
  434. } else if (bounced) {
  435. /* if SN is way different than what
  436. * we had then assume the other side
  437. * rebooted or restarted */;
  438. } else {
  439. process = false;
  440. fresh_info = false;
  441. }
  442. }
  443. } else {
  444. mpath = mesh_path_add(sdata, orig_addr);
  445. if (IS_ERR(mpath)) {
  446. rcu_read_unlock();
  447. return 0;
  448. }
  449. spin_lock_bh(&mpath->state_lock);
  450. }
  451. if (fresh_info) {
  452. if (rcu_access_pointer(mpath->next_hop) != sta) {
  453. mpath->path_change_count++;
  454. flush_mpath = true;
  455. }
  456. mesh_path_assign_nexthop(mpath, sta);
  457. mpath->flags |= MESH_PATH_SN_VALID;
  458. mpath->metric = new_metric;
  459. mpath->sn = orig_sn;
  460. mpath->exp_time = time_after(mpath->exp_time, exp_time)
  461. ? mpath->exp_time : exp_time;
  462. mpath->hop_count = hopcount;
  463. mesh_path_activate(mpath);
  464. spin_unlock_bh(&mpath->state_lock);
  465. if (flush_mpath)
  466. mesh_fast_tx_flush_mpath(mpath);
  467. ewma_mesh_fail_avg_init(&sta->mesh->fail_avg);
  468. /* init it at a low value - 0 start is tricky */
  469. ewma_mesh_fail_avg_add(&sta->mesh->fail_avg, 1);
  470. mesh_path_tx_pending(mpath);
  471. /* draft says preq_id should be saved to, but there does
  472. * not seem to be any use for it, skipping by now
  473. */
  474. } else
  475. spin_unlock_bh(&mpath->state_lock);
  476. }
  477. /* Update and check transmitter routing info */
  478. ta = mgmt->sa;
  479. if (ether_addr_equal(orig_addr, ta))
  480. fresh_info = false;
  481. else {
  482. fresh_info = true;
  483. mpath = mesh_path_lookup(sdata, ta);
  484. if (mpath) {
  485. spin_lock_bh(&mpath->state_lock);
  486. if ((mpath->flags & MESH_PATH_FIXED) ||
  487. ((mpath->flags & MESH_PATH_ACTIVE) &&
  488. ((rcu_access_pointer(mpath->next_hop) != sta ?
  489. !is_metric_better(last_hop_metric, mpath->metric) :
  490. last_hop_metric > mpath->metric))))
  491. fresh_info = false;
  492. } else {
  493. mpath = mesh_path_add(sdata, ta);
  494. if (IS_ERR(mpath)) {
  495. rcu_read_unlock();
  496. return 0;
  497. }
  498. spin_lock_bh(&mpath->state_lock);
  499. }
  500. if (fresh_info) {
  501. if (rcu_access_pointer(mpath->next_hop) != sta) {
  502. mpath->path_change_count++;
  503. flush_mpath = true;
  504. }
  505. mesh_path_assign_nexthop(mpath, sta);
  506. mpath->metric = last_hop_metric;
  507. mpath->exp_time = time_after(mpath->exp_time, exp_time)
  508. ? mpath->exp_time : exp_time;
  509. mpath->hop_count = 1;
  510. mesh_path_activate(mpath);
  511. spin_unlock_bh(&mpath->state_lock);
  512. if (flush_mpath)
  513. mesh_fast_tx_flush_mpath(mpath);
  514. ewma_mesh_fail_avg_init(&sta->mesh->fail_avg);
  515. /* init it at a low value - 0 start is tricky */
  516. ewma_mesh_fail_avg_add(&sta->mesh->fail_avg, 1);
  517. mesh_path_tx_pending(mpath);
  518. } else
  519. spin_unlock_bh(&mpath->state_lock);
  520. }
  521. rcu_read_unlock();
  522. return process ? new_metric : 0;
  523. }
  524. static void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata,
  525. struct ieee80211_mgmt *mgmt,
  526. const u8 *preq_elem, u32 orig_metric)
  527. {
  528. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  529. struct mesh_path *mpath = NULL;
  530. const u8 *target_addr, *orig_addr;
  531. const u8 *da;
  532. u8 target_flags, ttl, flags;
  533. u32 orig_sn, target_sn, lifetime, target_metric = 0;
  534. bool reply = false;
  535. bool forward = true;
  536. bool root_is_gate;
  537. /* Update target SN, if present */
  538. target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
  539. orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
  540. target_sn = PREQ_IE_TARGET_SN(preq_elem);
  541. orig_sn = PREQ_IE_ORIG_SN(preq_elem);
  542. target_flags = PREQ_IE_TARGET_F(preq_elem);
  543. /* Proactive PREQ gate announcements */
  544. flags = PREQ_IE_FLAGS(preq_elem);
  545. root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
  546. mhwmp_dbg(sdata, "received PREQ from %pM\n", orig_addr);
  547. if (ether_addr_equal(target_addr, sdata->vif.addr)) {
  548. mhwmp_dbg(sdata, "PREQ is for us\n");
  549. forward = false;
  550. reply = true;
  551. target_metric = 0;
  552. if (SN_GT(target_sn, ifmsh->sn))
  553. ifmsh->sn = target_sn;
  554. if (time_after(jiffies, ifmsh->last_sn_update +
  555. net_traversal_jiffies(sdata)) ||
  556. time_before(jiffies, ifmsh->last_sn_update)) {
  557. ++ifmsh->sn;
  558. ifmsh->last_sn_update = jiffies;
  559. }
  560. target_sn = ifmsh->sn;
  561. } else if (is_broadcast_ether_addr(target_addr) &&
  562. (target_flags & IEEE80211_PREQ_TO_FLAG)) {
  563. rcu_read_lock();
  564. mpath = mesh_path_lookup(sdata, orig_addr);
  565. if (mpath) {
  566. if (flags & IEEE80211_PREQ_PROACTIVE_PREP_FLAG) {
  567. reply = true;
  568. target_addr = sdata->vif.addr;
  569. target_sn = ++ifmsh->sn;
  570. target_metric = 0;
  571. ifmsh->last_sn_update = jiffies;
  572. }
  573. if (root_is_gate)
  574. mesh_path_add_gate(mpath);
  575. }
  576. rcu_read_unlock();
  577. } else if (ifmsh->mshcfg.dot11MeshForwarding) {
  578. rcu_read_lock();
  579. mpath = mesh_path_lookup(sdata, target_addr);
  580. if (mpath) {
  581. if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
  582. SN_LT(mpath->sn, target_sn)) {
  583. mpath->sn = target_sn;
  584. mpath->flags |= MESH_PATH_SN_VALID;
  585. } else if ((!(target_flags & IEEE80211_PREQ_TO_FLAG)) &&
  586. (mpath->flags & MESH_PATH_ACTIVE)) {
  587. reply = true;
  588. target_metric = mpath->metric;
  589. target_sn = mpath->sn;
  590. /* Case E2 of sec 13.10.9.3 IEEE 802.11-2012*/
  591. target_flags |= IEEE80211_PREQ_TO_FLAG;
  592. }
  593. }
  594. rcu_read_unlock();
  595. } else {
  596. forward = false;
  597. }
  598. if (reply) {
  599. lifetime = PREQ_IE_LIFETIME(preq_elem);
  600. ttl = ifmsh->mshcfg.element_ttl;
  601. if (ttl != 0) {
  602. mhwmp_dbg(sdata, "replying to the PREQ\n");
  603. mesh_path_sel_frame_tx(MPATH_PREP, 0, orig_addr,
  604. orig_sn, 0, target_addr,
  605. target_sn, mgmt->sa, 0, ttl,
  606. lifetime, target_metric, 0,
  607. sdata);
  608. } else {
  609. ifmsh->mshstats.dropped_frames_ttl++;
  610. }
  611. }
  612. if (forward) {
  613. u32 preq_id;
  614. u8 hopcount;
  615. ttl = PREQ_IE_TTL(preq_elem);
  616. lifetime = PREQ_IE_LIFETIME(preq_elem);
  617. if (ttl <= 1) {
  618. ifmsh->mshstats.dropped_frames_ttl++;
  619. return;
  620. }
  621. mhwmp_dbg(sdata, "forwarding the PREQ from %pM\n", orig_addr);
  622. --ttl;
  623. preq_id = PREQ_IE_PREQ_ID(preq_elem);
  624. hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
  625. da = (mpath && mpath->is_root) ?
  626. mpath->rann_snd_addr : broadcast_addr;
  627. if (flags & IEEE80211_PREQ_PROACTIVE_PREP_FLAG) {
  628. target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
  629. target_sn = PREQ_IE_TARGET_SN(preq_elem);
  630. }
  631. mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
  632. orig_sn, target_flags, target_addr,
  633. target_sn, da, hopcount, ttl, lifetime,
  634. orig_metric, preq_id, sdata);
  635. if (!is_multicast_ether_addr(da))
  636. ifmsh->mshstats.fwded_unicast++;
  637. else
  638. ifmsh->mshstats.fwded_mcast++;
  639. ifmsh->mshstats.fwded_frames++;
  640. }
  641. }
  642. static inline struct sta_info *
  643. next_hop_deref_protected(struct mesh_path *mpath)
  644. {
  645. return rcu_dereference_protected(mpath->next_hop,
  646. lockdep_is_held(&mpath->state_lock));
  647. }
  648. static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
  649. struct ieee80211_mgmt *mgmt,
  650. const u8 *prep_elem, u32 metric)
  651. {
  652. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  653. struct mesh_path *mpath;
  654. const u8 *target_addr, *orig_addr;
  655. u8 ttl, hopcount, flags;
  656. u8 next_hop[ETH_ALEN];
  657. u32 target_sn, orig_sn, lifetime;
  658. mhwmp_dbg(sdata, "received PREP from %pM\n",
  659. PREP_IE_TARGET_ADDR(prep_elem));
  660. orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
  661. if (ether_addr_equal(orig_addr, sdata->vif.addr))
  662. /* destination, no forwarding required */
  663. return;
  664. if (!ifmsh->mshcfg.dot11MeshForwarding)
  665. return;
  666. ttl = PREP_IE_TTL(prep_elem);
  667. if (ttl <= 1) {
  668. sdata->u.mesh.mshstats.dropped_frames_ttl++;
  669. return;
  670. }
  671. rcu_read_lock();
  672. mpath = mesh_path_lookup(sdata, orig_addr);
  673. if (mpath)
  674. spin_lock_bh(&mpath->state_lock);
  675. else
  676. goto fail;
  677. if (!(mpath->flags & MESH_PATH_ACTIVE)) {
  678. spin_unlock_bh(&mpath->state_lock);
  679. goto fail;
  680. }
  681. memcpy(next_hop, next_hop_deref_protected(mpath)->sta.addr, ETH_ALEN);
  682. spin_unlock_bh(&mpath->state_lock);
  683. --ttl;
  684. flags = PREP_IE_FLAGS(prep_elem);
  685. lifetime = PREP_IE_LIFETIME(prep_elem);
  686. hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
  687. target_addr = PREP_IE_TARGET_ADDR(prep_elem);
  688. target_sn = PREP_IE_TARGET_SN(prep_elem);
  689. orig_sn = PREP_IE_ORIG_SN(prep_elem);
  690. mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr, orig_sn, 0,
  691. target_addr, target_sn, next_hop, hopcount,
  692. ttl, lifetime, metric, 0, sdata);
  693. rcu_read_unlock();
  694. sdata->u.mesh.mshstats.fwded_unicast++;
  695. sdata->u.mesh.mshstats.fwded_frames++;
  696. return;
  697. fail:
  698. rcu_read_unlock();
  699. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  700. }
  701. static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
  702. struct ieee80211_mgmt *mgmt,
  703. const u8 *perr_elem)
  704. {
  705. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  706. struct mesh_path *mpath;
  707. u8 ttl;
  708. const u8 *ta, *target_addr;
  709. u32 target_sn;
  710. u16 target_rcode;
  711. ta = mgmt->sa;
  712. ttl = PERR_IE_TTL(perr_elem);
  713. if (ttl <= 1) {
  714. ifmsh->mshstats.dropped_frames_ttl++;
  715. return;
  716. }
  717. ttl--;
  718. target_addr = PERR_IE_TARGET_ADDR(perr_elem);
  719. target_sn = PERR_IE_TARGET_SN(perr_elem);
  720. target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
  721. rcu_read_lock();
  722. mpath = mesh_path_lookup(sdata, target_addr);
  723. if (mpath) {
  724. struct sta_info *sta;
  725. spin_lock_bh(&mpath->state_lock);
  726. sta = next_hop_deref_protected(mpath);
  727. if (mpath->flags & MESH_PATH_ACTIVE &&
  728. ether_addr_equal(ta, sta->sta.addr) &&
  729. !(mpath->flags & MESH_PATH_FIXED) &&
  730. (!(mpath->flags & MESH_PATH_SN_VALID) ||
  731. SN_GT(target_sn, mpath->sn) || target_sn == 0)) {
  732. mpath->flags &= ~MESH_PATH_ACTIVE;
  733. if (target_sn != 0)
  734. mpath->sn = target_sn;
  735. else
  736. mpath->sn += 1;
  737. spin_unlock_bh(&mpath->state_lock);
  738. if (!ifmsh->mshcfg.dot11MeshForwarding)
  739. goto endperr;
  740. mesh_path_error_tx(sdata, ttl, target_addr,
  741. target_sn, target_rcode,
  742. broadcast_addr);
  743. } else
  744. spin_unlock_bh(&mpath->state_lock);
  745. }
  746. endperr:
  747. rcu_read_unlock();
  748. }
  749. static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
  750. struct ieee80211_mgmt *mgmt,
  751. const struct ieee80211_rann_ie *rann)
  752. {
  753. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  754. struct ieee80211_local *local = sdata->local;
  755. struct sta_info *sta;
  756. struct mesh_path *mpath;
  757. u8 ttl, flags, hopcount;
  758. const u8 *orig_addr;
  759. u32 orig_sn, new_metric, orig_metric, last_hop_metric, interval;
  760. bool root_is_gate;
  761. ttl = rann->rann_ttl;
  762. flags = rann->rann_flags;
  763. root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
  764. orig_addr = rann->rann_addr;
  765. orig_sn = le32_to_cpu(rann->rann_seq);
  766. interval = le32_to_cpu(rann->rann_interval);
  767. hopcount = rann->rann_hopcount;
  768. hopcount++;
  769. orig_metric = le32_to_cpu(rann->rann_metric);
  770. /* Ignore our own RANNs */
  771. if (ether_addr_equal(orig_addr, sdata->vif.addr))
  772. return;
  773. mhwmp_dbg(sdata,
  774. "received RANN from %pM via neighbour %pM (is_gate=%d)\n",
  775. orig_addr, mgmt->sa, root_is_gate);
  776. rcu_read_lock();
  777. sta = sta_info_get(sdata, mgmt->sa);
  778. if (!sta) {
  779. rcu_read_unlock();
  780. return;
  781. }
  782. last_hop_metric = airtime_link_metric_get(local, sta);
  783. new_metric = orig_metric + last_hop_metric;
  784. if (new_metric < orig_metric)
  785. new_metric = MAX_METRIC;
  786. mpath = mesh_path_lookup(sdata, orig_addr);
  787. if (!mpath) {
  788. mpath = mesh_path_add(sdata, orig_addr);
  789. if (IS_ERR(mpath)) {
  790. rcu_read_unlock();
  791. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  792. return;
  793. }
  794. }
  795. if (!(SN_LT(mpath->sn, orig_sn)) &&
  796. !(mpath->sn == orig_sn && new_metric < mpath->rann_metric)) {
  797. rcu_read_unlock();
  798. return;
  799. }
  800. if ((!(mpath->flags & (MESH_PATH_ACTIVE | MESH_PATH_RESOLVING)) ||
  801. (time_after(jiffies, mpath->last_preq_to_root +
  802. root_path_confirmation_jiffies(sdata)) ||
  803. time_before(jiffies, mpath->last_preq_to_root))) &&
  804. !(mpath->flags & MESH_PATH_FIXED) && (ttl != 0)) {
  805. mhwmp_dbg(sdata,
  806. "time to refresh root mpath %pM\n",
  807. orig_addr);
  808. mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
  809. mpath->last_preq_to_root = jiffies;
  810. }
  811. mpath->sn = orig_sn;
  812. mpath->rann_metric = new_metric;
  813. mpath->is_root = true;
  814. /* Recording RANNs sender address to send individually
  815. * addressed PREQs destined for root mesh STA */
  816. memcpy(mpath->rann_snd_addr, mgmt->sa, ETH_ALEN);
  817. if (root_is_gate)
  818. mesh_path_add_gate(mpath);
  819. if (ttl <= 1) {
  820. ifmsh->mshstats.dropped_frames_ttl++;
  821. rcu_read_unlock();
  822. return;
  823. }
  824. ttl--;
  825. if (ifmsh->mshcfg.dot11MeshForwarding) {
  826. mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
  827. orig_sn, 0, NULL, 0, broadcast_addr,
  828. hopcount, ttl, interval,
  829. new_metric, 0, sdata);
  830. }
  831. rcu_read_unlock();
  832. }
  833. void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
  834. struct ieee80211_mgmt *mgmt, size_t len)
  835. {
  836. struct ieee802_11_elems *elems;
  837. size_t baselen;
  838. u32 path_metric;
  839. struct sta_info *sta;
  840. /* need action_code */
  841. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  842. return;
  843. rcu_read_lock();
  844. sta = sta_info_get(sdata, mgmt->sa);
  845. if (!sta || sta->mesh->plink_state != NL80211_PLINK_ESTAB) {
  846. rcu_read_unlock();
  847. return;
  848. }
  849. rcu_read_unlock();
  850. baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
  851. elems = ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
  852. len - baselen, false, NULL);
  853. if (!elems)
  854. return;
  855. if (elems->preq) {
  856. if (elems->preq_len != 37)
  857. /* Right now we support just 1 destination and no AE */
  858. goto free;
  859. path_metric = hwmp_route_info_get(sdata, mgmt, elems->preq,
  860. MPATH_PREQ);
  861. if (path_metric)
  862. hwmp_preq_frame_process(sdata, mgmt, elems->preq,
  863. path_metric);
  864. }
  865. if (elems->prep) {
  866. if (elems->prep_len != 31)
  867. /* Right now we support no AE */
  868. goto free;
  869. path_metric = hwmp_route_info_get(sdata, mgmt, elems->prep,
  870. MPATH_PREP);
  871. if (path_metric)
  872. hwmp_prep_frame_process(sdata, mgmt, elems->prep,
  873. path_metric);
  874. }
  875. if (elems->perr) {
  876. if (elems->perr_len != 15)
  877. /* Right now we support only one destination per PERR */
  878. goto free;
  879. hwmp_perr_frame_process(sdata, mgmt, elems->perr);
  880. }
  881. if (elems->rann)
  882. hwmp_rann_frame_process(sdata, mgmt, elems->rann);
  883. free:
  884. kfree(elems);
  885. }
  886. /**
  887. * mesh_queue_preq - queue a PREQ to a given destination
  888. *
  889. * @mpath: mesh path to discover
  890. * @flags: special attributes of the PREQ to be sent
  891. *
  892. * Locking: the function must be called from within a rcu read lock block.
  893. *
  894. */
  895. static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
  896. {
  897. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  898. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  899. struct mesh_preq_queue *preq_node;
  900. preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
  901. if (!preq_node) {
  902. mhwmp_dbg(sdata, "could not allocate PREQ node\n");
  903. return;
  904. }
  905. spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
  906. if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
  907. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  908. kfree(preq_node);
  909. if (printk_ratelimit())
  910. mhwmp_dbg(sdata, "PREQ node queue full\n");
  911. return;
  912. }
  913. spin_lock(&mpath->state_lock);
  914. if (mpath->flags & MESH_PATH_REQ_QUEUED) {
  915. spin_unlock(&mpath->state_lock);
  916. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  917. kfree(preq_node);
  918. return;
  919. }
  920. memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
  921. preq_node->flags = flags;
  922. mpath->flags |= MESH_PATH_REQ_QUEUED;
  923. spin_unlock(&mpath->state_lock);
  924. list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
  925. ++ifmsh->preq_queue_len;
  926. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  927. if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
  928. wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
  929. else if (time_before(jiffies, ifmsh->last_preq)) {
  930. /* avoid long wait if did not send preqs for a long time
  931. * and jiffies wrapped around
  932. */
  933. ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
  934. wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
  935. } else
  936. mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
  937. min_preq_int_jiff(sdata));
  938. }
  939. /**
  940. * mesh_path_start_discovery - launch a path discovery from the PREQ queue
  941. *
  942. * @sdata: local mesh subif
  943. */
  944. void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
  945. {
  946. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  947. struct mesh_preq_queue *preq_node;
  948. struct mesh_path *mpath;
  949. u8 ttl, target_flags = 0;
  950. const u8 *da;
  951. u32 lifetime;
  952. spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
  953. if (!ifmsh->preq_queue_len ||
  954. time_before(jiffies, ifmsh->last_preq +
  955. min_preq_int_jiff(sdata))) {
  956. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  957. return;
  958. }
  959. preq_node = list_first_entry(&ifmsh->preq_queue.list,
  960. struct mesh_preq_queue, list);
  961. list_del(&preq_node->list);
  962. --ifmsh->preq_queue_len;
  963. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  964. rcu_read_lock();
  965. mpath = mesh_path_lookup(sdata, preq_node->dst);
  966. if (!mpath)
  967. goto enddiscovery;
  968. spin_lock_bh(&mpath->state_lock);
  969. if (mpath->flags & (MESH_PATH_DELETED | MESH_PATH_FIXED)) {
  970. spin_unlock_bh(&mpath->state_lock);
  971. goto enddiscovery;
  972. }
  973. mpath->flags &= ~MESH_PATH_REQ_QUEUED;
  974. if (preq_node->flags & PREQ_Q_F_START) {
  975. if (mpath->flags & MESH_PATH_RESOLVING) {
  976. spin_unlock_bh(&mpath->state_lock);
  977. goto enddiscovery;
  978. } else {
  979. mpath->flags &= ~MESH_PATH_RESOLVED;
  980. mpath->flags |= MESH_PATH_RESOLVING;
  981. mpath->discovery_retries = 0;
  982. mpath->discovery_timeout = disc_timeout_jiff(sdata);
  983. }
  984. } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
  985. mpath->flags & MESH_PATH_RESOLVED) {
  986. mpath->flags &= ~MESH_PATH_RESOLVING;
  987. spin_unlock_bh(&mpath->state_lock);
  988. goto enddiscovery;
  989. }
  990. ifmsh->last_preq = jiffies;
  991. if (time_after(jiffies, ifmsh->last_sn_update +
  992. net_traversal_jiffies(sdata)) ||
  993. time_before(jiffies, ifmsh->last_sn_update)) {
  994. ++ifmsh->sn;
  995. sdata->u.mesh.last_sn_update = jiffies;
  996. }
  997. lifetime = default_lifetime(sdata);
  998. ttl = sdata->u.mesh.mshcfg.element_ttl;
  999. if (ttl == 0) {
  1000. sdata->u.mesh.mshstats.dropped_frames_ttl++;
  1001. spin_unlock_bh(&mpath->state_lock);
  1002. goto enddiscovery;
  1003. }
  1004. if (preq_node->flags & PREQ_Q_F_REFRESH)
  1005. target_flags |= IEEE80211_PREQ_TO_FLAG;
  1006. else
  1007. target_flags &= ~IEEE80211_PREQ_TO_FLAG;
  1008. spin_unlock_bh(&mpath->state_lock);
  1009. da = (mpath->is_root) ? mpath->rann_snd_addr : broadcast_addr;
  1010. mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr, ifmsh->sn,
  1011. target_flags, mpath->dst, mpath->sn, da, 0,
  1012. ttl, lifetime, 0, ifmsh->preq_id++, sdata);
  1013. spin_lock_bh(&mpath->state_lock);
  1014. if (!(mpath->flags & MESH_PATH_DELETED))
  1015. mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
  1016. spin_unlock_bh(&mpath->state_lock);
  1017. enddiscovery:
  1018. rcu_read_unlock();
  1019. kfree(preq_node);
  1020. }
  1021. /**
  1022. * mesh_nexthop_resolve - lookup next hop; conditionally start path discovery
  1023. *
  1024. * @skb: 802.11 frame to be sent
  1025. * @sdata: network subif the frame will be sent through
  1026. *
  1027. * Lookup next hop for given skb and start path discovery if no
  1028. * forwarding information is found.
  1029. *
  1030. * Returns: 0 if the next hop was found and -ENOENT if the frame was queued.
  1031. * skb is freed here if no mpath could be allocated.
  1032. */
  1033. int mesh_nexthop_resolve(struct ieee80211_sub_if_data *sdata,
  1034. struct sk_buff *skb)
  1035. {
  1036. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1037. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1038. struct mesh_path *mpath;
  1039. struct sk_buff *skb_to_free = NULL;
  1040. u8 *target_addr = hdr->addr3;
  1041. /* Nulls are only sent to peers for PS and should be pre-addressed */
  1042. if (ieee80211_is_qos_nullfunc(hdr->frame_control))
  1043. return 0;
  1044. /* Allow injected packets to bypass mesh routing */
  1045. if (info->control.flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP)
  1046. return 0;
  1047. if (!mesh_nexthop_lookup(sdata, skb))
  1048. return 0;
  1049. /* no nexthop found, start resolving */
  1050. mpath = mesh_path_lookup(sdata, target_addr);
  1051. if (!mpath) {
  1052. mpath = mesh_path_add(sdata, target_addr);
  1053. if (IS_ERR(mpath)) {
  1054. mesh_path_discard_frame(sdata, skb);
  1055. return PTR_ERR(mpath);
  1056. }
  1057. }
  1058. if (!(mpath->flags & MESH_PATH_RESOLVING) &&
  1059. mesh_path_sel_is_hwmp(sdata))
  1060. mesh_queue_preq(mpath, PREQ_Q_F_START);
  1061. if (skb_queue_len(&mpath->frame_queue) >= MESH_FRAME_QUEUE_LEN)
  1062. skb_to_free = skb_dequeue(&mpath->frame_queue);
  1063. info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
  1064. ieee80211_set_qos_hdr(sdata, skb);
  1065. skb_queue_tail(&mpath->frame_queue, skb);
  1066. if (skb_to_free)
  1067. mesh_path_discard_frame(sdata, skb_to_free);
  1068. return -ENOENT;
  1069. }
  1070. /**
  1071. * mesh_nexthop_lookup_nolearn - try to set next hop without path discovery
  1072. * @skb: 802.11 frame to be sent
  1073. * @sdata: network subif the frame will be sent through
  1074. *
  1075. * Check if the meshDA (addr3) of a unicast frame is a direct neighbor.
  1076. * And if so, set the RA (addr1) to it to transmit to this node directly,
  1077. * avoiding PREQ/PREP path discovery.
  1078. *
  1079. * Returns: 0 if the next hop was found and -ENOENT otherwise.
  1080. */
  1081. static int mesh_nexthop_lookup_nolearn(struct ieee80211_sub_if_data *sdata,
  1082. struct sk_buff *skb)
  1083. {
  1084. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1085. struct sta_info *sta;
  1086. if (is_multicast_ether_addr(hdr->addr1))
  1087. return -ENOENT;
  1088. rcu_read_lock();
  1089. sta = sta_info_get(sdata, hdr->addr3);
  1090. if (!sta || sta->mesh->plink_state != NL80211_PLINK_ESTAB) {
  1091. rcu_read_unlock();
  1092. return -ENOENT;
  1093. }
  1094. rcu_read_unlock();
  1095. memcpy(hdr->addr1, hdr->addr3, ETH_ALEN);
  1096. memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
  1097. return 0;
  1098. }
  1099. void mesh_path_refresh(struct ieee80211_sub_if_data *sdata,
  1100. struct mesh_path *mpath, const u8 *addr)
  1101. {
  1102. if (mpath->flags & (MESH_PATH_REQ_QUEUED | MESH_PATH_FIXED |
  1103. MESH_PATH_RESOLVING))
  1104. return;
  1105. if (time_after(jiffies,
  1106. mpath->exp_time -
  1107. msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
  1108. (!addr || ether_addr_equal(sdata->vif.addr, addr)))
  1109. mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
  1110. }
  1111. /**
  1112. * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame. Calling
  1113. * this function is considered "using" the associated mpath, so preempt a path
  1114. * refresh if this mpath expires soon.
  1115. *
  1116. * @skb: 802.11 frame to be sent
  1117. * @sdata: network subif the frame will be sent through
  1118. *
  1119. * Returns: 0 if the next hop was found. Nonzero otherwise.
  1120. */
  1121. int mesh_nexthop_lookup(struct ieee80211_sub_if_data *sdata,
  1122. struct sk_buff *skb)
  1123. {
  1124. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  1125. struct mesh_path *mpath;
  1126. struct sta_info *next_hop;
  1127. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1128. u8 *target_addr = hdr->addr3;
  1129. if (ifmsh->mshcfg.dot11MeshNolearn &&
  1130. !mesh_nexthop_lookup_nolearn(sdata, skb))
  1131. return 0;
  1132. mpath = mesh_path_lookup(sdata, target_addr);
  1133. if (!mpath || !(mpath->flags & MESH_PATH_ACTIVE))
  1134. return -ENOENT;
  1135. mesh_path_refresh(sdata, mpath, hdr->addr4);
  1136. next_hop = rcu_dereference(mpath->next_hop);
  1137. if (next_hop) {
  1138. memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
  1139. memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
  1140. ieee80211_mps_set_frame_flags(sdata, next_hop, hdr);
  1141. if (ieee80211_hw_check(&sdata->local->hw, SUPPORT_FAST_XMIT))
  1142. mesh_fast_tx_cache(sdata, skb, mpath);
  1143. return 0;
  1144. }
  1145. return -ENOENT;
  1146. }
  1147. void mesh_path_timer(struct timer_list *t)
  1148. {
  1149. struct mesh_path *mpath = from_timer(mpath, t, timer);
  1150. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  1151. int ret;
  1152. if (sdata->local->quiescing)
  1153. return;
  1154. spin_lock_bh(&mpath->state_lock);
  1155. if (mpath->flags & MESH_PATH_RESOLVED ||
  1156. (!(mpath->flags & MESH_PATH_RESOLVING))) {
  1157. mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
  1158. spin_unlock_bh(&mpath->state_lock);
  1159. } else if (mpath->discovery_retries < max_preq_retries(sdata)) {
  1160. ++mpath->discovery_retries;
  1161. mpath->discovery_timeout *= 2;
  1162. mpath->flags &= ~MESH_PATH_REQ_QUEUED;
  1163. spin_unlock_bh(&mpath->state_lock);
  1164. mesh_queue_preq(mpath, 0);
  1165. } else {
  1166. mpath->flags &= ~(MESH_PATH_RESOLVING |
  1167. MESH_PATH_RESOLVED |
  1168. MESH_PATH_REQ_QUEUED);
  1169. mpath->exp_time = jiffies;
  1170. spin_unlock_bh(&mpath->state_lock);
  1171. if (!mpath->is_gate && mesh_gate_num(sdata) > 0) {
  1172. ret = mesh_path_send_to_gates(mpath);
  1173. if (ret)
  1174. mhwmp_dbg(sdata, "no gate was reachable\n");
  1175. } else
  1176. mesh_path_flush_pending(mpath);
  1177. }
  1178. }
  1179. void mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
  1180. {
  1181. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  1182. u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
  1183. u8 flags, target_flags = 0;
  1184. flags = (ifmsh->mshcfg.dot11MeshGateAnnouncementProtocol)
  1185. ? RANN_FLAG_IS_GATE : 0;
  1186. switch (ifmsh->mshcfg.dot11MeshHWMPRootMode) {
  1187. case IEEE80211_PROACTIVE_RANN:
  1188. mesh_path_sel_frame_tx(MPATH_RANN, flags, sdata->vif.addr,
  1189. ++ifmsh->sn, 0, NULL, 0, broadcast_addr,
  1190. 0, ifmsh->mshcfg.element_ttl,
  1191. interval, 0, 0, sdata);
  1192. break;
  1193. case IEEE80211_PROACTIVE_PREQ_WITH_PREP:
  1194. flags |= IEEE80211_PREQ_PROACTIVE_PREP_FLAG;
  1195. fallthrough;
  1196. case IEEE80211_PROACTIVE_PREQ_NO_PREP:
  1197. interval = ifmsh->mshcfg.dot11MeshHWMPactivePathToRootTimeout;
  1198. target_flags |= IEEE80211_PREQ_TO_FLAG |
  1199. IEEE80211_PREQ_USN_FLAG;
  1200. mesh_path_sel_frame_tx(MPATH_PREQ, flags, sdata->vif.addr,
  1201. ++ifmsh->sn, target_flags,
  1202. (u8 *) broadcast_addr, 0, broadcast_addr,
  1203. 0, ifmsh->mshcfg.element_ttl, interval,
  1204. 0, ifmsh->preq_id++, sdata);
  1205. break;
  1206. default:
  1207. mhwmp_dbg(sdata, "Proactive mechanism not supported\n");
  1208. return;
  1209. }
  1210. }