mesh_hwmp.c 34 KB

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