mesh_hwmp.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  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", orig_addr);
  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. mpath = mesh_path_add(sdata, orig_addr);
  405. if (IS_ERR(mpath)) {
  406. rcu_read_unlock();
  407. return 0;
  408. }
  409. spin_lock_bh(&mpath->state_lock);
  410. }
  411. if (fresh_info) {
  412. mesh_path_assign_nexthop(mpath, sta);
  413. mpath->flags |= MESH_PATH_SN_VALID;
  414. mpath->metric = new_metric;
  415. mpath->sn = orig_sn;
  416. mpath->exp_time = time_after(mpath->exp_time, exp_time)
  417. ? mpath->exp_time : exp_time;
  418. mesh_path_activate(mpath);
  419. spin_unlock_bh(&mpath->state_lock);
  420. mesh_path_tx_pending(mpath);
  421. /* draft says preq_id should be saved to, but there does
  422. * not seem to be any use for it, skipping by now
  423. */
  424. } else
  425. spin_unlock_bh(&mpath->state_lock);
  426. }
  427. /* Update and check transmitter routing info */
  428. ta = mgmt->sa;
  429. if (ether_addr_equal(orig_addr, ta))
  430. fresh_info = false;
  431. else {
  432. fresh_info = true;
  433. mpath = mesh_path_lookup(sdata, ta);
  434. if (mpath) {
  435. spin_lock_bh(&mpath->state_lock);
  436. if ((mpath->flags & MESH_PATH_FIXED) ||
  437. ((mpath->flags & MESH_PATH_ACTIVE) &&
  438. (last_hop_metric > mpath->metric)))
  439. fresh_info = false;
  440. } else {
  441. mpath = mesh_path_add(sdata, ta);
  442. if (IS_ERR(mpath)) {
  443. rcu_read_unlock();
  444. return 0;
  445. }
  446. spin_lock_bh(&mpath->state_lock);
  447. }
  448. if (fresh_info) {
  449. mesh_path_assign_nexthop(mpath, sta);
  450. mpath->metric = last_hop_metric;
  451. mpath->exp_time = time_after(mpath->exp_time, exp_time)
  452. ? mpath->exp_time : exp_time;
  453. mesh_path_activate(mpath);
  454. spin_unlock_bh(&mpath->state_lock);
  455. mesh_path_tx_pending(mpath);
  456. } else
  457. spin_unlock_bh(&mpath->state_lock);
  458. }
  459. rcu_read_unlock();
  460. return process ? new_metric : 0;
  461. }
  462. static void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata,
  463. struct ieee80211_mgmt *mgmt,
  464. const u8 *preq_elem, u32 metric)
  465. {
  466. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  467. struct mesh_path *mpath = NULL;
  468. const u8 *target_addr, *orig_addr;
  469. const u8 *da;
  470. u8 target_flags, ttl, flags;
  471. u32 orig_sn, target_sn, lifetime, orig_metric;
  472. bool reply = false;
  473. bool forward = true;
  474. bool root_is_gate;
  475. /* Update target SN, if present */
  476. target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
  477. orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
  478. target_sn = PREQ_IE_TARGET_SN(preq_elem);
  479. orig_sn = PREQ_IE_ORIG_SN(preq_elem);
  480. target_flags = PREQ_IE_TARGET_F(preq_elem);
  481. orig_metric = metric;
  482. /* Proactive PREQ gate announcements */
  483. flags = PREQ_IE_FLAGS(preq_elem);
  484. root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
  485. mhwmp_dbg(sdata, "received PREQ from %pM\n", orig_addr);
  486. if (ether_addr_equal(target_addr, sdata->vif.addr)) {
  487. mhwmp_dbg(sdata, "PREQ is for us\n");
  488. forward = false;
  489. reply = true;
  490. metric = 0;
  491. if (time_after(jiffies, ifmsh->last_sn_update +
  492. net_traversal_jiffies(sdata)) ||
  493. time_before(jiffies, ifmsh->last_sn_update)) {
  494. target_sn = ++ifmsh->sn;
  495. ifmsh->last_sn_update = jiffies;
  496. }
  497. } else if (is_broadcast_ether_addr(target_addr) &&
  498. (target_flags & IEEE80211_PREQ_TO_FLAG)) {
  499. rcu_read_lock();
  500. mpath = mesh_path_lookup(sdata, orig_addr);
  501. if (mpath) {
  502. if (flags & IEEE80211_PREQ_PROACTIVE_PREP_FLAG) {
  503. reply = true;
  504. target_addr = sdata->vif.addr;
  505. target_sn = ++ifmsh->sn;
  506. metric = 0;
  507. ifmsh->last_sn_update = jiffies;
  508. }
  509. if (root_is_gate)
  510. mesh_path_add_gate(mpath);
  511. }
  512. rcu_read_unlock();
  513. } else {
  514. rcu_read_lock();
  515. mpath = mesh_path_lookup(sdata, target_addr);
  516. if (mpath) {
  517. if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
  518. SN_LT(mpath->sn, target_sn)) {
  519. mpath->sn = target_sn;
  520. mpath->flags |= MESH_PATH_SN_VALID;
  521. } else if ((!(target_flags & MP_F_DO)) &&
  522. (mpath->flags & MESH_PATH_ACTIVE)) {
  523. reply = true;
  524. metric = mpath->metric;
  525. target_sn = mpath->sn;
  526. if (target_flags & MP_F_RF)
  527. target_flags |= MP_F_DO;
  528. else
  529. forward = false;
  530. }
  531. }
  532. rcu_read_unlock();
  533. }
  534. if (reply) {
  535. lifetime = PREQ_IE_LIFETIME(preq_elem);
  536. ttl = ifmsh->mshcfg.element_ttl;
  537. if (ttl != 0) {
  538. mhwmp_dbg(sdata, "replying to the PREQ\n");
  539. mesh_path_sel_frame_tx(MPATH_PREP, 0, orig_addr,
  540. cpu_to_le32(orig_sn), 0, target_addr,
  541. cpu_to_le32(target_sn), mgmt->sa, 0, ttl,
  542. cpu_to_le32(lifetime), cpu_to_le32(metric),
  543. 0, sdata);
  544. } else {
  545. ifmsh->mshstats.dropped_frames_ttl++;
  546. }
  547. }
  548. if (forward && ifmsh->mshcfg.dot11MeshForwarding) {
  549. u32 preq_id;
  550. u8 hopcount;
  551. ttl = PREQ_IE_TTL(preq_elem);
  552. lifetime = PREQ_IE_LIFETIME(preq_elem);
  553. if (ttl <= 1) {
  554. ifmsh->mshstats.dropped_frames_ttl++;
  555. return;
  556. }
  557. mhwmp_dbg(sdata, "forwarding the PREQ from %pM\n", orig_addr);
  558. --ttl;
  559. preq_id = PREQ_IE_PREQ_ID(preq_elem);
  560. hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
  561. da = (mpath && mpath->is_root) ?
  562. mpath->rann_snd_addr : broadcast_addr;
  563. if (flags & IEEE80211_PREQ_PROACTIVE_PREP_FLAG) {
  564. target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
  565. target_sn = PREQ_IE_TARGET_SN(preq_elem);
  566. metric = orig_metric;
  567. }
  568. mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
  569. cpu_to_le32(orig_sn), target_flags, target_addr,
  570. cpu_to_le32(target_sn), da,
  571. hopcount, ttl, cpu_to_le32(lifetime),
  572. cpu_to_le32(metric), cpu_to_le32(preq_id),
  573. sdata);
  574. if (!is_multicast_ether_addr(da))
  575. ifmsh->mshstats.fwded_unicast++;
  576. else
  577. ifmsh->mshstats.fwded_mcast++;
  578. ifmsh->mshstats.fwded_frames++;
  579. }
  580. }
  581. static inline struct sta_info *
  582. next_hop_deref_protected(struct mesh_path *mpath)
  583. {
  584. return rcu_dereference_protected(mpath->next_hop,
  585. lockdep_is_held(&mpath->state_lock));
  586. }
  587. static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
  588. struct ieee80211_mgmt *mgmt,
  589. const u8 *prep_elem, u32 metric)
  590. {
  591. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  592. struct mesh_path *mpath;
  593. const u8 *target_addr, *orig_addr;
  594. u8 ttl, hopcount, flags;
  595. u8 next_hop[ETH_ALEN];
  596. u32 target_sn, orig_sn, lifetime;
  597. mhwmp_dbg(sdata, "received PREP from %pM\n",
  598. PREP_IE_TARGET_ADDR(prep_elem));
  599. orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
  600. if (ether_addr_equal(orig_addr, sdata->vif.addr))
  601. /* destination, no forwarding required */
  602. return;
  603. if (!ifmsh->mshcfg.dot11MeshForwarding)
  604. return;
  605. ttl = PREP_IE_TTL(prep_elem);
  606. if (ttl <= 1) {
  607. sdata->u.mesh.mshstats.dropped_frames_ttl++;
  608. return;
  609. }
  610. rcu_read_lock();
  611. mpath = mesh_path_lookup(sdata, orig_addr);
  612. if (mpath)
  613. spin_lock_bh(&mpath->state_lock);
  614. else
  615. goto fail;
  616. if (!(mpath->flags & MESH_PATH_ACTIVE)) {
  617. spin_unlock_bh(&mpath->state_lock);
  618. goto fail;
  619. }
  620. memcpy(next_hop, next_hop_deref_protected(mpath)->sta.addr, ETH_ALEN);
  621. spin_unlock_bh(&mpath->state_lock);
  622. --ttl;
  623. flags = PREP_IE_FLAGS(prep_elem);
  624. lifetime = PREP_IE_LIFETIME(prep_elem);
  625. hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
  626. target_addr = PREP_IE_TARGET_ADDR(prep_elem);
  627. target_sn = PREP_IE_TARGET_SN(prep_elem);
  628. orig_sn = PREP_IE_ORIG_SN(prep_elem);
  629. mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr,
  630. cpu_to_le32(orig_sn), 0, target_addr,
  631. cpu_to_le32(target_sn), next_hop, hopcount,
  632. ttl, cpu_to_le32(lifetime), cpu_to_le32(metric),
  633. 0, sdata);
  634. rcu_read_unlock();
  635. sdata->u.mesh.mshstats.fwded_unicast++;
  636. sdata->u.mesh.mshstats.fwded_frames++;
  637. return;
  638. fail:
  639. rcu_read_unlock();
  640. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  641. }
  642. static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
  643. struct ieee80211_mgmt *mgmt,
  644. const u8 *perr_elem)
  645. {
  646. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  647. struct mesh_path *mpath;
  648. u8 ttl;
  649. const u8 *ta, *target_addr;
  650. u32 target_sn;
  651. u16 target_rcode;
  652. ta = mgmt->sa;
  653. ttl = PERR_IE_TTL(perr_elem);
  654. if (ttl <= 1) {
  655. ifmsh->mshstats.dropped_frames_ttl++;
  656. return;
  657. }
  658. ttl--;
  659. target_addr = PERR_IE_TARGET_ADDR(perr_elem);
  660. target_sn = PERR_IE_TARGET_SN(perr_elem);
  661. target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
  662. rcu_read_lock();
  663. mpath = mesh_path_lookup(sdata, target_addr);
  664. if (mpath) {
  665. struct sta_info *sta;
  666. spin_lock_bh(&mpath->state_lock);
  667. sta = next_hop_deref_protected(mpath);
  668. if (mpath->flags & MESH_PATH_ACTIVE &&
  669. ether_addr_equal(ta, sta->sta.addr) &&
  670. (!(mpath->flags & MESH_PATH_SN_VALID) ||
  671. SN_GT(target_sn, mpath->sn))) {
  672. mpath->flags &= ~MESH_PATH_ACTIVE;
  673. mpath->sn = target_sn;
  674. spin_unlock_bh(&mpath->state_lock);
  675. if (!ifmsh->mshcfg.dot11MeshForwarding)
  676. goto endperr;
  677. mesh_path_error_tx(sdata, ttl, target_addr,
  678. cpu_to_le32(target_sn),
  679. cpu_to_le16(target_rcode),
  680. broadcast_addr);
  681. } else
  682. spin_unlock_bh(&mpath->state_lock);
  683. }
  684. endperr:
  685. rcu_read_unlock();
  686. }
  687. static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
  688. struct ieee80211_mgmt *mgmt,
  689. const struct ieee80211_rann_ie *rann)
  690. {
  691. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  692. struct ieee80211_local *local = sdata->local;
  693. struct sta_info *sta;
  694. struct mesh_path *mpath;
  695. u8 ttl, flags, hopcount;
  696. const u8 *orig_addr;
  697. u32 orig_sn, metric, metric_txsta, interval;
  698. bool root_is_gate;
  699. ttl = rann->rann_ttl;
  700. flags = rann->rann_flags;
  701. root_is_gate = !!(flags & RANN_FLAG_IS_GATE);
  702. orig_addr = rann->rann_addr;
  703. orig_sn = le32_to_cpu(rann->rann_seq);
  704. interval = le32_to_cpu(rann->rann_interval);
  705. hopcount = rann->rann_hopcount;
  706. hopcount++;
  707. metric = le32_to_cpu(rann->rann_metric);
  708. /* Ignore our own RANNs */
  709. if (ether_addr_equal(orig_addr, sdata->vif.addr))
  710. return;
  711. mhwmp_dbg(sdata,
  712. "received RANN from %pM via neighbour %pM (is_gate=%d)\n",
  713. orig_addr, mgmt->sa, root_is_gate);
  714. rcu_read_lock();
  715. sta = sta_info_get(sdata, mgmt->sa);
  716. if (!sta) {
  717. rcu_read_unlock();
  718. return;
  719. }
  720. metric_txsta = airtime_link_metric_get(local, sta);
  721. mpath = mesh_path_lookup(sdata, orig_addr);
  722. if (!mpath) {
  723. mpath = mesh_path_add(sdata, orig_addr);
  724. if (IS_ERR(mpath)) {
  725. rcu_read_unlock();
  726. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  727. return;
  728. }
  729. }
  730. if (!(SN_LT(mpath->sn, orig_sn)) &&
  731. !(mpath->sn == orig_sn && metric < mpath->rann_metric)) {
  732. rcu_read_unlock();
  733. return;
  734. }
  735. if ((!(mpath->flags & (MESH_PATH_ACTIVE | MESH_PATH_RESOLVING)) ||
  736. (time_after(jiffies, mpath->last_preq_to_root +
  737. root_path_confirmation_jiffies(sdata)) ||
  738. time_before(jiffies, mpath->last_preq_to_root))) &&
  739. !(mpath->flags & MESH_PATH_FIXED) && (ttl != 0)) {
  740. mhwmp_dbg(sdata,
  741. "time to refresh root mpath %pM\n",
  742. orig_addr);
  743. mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
  744. mpath->last_preq_to_root = jiffies;
  745. }
  746. mpath->sn = orig_sn;
  747. mpath->rann_metric = metric + metric_txsta;
  748. mpath->is_root = true;
  749. /* Recording RANNs sender address to send individually
  750. * addressed PREQs destined for root mesh STA */
  751. memcpy(mpath->rann_snd_addr, mgmt->sa, ETH_ALEN);
  752. if (root_is_gate)
  753. mesh_path_add_gate(mpath);
  754. if (ttl <= 1) {
  755. ifmsh->mshstats.dropped_frames_ttl++;
  756. rcu_read_unlock();
  757. return;
  758. }
  759. ttl--;
  760. if (ifmsh->mshcfg.dot11MeshForwarding) {
  761. mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
  762. cpu_to_le32(orig_sn),
  763. 0, NULL, 0, broadcast_addr,
  764. hopcount, ttl, cpu_to_le32(interval),
  765. cpu_to_le32(metric + metric_txsta),
  766. 0, sdata);
  767. }
  768. rcu_read_unlock();
  769. }
  770. void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
  771. struct ieee80211_mgmt *mgmt, size_t len)
  772. {
  773. struct ieee802_11_elems elems;
  774. size_t baselen;
  775. u32 last_hop_metric;
  776. struct sta_info *sta;
  777. /* need action_code */
  778. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  779. return;
  780. rcu_read_lock();
  781. sta = sta_info_get(sdata, mgmt->sa);
  782. if (!sta || sta->plink_state != NL80211_PLINK_ESTAB) {
  783. rcu_read_unlock();
  784. return;
  785. }
  786. rcu_read_unlock();
  787. baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
  788. ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
  789. len - baselen, &elems);
  790. if (elems.preq) {
  791. if (elems.preq_len != 37)
  792. /* Right now we support just 1 destination and no AE */
  793. return;
  794. last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
  795. MPATH_PREQ);
  796. if (last_hop_metric)
  797. hwmp_preq_frame_process(sdata, mgmt, elems.preq,
  798. last_hop_metric);
  799. }
  800. if (elems.prep) {
  801. if (elems.prep_len != 31)
  802. /* Right now we support no AE */
  803. return;
  804. last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
  805. MPATH_PREP);
  806. if (last_hop_metric)
  807. hwmp_prep_frame_process(sdata, mgmt, elems.prep,
  808. last_hop_metric);
  809. }
  810. if (elems.perr) {
  811. if (elems.perr_len != 15)
  812. /* Right now we support only one destination per PERR */
  813. return;
  814. hwmp_perr_frame_process(sdata, mgmt, elems.perr);
  815. }
  816. if (elems.rann)
  817. hwmp_rann_frame_process(sdata, mgmt, elems.rann);
  818. }
  819. /**
  820. * mesh_queue_preq - queue a PREQ to a given destination
  821. *
  822. * @mpath: mesh path to discover
  823. * @flags: special attributes of the PREQ to be sent
  824. *
  825. * Locking: the function must be called from within a rcu read lock block.
  826. *
  827. */
  828. static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
  829. {
  830. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  831. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  832. struct mesh_preq_queue *preq_node;
  833. preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
  834. if (!preq_node) {
  835. mhwmp_dbg(sdata, "could not allocate PREQ node\n");
  836. return;
  837. }
  838. spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
  839. if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
  840. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  841. kfree(preq_node);
  842. if (printk_ratelimit())
  843. mhwmp_dbg(sdata, "PREQ node queue full\n");
  844. return;
  845. }
  846. spin_lock(&mpath->state_lock);
  847. if (mpath->flags & MESH_PATH_REQ_QUEUED) {
  848. spin_unlock(&mpath->state_lock);
  849. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  850. kfree(preq_node);
  851. return;
  852. }
  853. memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
  854. preq_node->flags = flags;
  855. mpath->flags |= MESH_PATH_REQ_QUEUED;
  856. spin_unlock(&mpath->state_lock);
  857. list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
  858. ++ifmsh->preq_queue_len;
  859. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  860. if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
  861. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  862. else if (time_before(jiffies, ifmsh->last_preq)) {
  863. /* avoid long wait if did not send preqs for a long time
  864. * and jiffies wrapped around
  865. */
  866. ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
  867. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  868. } else
  869. mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
  870. min_preq_int_jiff(sdata));
  871. }
  872. /**
  873. * mesh_path_start_discovery - launch a path discovery from the PREQ queue
  874. *
  875. * @sdata: local mesh subif
  876. */
  877. void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
  878. {
  879. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  880. struct mesh_preq_queue *preq_node;
  881. struct mesh_path *mpath;
  882. u8 ttl, target_flags;
  883. const u8 *da;
  884. u32 lifetime;
  885. spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
  886. if (!ifmsh->preq_queue_len ||
  887. time_before(jiffies, ifmsh->last_preq +
  888. min_preq_int_jiff(sdata))) {
  889. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  890. return;
  891. }
  892. preq_node = list_first_entry(&ifmsh->preq_queue.list,
  893. struct mesh_preq_queue, list);
  894. list_del(&preq_node->list);
  895. --ifmsh->preq_queue_len;
  896. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  897. rcu_read_lock();
  898. mpath = mesh_path_lookup(sdata, preq_node->dst);
  899. if (!mpath)
  900. goto enddiscovery;
  901. spin_lock_bh(&mpath->state_lock);
  902. mpath->flags &= ~MESH_PATH_REQ_QUEUED;
  903. if (preq_node->flags & PREQ_Q_F_START) {
  904. if (mpath->flags & MESH_PATH_RESOLVING) {
  905. spin_unlock_bh(&mpath->state_lock);
  906. goto enddiscovery;
  907. } else {
  908. mpath->flags &= ~MESH_PATH_RESOLVED;
  909. mpath->flags |= MESH_PATH_RESOLVING;
  910. mpath->discovery_retries = 0;
  911. mpath->discovery_timeout = disc_timeout_jiff(sdata);
  912. }
  913. } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
  914. mpath->flags & MESH_PATH_RESOLVED) {
  915. mpath->flags &= ~MESH_PATH_RESOLVING;
  916. spin_unlock_bh(&mpath->state_lock);
  917. goto enddiscovery;
  918. }
  919. ifmsh->last_preq = jiffies;
  920. if (time_after(jiffies, ifmsh->last_sn_update +
  921. net_traversal_jiffies(sdata)) ||
  922. time_before(jiffies, ifmsh->last_sn_update)) {
  923. ++ifmsh->sn;
  924. sdata->u.mesh.last_sn_update = jiffies;
  925. }
  926. lifetime = default_lifetime(sdata);
  927. ttl = sdata->u.mesh.mshcfg.element_ttl;
  928. if (ttl == 0) {
  929. sdata->u.mesh.mshstats.dropped_frames_ttl++;
  930. spin_unlock_bh(&mpath->state_lock);
  931. goto enddiscovery;
  932. }
  933. if (preq_node->flags & PREQ_Q_F_REFRESH)
  934. target_flags = MP_F_DO;
  935. else
  936. target_flags = MP_F_RF;
  937. spin_unlock_bh(&mpath->state_lock);
  938. da = (mpath->is_root) ? mpath->rann_snd_addr : broadcast_addr;
  939. mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr,
  940. cpu_to_le32(ifmsh->sn), target_flags, mpath->dst,
  941. cpu_to_le32(mpath->sn), da, 0,
  942. ttl, cpu_to_le32(lifetime), 0,
  943. cpu_to_le32(ifmsh->preq_id++), sdata);
  944. mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
  945. enddiscovery:
  946. rcu_read_unlock();
  947. kfree(preq_node);
  948. }
  949. /**
  950. * mesh_nexthop_resolve - lookup next hop; conditionally start path discovery
  951. *
  952. * @skb: 802.11 frame to be sent
  953. * @sdata: network subif the frame will be sent through
  954. *
  955. * Lookup next hop for given skb and start path discovery if no
  956. * forwarding information is found.
  957. *
  958. * Returns: 0 if the next hop was found and -ENOENT if the frame was queued.
  959. * skb is freeed here if no mpath could be allocated.
  960. */
  961. int mesh_nexthop_resolve(struct ieee80211_sub_if_data *sdata,
  962. struct sk_buff *skb)
  963. {
  964. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  965. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  966. struct mesh_path *mpath;
  967. struct sk_buff *skb_to_free = NULL;
  968. u8 *target_addr = hdr->addr3;
  969. int err = 0;
  970. /* Nulls are only sent to peers for PS and should be pre-addressed */
  971. if (ieee80211_is_qos_nullfunc(hdr->frame_control))
  972. return 0;
  973. rcu_read_lock();
  974. err = mesh_nexthop_lookup(sdata, skb);
  975. if (!err)
  976. goto endlookup;
  977. /* no nexthop found, start resolving */
  978. mpath = mesh_path_lookup(sdata, target_addr);
  979. if (!mpath) {
  980. mpath = mesh_path_add(sdata, target_addr);
  981. if (IS_ERR(mpath)) {
  982. mesh_path_discard_frame(sdata, skb);
  983. err = PTR_ERR(mpath);
  984. goto endlookup;
  985. }
  986. }
  987. if (!(mpath->flags & MESH_PATH_RESOLVING))
  988. mesh_queue_preq(mpath, PREQ_Q_F_START);
  989. if (skb_queue_len(&mpath->frame_queue) >= MESH_FRAME_QUEUE_LEN)
  990. skb_to_free = skb_dequeue(&mpath->frame_queue);
  991. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  992. ieee80211_set_qos_hdr(sdata, skb);
  993. skb_queue_tail(&mpath->frame_queue, skb);
  994. err = -ENOENT;
  995. if (skb_to_free)
  996. mesh_path_discard_frame(sdata, skb_to_free);
  997. endlookup:
  998. rcu_read_unlock();
  999. return err;
  1000. }
  1001. /**
  1002. * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame. Calling
  1003. * this function is considered "using" the associated mpath, so preempt a path
  1004. * refresh if this mpath expires soon.
  1005. *
  1006. * @skb: 802.11 frame to be sent
  1007. * @sdata: network subif the frame will be sent through
  1008. *
  1009. * Returns: 0 if the next hop was found. Nonzero otherwise.
  1010. */
  1011. int mesh_nexthop_lookup(struct ieee80211_sub_if_data *sdata,
  1012. struct sk_buff *skb)
  1013. {
  1014. struct mesh_path *mpath;
  1015. struct sta_info *next_hop;
  1016. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1017. u8 *target_addr = hdr->addr3;
  1018. int err = -ENOENT;
  1019. rcu_read_lock();
  1020. mpath = mesh_path_lookup(sdata, target_addr);
  1021. if (!mpath || !(mpath->flags & MESH_PATH_ACTIVE))
  1022. goto endlookup;
  1023. if (time_after(jiffies,
  1024. mpath->exp_time -
  1025. msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
  1026. ether_addr_equal(sdata->vif.addr, hdr->addr4) &&
  1027. !(mpath->flags & MESH_PATH_RESOLVING) &&
  1028. !(mpath->flags & MESH_PATH_FIXED))
  1029. mesh_queue_preq(mpath, PREQ_Q_F_START | PREQ_Q_F_REFRESH);
  1030. next_hop = rcu_dereference(mpath->next_hop);
  1031. if (next_hop) {
  1032. memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
  1033. memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
  1034. ieee80211_mps_set_frame_flags(sdata, next_hop, hdr);
  1035. err = 0;
  1036. }
  1037. endlookup:
  1038. rcu_read_unlock();
  1039. return err;
  1040. }
  1041. void mesh_path_timer(unsigned long data)
  1042. {
  1043. struct mesh_path *mpath = (void *) data;
  1044. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  1045. int ret;
  1046. if (sdata->local->quiescing)
  1047. return;
  1048. spin_lock_bh(&mpath->state_lock);
  1049. if (mpath->flags & MESH_PATH_RESOLVED ||
  1050. (!(mpath->flags & MESH_PATH_RESOLVING))) {
  1051. mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
  1052. spin_unlock_bh(&mpath->state_lock);
  1053. } else if (mpath->discovery_retries < max_preq_retries(sdata)) {
  1054. ++mpath->discovery_retries;
  1055. mpath->discovery_timeout *= 2;
  1056. mpath->flags &= ~MESH_PATH_REQ_QUEUED;
  1057. spin_unlock_bh(&mpath->state_lock);
  1058. mesh_queue_preq(mpath, 0);
  1059. } else {
  1060. mpath->flags = 0;
  1061. mpath->exp_time = jiffies;
  1062. spin_unlock_bh(&mpath->state_lock);
  1063. if (!mpath->is_gate && mesh_gate_num(sdata) > 0) {
  1064. ret = mesh_path_send_to_gates(mpath);
  1065. if (ret)
  1066. mhwmp_dbg(sdata, "no gate was reachable\n");
  1067. } else
  1068. mesh_path_flush_pending(mpath);
  1069. }
  1070. }
  1071. void mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
  1072. {
  1073. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  1074. u32 interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
  1075. u8 flags, target_flags = 0;
  1076. flags = (ifmsh->mshcfg.dot11MeshGateAnnouncementProtocol)
  1077. ? RANN_FLAG_IS_GATE : 0;
  1078. switch (ifmsh->mshcfg.dot11MeshHWMPRootMode) {
  1079. case IEEE80211_PROACTIVE_RANN:
  1080. mesh_path_sel_frame_tx(MPATH_RANN, flags, sdata->vif.addr,
  1081. cpu_to_le32(++ifmsh->sn),
  1082. 0, NULL, 0, broadcast_addr,
  1083. 0, ifmsh->mshcfg.element_ttl,
  1084. cpu_to_le32(interval), 0, 0, sdata);
  1085. break;
  1086. case IEEE80211_PROACTIVE_PREQ_WITH_PREP:
  1087. flags |= IEEE80211_PREQ_PROACTIVE_PREP_FLAG;
  1088. case IEEE80211_PROACTIVE_PREQ_NO_PREP:
  1089. interval = ifmsh->mshcfg.dot11MeshHWMPactivePathToRootTimeout;
  1090. target_flags |= IEEE80211_PREQ_TO_FLAG |
  1091. IEEE80211_PREQ_USN_FLAG;
  1092. mesh_path_sel_frame_tx(MPATH_PREQ, flags, sdata->vif.addr,
  1093. cpu_to_le32(++ifmsh->sn), target_flags,
  1094. (u8 *) broadcast_addr, 0, broadcast_addr,
  1095. 0, ifmsh->mshcfg.element_ttl,
  1096. cpu_to_le32(interval),
  1097. 0, cpu_to_le32(ifmsh->preq_id++), sdata);
  1098. break;
  1099. default:
  1100. mhwmp_dbg(sdata, "Proactive mechanism not supported\n");
  1101. return;
  1102. }
  1103. }