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