mesh_hwmp.c 36 KB

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