mesh_hwmp.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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. if (sta->mesh->plink_state != NL80211_PLINK_ESTAB)
  295. return MAX_METRIC;
  296. /* Try to get rate based on HW/SW RC algorithm.
  297. * Rate is returned in units of Kbps, correct this
  298. * to comply with airtime calculation units
  299. * Round up in case we get rate < 100Kbps
  300. */
  301. rate = DIV_ROUND_UP(sta_get_expected_throughput(sta), 100);
  302. if (rate) {
  303. err = 0;
  304. } else {
  305. if (fail_avg > LINK_FAIL_THRESH)
  306. return MAX_METRIC;
  307. sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate, &rinfo);
  308. rate = cfg80211_calculate_bitrate(&rinfo);
  309. if (WARN_ON(!rate))
  310. return MAX_METRIC;
  311. err = (fail_avg << ARITH_SHIFT) / 100;
  312. }
  313. /* bitrate is in units of 100 Kbps, while we need rate in units of
  314. * 1Mbps. This will be corrected on tx_time computation.
  315. */
  316. tx_time = (device_constant + 10 * test_frame_len / rate);
  317. estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err));
  318. result = (tx_time * estimated_retx) >> (2 * ARITH_SHIFT);
  319. return (u32)result;
  320. }
  321. /**
  322. * hwmp_route_info_get - Update routing info to originator and transmitter
  323. *
  324. * @sdata: local mesh subif
  325. * @mgmt: mesh management frame
  326. * @hwmp_ie: hwmp information element (PREP or PREQ)
  327. * @action: type of hwmp ie
  328. *
  329. * This function updates the path routing information to the originator and the
  330. * transmitter of a HWMP PREQ or PREP frame.
  331. *
  332. * Returns: metric to frame originator or 0 if the frame should not be further
  333. * processed
  334. *
  335. * Notes: this function is the only place (besides user-provided info) where
  336. * path routing information is updated.
  337. */
  338. static u32 hwmp_route_info_get(struct ieee80211_sub_if_data *sdata,
  339. struct ieee80211_mgmt *mgmt,
  340. const u8 *hwmp_ie, enum mpath_frame_type action)
  341. {
  342. struct ieee80211_local *local = sdata->local;
  343. struct mesh_path *mpath;
  344. struct sta_info *sta;
  345. bool fresh_info;
  346. const u8 *orig_addr, *ta;
  347. u32 orig_sn, orig_metric;
  348. unsigned long orig_lifetime, exp_time;
  349. u32 last_hop_metric, new_metric;
  350. bool process = true;
  351. rcu_read_lock();
  352. sta = sta_info_get(sdata, mgmt->sa);
  353. if (!sta) {
  354. rcu_read_unlock();
  355. return 0;
  356. }
  357. last_hop_metric = airtime_link_metric_get(local, sta);
  358. /* Update and check originator routing info */
  359. fresh_info = true;
  360. switch (action) {
  361. case MPATH_PREQ:
  362. orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie);
  363. orig_sn = PREQ_IE_ORIG_SN(hwmp_ie);
  364. orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie);
  365. orig_metric = PREQ_IE_METRIC(hwmp_ie);
  366. break;
  367. case MPATH_PREP:
  368. /* Originator here refers to the MP that was the target in the
  369. * Path Request. We divert from the nomenclature in the draft
  370. * so that we can easily use a single function to gather path
  371. * information from both PREQ and PREP frames.
  372. */
  373. orig_addr = PREP_IE_TARGET_ADDR(hwmp_ie);
  374. orig_sn = PREP_IE_TARGET_SN(hwmp_ie);
  375. orig_lifetime = PREP_IE_LIFETIME(hwmp_ie);
  376. orig_metric = PREP_IE_METRIC(hwmp_ie);
  377. break;
  378. default:
  379. rcu_read_unlock();
  380. return 0;
  381. }
  382. new_metric = orig_metric + last_hop_metric;
  383. if (new_metric < orig_metric)
  384. new_metric = MAX_METRIC;
  385. exp_time = TU_TO_EXP_TIME(orig_lifetime);
  386. if (ether_addr_equal(orig_addr, sdata->vif.addr)) {
  387. /* This MP is the originator, we are not interested in this
  388. * frame, except for updating transmitter's path info.
  389. */
  390. process = false;
  391. fresh_info = false;
  392. } else {
  393. mpath = mesh_path_lookup(sdata, orig_addr);
  394. if (mpath) {
  395. spin_lock_bh(&mpath->state_lock);
  396. if (mpath->flags & MESH_PATH_FIXED)
  397. fresh_info = false;
  398. else if ((mpath->flags & MESH_PATH_ACTIVE) &&
  399. (mpath->flags & MESH_PATH_SN_VALID)) {
  400. if (SN_GT(mpath->sn, orig_sn) ||
  401. (mpath->sn == orig_sn &&
  402. new_metric >= mpath->metric)) {
  403. process = false;
  404. fresh_info = false;
  405. }
  406. } else if (!(mpath->flags & MESH_PATH_ACTIVE)) {
  407. bool have_sn, newer_sn, bounced;
  408. have_sn = mpath->flags & MESH_PATH_SN_VALID;
  409. newer_sn = have_sn && SN_GT(orig_sn, mpath->sn);
  410. bounced = have_sn &&
  411. (SN_DELTA(orig_sn, mpath->sn) >
  412. MAX_SANE_SN_DELTA);
  413. if (!have_sn || newer_sn) {
  414. /* if SN is newer than what we had
  415. * then we can take it */;
  416. } else if (bounced) {
  417. /* if SN is way different than what
  418. * we had then assume the other side
  419. * rebooted or restarted */;
  420. } else {
  421. process = false;
  422. fresh_info = false;
  423. }
  424. }
  425. } else {
  426. mpath = mesh_path_add(sdata, orig_addr);
  427. if (IS_ERR(mpath)) {
  428. rcu_read_unlock();
  429. return 0;
  430. }
  431. spin_lock_bh(&mpath->state_lock);
  432. }
  433. if (fresh_info) {
  434. mesh_path_assign_nexthop(mpath, sta);
  435. mpath->flags |= MESH_PATH_SN_VALID;
  436. mpath->metric = new_metric;
  437. mpath->sn = orig_sn;
  438. mpath->exp_time = time_after(mpath->exp_time, exp_time)
  439. ? mpath->exp_time : exp_time;
  440. mesh_path_activate(mpath);
  441. spin_unlock_bh(&mpath->state_lock);
  442. ewma_mesh_fail_avg_init(&sta->mesh->fail_avg);
  443. /* init it at a low value - 0 start is tricky */
  444. ewma_mesh_fail_avg_add(&sta->mesh->fail_avg, 1);
  445. mesh_path_tx_pending(mpath);
  446. /* draft says preq_id should be saved to, but there does
  447. * not seem to be any use for it, skipping by now
  448. */
  449. } else
  450. spin_unlock_bh(&mpath->state_lock);
  451. }
  452. /* Update and check transmitter routing info */
  453. ta = mgmt->sa;
  454. if (ether_addr_equal(orig_addr, ta))
  455. fresh_info = false;
  456. else {
  457. fresh_info = true;
  458. mpath = mesh_path_lookup(sdata, ta);
  459. if (mpath) {
  460. spin_lock_bh(&mpath->state_lock);
  461. if ((mpath->flags & MESH_PATH_FIXED) ||
  462. ((mpath->flags & MESH_PATH_ACTIVE) &&
  463. (last_hop_metric > mpath->metric)))
  464. fresh_info = false;
  465. } else {
  466. mpath = mesh_path_add(sdata, ta);
  467. if (IS_ERR(mpath)) {
  468. rcu_read_unlock();
  469. return 0;
  470. }
  471. spin_lock_bh(&mpath->state_lock);
  472. }
  473. if (fresh_info) {
  474. mesh_path_assign_nexthop(mpath, sta);
  475. mpath->metric = last_hop_metric;
  476. mpath->exp_time = time_after(mpath->exp_time, exp_time)
  477. ? mpath->exp_time : exp_time;
  478. mesh_path_activate(mpath);
  479. spin_unlock_bh(&mpath->state_lock);
  480. ewma_mesh_fail_avg_init(&sta->mesh->fail_avg);
  481. /* init it at a low value - 0 start is tricky */
  482. ewma_mesh_fail_avg_add(&sta->mesh->fail_avg, 1);
  483. mesh_path_tx_pending(mpath);
  484. } else
  485. spin_unlock_bh(&mpath->state_lock);
  486. }
  487. rcu_read_unlock();
  488. return process ? new_metric : 0;
  489. }
  490. static void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata,
  491. struct ieee80211_mgmt *mgmt,
  492. const u8 *preq_elem, u32 orig_metric)
  493. {
  494. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  495. struct mesh_path *mpath = NULL;
  496. const u8 *target_addr, *orig_addr;
  497. const u8 *da;
  498. u8 target_flags, ttl, flags;
  499. u32 orig_sn, target_sn, lifetime, target_metric = 0;
  500. bool reply = false;
  501. bool forward = true;
  502. bool root_is_gate;
  503. /* Update target SN, if present */
  504. target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
  505. orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
  506. target_sn = PREQ_IE_TARGET_SN(preq_elem);
  507. orig_sn = PREQ_IE_ORIG_SN(preq_elem);
  508. target_flags = PREQ_IE_TARGET_F(preq_elem);
  509. /* Proactive PREQ gate announcements */
  510. flags = PREQ_IE_FLAGS(preq_elem);
  511. root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
  512. mhwmp_dbg(sdata, "received PREQ from %pM\n", orig_addr);
  513. if (ether_addr_equal(target_addr, sdata->vif.addr)) {
  514. mhwmp_dbg(sdata, "PREQ is for us\n");
  515. forward = false;
  516. reply = true;
  517. target_metric = 0;
  518. if (SN_GT(target_sn, ifmsh->sn))
  519. ifmsh->sn = target_sn;
  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, new_metric, orig_metric, last_hop_metric, 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. orig_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. last_hop_metric = airtime_link_metric_get(local, sta);
  747. new_metric = orig_metric + last_hop_metric;
  748. if (new_metric < orig_metric)
  749. new_metric = MAX_METRIC;
  750. mpath = mesh_path_lookup(sdata, orig_addr);
  751. if (!mpath) {
  752. mpath = mesh_path_add(sdata, orig_addr);
  753. if (IS_ERR(mpath)) {
  754. rcu_read_unlock();
  755. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  756. return;
  757. }
  758. }
  759. if (!(SN_LT(mpath->sn, orig_sn)) &&
  760. !(mpath->sn == orig_sn && new_metric < mpath->rann_metric)) {
  761. rcu_read_unlock();
  762. return;
  763. }
  764. if ((!(mpath->flags & (MESH_PATH_ACTIVE | MESH_PATH_RESOLVING)) ||
  765. (time_after(jiffies, mpath->last_preq_to_root +
  766. root_path_confirmation_jiffies(sdata)) ||
  767. time_before(jiffies, mpath->last_preq_to_root))) &&
  768. !(mpath->flags & MESH_PATH_FIXED) && (ttl != 0)) {
  769. mhwmp_dbg(sdata,
  770. "time to refresh root mpath %pM\n",
  771. orig_addr);
  772. mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
  773. mpath->last_preq_to_root = jiffies;
  774. }
  775. mpath->sn = orig_sn;
  776. mpath->rann_metric = new_metric;
  777. mpath->is_root = true;
  778. /* Recording RANNs sender address to send individually
  779. * addressed PREQs destined for root mesh STA */
  780. memcpy(mpath->rann_snd_addr, mgmt->sa, ETH_ALEN);
  781. if (root_is_gate)
  782. mesh_path_add_gate(mpath);
  783. if (ttl <= 1) {
  784. ifmsh->mshstats.dropped_frames_ttl++;
  785. rcu_read_unlock();
  786. return;
  787. }
  788. ttl--;
  789. if (ifmsh->mshcfg.dot11MeshForwarding) {
  790. mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
  791. orig_sn, 0, NULL, 0, broadcast_addr,
  792. hopcount, ttl, interval,
  793. new_metric, 0, sdata);
  794. }
  795. rcu_read_unlock();
  796. }
  797. void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
  798. struct ieee80211_mgmt *mgmt, size_t len)
  799. {
  800. struct ieee802_11_elems elems;
  801. size_t baselen;
  802. u32 path_metric;
  803. struct sta_info *sta;
  804. /* need action_code */
  805. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  806. return;
  807. rcu_read_lock();
  808. sta = sta_info_get(sdata, mgmt->sa);
  809. if (!sta || sta->mesh->plink_state != NL80211_PLINK_ESTAB) {
  810. rcu_read_unlock();
  811. return;
  812. }
  813. rcu_read_unlock();
  814. baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
  815. ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
  816. len - baselen, false, &elems);
  817. if (elems.preq) {
  818. if (elems.preq_len != 37)
  819. /* Right now we support just 1 destination and no AE */
  820. return;
  821. path_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
  822. MPATH_PREQ);
  823. if (path_metric)
  824. hwmp_preq_frame_process(sdata, mgmt, elems.preq,
  825. path_metric);
  826. }
  827. if (elems.prep) {
  828. if (elems.prep_len != 31)
  829. /* Right now we support no AE */
  830. return;
  831. path_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
  832. MPATH_PREP);
  833. if (path_metric)
  834. hwmp_prep_frame_process(sdata, mgmt, elems.prep,
  835. path_metric);
  836. }
  837. if (elems.perr) {
  838. if (elems.perr_len != 15)
  839. /* Right now we support only one destination per PERR */
  840. return;
  841. hwmp_perr_frame_process(sdata, mgmt, elems.perr);
  842. }
  843. if (elems.rann)
  844. hwmp_rann_frame_process(sdata, mgmt, elems.rann);
  845. }
  846. /**
  847. * mesh_queue_preq - queue a PREQ to a given destination
  848. *
  849. * @mpath: mesh path to discover
  850. * @flags: special attributes of the PREQ to be sent
  851. *
  852. * Locking: the function must be called from within a rcu read lock block.
  853. *
  854. */
  855. static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
  856. {
  857. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  858. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  859. struct mesh_preq_queue *preq_node;
  860. preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
  861. if (!preq_node) {
  862. mhwmp_dbg(sdata, "could not allocate PREQ node\n");
  863. return;
  864. }
  865. spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
  866. if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
  867. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  868. kfree(preq_node);
  869. if (printk_ratelimit())
  870. mhwmp_dbg(sdata, "PREQ node queue full\n");
  871. return;
  872. }
  873. spin_lock(&mpath->state_lock);
  874. if (mpath->flags & MESH_PATH_REQ_QUEUED) {
  875. spin_unlock(&mpath->state_lock);
  876. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  877. kfree(preq_node);
  878. return;
  879. }
  880. memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
  881. preq_node->flags = flags;
  882. mpath->flags |= MESH_PATH_REQ_QUEUED;
  883. spin_unlock(&mpath->state_lock);
  884. list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
  885. ++ifmsh->preq_queue_len;
  886. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  887. if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
  888. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  889. else if (time_before(jiffies, ifmsh->last_preq)) {
  890. /* avoid long wait if did not send preqs for a long time
  891. * and jiffies wrapped around
  892. */
  893. ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
  894. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  895. } else
  896. mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
  897. min_preq_int_jiff(sdata));
  898. }
  899. /**
  900. * mesh_path_start_discovery - launch a path discovery from the PREQ queue
  901. *
  902. * @sdata: local mesh subif
  903. */
  904. void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
  905. {
  906. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  907. struct mesh_preq_queue *preq_node;
  908. struct mesh_path *mpath;
  909. u8 ttl, target_flags = 0;
  910. const u8 *da;
  911. u32 lifetime;
  912. spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
  913. if (!ifmsh->preq_queue_len ||
  914. time_before(jiffies, ifmsh->last_preq +
  915. min_preq_int_jiff(sdata))) {
  916. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  917. return;
  918. }
  919. preq_node = list_first_entry(&ifmsh->preq_queue.list,
  920. struct mesh_preq_queue, list);
  921. list_del(&preq_node->list);
  922. --ifmsh->preq_queue_len;
  923. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  924. rcu_read_lock();
  925. mpath = mesh_path_lookup(sdata, preq_node->dst);
  926. if (!mpath)
  927. goto enddiscovery;
  928. spin_lock_bh(&mpath->state_lock);
  929. if (mpath->flags & (MESH_PATH_DELETED | MESH_PATH_FIXED)) {
  930. spin_unlock_bh(&mpath->state_lock);
  931. goto enddiscovery;
  932. }
  933. mpath->flags &= ~MESH_PATH_REQ_QUEUED;
  934. if (preq_node->flags & PREQ_Q_F_START) {
  935. if (mpath->flags & MESH_PATH_RESOLVING) {
  936. spin_unlock_bh(&mpath->state_lock);
  937. goto enddiscovery;
  938. } else {
  939. mpath->flags &= ~MESH_PATH_RESOLVED;
  940. mpath->flags |= MESH_PATH_RESOLVING;
  941. mpath->discovery_retries = 0;
  942. mpath->discovery_timeout = disc_timeout_jiff(sdata);
  943. }
  944. } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
  945. mpath->flags & MESH_PATH_RESOLVED) {
  946. mpath->flags &= ~MESH_PATH_RESOLVING;
  947. spin_unlock_bh(&mpath->state_lock);
  948. goto enddiscovery;
  949. }
  950. ifmsh->last_preq = jiffies;
  951. if (time_after(jiffies, ifmsh->last_sn_update +
  952. net_traversal_jiffies(sdata)) ||
  953. time_before(jiffies, ifmsh->last_sn_update)) {
  954. ++ifmsh->sn;
  955. sdata->u.mesh.last_sn_update = jiffies;
  956. }
  957. lifetime = default_lifetime(sdata);
  958. ttl = sdata->u.mesh.mshcfg.element_ttl;
  959. if (ttl == 0) {
  960. sdata->u.mesh.mshstats.dropped_frames_ttl++;
  961. spin_unlock_bh(&mpath->state_lock);
  962. goto enddiscovery;
  963. }
  964. if (preq_node->flags & PREQ_Q_F_REFRESH)
  965. target_flags |= IEEE80211_PREQ_TO_FLAG;
  966. else
  967. target_flags &= ~IEEE80211_PREQ_TO_FLAG;
  968. spin_unlock_bh(&mpath->state_lock);
  969. da = (mpath->is_root) ? mpath->rann_snd_addr : broadcast_addr;
  970. mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr, ifmsh->sn,
  971. target_flags, mpath->dst, mpath->sn, da, 0,
  972. ttl, lifetime, 0, ifmsh->preq_id++, sdata);
  973. mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
  974. enddiscovery:
  975. rcu_read_unlock();
  976. kfree(preq_node);
  977. }
  978. /**
  979. * mesh_nexthop_resolve - lookup next hop; conditionally start path discovery
  980. *
  981. * @skb: 802.11 frame to be sent
  982. * @sdata: network subif the frame will be sent through
  983. *
  984. * Lookup next hop for given skb and start path discovery if no
  985. * forwarding information is found.
  986. *
  987. * Returns: 0 if the next hop was found and -ENOENT if the frame was queued.
  988. * skb is freeed here if no mpath could be allocated.
  989. */
  990. int mesh_nexthop_resolve(struct ieee80211_sub_if_data *sdata,
  991. struct sk_buff *skb)
  992. {
  993. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  994. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  995. struct mesh_path *mpath;
  996. struct sk_buff *skb_to_free = NULL;
  997. u8 *target_addr = hdr->addr3;
  998. int err = 0;
  999. /* Nulls are only sent to peers for PS and should be pre-addressed */
  1000. if (ieee80211_is_qos_nullfunc(hdr->frame_control))
  1001. return 0;
  1002. rcu_read_lock();
  1003. err = mesh_nexthop_lookup(sdata, skb);
  1004. if (!err)
  1005. goto endlookup;
  1006. /* no nexthop found, start resolving */
  1007. mpath = mesh_path_lookup(sdata, target_addr);
  1008. if (!mpath) {
  1009. mpath = mesh_path_add(sdata, target_addr);
  1010. if (IS_ERR(mpath)) {
  1011. mesh_path_discard_frame(sdata, skb);
  1012. err = PTR_ERR(mpath);
  1013. goto endlookup;
  1014. }
  1015. }
  1016. if (!(mpath->flags & MESH_PATH_RESOLVING) &&
  1017. mesh_path_sel_is_hwmp(sdata))
  1018. mesh_queue_preq(mpath, PREQ_Q_F_START);
  1019. if (skb_queue_len(&mpath->frame_queue) >= MESH_FRAME_QUEUE_LEN)
  1020. skb_to_free = skb_dequeue(&mpath->frame_queue);
  1021. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  1022. ieee80211_set_qos_hdr(sdata, skb);
  1023. skb_queue_tail(&mpath->frame_queue, skb);
  1024. err = -ENOENT;
  1025. if (skb_to_free)
  1026. mesh_path_discard_frame(sdata, skb_to_free);
  1027. endlookup:
  1028. rcu_read_unlock();
  1029. return err;
  1030. }
  1031. /**
  1032. * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame. Calling
  1033. * this function is considered "using" the associated mpath, so preempt a path
  1034. * refresh if this mpath expires soon.
  1035. *
  1036. * @skb: 802.11 frame to be sent
  1037. * @sdata: network subif the frame will be sent through
  1038. *
  1039. * Returns: 0 if the next hop was found. Nonzero otherwise.
  1040. */
  1041. int mesh_nexthop_lookup(struct ieee80211_sub_if_data *sdata,
  1042. struct sk_buff *skb)
  1043. {
  1044. struct mesh_path *mpath;
  1045. struct sta_info *next_hop;
  1046. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1047. u8 *target_addr = hdr->addr3;
  1048. int err = -ENOENT;
  1049. rcu_read_lock();
  1050. mpath = mesh_path_lookup(sdata, target_addr);
  1051. if (!mpath || !(mpath->flags & MESH_PATH_ACTIVE))
  1052. goto endlookup;
  1053. if (time_after(jiffies,
  1054. mpath->exp_time -
  1055. msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
  1056. ether_addr_equal(sdata->vif.addr, hdr->addr4) &&
  1057. !(mpath->flags & MESH_PATH_RESOLVING) &&
  1058. !(mpath->flags & MESH_PATH_FIXED))
  1059. mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
  1060. next_hop = rcu_dereference(mpath->next_hop);
  1061. if (next_hop) {
  1062. memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
  1063. memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
  1064. ieee80211_mps_set_frame_flags(sdata, next_hop, hdr);
  1065. err = 0;
  1066. }
  1067. endlookup:
  1068. rcu_read_unlock();
  1069. return err;
  1070. }
  1071. void mesh_path_timer(struct timer_list *t)
  1072. {
  1073. struct mesh_path *mpath = from_timer(mpath, t, timer);
  1074. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  1075. int ret;
  1076. if (sdata->local->quiescing)
  1077. return;
  1078. spin_lock_bh(&mpath->state_lock);
  1079. if (mpath->flags & MESH_PATH_RESOLVED ||
  1080. (!(mpath->flags & MESH_PATH_RESOLVING))) {
  1081. mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
  1082. spin_unlock_bh(&mpath->state_lock);
  1083. } else if (mpath->discovery_retries < max_preq_retries(sdata)) {
  1084. ++mpath->discovery_retries;
  1085. mpath->discovery_timeout *= 2;
  1086. mpath->flags &= ~MESH_PATH_REQ_QUEUED;
  1087. spin_unlock_bh(&mpath->state_lock);
  1088. mesh_queue_preq(mpath, 0);
  1089. } else {
  1090. mpath->flags &= ~(MESH_PATH_RESOLVING |
  1091. MESH_PATH_RESOLVED |
  1092. MESH_PATH_REQ_QUEUED);
  1093. mpath->exp_time = jiffies;
  1094. spin_unlock_bh(&mpath->state_lock);
  1095. if (!mpath->is_gate && mesh_gate_num(sdata) > 0) {
  1096. ret = mesh_path_send_to_gates(mpath);
  1097. if (ret)
  1098. mhwmp_dbg(sdata, "no gate was reachable\n");
  1099. } else
  1100. mesh_path_flush_pending(mpath);
  1101. }
  1102. }
  1103. void mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
  1104. {
  1105. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  1106. u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
  1107. u8 flags, target_flags = 0;
  1108. flags = (ifmsh->mshcfg.dot11MeshGateAnnouncementProtocol)
  1109. ? RANN_FLAG_IS_GATE : 0;
  1110. switch (ifmsh->mshcfg.dot11MeshHWMPRootMode) {
  1111. case IEEE80211_PROACTIVE_RANN:
  1112. mesh_path_sel_frame_tx(MPATH_RANN, flags, sdata->vif.addr,
  1113. ++ifmsh->sn, 0, NULL, 0, broadcast_addr,
  1114. 0, ifmsh->mshcfg.element_ttl,
  1115. interval, 0, 0, sdata);
  1116. break;
  1117. case IEEE80211_PROACTIVE_PREQ_WITH_PREP:
  1118. flags |= IEEE80211_PREQ_PROACTIVE_PREP_FLAG;
  1119. /* fall through */
  1120. case IEEE80211_PROACTIVE_PREQ_NO_PREP:
  1121. interval = ifmsh->mshcfg.dot11MeshHWMPactivePathToRootTimeout;
  1122. target_flags |= IEEE80211_PREQ_TO_FLAG |
  1123. IEEE80211_PREQ_USN_FLAG;
  1124. mesh_path_sel_frame_tx(MPATH_PREQ, flags, sdata->vif.addr,
  1125. ++ifmsh->sn, target_flags,
  1126. (u8 *) broadcast_addr, 0, broadcast_addr,
  1127. 0, ifmsh->mshcfg.element_ttl, interval,
  1128. 0, ifmsh->preq_id++, sdata);
  1129. break;
  1130. default:
  1131. mhwmp_dbg(sdata, "Proactive mechanism not supported\n");
  1132. return;
  1133. }
  1134. }