mesh_hwmp.c 35 KB

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