mesh_hwmp.c 35 KB

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