mesh_hwmp.c 32 KB

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