mesh_hwmp.c 35 KB

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