mesh_hwmp.c 34 KB

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