mesh_hwmp.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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. pr_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) ((s32)(y - x) < 0)
  77. #define SN_LT(x, y) ((s32)(x - 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. #define root_path_confirmation_jiffies(s) \
  88. msecs_to_jiffies(sdata->u.mesh.mshcfg.dot11MeshHWMPconfirmationInterval)
  89. enum mpath_frame_type {
  90. MPATH_PREQ = 0,
  91. MPATH_PREP,
  92. MPATH_PERR,
  93. MPATH_RANN
  94. };
  95. static const u8 broadcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  96. static int mesh_path_sel_frame_tx(enum mpath_frame_type action, u8 flags,
  97. u8 *orig_addr, __le32 orig_sn, u8 target_flags, u8 *target,
  98. __le32 target_sn, const u8 *da, u8 hop_count, u8 ttl,
  99. __le32 lifetime, __le32 metric, __le32 preq_id,
  100. struct ieee80211_sub_if_data *sdata)
  101. {
  102. struct ieee80211_local *local = sdata->local;
  103. struct sk_buff *skb;
  104. struct ieee80211_mgmt *mgmt;
  105. u8 *pos, ie_len;
  106. int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.mesh_action) +
  107. sizeof(mgmt->u.action.u.mesh_action);
  108. skb = dev_alloc_skb(local->tx_headroom +
  109. hdr_len +
  110. 2 + 37); /* max HWMP IE */
  111. if (!skb)
  112. return -1;
  113. skb_reserve(skb, local->tx_headroom);
  114. mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
  115. memset(mgmt, 0, hdr_len);
  116. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  117. IEEE80211_STYPE_ACTION);
  118. memcpy(mgmt->da, da, ETH_ALEN);
  119. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  120. /* BSSID == SA */
  121. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  122. mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
  123. mgmt->u.action.u.mesh_action.action_code =
  124. WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
  125. switch (action) {
  126. case MPATH_PREQ:
  127. mhwmp_dbg("sending PREQ to %pM", target);
  128. ie_len = 37;
  129. pos = skb_put(skb, 2 + ie_len);
  130. *pos++ = WLAN_EID_PREQ;
  131. break;
  132. case MPATH_PREP:
  133. mhwmp_dbg("sending PREP to %pM", target);
  134. ie_len = 31;
  135. pos = skb_put(skb, 2 + ie_len);
  136. *pos++ = WLAN_EID_PREP;
  137. break;
  138. case MPATH_RANN:
  139. mhwmp_dbg("sending RANN from %pM", orig_addr);
  140. ie_len = sizeof(struct ieee80211_rann_ie);
  141. pos = skb_put(skb, 2 + ie_len);
  142. *pos++ = WLAN_EID_RANN;
  143. break;
  144. default:
  145. kfree_skb(skb);
  146. return -ENOTSUPP;
  147. break;
  148. }
  149. *pos++ = ie_len;
  150. *pos++ = flags;
  151. *pos++ = hop_count;
  152. *pos++ = ttl;
  153. if (action == MPATH_PREP) {
  154. memcpy(pos, target, ETH_ALEN);
  155. pos += ETH_ALEN;
  156. memcpy(pos, &target_sn, 4);
  157. pos += 4;
  158. } else {
  159. if (action == MPATH_PREQ) {
  160. memcpy(pos, &preq_id, 4);
  161. pos += 4;
  162. }
  163. memcpy(pos, orig_addr, ETH_ALEN);
  164. pos += ETH_ALEN;
  165. memcpy(pos, &orig_sn, 4);
  166. pos += 4;
  167. }
  168. memcpy(pos, &lifetime, 4); /* interval for RANN */
  169. pos += 4;
  170. memcpy(pos, &metric, 4);
  171. pos += 4;
  172. if (action == MPATH_PREQ) {
  173. *pos++ = 1; /* destination count */
  174. *pos++ = target_flags;
  175. memcpy(pos, target, ETH_ALEN);
  176. pos += ETH_ALEN;
  177. memcpy(pos, &target_sn, 4);
  178. pos += 4;
  179. } else if (action == MPATH_PREP) {
  180. memcpy(pos, orig_addr, ETH_ALEN);
  181. pos += ETH_ALEN;
  182. memcpy(pos, &orig_sn, 4);
  183. pos += 4;
  184. }
  185. ieee80211_tx_skb(sdata, skb);
  186. return 0;
  187. }
  188. /* Headroom is not adjusted. Caller should ensure that skb has sufficient
  189. * headroom in case the frame is encrypted. */
  190. static void prepare_frame_for_deferred_tx(struct ieee80211_sub_if_data *sdata,
  191. struct sk_buff *skb)
  192. {
  193. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  194. skb_set_mac_header(skb, 0);
  195. skb_set_network_header(skb, 0);
  196. skb_set_transport_header(skb, 0);
  197. /* Send all internal mgmt frames on VO. Accordingly set TID to 7. */
  198. skb_set_queue_mapping(skb, IEEE80211_AC_VO);
  199. skb->priority = 7;
  200. info->control.vif = &sdata->vif;
  201. ieee80211_set_qos_hdr(sdata, skb);
  202. }
  203. /**
  204. * mesh_send_path error - Sends a PERR mesh management frame
  205. *
  206. * @target: broken destination
  207. * @target_sn: SN of the broken destination
  208. * @target_rcode: reason code for this PERR
  209. * @ra: node this frame is addressed to
  210. *
  211. * Note: This function may be called with driver locks taken that the driver
  212. * also acquires in the TX path. To avoid a deadlock we don't transmit the
  213. * frame directly but add it to the pending queue instead.
  214. */
  215. int mesh_path_error_tx(u8 ttl, u8 *target, __le32 target_sn,
  216. __le16 target_rcode, const u8 *ra,
  217. struct ieee80211_sub_if_data *sdata)
  218. {
  219. struct ieee80211_local *local = sdata->local;
  220. struct sk_buff *skb;
  221. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  222. struct ieee80211_mgmt *mgmt;
  223. u8 *pos, ie_len;
  224. int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.mesh_action) +
  225. sizeof(mgmt->u.action.u.mesh_action);
  226. if (time_before(jiffies, ifmsh->next_perr))
  227. return -EAGAIN;
  228. skb = dev_alloc_skb(local->tx_headroom +
  229. hdr_len +
  230. 2 + 15 /* PERR IE */);
  231. if (!skb)
  232. return -1;
  233. skb_reserve(skb, local->tx_headroom);
  234. mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
  235. memset(mgmt, 0, hdr_len);
  236. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  237. IEEE80211_STYPE_ACTION);
  238. memcpy(mgmt->da, ra, ETH_ALEN);
  239. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  240. /* BSSID == SA */
  241. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  242. mgmt->u.action.category = WLAN_CATEGORY_MESH_ACTION;
  243. mgmt->u.action.u.mesh_action.action_code =
  244. WLAN_MESH_ACTION_HWMP_PATH_SELECTION;
  245. ie_len = 15;
  246. pos = skb_put(skb, 2 + ie_len);
  247. *pos++ = WLAN_EID_PERR;
  248. *pos++ = ie_len;
  249. /* ttl */
  250. *pos++ = ttl;
  251. /* number of destinations */
  252. *pos++ = 1;
  253. /*
  254. * flags bit, bit 1 is unset if we know the sequence number and
  255. * bit 2 is set if we have a reason code
  256. */
  257. *pos = 0;
  258. if (!target_sn)
  259. *pos |= MP_F_USN;
  260. if (target_rcode)
  261. *pos |= MP_F_RCODE;
  262. pos++;
  263. memcpy(pos, target, ETH_ALEN);
  264. pos += ETH_ALEN;
  265. memcpy(pos, &target_sn, 4);
  266. pos += 4;
  267. memcpy(pos, &target_rcode, 2);
  268. /* see note in function header */
  269. prepare_frame_for_deferred_tx(sdata, skb);
  270. ifmsh->next_perr = TU_TO_EXP_TIME(
  271. ifmsh->mshcfg.dot11MeshHWMPperrMinInterval);
  272. ieee80211_add_pending_skb(local, skb);
  273. return 0;
  274. }
  275. void ieee80211s_update_metric(struct ieee80211_local *local,
  276. struct sta_info *sta, struct sk_buff *skb)
  277. {
  278. struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
  279. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  280. int failed;
  281. if (!ieee80211_is_data(hdr->frame_control))
  282. return;
  283. failed = !(txinfo->flags & IEEE80211_TX_STAT_ACK);
  284. /* moving average, scaled to 100 */
  285. sta->fail_avg = ((80 * sta->fail_avg + 5) / 100 + 20 * failed);
  286. if (sta->fail_avg > 95)
  287. mesh_plink_broken(sta);
  288. }
  289. static u32 airtime_link_metric_get(struct ieee80211_local *local,
  290. struct sta_info *sta)
  291. {
  292. struct rate_info rinfo;
  293. /* This should be adjusted for each device */
  294. int device_constant = 1 << ARITH_SHIFT;
  295. int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT;
  296. int s_unit = 1 << ARITH_SHIFT;
  297. int rate, err;
  298. u32 tx_time, estimated_retx;
  299. u64 result;
  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 (ether_addr_equal(orig_addr, sdata->vif.addr)) {
  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 (ether_addr_equal(orig_addr, ta))
  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, flags;
  470. u32 orig_sn, target_sn, lifetime, orig_metric;
  471. bool reply = false;
  472. bool forward = true;
  473. bool root_is_gate;
  474. /* Update target SN, if present */
  475. target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
  476. orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
  477. target_sn = PREQ_IE_TARGET_SN(preq_elem);
  478. orig_sn = PREQ_IE_ORIG_SN(preq_elem);
  479. target_flags = PREQ_IE_TARGET_F(preq_elem);
  480. orig_metric = metric;
  481. /* Proactive PREQ gate announcements */
  482. flags = PREQ_IE_FLAGS(preq_elem);
  483. root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
  484. mhwmp_dbg("received PREQ from %pM", orig_addr);
  485. if (ether_addr_equal(target_addr, sdata->vif.addr)) {
  486. mhwmp_dbg("PREQ is for us");
  487. forward = false;
  488. reply = true;
  489. metric = 0;
  490. if (time_after(jiffies, ifmsh->last_sn_update +
  491. net_traversal_jiffies(sdata)) ||
  492. time_before(jiffies, ifmsh->last_sn_update)) {
  493. target_sn = ++ifmsh->sn;
  494. ifmsh->last_sn_update = jiffies;
  495. }
  496. } else if (is_broadcast_ether_addr(target_addr) &&
  497. (target_flags & IEEE80211_PREQ_TO_FLAG)) {
  498. rcu_read_lock();
  499. mpath = mesh_path_lookup(orig_addr, sdata);
  500. if (mpath) {
  501. if (flags & IEEE80211_PREQ_PROACTIVE_PREP_FLAG) {
  502. reply = true;
  503. target_addr = sdata->vif.addr;
  504. target_sn = ++ifmsh->sn;
  505. metric = 0;
  506. ifmsh->last_sn_update = jiffies;
  507. }
  508. if (root_is_gate)
  509. mesh_path_add_gate(mpath);
  510. }
  511. rcu_read_unlock();
  512. } else {
  513. rcu_read_lock();
  514. mpath = mesh_path_lookup(target_addr, sdata);
  515. if (mpath) {
  516. if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
  517. SN_LT(mpath->sn, target_sn)) {
  518. mpath->sn = target_sn;
  519. mpath->flags |= MESH_PATH_SN_VALID;
  520. } else if ((!(target_flags & MP_F_DO)) &&
  521. (mpath->flags & MESH_PATH_ACTIVE)) {
  522. reply = true;
  523. metric = mpath->metric;
  524. target_sn = mpath->sn;
  525. if (target_flags & MP_F_RF)
  526. target_flags |= MP_F_DO;
  527. else
  528. forward = false;
  529. }
  530. }
  531. rcu_read_unlock();
  532. }
  533. if (reply) {
  534. lifetime = PREQ_IE_LIFETIME(preq_elem);
  535. ttl = ifmsh->mshcfg.element_ttl;
  536. if (ttl != 0) {
  537. mhwmp_dbg("replying to the PREQ");
  538. mesh_path_sel_frame_tx(MPATH_PREP, 0, orig_addr,
  539. cpu_to_le32(orig_sn), 0, target_addr,
  540. cpu_to_le32(target_sn), mgmt->sa, 0, ttl,
  541. cpu_to_le32(lifetime), cpu_to_le32(metric),
  542. 0, sdata);
  543. } else {
  544. ifmsh->mshstats.dropped_frames_ttl++;
  545. }
  546. }
  547. if (forward && ifmsh->mshcfg.dot11MeshForwarding) {
  548. u32 preq_id;
  549. u8 hopcount;
  550. ttl = PREQ_IE_TTL(preq_elem);
  551. lifetime = PREQ_IE_LIFETIME(preq_elem);
  552. if (ttl <= 1) {
  553. ifmsh->mshstats.dropped_frames_ttl++;
  554. return;
  555. }
  556. mhwmp_dbg("forwarding the PREQ from %pM", orig_addr);
  557. --ttl;
  558. preq_id = PREQ_IE_PREQ_ID(preq_elem);
  559. hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
  560. da = (mpath && mpath->is_root) ?
  561. mpath->rann_snd_addr : broadcast_addr;
  562. if (flags & IEEE80211_PREQ_PROACTIVE_PREP_FLAG) {
  563. target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
  564. target_sn = PREQ_IE_TARGET_SN(preq_elem);
  565. metric = orig_metric;
  566. }
  567. mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
  568. cpu_to_le32(orig_sn), target_flags, target_addr,
  569. cpu_to_le32(target_sn), da,
  570. hopcount, ttl, cpu_to_le32(lifetime),
  571. cpu_to_le32(metric), cpu_to_le32(preq_id),
  572. sdata);
  573. if (!is_multicast_ether_addr(da))
  574. ifmsh->mshstats.fwded_unicast++;
  575. else
  576. ifmsh->mshstats.fwded_mcast++;
  577. ifmsh->mshstats.fwded_frames++;
  578. }
  579. }
  580. static inline struct sta_info *
  581. next_hop_deref_protected(struct mesh_path *mpath)
  582. {
  583. return rcu_dereference_protected(mpath->next_hop,
  584. lockdep_is_held(&mpath->state_lock));
  585. }
  586. static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
  587. struct ieee80211_mgmt *mgmt,
  588. u8 *prep_elem, u32 metric)
  589. {
  590. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  591. struct mesh_path *mpath;
  592. u8 *target_addr, *orig_addr;
  593. u8 ttl, hopcount, flags;
  594. u8 next_hop[ETH_ALEN];
  595. u32 target_sn, orig_sn, lifetime;
  596. mhwmp_dbg("received PREP from %pM", PREP_IE_ORIG_ADDR(prep_elem));
  597. orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
  598. if (ether_addr_equal(orig_addr, sdata->vif.addr))
  599. /* destination, no forwarding required */
  600. return;
  601. if (!ifmsh->mshcfg.dot11MeshForwarding)
  602. return;
  603. ttl = PREP_IE_TTL(prep_elem);
  604. if (ttl <= 1) {
  605. sdata->u.mesh.mshstats.dropped_frames_ttl++;
  606. return;
  607. }
  608. rcu_read_lock();
  609. mpath = mesh_path_lookup(orig_addr, sdata);
  610. if (mpath)
  611. spin_lock_bh(&mpath->state_lock);
  612. else
  613. goto fail;
  614. if (!(mpath->flags & MESH_PATH_ACTIVE)) {
  615. spin_unlock_bh(&mpath->state_lock);
  616. goto fail;
  617. }
  618. memcpy(next_hop, next_hop_deref_protected(mpath)->sta.addr, ETH_ALEN);
  619. spin_unlock_bh(&mpath->state_lock);
  620. --ttl;
  621. flags = PREP_IE_FLAGS(prep_elem);
  622. lifetime = PREP_IE_LIFETIME(prep_elem);
  623. hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
  624. target_addr = PREP_IE_TARGET_ADDR(prep_elem);
  625. target_sn = PREP_IE_TARGET_SN(prep_elem);
  626. orig_sn = PREP_IE_ORIG_SN(prep_elem);
  627. mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr,
  628. cpu_to_le32(orig_sn), 0, target_addr,
  629. cpu_to_le32(target_sn), next_hop, hopcount,
  630. ttl, cpu_to_le32(lifetime), cpu_to_le32(metric),
  631. 0, sdata);
  632. rcu_read_unlock();
  633. sdata->u.mesh.mshstats.fwded_unicast++;
  634. sdata->u.mesh.mshstats.fwded_frames++;
  635. return;
  636. fail:
  637. rcu_read_unlock();
  638. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  639. }
  640. static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
  641. struct ieee80211_mgmt *mgmt, u8 *perr_elem)
  642. {
  643. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  644. struct mesh_path *mpath;
  645. u8 ttl;
  646. u8 *ta, *target_addr;
  647. u32 target_sn;
  648. u16 target_rcode;
  649. ta = mgmt->sa;
  650. ttl = PERR_IE_TTL(perr_elem);
  651. if (ttl <= 1) {
  652. ifmsh->mshstats.dropped_frames_ttl++;
  653. return;
  654. }
  655. ttl--;
  656. target_addr = PERR_IE_TARGET_ADDR(perr_elem);
  657. target_sn = PERR_IE_TARGET_SN(perr_elem);
  658. target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
  659. rcu_read_lock();
  660. mpath = mesh_path_lookup(target_addr, sdata);
  661. if (mpath) {
  662. struct sta_info *sta;
  663. spin_lock_bh(&mpath->state_lock);
  664. sta = next_hop_deref_protected(mpath);
  665. if (mpath->flags & MESH_PATH_ACTIVE &&
  666. ether_addr_equal(ta, sta->sta.addr) &&
  667. (!(mpath->flags & MESH_PATH_SN_VALID) ||
  668. SN_GT(target_sn, mpath->sn))) {
  669. mpath->flags &= ~MESH_PATH_ACTIVE;
  670. mpath->sn = target_sn;
  671. spin_unlock_bh(&mpath->state_lock);
  672. if (!ifmsh->mshcfg.dot11MeshForwarding)
  673. goto endperr;
  674. mesh_path_error_tx(ttl, target_addr, cpu_to_le32(target_sn),
  675. cpu_to_le16(target_rcode),
  676. broadcast_addr, sdata);
  677. } else
  678. spin_unlock_bh(&mpath->state_lock);
  679. }
  680. endperr:
  681. rcu_read_unlock();
  682. }
  683. static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
  684. struct ieee80211_mgmt *mgmt,
  685. struct ieee80211_rann_ie *rann)
  686. {
  687. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  688. struct ieee80211_local *local = sdata->local;
  689. struct sta_info *sta;
  690. struct mesh_path *mpath;
  691. u8 ttl, flags, hopcount;
  692. u8 *orig_addr;
  693. u32 orig_sn, metric, metric_txsta, interval;
  694. bool root_is_gate;
  695. ttl = rann->rann_ttl;
  696. if (ttl <= 1) {
  697. ifmsh->mshstats.dropped_frames_ttl++;
  698. return;
  699. }
  700. ttl--;
  701. flags = rann->rann_flags;
  702. root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
  703. orig_addr = rann->rann_addr;
  704. orig_sn = le32_to_cpu(rann->rann_seq);
  705. interval = le32_to_cpu(rann->rann_interval);
  706. hopcount = rann->rann_hopcount;
  707. hopcount++;
  708. metric = le32_to_cpu(rann->rann_metric);
  709. /* Ignore our own RANNs */
  710. if (ether_addr_equal(orig_addr, sdata->vif.addr))
  711. return;
  712. mhwmp_dbg("received RANN from %pM via neighbour %pM (is_gate=%d)",
  713. orig_addr, mgmt->sa, root_is_gate);
  714. rcu_read_lock();
  715. sta = sta_info_get(sdata, mgmt->sa);
  716. if (!sta) {
  717. rcu_read_unlock();
  718. return;
  719. }
  720. metric_txsta = airtime_link_metric_get(local, sta);
  721. mpath = mesh_path_lookup(orig_addr, sdata);
  722. if (!mpath) {
  723. mesh_path_add(orig_addr, sdata);
  724. mpath = mesh_path_lookup(orig_addr, sdata);
  725. if (!mpath) {
  726. rcu_read_unlock();
  727. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  728. return;
  729. }
  730. }
  731. if ((!(mpath->flags & (MESH_PATH_ACTIVE | MESH_PATH_RESOLVING)) ||
  732. (time_after(jiffies, mpath->last_preq_to_root +
  733. root_path_confirmation_jiffies(sdata)) ||
  734. time_before(jiffies, mpath->last_preq_to_root))) &&
  735. !(mpath->flags & MESH_PATH_FIXED)) {
  736. mhwmp_dbg("%s time to refresh root mpath %pM", sdata->name,
  737. orig_addr);
  738. mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
  739. mpath->last_preq_to_root = jiffies;
  740. }
  741. if ((SN_LT(mpath->sn, orig_sn) || (mpath->sn == orig_sn &&
  742. metric < mpath->rann_metric)) && ifmsh->mshcfg.dot11MeshForwarding) {
  743. mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
  744. cpu_to_le32(orig_sn),
  745. 0, NULL, 0, broadcast_addr,
  746. hopcount, ttl, cpu_to_le32(interval),
  747. cpu_to_le32(metric + metric_txsta),
  748. 0, sdata);
  749. mpath->sn = orig_sn;
  750. mpath->rann_metric = metric + metric_txsta;
  751. /* Recording RANNs sender address to send individually
  752. * addressed PREQs destined for root mesh STA */
  753. memcpy(mpath->rann_snd_addr, mgmt->sa, ETH_ALEN);
  754. }
  755. mpath->is_root = true;
  756. if (root_is_gate)
  757. mesh_path_add_gate(mpath);
  758. rcu_read_unlock();
  759. }
  760. void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
  761. struct ieee80211_mgmt *mgmt,
  762. size_t len)
  763. {
  764. struct ieee802_11_elems elems;
  765. size_t baselen;
  766. u32 last_hop_metric;
  767. struct sta_info *sta;
  768. /* need action_code */
  769. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  770. return;
  771. rcu_read_lock();
  772. sta = sta_info_get(sdata, mgmt->sa);
  773. if (!sta || sta->plink_state != NL80211_PLINK_ESTAB) {
  774. rcu_read_unlock();
  775. return;
  776. }
  777. rcu_read_unlock();
  778. baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
  779. ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
  780. len - baselen, &elems);
  781. if (elems.preq) {
  782. if (elems.preq_len != 37)
  783. /* Right now we support just 1 destination and no AE */
  784. return;
  785. last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
  786. MPATH_PREQ);
  787. if (last_hop_metric)
  788. hwmp_preq_frame_process(sdata, mgmt, elems.preq,
  789. last_hop_metric);
  790. }
  791. if (elems.prep) {
  792. if (elems.prep_len != 31)
  793. /* Right now we support no AE */
  794. return;
  795. last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
  796. MPATH_PREP);
  797. if (last_hop_metric)
  798. hwmp_prep_frame_process(sdata, mgmt, elems.prep,
  799. last_hop_metric);
  800. }
  801. if (elems.perr) {
  802. if (elems.perr_len != 15)
  803. /* Right now we support only one destination per PERR */
  804. return;
  805. hwmp_perr_frame_process(sdata, mgmt, elems.perr);
  806. }
  807. if (elems.rann)
  808. hwmp_rann_frame_process(sdata, mgmt, elems.rann);
  809. }
  810. /**
  811. * mesh_queue_preq - queue a PREQ to a given destination
  812. *
  813. * @mpath: mesh path to discover
  814. * @flags: special attributes of the PREQ to be sent
  815. *
  816. * Locking: the function must be called from within a rcu read lock block.
  817. *
  818. */
  819. static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
  820. {
  821. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  822. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  823. struct mesh_preq_queue *preq_node;
  824. preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
  825. if (!preq_node) {
  826. mhwmp_dbg("could not allocate PREQ node");
  827. return;
  828. }
  829. spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
  830. if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
  831. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  832. kfree(preq_node);
  833. if (printk_ratelimit())
  834. mhwmp_dbg("PREQ node queue full");
  835. return;
  836. }
  837. spin_lock(&mpath->state_lock);
  838. if (mpath->flags & MESH_PATH_REQ_QUEUED) {
  839. spin_unlock(&mpath->state_lock);
  840. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  841. kfree(preq_node);
  842. return;
  843. }
  844. memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
  845. preq_node->flags = flags;
  846. mpath->flags |= MESH_PATH_REQ_QUEUED;
  847. spin_unlock(&mpath->state_lock);
  848. list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
  849. ++ifmsh->preq_queue_len;
  850. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  851. if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
  852. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  853. else if (time_before(jiffies, ifmsh->last_preq)) {
  854. /* avoid long wait if did not send preqs for a long time
  855. * and jiffies wrapped around
  856. */
  857. ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
  858. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  859. } else
  860. mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
  861. min_preq_int_jiff(sdata));
  862. }
  863. /**
  864. * mesh_path_start_discovery - launch a path discovery from the PREQ queue
  865. *
  866. * @sdata: local mesh subif
  867. */
  868. void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
  869. {
  870. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  871. struct mesh_preq_queue *preq_node;
  872. struct mesh_path *mpath;
  873. u8 ttl, target_flags;
  874. const u8 *da;
  875. u32 lifetime;
  876. spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
  877. if (!ifmsh->preq_queue_len ||
  878. time_before(jiffies, ifmsh->last_preq +
  879. min_preq_int_jiff(sdata))) {
  880. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  881. return;
  882. }
  883. preq_node = list_first_entry(&ifmsh->preq_queue.list,
  884. struct mesh_preq_queue, list);
  885. list_del(&preq_node->list);
  886. --ifmsh->preq_queue_len;
  887. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  888. rcu_read_lock();
  889. mpath = mesh_path_lookup(preq_node->dst, sdata);
  890. if (!mpath)
  891. goto enddiscovery;
  892. spin_lock_bh(&mpath->state_lock);
  893. mpath->flags &= ~MESH_PATH_REQ_QUEUED;
  894. if (preq_node->flags & PREQ_Q_F_START) {
  895. if (mpath->flags & MESH_PATH_RESOLVING) {
  896. spin_unlock_bh(&mpath->state_lock);
  897. goto enddiscovery;
  898. } else {
  899. mpath->flags &= ~MESH_PATH_RESOLVED;
  900. mpath->flags |= MESH_PATH_RESOLVING;
  901. mpath->discovery_retries = 0;
  902. mpath->discovery_timeout = disc_timeout_jiff(sdata);
  903. }
  904. } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
  905. mpath->flags & MESH_PATH_RESOLVED) {
  906. mpath->flags &= ~MESH_PATH_RESOLVING;
  907. spin_unlock_bh(&mpath->state_lock);
  908. goto enddiscovery;
  909. }
  910. ifmsh->last_preq = jiffies;
  911. if (time_after(jiffies, ifmsh->last_sn_update +
  912. net_traversal_jiffies(sdata)) ||
  913. time_before(jiffies, ifmsh->last_sn_update)) {
  914. ++ifmsh->sn;
  915. sdata->u.mesh.last_sn_update = jiffies;
  916. }
  917. lifetime = default_lifetime(sdata);
  918. ttl = sdata->u.mesh.mshcfg.element_ttl;
  919. if (ttl == 0) {
  920. sdata->u.mesh.mshstats.dropped_frames_ttl++;
  921. spin_unlock_bh(&mpath->state_lock);
  922. goto enddiscovery;
  923. }
  924. if (preq_node->flags & PREQ_Q_F_REFRESH)
  925. target_flags = MP_F_DO;
  926. else
  927. target_flags = MP_F_RF;
  928. spin_unlock_bh(&mpath->state_lock);
  929. da = (mpath->is_root) ? mpath->rann_snd_addr : broadcast_addr;
  930. mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr,
  931. cpu_to_le32(ifmsh->sn), target_flags, mpath->dst,
  932. cpu_to_le32(mpath->sn), da, 0,
  933. ttl, cpu_to_le32(lifetime), 0,
  934. cpu_to_le32(ifmsh->preq_id++), sdata);
  935. mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
  936. enddiscovery:
  937. rcu_read_unlock();
  938. kfree(preq_node);
  939. }
  940. /* mesh_nexthop_resolve - lookup next hop for given skb and start path
  941. * discovery if no forwarding information is found.
  942. *
  943. * @skb: 802.11 frame to be sent
  944. * @sdata: network subif the frame will be sent through
  945. *
  946. * Returns: 0 if the next hop was found and -ENOENT if the frame was queued.
  947. * skb is freeed here if no mpath could be allocated.
  948. */
  949. int mesh_nexthop_resolve(struct sk_buff *skb,
  950. struct ieee80211_sub_if_data *sdata)
  951. {
  952. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  953. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  954. struct mesh_path *mpath;
  955. struct sk_buff *skb_to_free = NULL;
  956. u8 *target_addr = hdr->addr3;
  957. int err = 0;
  958. rcu_read_lock();
  959. err = mesh_nexthop_lookup(skb, sdata);
  960. if (!err)
  961. goto endlookup;
  962. /* no nexthop found, start resolving */
  963. mpath = mesh_path_lookup(target_addr, sdata);
  964. if (!mpath) {
  965. mesh_path_add(target_addr, sdata);
  966. mpath = mesh_path_lookup(target_addr, sdata);
  967. if (!mpath) {
  968. mesh_path_discard_frame(skb, sdata);
  969. err = -ENOSPC;
  970. goto endlookup;
  971. }
  972. }
  973. if (!(mpath->flags & MESH_PATH_RESOLVING))
  974. mesh_queue_preq(mpath, PREQ_Q_F_START);
  975. if (skb_queue_len(&mpath->frame_queue) >= MESH_FRAME_QUEUE_LEN)
  976. skb_to_free = skb_dequeue(&mpath->frame_queue);
  977. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  978. ieee80211_set_qos_hdr(sdata, skb);
  979. skb_queue_tail(&mpath->frame_queue, skb);
  980. err = -ENOENT;
  981. if (skb_to_free)
  982. mesh_path_discard_frame(skb_to_free, sdata);
  983. endlookup:
  984. rcu_read_unlock();
  985. return err;
  986. }
  987. /**
  988. * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame. Calling
  989. * this function is considered "using" the associated mpath, so preempt a path
  990. * refresh if this mpath expires soon.
  991. *
  992. * @skb: 802.11 frame to be sent
  993. * @sdata: network subif the frame will be sent through
  994. *
  995. * Returns: 0 if the next hop was found. Nonzero otherwise.
  996. */
  997. int mesh_nexthop_lookup(struct sk_buff *skb,
  998. struct ieee80211_sub_if_data *sdata)
  999. {
  1000. struct mesh_path *mpath;
  1001. struct sta_info *next_hop;
  1002. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1003. u8 *target_addr = hdr->addr3;
  1004. int err = -ENOENT;
  1005. rcu_read_lock();
  1006. mpath = mesh_path_lookup(target_addr, sdata);
  1007. if (!mpath || !(mpath->flags & MESH_PATH_ACTIVE))
  1008. goto endlookup;
  1009. if (time_after(jiffies,
  1010. mpath->exp_time -
  1011. msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
  1012. ether_addr_equal(sdata->vif.addr, hdr->addr4) &&
  1013. !(mpath->flags & MESH_PATH_RESOLVING) &&
  1014. !(mpath->flags & MESH_PATH_FIXED))
  1015. mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
  1016. next_hop = rcu_dereference(mpath->next_hop);
  1017. if (next_hop) {
  1018. memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
  1019. memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
  1020. err = 0;
  1021. }
  1022. endlookup:
  1023. rcu_read_unlock();
  1024. return err;
  1025. }
  1026. void mesh_path_timer(unsigned long data)
  1027. {
  1028. struct mesh_path *mpath = (void *) data;
  1029. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  1030. int ret;
  1031. if (sdata->local->quiescing)
  1032. return;
  1033. spin_lock_bh(&mpath->state_lock);
  1034. if (mpath->flags & MESH_PATH_RESOLVED ||
  1035. (!(mpath->flags & MESH_PATH_RESOLVING))) {
  1036. mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
  1037. spin_unlock_bh(&mpath->state_lock);
  1038. } else if (mpath->discovery_retries < max_preq_retries(sdata)) {
  1039. ++mpath->discovery_retries;
  1040. mpath->discovery_timeout *= 2;
  1041. mpath->flags &= ~MESH_PATH_REQ_QUEUED;
  1042. spin_unlock_bh(&mpath->state_lock);
  1043. mesh_queue_preq(mpath, 0);
  1044. } else {
  1045. mpath->flags = 0;
  1046. mpath->exp_time = jiffies;
  1047. spin_unlock_bh(&mpath->state_lock);
  1048. if (!mpath->is_gate && mesh_gate_num(sdata) > 0) {
  1049. ret = mesh_path_send_to_gates(mpath);
  1050. if (ret)
  1051. mhwmp_dbg("no gate was reachable");
  1052. } else
  1053. mesh_path_flush_pending(mpath);
  1054. }
  1055. }
  1056. void
  1057. mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
  1058. {
  1059. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  1060. u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
  1061. u8 flags, target_flags = 0;
  1062. flags = (ifmsh->mshcfg.dot11MeshGateAnnouncementProtocol)
  1063. ? RANN_FLAG_IS_GATE : 0;
  1064. switch (ifmsh->mshcfg.dot11MeshHWMPRootMode) {
  1065. case IEEE80211_PROACTIVE_RANN:
  1066. mesh_path_sel_frame_tx(MPATH_RANN, flags, sdata->vif.addr,
  1067. cpu_to_le32(++ifmsh->sn),
  1068. 0, NULL, 0, broadcast_addr,
  1069. 0, ifmsh->mshcfg.element_ttl,
  1070. cpu_to_le32(interval), 0, 0, sdata);
  1071. break;
  1072. case IEEE80211_PROACTIVE_PREQ_WITH_PREP:
  1073. flags |= IEEE80211_PREQ_PROACTIVE_PREP_FLAG;
  1074. case IEEE80211_PROACTIVE_PREQ_NO_PREP:
  1075. interval = ifmsh->mshcfg.dot11MeshHWMPactivePathToRootTimeout;
  1076. target_flags |= IEEE80211_PREQ_TO_FLAG |
  1077. IEEE80211_PREQ_USN_FLAG;
  1078. mesh_path_sel_frame_tx(MPATH_PREQ, flags, sdata->vif.addr,
  1079. cpu_to_le32(++ifmsh->sn), target_flags,
  1080. (u8 *) broadcast_addr, 0, broadcast_addr,
  1081. 0, ifmsh->mshcfg.element_ttl,
  1082. cpu_to_le32(interval),
  1083. 0, cpu_to_le32(ifmsh->preq_id++), sdata);
  1084. break;
  1085. default:
  1086. mhwmp_dbg("Proactive mechanism not supported");
  1087. return;
  1088. }
  1089. }