mesh_hwmp.c 36 KB

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