mesh_hwmp.c 38 KB

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