mesh_hwmp.c 34 KB

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