mesh_hwmp.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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. struct rate_info rinfo;
  292. /* This should be adjusted for each device */
  293. int device_constant = 1 << ARITH_SHIFT;
  294. int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT;
  295. int s_unit = 1 << ARITH_SHIFT;
  296. int rate, err;
  297. u32 tx_time, estimated_retx;
  298. u64 result;
  299. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  300. if (sta->fail_avg >= 100)
  301. return MAX_METRIC;
  302. sta_set_rate_info_tx(sta, &sta->last_tx_rate, &rinfo);
  303. rate = cfg80211_calculate_bitrate(&rinfo);
  304. if (WARN_ON(!rate))
  305. return MAX_METRIC;
  306. err = (sta->fail_avg << ARITH_SHIFT) / 100;
  307. /* bitrate is in units of 100 Kbps, while we need rate in units of
  308. * 1Mbps. This will be corrected on tx_time computation.
  309. */
  310. tx_time = (device_constant + 10 * test_frame_len / rate);
  311. estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err));
  312. result = (tx_time * estimated_retx) >> (2 * ARITH_SHIFT) ;
  313. return (u32)result;
  314. }
  315. /**
  316. * hwmp_route_info_get - Update routing info to originator and transmitter
  317. *
  318. * @sdata: local mesh subif
  319. * @mgmt: mesh management frame
  320. * @hwmp_ie: hwmp information element (PREP or PREQ)
  321. *
  322. * This function updates the path routing information to the originator and the
  323. * transmitter of a HWMP PREQ or PREP frame.
  324. *
  325. * Returns: metric to frame originator or 0 if the frame should not be further
  326. * processed
  327. *
  328. * Notes: this function is the only place (besides user-provided info) where
  329. * path routing information is updated.
  330. */
  331. static u32 hwmp_route_info_get(struct ieee80211_sub_if_data *sdata,
  332. struct ieee80211_mgmt *mgmt,
  333. u8 *hwmp_ie, enum mpath_frame_type action)
  334. {
  335. struct ieee80211_local *local = sdata->local;
  336. struct mesh_path *mpath;
  337. struct sta_info *sta;
  338. bool fresh_info;
  339. u8 *orig_addr, *ta;
  340. u32 orig_sn, orig_metric;
  341. unsigned long orig_lifetime, exp_time;
  342. u32 last_hop_metric, new_metric;
  343. bool process = true;
  344. rcu_read_lock();
  345. sta = sta_info_get(sdata, mgmt->sa);
  346. if (!sta) {
  347. rcu_read_unlock();
  348. return 0;
  349. }
  350. last_hop_metric = airtime_link_metric_get(local, sta);
  351. /* Update and check originator routing info */
  352. fresh_info = true;
  353. switch (action) {
  354. case MPATH_PREQ:
  355. orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie);
  356. orig_sn = PREQ_IE_ORIG_SN(hwmp_ie);
  357. orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie);
  358. orig_metric = PREQ_IE_METRIC(hwmp_ie);
  359. break;
  360. case MPATH_PREP:
  361. /* Originator here refers to the MP that was the target in the
  362. * Path Request. We divert from the nomenclature in the draft
  363. * so that we can easily use a single function to gather path
  364. * information from both PREQ and PREP frames.
  365. */
  366. orig_addr = PREP_IE_TARGET_ADDR(hwmp_ie);
  367. orig_sn = PREP_IE_TARGET_SN(hwmp_ie);
  368. orig_lifetime = PREP_IE_LIFETIME(hwmp_ie);
  369. orig_metric = PREP_IE_METRIC(hwmp_ie);
  370. break;
  371. default:
  372. rcu_read_unlock();
  373. return 0;
  374. }
  375. new_metric = orig_metric + last_hop_metric;
  376. if (new_metric < orig_metric)
  377. new_metric = MAX_METRIC;
  378. exp_time = TU_TO_EXP_TIME(orig_lifetime);
  379. if (compare_ether_addr(orig_addr, sdata->vif.addr) == 0) {
  380. /* This MP is the originator, we are not interested in this
  381. * frame, except for updating transmitter's path info.
  382. */
  383. process = false;
  384. fresh_info = false;
  385. } else {
  386. mpath = mesh_path_lookup(orig_addr, sdata);
  387. if (mpath) {
  388. spin_lock_bh(&mpath->state_lock);
  389. if (mpath->flags & MESH_PATH_FIXED)
  390. fresh_info = false;
  391. else if ((mpath->flags & MESH_PATH_ACTIVE) &&
  392. (mpath->flags & MESH_PATH_SN_VALID)) {
  393. if (SN_GT(mpath->sn, orig_sn) ||
  394. (mpath->sn == orig_sn &&
  395. new_metric >= mpath->metric)) {
  396. process = false;
  397. fresh_info = false;
  398. }
  399. }
  400. } else {
  401. mesh_path_add(orig_addr, sdata);
  402. mpath = mesh_path_lookup(orig_addr, sdata);
  403. if (!mpath) {
  404. rcu_read_unlock();
  405. return 0;
  406. }
  407. spin_lock_bh(&mpath->state_lock);
  408. }
  409. if (fresh_info) {
  410. mesh_path_assign_nexthop(mpath, sta);
  411. mpath->flags |= MESH_PATH_SN_VALID;
  412. mpath->metric = new_metric;
  413. mpath->sn = orig_sn;
  414. mpath->exp_time = time_after(mpath->exp_time, exp_time)
  415. ? mpath->exp_time : exp_time;
  416. mesh_path_activate(mpath);
  417. spin_unlock_bh(&mpath->state_lock);
  418. mesh_path_tx_pending(mpath);
  419. /* draft says preq_id should be saved to, but there does
  420. * not seem to be any use for it, skipping by now
  421. */
  422. } else
  423. spin_unlock_bh(&mpath->state_lock);
  424. }
  425. /* Update and check transmitter routing info */
  426. ta = mgmt->sa;
  427. if (compare_ether_addr(orig_addr, ta) == 0)
  428. fresh_info = false;
  429. else {
  430. fresh_info = true;
  431. mpath = mesh_path_lookup(ta, sdata);
  432. if (mpath) {
  433. spin_lock_bh(&mpath->state_lock);
  434. if ((mpath->flags & MESH_PATH_FIXED) ||
  435. ((mpath->flags & MESH_PATH_ACTIVE) &&
  436. (last_hop_metric > mpath->metric)))
  437. fresh_info = false;
  438. } else {
  439. mesh_path_add(ta, sdata);
  440. mpath = mesh_path_lookup(ta, sdata);
  441. if (!mpath) {
  442. rcu_read_unlock();
  443. return 0;
  444. }
  445. spin_lock_bh(&mpath->state_lock);
  446. }
  447. if (fresh_info) {
  448. mesh_path_assign_nexthop(mpath, sta);
  449. mpath->metric = last_hop_metric;
  450. mpath->exp_time = time_after(mpath->exp_time, exp_time)
  451. ? mpath->exp_time : exp_time;
  452. mesh_path_activate(mpath);
  453. spin_unlock_bh(&mpath->state_lock);
  454. mesh_path_tx_pending(mpath);
  455. } else
  456. spin_unlock_bh(&mpath->state_lock);
  457. }
  458. rcu_read_unlock();
  459. return process ? new_metric : 0;
  460. }
  461. static void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata,
  462. struct ieee80211_mgmt *mgmt,
  463. u8 *preq_elem, u32 metric)
  464. {
  465. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  466. struct mesh_path *mpath = NULL;
  467. u8 *target_addr, *orig_addr;
  468. const u8 *da;
  469. u8 target_flags, ttl;
  470. u32 orig_sn, target_sn, lifetime;
  471. bool reply = false;
  472. bool forward = true;
  473. /* Update target SN, if present */
  474. target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
  475. orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
  476. target_sn = PREQ_IE_TARGET_SN(preq_elem);
  477. orig_sn = PREQ_IE_ORIG_SN(preq_elem);
  478. target_flags = PREQ_IE_TARGET_F(preq_elem);
  479. mhwmp_dbg("received PREQ from %pM", orig_addr);
  480. if (compare_ether_addr(target_addr, sdata->vif.addr) == 0) {
  481. mhwmp_dbg("PREQ is for us");
  482. forward = false;
  483. reply = true;
  484. metric = 0;
  485. if (time_after(jiffies, ifmsh->last_sn_update +
  486. net_traversal_jiffies(sdata)) ||
  487. time_before(jiffies, ifmsh->last_sn_update)) {
  488. target_sn = ++ifmsh->sn;
  489. ifmsh->last_sn_update = jiffies;
  490. }
  491. } else {
  492. rcu_read_lock();
  493. mpath = mesh_path_lookup(target_addr, sdata);
  494. if (mpath) {
  495. if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
  496. SN_LT(mpath->sn, target_sn)) {
  497. mpath->sn = target_sn;
  498. mpath->flags |= MESH_PATH_SN_VALID;
  499. } else if ((!(target_flags & MP_F_DO)) &&
  500. (mpath->flags & MESH_PATH_ACTIVE)) {
  501. reply = true;
  502. metric = mpath->metric;
  503. target_sn = mpath->sn;
  504. if (target_flags & MP_F_RF)
  505. target_flags |= MP_F_DO;
  506. else
  507. forward = false;
  508. }
  509. }
  510. rcu_read_unlock();
  511. }
  512. if (reply) {
  513. lifetime = PREQ_IE_LIFETIME(preq_elem);
  514. ttl = ifmsh->mshcfg.element_ttl;
  515. if (ttl != 0) {
  516. mhwmp_dbg("replying to the PREQ");
  517. mesh_path_sel_frame_tx(MPATH_PREP, 0, orig_addr,
  518. cpu_to_le32(orig_sn), 0, target_addr,
  519. cpu_to_le32(target_sn), mgmt->sa, 0, ttl,
  520. cpu_to_le32(lifetime), cpu_to_le32(metric),
  521. 0, sdata);
  522. } else
  523. ifmsh->mshstats.dropped_frames_ttl++;
  524. }
  525. if (forward && ifmsh->mshcfg.dot11MeshForwarding) {
  526. u32 preq_id;
  527. u8 hopcount, flags;
  528. ttl = PREQ_IE_TTL(preq_elem);
  529. lifetime = PREQ_IE_LIFETIME(preq_elem);
  530. if (ttl <= 1) {
  531. ifmsh->mshstats.dropped_frames_ttl++;
  532. return;
  533. }
  534. mhwmp_dbg("forwarding the PREQ from %pM", orig_addr);
  535. --ttl;
  536. flags = PREQ_IE_FLAGS(preq_elem);
  537. preq_id = PREQ_IE_PREQ_ID(preq_elem);
  538. hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
  539. da = (mpath && mpath->is_root) ?
  540. mpath->rann_snd_addr : broadcast_addr;
  541. mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
  542. cpu_to_le32(orig_sn), target_flags, target_addr,
  543. cpu_to_le32(target_sn), da,
  544. hopcount, ttl, cpu_to_le32(lifetime),
  545. cpu_to_le32(metric), cpu_to_le32(preq_id),
  546. sdata);
  547. ifmsh->mshstats.fwded_mcast++;
  548. ifmsh->mshstats.fwded_frames++;
  549. }
  550. }
  551. static inline struct sta_info *
  552. next_hop_deref_protected(struct mesh_path *mpath)
  553. {
  554. return rcu_dereference_protected(mpath->next_hop,
  555. lockdep_is_held(&mpath->state_lock));
  556. }
  557. static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
  558. struct ieee80211_mgmt *mgmt,
  559. u8 *prep_elem, u32 metric)
  560. {
  561. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  562. struct mesh_path *mpath;
  563. u8 *target_addr, *orig_addr;
  564. u8 ttl, hopcount, flags;
  565. u8 next_hop[ETH_ALEN];
  566. u32 target_sn, orig_sn, lifetime;
  567. mhwmp_dbg("received PREP from %pM", PREP_IE_ORIG_ADDR(prep_elem));
  568. orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
  569. if (compare_ether_addr(orig_addr, sdata->vif.addr) == 0)
  570. /* destination, no forwarding required */
  571. return;
  572. if (!ifmsh->mshcfg.dot11MeshForwarding)
  573. return;
  574. ttl = PREP_IE_TTL(prep_elem);
  575. if (ttl <= 1) {
  576. sdata->u.mesh.mshstats.dropped_frames_ttl++;
  577. return;
  578. }
  579. rcu_read_lock();
  580. mpath = mesh_path_lookup(orig_addr, sdata);
  581. if (mpath)
  582. spin_lock_bh(&mpath->state_lock);
  583. else
  584. goto fail;
  585. if (!(mpath->flags & MESH_PATH_ACTIVE)) {
  586. spin_unlock_bh(&mpath->state_lock);
  587. goto fail;
  588. }
  589. memcpy(next_hop, next_hop_deref_protected(mpath)->sta.addr, ETH_ALEN);
  590. spin_unlock_bh(&mpath->state_lock);
  591. --ttl;
  592. flags = PREP_IE_FLAGS(prep_elem);
  593. lifetime = PREP_IE_LIFETIME(prep_elem);
  594. hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
  595. target_addr = PREP_IE_TARGET_ADDR(prep_elem);
  596. target_sn = PREP_IE_TARGET_SN(prep_elem);
  597. orig_sn = PREP_IE_ORIG_SN(prep_elem);
  598. mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr,
  599. cpu_to_le32(orig_sn), 0, target_addr,
  600. cpu_to_le32(target_sn), next_hop, hopcount,
  601. ttl, cpu_to_le32(lifetime), cpu_to_le32(metric),
  602. 0, sdata);
  603. rcu_read_unlock();
  604. sdata->u.mesh.mshstats.fwded_unicast++;
  605. sdata->u.mesh.mshstats.fwded_frames++;
  606. return;
  607. fail:
  608. rcu_read_unlock();
  609. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  610. }
  611. static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
  612. struct ieee80211_mgmt *mgmt, u8 *perr_elem)
  613. {
  614. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  615. struct mesh_path *mpath;
  616. u8 ttl;
  617. u8 *ta, *target_addr;
  618. u32 target_sn;
  619. u16 target_rcode;
  620. ta = mgmt->sa;
  621. ttl = PERR_IE_TTL(perr_elem);
  622. if (ttl <= 1) {
  623. ifmsh->mshstats.dropped_frames_ttl++;
  624. return;
  625. }
  626. ttl--;
  627. target_addr = PERR_IE_TARGET_ADDR(perr_elem);
  628. target_sn = PERR_IE_TARGET_SN(perr_elem);
  629. target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
  630. rcu_read_lock();
  631. mpath = mesh_path_lookup(target_addr, sdata);
  632. if (mpath) {
  633. struct sta_info *sta;
  634. spin_lock_bh(&mpath->state_lock);
  635. sta = next_hop_deref_protected(mpath);
  636. if (mpath->flags & MESH_PATH_ACTIVE &&
  637. compare_ether_addr(ta, sta->sta.addr) == 0 &&
  638. (!(mpath->flags & MESH_PATH_SN_VALID) ||
  639. SN_GT(target_sn, mpath->sn))) {
  640. mpath->flags &= ~MESH_PATH_ACTIVE;
  641. mpath->sn = target_sn;
  642. spin_unlock_bh(&mpath->state_lock);
  643. if (!ifmsh->mshcfg.dot11MeshForwarding)
  644. goto endperr;
  645. mesh_path_error_tx(ttl, target_addr, cpu_to_le32(target_sn),
  646. cpu_to_le16(target_rcode),
  647. broadcast_addr, sdata);
  648. } else
  649. spin_unlock_bh(&mpath->state_lock);
  650. }
  651. endperr:
  652. rcu_read_unlock();
  653. }
  654. static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
  655. struct ieee80211_mgmt *mgmt,
  656. struct ieee80211_rann_ie *rann)
  657. {
  658. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  659. struct mesh_path *mpath;
  660. u8 ttl, flags, hopcount;
  661. u8 *orig_addr;
  662. u32 orig_sn, metric;
  663. u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
  664. bool root_is_gate;
  665. ttl = rann->rann_ttl;
  666. if (ttl <= 1) {
  667. ifmsh->mshstats.dropped_frames_ttl++;
  668. return;
  669. }
  670. ttl--;
  671. flags = rann->rann_flags;
  672. root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
  673. orig_addr = rann->rann_addr;
  674. orig_sn = rann->rann_seq;
  675. hopcount = rann->rann_hopcount;
  676. hopcount++;
  677. metric = rann->rann_metric;
  678. /* Ignore our own RANNs */
  679. if (compare_ether_addr(orig_addr, sdata->vif.addr) == 0)
  680. return;
  681. mhwmp_dbg("received RANN from %pM via neighbour %pM (is_gate=%d)",
  682. orig_addr, mgmt->sa, root_is_gate);
  683. rcu_read_lock();
  684. mpath = mesh_path_lookup(orig_addr, sdata);
  685. if (!mpath) {
  686. mesh_path_add(orig_addr, sdata);
  687. mpath = mesh_path_lookup(orig_addr, sdata);
  688. if (!mpath) {
  689. rcu_read_unlock();
  690. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  691. return;
  692. }
  693. }
  694. if ((!(mpath->flags & (MESH_PATH_ACTIVE | MESH_PATH_RESOLVING)) ||
  695. time_after(jiffies, mpath->exp_time - 1*HZ)) &&
  696. !(mpath->flags & MESH_PATH_FIXED)) {
  697. mhwmp_dbg("%s time to refresh root mpath %pM", sdata->name,
  698. orig_addr);
  699. mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
  700. }
  701. if (mpath->sn < orig_sn && ifmsh->mshcfg.dot11MeshForwarding) {
  702. mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
  703. cpu_to_le32(orig_sn),
  704. 0, NULL, 0, broadcast_addr,
  705. hopcount, ttl, cpu_to_le32(interval),
  706. cpu_to_le32(metric + mpath->metric),
  707. 0, sdata);
  708. mpath->sn = orig_sn;
  709. }
  710. /* Using individually addressed PREQ for root node */
  711. memcpy(mpath->rann_snd_addr, mgmt->sa, ETH_ALEN);
  712. mpath->is_root = true;
  713. if (root_is_gate)
  714. mesh_path_add_gate(mpath);
  715. rcu_read_unlock();
  716. }
  717. void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
  718. struct ieee80211_mgmt *mgmt,
  719. size_t len)
  720. {
  721. struct ieee802_11_elems elems;
  722. size_t baselen;
  723. u32 last_hop_metric;
  724. struct sta_info *sta;
  725. /* need action_code */
  726. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  727. return;
  728. rcu_read_lock();
  729. sta = sta_info_get(sdata, mgmt->sa);
  730. if (!sta || sta->plink_state != NL80211_PLINK_ESTAB) {
  731. rcu_read_unlock();
  732. return;
  733. }
  734. rcu_read_unlock();
  735. baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
  736. ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
  737. len - baselen, &elems);
  738. if (elems.preq) {
  739. if (elems.preq_len != 37)
  740. /* Right now we support just 1 destination and no AE */
  741. return;
  742. last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
  743. MPATH_PREQ);
  744. if (last_hop_metric)
  745. hwmp_preq_frame_process(sdata, mgmt, elems.preq,
  746. last_hop_metric);
  747. }
  748. if (elems.prep) {
  749. if (elems.prep_len != 31)
  750. /* Right now we support no AE */
  751. return;
  752. last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
  753. MPATH_PREP);
  754. if (last_hop_metric)
  755. hwmp_prep_frame_process(sdata, mgmt, elems.prep,
  756. last_hop_metric);
  757. }
  758. if (elems.perr) {
  759. if (elems.perr_len != 15)
  760. /* Right now we support only one destination per PERR */
  761. return;
  762. hwmp_perr_frame_process(sdata, mgmt, elems.perr);
  763. }
  764. if (elems.rann)
  765. hwmp_rann_frame_process(sdata, mgmt, elems.rann);
  766. }
  767. /**
  768. * mesh_queue_preq - queue a PREQ to a given destination
  769. *
  770. * @mpath: mesh path to discover
  771. * @flags: special attributes of the PREQ to be sent
  772. *
  773. * Locking: the function must be called from within a rcu read lock block.
  774. *
  775. */
  776. static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
  777. {
  778. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  779. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  780. struct mesh_preq_queue *preq_node;
  781. preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
  782. if (!preq_node) {
  783. mhwmp_dbg("could not allocate PREQ node");
  784. return;
  785. }
  786. spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
  787. if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
  788. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  789. kfree(preq_node);
  790. if (printk_ratelimit())
  791. mhwmp_dbg("PREQ node queue full");
  792. return;
  793. }
  794. spin_lock(&mpath->state_lock);
  795. if (mpath->flags & MESH_PATH_REQ_QUEUED) {
  796. spin_unlock(&mpath->state_lock);
  797. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  798. kfree(preq_node);
  799. return;
  800. }
  801. memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
  802. preq_node->flags = flags;
  803. mpath->flags |= MESH_PATH_REQ_QUEUED;
  804. spin_unlock(&mpath->state_lock);
  805. list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
  806. ++ifmsh->preq_queue_len;
  807. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  808. if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
  809. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  810. else if (time_before(jiffies, ifmsh->last_preq)) {
  811. /* avoid long wait if did not send preqs for a long time
  812. * and jiffies wrapped around
  813. */
  814. ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
  815. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  816. } else
  817. mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
  818. min_preq_int_jiff(sdata));
  819. }
  820. /**
  821. * mesh_path_start_discovery - launch a path discovery from the PREQ queue
  822. *
  823. * @sdata: local mesh subif
  824. */
  825. void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
  826. {
  827. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  828. struct mesh_preq_queue *preq_node;
  829. struct mesh_path *mpath;
  830. u8 ttl, target_flags;
  831. const u8 *da;
  832. u32 lifetime;
  833. spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
  834. if (!ifmsh->preq_queue_len ||
  835. time_before(jiffies, ifmsh->last_preq +
  836. min_preq_int_jiff(sdata))) {
  837. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  838. return;
  839. }
  840. preq_node = list_first_entry(&ifmsh->preq_queue.list,
  841. struct mesh_preq_queue, list);
  842. list_del(&preq_node->list);
  843. --ifmsh->preq_queue_len;
  844. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  845. rcu_read_lock();
  846. mpath = mesh_path_lookup(preq_node->dst, sdata);
  847. if (!mpath)
  848. goto enddiscovery;
  849. spin_lock_bh(&mpath->state_lock);
  850. mpath->flags &= ~MESH_PATH_REQ_QUEUED;
  851. if (preq_node->flags & PREQ_Q_F_START) {
  852. if (mpath->flags & MESH_PATH_RESOLVING) {
  853. spin_unlock_bh(&mpath->state_lock);
  854. goto enddiscovery;
  855. } else {
  856. mpath->flags &= ~MESH_PATH_RESOLVED;
  857. mpath->flags |= MESH_PATH_RESOLVING;
  858. mpath->discovery_retries = 0;
  859. mpath->discovery_timeout = disc_timeout_jiff(sdata);
  860. }
  861. } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
  862. mpath->flags & MESH_PATH_RESOLVED) {
  863. mpath->flags &= ~MESH_PATH_RESOLVING;
  864. spin_unlock_bh(&mpath->state_lock);
  865. goto enddiscovery;
  866. }
  867. ifmsh->last_preq = jiffies;
  868. if (time_after(jiffies, ifmsh->last_sn_update +
  869. net_traversal_jiffies(sdata)) ||
  870. time_before(jiffies, ifmsh->last_sn_update)) {
  871. ++ifmsh->sn;
  872. sdata->u.mesh.last_sn_update = jiffies;
  873. }
  874. lifetime = default_lifetime(sdata);
  875. ttl = sdata->u.mesh.mshcfg.element_ttl;
  876. if (ttl == 0) {
  877. sdata->u.mesh.mshstats.dropped_frames_ttl++;
  878. spin_unlock_bh(&mpath->state_lock);
  879. goto enddiscovery;
  880. }
  881. if (preq_node->flags & PREQ_Q_F_REFRESH)
  882. target_flags = MP_F_DO;
  883. else
  884. target_flags = MP_F_RF;
  885. spin_unlock_bh(&mpath->state_lock);
  886. da = (mpath->is_root) ? mpath->rann_snd_addr : broadcast_addr;
  887. mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr,
  888. cpu_to_le32(ifmsh->sn), target_flags, mpath->dst,
  889. cpu_to_le32(mpath->sn), da, 0,
  890. ttl, cpu_to_le32(lifetime), 0,
  891. cpu_to_le32(ifmsh->preq_id++), sdata);
  892. mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
  893. enddiscovery:
  894. rcu_read_unlock();
  895. kfree(preq_node);
  896. }
  897. /* mesh_nexthop_resolve - lookup next hop for given skb and start path
  898. * discovery if no forwarding information is found.
  899. *
  900. * @skb: 802.11 frame to be sent
  901. * @sdata: network subif the frame will be sent through
  902. *
  903. * Returns: 0 if the next hop was found and -ENOENT if the frame was queued.
  904. * skb is freeed here if no mpath could be allocated.
  905. */
  906. int mesh_nexthop_resolve(struct sk_buff *skb,
  907. struct ieee80211_sub_if_data *sdata)
  908. {
  909. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  910. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  911. struct mesh_path *mpath;
  912. struct sk_buff *skb_to_free = NULL;
  913. u8 *target_addr = hdr->addr3;
  914. int err = 0;
  915. rcu_read_lock();
  916. err = mesh_nexthop_lookup(skb, sdata);
  917. if (!err)
  918. goto endlookup;
  919. /* no nexthop found, start resolving */
  920. mpath = mesh_path_lookup(target_addr, sdata);
  921. if (!mpath) {
  922. mesh_path_add(target_addr, sdata);
  923. mpath = mesh_path_lookup(target_addr, sdata);
  924. if (!mpath) {
  925. mesh_path_discard_frame(skb, sdata);
  926. err = -ENOSPC;
  927. goto endlookup;
  928. }
  929. }
  930. if (!(mpath->flags & MESH_PATH_RESOLVING))
  931. mesh_queue_preq(mpath, PREQ_Q_F_START);
  932. if (skb_queue_len(&mpath->frame_queue) >= MESH_FRAME_QUEUE_LEN)
  933. skb_to_free = skb_dequeue(&mpath->frame_queue);
  934. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  935. ieee80211_set_qos_hdr(sdata, skb);
  936. skb_queue_tail(&mpath->frame_queue, skb);
  937. err = -ENOENT;
  938. if (skb_to_free)
  939. mesh_path_discard_frame(skb_to_free, sdata);
  940. endlookup:
  941. rcu_read_unlock();
  942. return err;
  943. }
  944. /**
  945. * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame. Calling
  946. * this function is considered "using" the associated mpath, so preempt a path
  947. * refresh if this mpath expires soon.
  948. *
  949. * @skb: 802.11 frame to be sent
  950. * @sdata: network subif the frame will be sent through
  951. *
  952. * Returns: 0 if the next hop was found. Nonzero otherwise.
  953. */
  954. int mesh_nexthop_lookup(struct sk_buff *skb,
  955. struct ieee80211_sub_if_data *sdata)
  956. {
  957. struct mesh_path *mpath;
  958. struct sta_info *next_hop;
  959. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  960. u8 *target_addr = hdr->addr3;
  961. int err = -ENOENT;
  962. rcu_read_lock();
  963. mpath = mesh_path_lookup(target_addr, sdata);
  964. if (!mpath || !(mpath->flags & MESH_PATH_ACTIVE))
  965. goto endlookup;
  966. if (time_after(jiffies,
  967. mpath->exp_time -
  968. msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
  969. !compare_ether_addr(sdata->vif.addr, hdr->addr4) &&
  970. !(mpath->flags & MESH_PATH_RESOLVING) &&
  971. !(mpath->flags & MESH_PATH_FIXED))
  972. mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
  973. next_hop = rcu_dereference(mpath->next_hop);
  974. if (next_hop) {
  975. memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
  976. memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
  977. err = 0;
  978. }
  979. endlookup:
  980. rcu_read_unlock();
  981. return err;
  982. }
  983. void mesh_path_timer(unsigned long data)
  984. {
  985. struct mesh_path *mpath = (void *) data;
  986. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  987. int ret;
  988. if (sdata->local->quiescing)
  989. return;
  990. spin_lock_bh(&mpath->state_lock);
  991. if (mpath->flags & MESH_PATH_RESOLVED ||
  992. (!(mpath->flags & MESH_PATH_RESOLVING))) {
  993. mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
  994. spin_unlock_bh(&mpath->state_lock);
  995. } else if (mpath->discovery_retries < max_preq_retries(sdata)) {
  996. ++mpath->discovery_retries;
  997. mpath->discovery_timeout *= 2;
  998. mpath->flags &= ~MESH_PATH_REQ_QUEUED;
  999. spin_unlock_bh(&mpath->state_lock);
  1000. mesh_queue_preq(mpath, 0);
  1001. } else {
  1002. mpath->flags = 0;
  1003. mpath->exp_time = jiffies;
  1004. spin_unlock_bh(&mpath->state_lock);
  1005. if (!mpath->is_gate && mesh_gate_num(sdata) > 0) {
  1006. ret = mesh_path_send_to_gates(mpath);
  1007. if (ret)
  1008. mhwmp_dbg("no gate was reachable");
  1009. } else
  1010. mesh_path_flush_pending(mpath);
  1011. }
  1012. }
  1013. void
  1014. mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
  1015. {
  1016. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  1017. u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
  1018. u8 flags;
  1019. flags = (ifmsh->mshcfg.dot11MeshGateAnnouncementProtocol)
  1020. ? RANN_FLAG_IS_GATE : 0;
  1021. mesh_path_sel_frame_tx(MPATH_RANN, flags, sdata->vif.addr,
  1022. cpu_to_le32(++ifmsh->sn),
  1023. 0, NULL, 0, broadcast_addr,
  1024. 0, sdata->u.mesh.mshcfg.element_ttl,
  1025. cpu_to_le32(interval), 0, 0, sdata);
  1026. }