mesh_hwmp.c 35 KB

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