mesh_hwmp.c 34 KB

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