mesh_hwmp.c 38 KB

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