mesh_hwmp.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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 "mesh.h"
  11. #ifdef CONFIG_MAC80211_VERBOSE_MHWMP_DEBUG
  12. #define mhwmp_dbg(fmt, args...) printk(KERN_DEBUG "Mesh HWMP: " fmt, ##args)
  13. #else
  14. #define mhwmp_dbg(fmt, args...) do { (void)(0); } while (0)
  15. #endif
  16. #define TEST_FRAME_LEN 8192
  17. #define MAX_METRIC 0xffffffff
  18. #define ARITH_SHIFT 8
  19. /* Number of frames buffered per destination for unresolved destinations */
  20. #define MESH_FRAME_QUEUE_LEN 10
  21. #define MAX_PREQ_QUEUE_LEN 64
  22. /* Destination only */
  23. #define MP_F_DO 0x1
  24. /* Reply and forward */
  25. #define MP_F_RF 0x2
  26. /* Unknown Sequence Number */
  27. #define MP_F_USN 0x01
  28. /* Reason code Present */
  29. #define MP_F_RCODE 0x02
  30. static void mesh_queue_preq(struct mesh_path *, u8);
  31. static inline u32 u32_field_get(u8 *preq_elem, int offset, bool ae)
  32. {
  33. if (ae)
  34. offset += 6;
  35. return get_unaligned_le32(preq_elem + offset);
  36. }
  37. static inline u32 u16_field_get(u8 *preq_elem, int offset, bool ae)
  38. {
  39. if (ae)
  40. offset += 6;
  41. return get_unaligned_le16(preq_elem + offset);
  42. }
  43. /* HWMP IE processing macros */
  44. #define AE_F (1<<6)
  45. #define AE_F_SET(x) (*x & AE_F)
  46. #define PREQ_IE_FLAGS(x) (*(x))
  47. #define PREQ_IE_HOPCOUNT(x) (*(x + 1))
  48. #define PREQ_IE_TTL(x) (*(x + 2))
  49. #define PREQ_IE_PREQ_ID(x) u32_field_get(x, 3, 0)
  50. #define PREQ_IE_ORIG_ADDR(x) (x + 7)
  51. #define PREQ_IE_ORIG_SN(x) u32_field_get(x, 13, 0);
  52. #define PREQ_IE_LIFETIME(x) u32_field_get(x, 17, AE_F_SET(x));
  53. #define PREQ_IE_METRIC(x) u32_field_get(x, 21, AE_F_SET(x));
  54. #define PREQ_IE_TARGET_F(x) (*(AE_F_SET(x) ? x + 32 : x + 26))
  55. #define PREQ_IE_TARGET_ADDR(x) (AE_F_SET(x) ? x + 33 : x + 27)
  56. #define PREQ_IE_TARGET_SN(x) u32_field_get(x, 33, AE_F_SET(x));
  57. #define PREP_IE_FLAGS(x) PREQ_IE_FLAGS(x)
  58. #define PREP_IE_HOPCOUNT(x) PREQ_IE_HOPCOUNT(x)
  59. #define PREP_IE_TTL(x) PREQ_IE_TTL(x)
  60. #define PREP_IE_ORIG_ADDR(x) (x + 3)
  61. #define PREP_IE_ORIG_SN(x) u32_field_get(x, 9, 0);
  62. #define PREP_IE_LIFETIME(x) u32_field_get(x, 13, AE_F_SET(x));
  63. #define PREP_IE_METRIC(x) u32_field_get(x, 17, AE_F_SET(x));
  64. #define PREP_IE_TARGET_ADDR(x) (AE_F_SET(x) ? x + 27 : x + 21)
  65. #define PREP_IE_TARGET_SN(x) u32_field_get(x, 27, AE_F_SET(x));
  66. #define PERR_IE_TTL(x) (*(x))
  67. #define PERR_IE_TARGET_FLAGS(x) (*(x + 2))
  68. #define PERR_IE_TARGET_ADDR(x) (x + 3)
  69. #define PERR_IE_TARGET_SN(x) u32_field_get(x, 9, 0);
  70. #define PERR_IE_TARGET_RCODE(x) u16_field_get(x, 13, 0);
  71. #define MSEC_TO_TU(x) (x*1000/1024)
  72. #define SN_GT(x, y) ((long) (y) - (long) (x) < 0)
  73. #define SN_LT(x, y) ((long) (x) - (long) (y) < 0)
  74. #define net_traversal_jiffies(s) \
  75. msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime)
  76. #define default_lifetime(s) \
  77. MSEC_TO_TU(s->u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout)
  78. #define min_preq_int_jiff(s) \
  79. (msecs_to_jiffies(s->u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval))
  80. #define max_preq_retries(s) (s->u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries)
  81. #define disc_timeout_jiff(s) \
  82. msecs_to_jiffies(sdata->u.mesh.mshcfg.min_discovery_timeout)
  83. enum mpath_frame_type {
  84. MPATH_PREQ = 0,
  85. MPATH_PREP,
  86. MPATH_PERR,
  87. MPATH_RANN
  88. };
  89. static const u8 broadcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  90. static int mesh_path_sel_frame_tx(enum mpath_frame_type action, u8 flags,
  91. u8 *orig_addr, __le32 orig_sn, u8 target_flags, u8 *target,
  92. __le32 target_sn, const u8 *da, u8 hop_count, u8 ttl,
  93. __le32 lifetime, __le32 metric, __le32 preq_id,
  94. struct ieee80211_sub_if_data *sdata)
  95. {
  96. struct ieee80211_local *local = sdata->local;
  97. struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
  98. struct ieee80211_mgmt *mgmt;
  99. u8 *pos;
  100. int ie_len;
  101. if (!skb)
  102. return -1;
  103. skb_reserve(skb, local->hw.extra_tx_headroom);
  104. /* 25 is the size of the common mgmt part (24) plus the size of the
  105. * common action part (1)
  106. */
  107. mgmt = (struct ieee80211_mgmt *)
  108. skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action));
  109. memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action));
  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_PATH_SEL;
  117. mgmt->u.action.u.mesh_action.action_code = MESH_PATH_SEL_ACTION;
  118. switch (action) {
  119. case MPATH_PREQ:
  120. mhwmp_dbg("sending PREQ to %pM\n", target);
  121. ie_len = 37;
  122. pos = skb_put(skb, 2 + ie_len);
  123. *pos++ = WLAN_EID_PREQ;
  124. break;
  125. case MPATH_PREP:
  126. mhwmp_dbg("sending PREP to %pM\n", target);
  127. ie_len = 31;
  128. pos = skb_put(skb, 2 + ie_len);
  129. *pos++ = WLAN_EID_PREP;
  130. break;
  131. case MPATH_RANN:
  132. mhwmp_dbg("sending RANN from %pM\n", orig_addr);
  133. ie_len = sizeof(struct ieee80211_rann_ie);
  134. pos = skb_put(skb, 2 + ie_len);
  135. *pos++ = WLAN_EID_RANN;
  136. break;
  137. default:
  138. kfree_skb(skb);
  139. return -ENOTSUPP;
  140. break;
  141. }
  142. *pos++ = ie_len;
  143. *pos++ = flags;
  144. *pos++ = hop_count;
  145. *pos++ = ttl;
  146. if (action == MPATH_PREQ) {
  147. memcpy(pos, &preq_id, 4);
  148. pos += 4;
  149. }
  150. memcpy(pos, orig_addr, ETH_ALEN);
  151. pos += ETH_ALEN;
  152. memcpy(pos, &orig_sn, 4);
  153. pos += 4;
  154. if (action != MPATH_RANN) {
  155. memcpy(pos, &lifetime, 4);
  156. pos += 4;
  157. }
  158. memcpy(pos, &metric, 4);
  159. pos += 4;
  160. if (action == MPATH_PREQ) {
  161. /* destination count */
  162. *pos++ = 1;
  163. *pos++ = target_flags;
  164. }
  165. if (action != MPATH_RANN) {
  166. memcpy(pos, target, ETH_ALEN);
  167. pos += ETH_ALEN;
  168. memcpy(pos, &target_sn, 4);
  169. }
  170. ieee80211_tx_skb(sdata, skb);
  171. return 0;
  172. }
  173. /**
  174. * mesh_send_path error - Sends a PERR mesh management frame
  175. *
  176. * @target: broken destination
  177. * @target_sn: SN of the broken destination
  178. * @target_rcode: reason code for this PERR
  179. * @ra: node this frame is addressed to
  180. */
  181. int mesh_path_error_tx(u8 ttl, u8 *target, __le32 target_sn,
  182. __le16 target_rcode, const u8 *ra,
  183. struct ieee80211_sub_if_data *sdata)
  184. {
  185. struct ieee80211_local *local = sdata->local;
  186. struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
  187. struct ieee80211_mgmt *mgmt;
  188. u8 *pos;
  189. int ie_len;
  190. if (!skb)
  191. return -1;
  192. skb_reserve(skb, local->hw.extra_tx_headroom);
  193. /* 25 is the size of the common mgmt part (24) plus the size of the
  194. * common action part (1)
  195. */
  196. mgmt = (struct ieee80211_mgmt *)
  197. skb_put(skb, 25 + sizeof(mgmt->u.action.u.mesh_action));
  198. memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.mesh_action));
  199. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  200. IEEE80211_STYPE_ACTION);
  201. memcpy(mgmt->da, ra, ETH_ALEN);
  202. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  203. /* BSSID is left zeroed, wildcard value */
  204. mgmt->u.action.category = WLAN_CATEGORY_MESH_PATH_SEL;
  205. mgmt->u.action.u.mesh_action.action_code = MESH_PATH_SEL_ACTION;
  206. ie_len = 15;
  207. pos = skb_put(skb, 2 + ie_len);
  208. *pos++ = WLAN_EID_PERR;
  209. *pos++ = ie_len;
  210. /* ttl */
  211. *pos++ = ttl;
  212. /* number of destinations */
  213. *pos++ = 1;
  214. /*
  215. * flags bit, bit 1 is unset if we know the sequence number and
  216. * bit 2 is set if we have a reason code
  217. */
  218. *pos = 0;
  219. if (!target_sn)
  220. *pos |= MP_F_USN;
  221. if (target_rcode)
  222. *pos |= MP_F_RCODE;
  223. pos++;
  224. memcpy(pos, target, ETH_ALEN);
  225. pos += ETH_ALEN;
  226. memcpy(pos, &target_sn, 4);
  227. pos += 4;
  228. memcpy(pos, &target_rcode, 2);
  229. ieee80211_tx_skb(sdata, skb);
  230. return 0;
  231. }
  232. void ieee80211s_update_metric(struct ieee80211_local *local,
  233. struct sta_info *stainfo, struct sk_buff *skb)
  234. {
  235. struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
  236. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  237. int failed;
  238. if (!ieee80211_is_data(hdr->frame_control))
  239. return;
  240. failed = !(txinfo->flags & IEEE80211_TX_STAT_ACK);
  241. /* moving average, scaled to 100 */
  242. stainfo->fail_avg = ((80 * stainfo->fail_avg + 5) / 100 + 20 * failed);
  243. if (stainfo->fail_avg > 95)
  244. mesh_plink_broken(stainfo);
  245. }
  246. static u32 airtime_link_metric_get(struct ieee80211_local *local,
  247. struct sta_info *sta)
  248. {
  249. struct ieee80211_supported_band *sband;
  250. /* This should be adjusted for each device */
  251. int device_constant = 1 << ARITH_SHIFT;
  252. int test_frame_len = TEST_FRAME_LEN << ARITH_SHIFT;
  253. int s_unit = 1 << ARITH_SHIFT;
  254. int rate, err;
  255. u32 tx_time, estimated_retx;
  256. u64 result;
  257. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  258. if (sta->fail_avg >= 100)
  259. return MAX_METRIC;
  260. if (sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS)
  261. return MAX_METRIC;
  262. err = (sta->fail_avg << ARITH_SHIFT) / 100;
  263. /* bitrate is in units of 100 Kbps, while we need rate in units of
  264. * 1Mbps. This will be corrected on tx_time computation.
  265. */
  266. rate = sband->bitrates[sta->last_tx_rate.idx].bitrate;
  267. tx_time = (device_constant + 10 * test_frame_len / rate);
  268. estimated_retx = ((1 << (2 * ARITH_SHIFT)) / (s_unit - err));
  269. result = (tx_time * estimated_retx) >> (2 * ARITH_SHIFT) ;
  270. return (u32)result;
  271. }
  272. /**
  273. * hwmp_route_info_get - Update routing info to originator and transmitter
  274. *
  275. * @sdata: local mesh subif
  276. * @mgmt: mesh management frame
  277. * @hwmp_ie: hwmp information element (PREP or PREQ)
  278. *
  279. * This function updates the path routing information to the originator and the
  280. * transmitter of a HWMP PREQ or PREP frame.
  281. *
  282. * Returns: metric to frame originator or 0 if the frame should not be further
  283. * processed
  284. *
  285. * Notes: this function is the only place (besides user-provided info) where
  286. * path routing information is updated.
  287. */
  288. static u32 hwmp_route_info_get(struct ieee80211_sub_if_data *sdata,
  289. struct ieee80211_mgmt *mgmt,
  290. u8 *hwmp_ie, enum mpath_frame_type action)
  291. {
  292. struct ieee80211_local *local = sdata->local;
  293. struct mesh_path *mpath;
  294. struct sta_info *sta;
  295. bool fresh_info;
  296. u8 *orig_addr, *ta;
  297. u32 orig_sn, orig_metric;
  298. unsigned long orig_lifetime, exp_time;
  299. u32 last_hop_metric, new_metric;
  300. bool process = true;
  301. rcu_read_lock();
  302. sta = sta_info_get(sdata, mgmt->sa);
  303. if (!sta) {
  304. rcu_read_unlock();
  305. return 0;
  306. }
  307. last_hop_metric = airtime_link_metric_get(local, sta);
  308. /* Update and check originator routing info */
  309. fresh_info = true;
  310. switch (action) {
  311. case MPATH_PREQ:
  312. orig_addr = PREQ_IE_ORIG_ADDR(hwmp_ie);
  313. orig_sn = PREQ_IE_ORIG_SN(hwmp_ie);
  314. orig_lifetime = PREQ_IE_LIFETIME(hwmp_ie);
  315. orig_metric = PREQ_IE_METRIC(hwmp_ie);
  316. break;
  317. case MPATH_PREP:
  318. /* Originator here refers to the MP that was the destination in
  319. * the Path Request. The draft refers to that MP as the
  320. * destination address, even though usually it is the origin of
  321. * the PREP frame. We divert from the nomenclature in the draft
  322. * so that we can easily use a single function to gather path
  323. * information from both PREQ and PREP frames.
  324. */
  325. orig_addr = PREP_IE_ORIG_ADDR(hwmp_ie);
  326. orig_sn = PREP_IE_ORIG_SN(hwmp_ie);
  327. orig_lifetime = PREP_IE_LIFETIME(hwmp_ie);
  328. orig_metric = PREP_IE_METRIC(hwmp_ie);
  329. break;
  330. default:
  331. rcu_read_unlock();
  332. return 0;
  333. }
  334. new_metric = orig_metric + last_hop_metric;
  335. if (new_metric < orig_metric)
  336. new_metric = MAX_METRIC;
  337. exp_time = TU_TO_EXP_TIME(orig_lifetime);
  338. if (memcmp(orig_addr, sdata->vif.addr, ETH_ALEN) == 0) {
  339. /* This MP is the originator, we are not interested in this
  340. * frame, except for updating transmitter's path info.
  341. */
  342. process = false;
  343. fresh_info = false;
  344. } else {
  345. mpath = mesh_path_lookup(orig_addr, sdata);
  346. if (mpath) {
  347. spin_lock_bh(&mpath->state_lock);
  348. if (mpath->flags & MESH_PATH_FIXED)
  349. fresh_info = false;
  350. else if ((mpath->flags & MESH_PATH_ACTIVE) &&
  351. (mpath->flags & MESH_PATH_SN_VALID)) {
  352. if (SN_GT(mpath->sn, orig_sn) ||
  353. (mpath->sn == orig_sn &&
  354. new_metric >= mpath->metric)) {
  355. process = false;
  356. fresh_info = false;
  357. }
  358. }
  359. } else {
  360. mesh_path_add(orig_addr, sdata);
  361. mpath = mesh_path_lookup(orig_addr, sdata);
  362. if (!mpath) {
  363. rcu_read_unlock();
  364. return 0;
  365. }
  366. spin_lock_bh(&mpath->state_lock);
  367. }
  368. if (fresh_info) {
  369. mesh_path_assign_nexthop(mpath, sta);
  370. mpath->flags |= MESH_PATH_SN_VALID;
  371. mpath->metric = new_metric;
  372. mpath->sn = orig_sn;
  373. mpath->exp_time = time_after(mpath->exp_time, exp_time)
  374. ? mpath->exp_time : exp_time;
  375. mesh_path_activate(mpath);
  376. spin_unlock_bh(&mpath->state_lock);
  377. mesh_path_tx_pending(mpath);
  378. /* draft says preq_id should be saved to, but there does
  379. * not seem to be any use for it, skipping by now
  380. */
  381. } else
  382. spin_unlock_bh(&mpath->state_lock);
  383. }
  384. /* Update and check transmitter routing info */
  385. ta = mgmt->sa;
  386. if (memcmp(orig_addr, ta, ETH_ALEN) == 0)
  387. fresh_info = false;
  388. else {
  389. fresh_info = true;
  390. mpath = mesh_path_lookup(ta, sdata);
  391. if (mpath) {
  392. spin_lock_bh(&mpath->state_lock);
  393. if ((mpath->flags & MESH_PATH_FIXED) ||
  394. ((mpath->flags & MESH_PATH_ACTIVE) &&
  395. (last_hop_metric > mpath->metric)))
  396. fresh_info = false;
  397. } else {
  398. mesh_path_add(ta, sdata);
  399. mpath = mesh_path_lookup(ta, sdata);
  400. if (!mpath) {
  401. rcu_read_unlock();
  402. return 0;
  403. }
  404. spin_lock_bh(&mpath->state_lock);
  405. }
  406. if (fresh_info) {
  407. mesh_path_assign_nexthop(mpath, sta);
  408. mpath->flags &= ~MESH_PATH_SN_VALID;
  409. mpath->metric = last_hop_metric;
  410. mpath->exp_time = time_after(mpath->exp_time, exp_time)
  411. ? mpath->exp_time : exp_time;
  412. mesh_path_activate(mpath);
  413. spin_unlock_bh(&mpath->state_lock);
  414. mesh_path_tx_pending(mpath);
  415. } else
  416. spin_unlock_bh(&mpath->state_lock);
  417. }
  418. rcu_read_unlock();
  419. return process ? new_metric : 0;
  420. }
  421. static void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata,
  422. struct ieee80211_mgmt *mgmt,
  423. u8 *preq_elem, u32 metric)
  424. {
  425. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  426. struct mesh_path *mpath;
  427. u8 *target_addr, *orig_addr;
  428. u8 target_flags, ttl;
  429. u32 orig_sn, target_sn, lifetime;
  430. bool reply = false;
  431. bool forward = true;
  432. /* Update target SN, if present */
  433. target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
  434. orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
  435. target_sn = PREQ_IE_TARGET_SN(preq_elem);
  436. orig_sn = PREQ_IE_ORIG_SN(preq_elem);
  437. target_flags = PREQ_IE_TARGET_F(preq_elem);
  438. mhwmp_dbg("received PREQ from %pM\n", orig_addr);
  439. if (memcmp(target_addr, sdata->vif.addr, ETH_ALEN) == 0) {
  440. mhwmp_dbg("PREQ is for us\n");
  441. forward = false;
  442. reply = true;
  443. metric = 0;
  444. if (time_after(jiffies, ifmsh->last_sn_update +
  445. net_traversal_jiffies(sdata)) ||
  446. time_before(jiffies, ifmsh->last_sn_update)) {
  447. target_sn = ++ifmsh->sn;
  448. ifmsh->last_sn_update = jiffies;
  449. }
  450. } else {
  451. rcu_read_lock();
  452. mpath = mesh_path_lookup(target_addr, sdata);
  453. if (mpath) {
  454. if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
  455. SN_LT(mpath->sn, target_sn)) {
  456. mpath->sn = target_sn;
  457. mpath->flags |= MESH_PATH_SN_VALID;
  458. } else if ((!(target_flags & MP_F_DO)) &&
  459. (mpath->flags & MESH_PATH_ACTIVE)) {
  460. reply = true;
  461. metric = mpath->metric;
  462. target_sn = mpath->sn;
  463. if (target_flags & MP_F_RF)
  464. target_flags |= MP_F_DO;
  465. else
  466. forward = false;
  467. }
  468. }
  469. rcu_read_unlock();
  470. }
  471. if (reply) {
  472. lifetime = PREQ_IE_LIFETIME(preq_elem);
  473. ttl = ifmsh->mshcfg.element_ttl;
  474. if (ttl != 0) {
  475. mhwmp_dbg("replying to the PREQ\n");
  476. mesh_path_sel_frame_tx(MPATH_PREP, 0, target_addr,
  477. cpu_to_le32(target_sn), 0, orig_addr,
  478. cpu_to_le32(orig_sn), mgmt->sa, 0, ttl,
  479. cpu_to_le32(lifetime), cpu_to_le32(metric),
  480. 0, sdata);
  481. } else
  482. ifmsh->mshstats.dropped_frames_ttl++;
  483. }
  484. if (forward) {
  485. u32 preq_id;
  486. u8 hopcount, flags;
  487. ttl = PREQ_IE_TTL(preq_elem);
  488. lifetime = PREQ_IE_LIFETIME(preq_elem);
  489. if (ttl <= 1) {
  490. ifmsh->mshstats.dropped_frames_ttl++;
  491. return;
  492. }
  493. mhwmp_dbg("forwarding the PREQ from %pM\n", orig_addr);
  494. --ttl;
  495. flags = PREQ_IE_FLAGS(preq_elem);
  496. preq_id = PREQ_IE_PREQ_ID(preq_elem);
  497. hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
  498. mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
  499. cpu_to_le32(orig_sn), target_flags, target_addr,
  500. cpu_to_le32(target_sn), broadcast_addr,
  501. hopcount, ttl, cpu_to_le32(lifetime),
  502. cpu_to_le32(metric), cpu_to_le32(preq_id),
  503. sdata);
  504. ifmsh->mshstats.fwded_mcast++;
  505. ifmsh->mshstats.fwded_frames++;
  506. }
  507. }
  508. static inline struct sta_info *
  509. next_hop_deref_protected(struct mesh_path *mpath)
  510. {
  511. return rcu_dereference_protected(mpath->next_hop,
  512. lockdep_is_held(&mpath->state_lock));
  513. }
  514. static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
  515. struct ieee80211_mgmt *mgmt,
  516. u8 *prep_elem, u32 metric)
  517. {
  518. struct mesh_path *mpath;
  519. u8 *target_addr, *orig_addr;
  520. u8 ttl, hopcount, flags;
  521. u8 next_hop[ETH_ALEN];
  522. u32 target_sn, orig_sn, lifetime;
  523. mhwmp_dbg("received PREP from %pM\n", PREP_IE_ORIG_ADDR(prep_elem));
  524. /* Note that we divert from the draft nomenclature and denominate
  525. * destination to what the draft refers to as origininator. So in this
  526. * function destnation refers to the final destination of the PREP,
  527. * which corresponds with the originator of the PREQ which this PREP
  528. * replies
  529. */
  530. target_addr = PREP_IE_TARGET_ADDR(prep_elem);
  531. if (memcmp(target_addr, sdata->vif.addr, ETH_ALEN) == 0)
  532. /* destination, no forwarding required */
  533. return;
  534. ttl = PREP_IE_TTL(prep_elem);
  535. if (ttl <= 1) {
  536. sdata->u.mesh.mshstats.dropped_frames_ttl++;
  537. return;
  538. }
  539. rcu_read_lock();
  540. mpath = mesh_path_lookup(target_addr, sdata);
  541. if (mpath)
  542. spin_lock_bh(&mpath->state_lock);
  543. else
  544. goto fail;
  545. if (!(mpath->flags & MESH_PATH_ACTIVE)) {
  546. spin_unlock_bh(&mpath->state_lock);
  547. goto fail;
  548. }
  549. memcpy(next_hop, next_hop_deref_protected(mpath)->sta.addr, ETH_ALEN);
  550. spin_unlock_bh(&mpath->state_lock);
  551. --ttl;
  552. flags = PREP_IE_FLAGS(prep_elem);
  553. lifetime = PREP_IE_LIFETIME(prep_elem);
  554. hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
  555. orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
  556. target_sn = PREP_IE_TARGET_SN(prep_elem);
  557. orig_sn = PREP_IE_ORIG_SN(prep_elem);
  558. mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr,
  559. cpu_to_le32(orig_sn), 0, target_addr,
  560. cpu_to_le32(target_sn), next_hop, hopcount,
  561. ttl, cpu_to_le32(lifetime), cpu_to_le32(metric),
  562. 0, sdata);
  563. rcu_read_unlock();
  564. sdata->u.mesh.mshstats.fwded_unicast++;
  565. sdata->u.mesh.mshstats.fwded_frames++;
  566. return;
  567. fail:
  568. rcu_read_unlock();
  569. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  570. }
  571. static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
  572. struct ieee80211_mgmt *mgmt, u8 *perr_elem)
  573. {
  574. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  575. struct mesh_path *mpath;
  576. u8 ttl;
  577. u8 *ta, *target_addr;
  578. u32 target_sn;
  579. u16 target_rcode;
  580. ta = mgmt->sa;
  581. ttl = PERR_IE_TTL(perr_elem);
  582. if (ttl <= 1) {
  583. ifmsh->mshstats.dropped_frames_ttl++;
  584. return;
  585. }
  586. ttl--;
  587. target_addr = PERR_IE_TARGET_ADDR(perr_elem);
  588. target_sn = PERR_IE_TARGET_SN(perr_elem);
  589. target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
  590. rcu_read_lock();
  591. mpath = mesh_path_lookup(target_addr, sdata);
  592. if (mpath) {
  593. spin_lock_bh(&mpath->state_lock);
  594. if (mpath->flags & MESH_PATH_ACTIVE &&
  595. memcmp(ta, next_hop_deref_protected(mpath)->sta.addr,
  596. ETH_ALEN) == 0 &&
  597. (!(mpath->flags & MESH_PATH_SN_VALID) ||
  598. SN_GT(target_sn, mpath->sn))) {
  599. mpath->flags &= ~MESH_PATH_ACTIVE;
  600. mpath->sn = target_sn;
  601. spin_unlock_bh(&mpath->state_lock);
  602. mesh_path_error_tx(ttl, target_addr, cpu_to_le32(target_sn),
  603. cpu_to_le16(target_rcode),
  604. broadcast_addr, sdata);
  605. } else
  606. spin_unlock_bh(&mpath->state_lock);
  607. }
  608. rcu_read_unlock();
  609. }
  610. static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
  611. struct ieee80211_mgmt *mgmt,
  612. struct ieee80211_rann_ie *rann)
  613. {
  614. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  615. struct mesh_path *mpath;
  616. u8 ttl, flags, hopcount;
  617. u8 *orig_addr;
  618. u32 orig_sn, metric;
  619. ttl = rann->rann_ttl;
  620. if (ttl <= 1) {
  621. ifmsh->mshstats.dropped_frames_ttl++;
  622. return;
  623. }
  624. ttl--;
  625. flags = rann->rann_flags;
  626. orig_addr = rann->rann_addr;
  627. orig_sn = rann->rann_seq;
  628. hopcount = rann->rann_hopcount;
  629. hopcount++;
  630. metric = rann->rann_metric;
  631. mhwmp_dbg("received RANN from %pM\n", orig_addr);
  632. rcu_read_lock();
  633. mpath = mesh_path_lookup(orig_addr, sdata);
  634. if (!mpath) {
  635. mesh_path_add(orig_addr, sdata);
  636. mpath = mesh_path_lookup(orig_addr, sdata);
  637. if (!mpath) {
  638. rcu_read_unlock();
  639. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  640. return;
  641. }
  642. mesh_queue_preq(mpath,
  643. PREQ_Q_F_START | PREQ_Q_F_REFRESH);
  644. }
  645. if (mpath->sn < orig_sn) {
  646. mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
  647. cpu_to_le32(orig_sn),
  648. 0, NULL, 0, broadcast_addr,
  649. hopcount, ttl, 0,
  650. cpu_to_le32(metric + mpath->metric),
  651. 0, sdata);
  652. mpath->sn = orig_sn;
  653. }
  654. rcu_read_unlock();
  655. }
  656. void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
  657. struct ieee80211_mgmt *mgmt,
  658. size_t len)
  659. {
  660. struct ieee802_11_elems elems;
  661. size_t baselen;
  662. u32 last_hop_metric;
  663. /* need action_code */
  664. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  665. return;
  666. baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
  667. ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
  668. len - baselen, &elems);
  669. if (elems.preq) {
  670. if (elems.preq_len != 37)
  671. /* Right now we support just 1 destination and no AE */
  672. return;
  673. last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
  674. MPATH_PREQ);
  675. if (last_hop_metric)
  676. hwmp_preq_frame_process(sdata, mgmt, elems.preq,
  677. last_hop_metric);
  678. }
  679. if (elems.prep) {
  680. if (elems.prep_len != 31)
  681. /* Right now we support no AE */
  682. return;
  683. last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
  684. MPATH_PREP);
  685. if (last_hop_metric)
  686. hwmp_prep_frame_process(sdata, mgmt, elems.prep,
  687. last_hop_metric);
  688. }
  689. if (elems.perr) {
  690. if (elems.perr_len != 15)
  691. /* Right now we support only one destination per PERR */
  692. return;
  693. hwmp_perr_frame_process(sdata, mgmt, elems.perr);
  694. }
  695. if (elems.rann)
  696. hwmp_rann_frame_process(sdata, mgmt, elems.rann);
  697. }
  698. /**
  699. * mesh_queue_preq - queue a PREQ to a given destination
  700. *
  701. * @mpath: mesh path to discover
  702. * @flags: special attributes of the PREQ to be sent
  703. *
  704. * Locking: the function must be called from within a rcu read lock block.
  705. *
  706. */
  707. static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
  708. {
  709. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  710. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  711. struct mesh_preq_queue *preq_node;
  712. preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
  713. if (!preq_node) {
  714. mhwmp_dbg("could not allocate PREQ node\n");
  715. return;
  716. }
  717. spin_lock(&ifmsh->mesh_preq_queue_lock);
  718. if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
  719. spin_unlock(&ifmsh->mesh_preq_queue_lock);
  720. kfree(preq_node);
  721. if (printk_ratelimit())
  722. mhwmp_dbg("PREQ node queue full\n");
  723. return;
  724. }
  725. memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
  726. preq_node->flags = flags;
  727. list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
  728. ++ifmsh->preq_queue_len;
  729. spin_unlock(&ifmsh->mesh_preq_queue_lock);
  730. if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
  731. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  732. else if (time_before(jiffies, ifmsh->last_preq)) {
  733. /* avoid long wait if did not send preqs for a long time
  734. * and jiffies wrapped around
  735. */
  736. ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
  737. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  738. } else
  739. mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
  740. min_preq_int_jiff(sdata));
  741. }
  742. /**
  743. * mesh_path_start_discovery - launch a path discovery from the PREQ queue
  744. *
  745. * @sdata: local mesh subif
  746. */
  747. void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
  748. {
  749. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  750. struct mesh_preq_queue *preq_node;
  751. struct mesh_path *mpath;
  752. u8 ttl, target_flags;
  753. u32 lifetime;
  754. spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
  755. if (!ifmsh->preq_queue_len ||
  756. time_before(jiffies, ifmsh->last_preq +
  757. min_preq_int_jiff(sdata))) {
  758. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  759. return;
  760. }
  761. preq_node = list_first_entry(&ifmsh->preq_queue.list,
  762. struct mesh_preq_queue, list);
  763. list_del(&preq_node->list);
  764. --ifmsh->preq_queue_len;
  765. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  766. rcu_read_lock();
  767. mpath = mesh_path_lookup(preq_node->dst, sdata);
  768. if (!mpath)
  769. goto enddiscovery;
  770. spin_lock_bh(&mpath->state_lock);
  771. if (preq_node->flags & PREQ_Q_F_START) {
  772. if (mpath->flags & MESH_PATH_RESOLVING) {
  773. spin_unlock_bh(&mpath->state_lock);
  774. goto enddiscovery;
  775. } else {
  776. mpath->flags &= ~MESH_PATH_RESOLVED;
  777. mpath->flags |= MESH_PATH_RESOLVING;
  778. mpath->discovery_retries = 0;
  779. mpath->discovery_timeout = disc_timeout_jiff(sdata);
  780. }
  781. } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
  782. mpath->flags & MESH_PATH_RESOLVED) {
  783. mpath->flags &= ~MESH_PATH_RESOLVING;
  784. spin_unlock_bh(&mpath->state_lock);
  785. goto enddiscovery;
  786. }
  787. ifmsh->last_preq = jiffies;
  788. if (time_after(jiffies, ifmsh->last_sn_update +
  789. net_traversal_jiffies(sdata)) ||
  790. time_before(jiffies, ifmsh->last_sn_update)) {
  791. ++ifmsh->sn;
  792. sdata->u.mesh.last_sn_update = jiffies;
  793. }
  794. lifetime = default_lifetime(sdata);
  795. ttl = sdata->u.mesh.mshcfg.element_ttl;
  796. if (ttl == 0) {
  797. sdata->u.mesh.mshstats.dropped_frames_ttl++;
  798. spin_unlock_bh(&mpath->state_lock);
  799. goto enddiscovery;
  800. }
  801. if (preq_node->flags & PREQ_Q_F_REFRESH)
  802. target_flags = MP_F_DO;
  803. else
  804. target_flags = MP_F_RF;
  805. spin_unlock_bh(&mpath->state_lock);
  806. mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr,
  807. cpu_to_le32(ifmsh->sn), target_flags, mpath->dst,
  808. cpu_to_le32(mpath->sn), broadcast_addr, 0,
  809. ttl, cpu_to_le32(lifetime), 0,
  810. cpu_to_le32(ifmsh->preq_id++), sdata);
  811. mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
  812. enddiscovery:
  813. rcu_read_unlock();
  814. kfree(preq_node);
  815. }
  816. /**
  817. * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame
  818. *
  819. * @skb: 802.11 frame to be sent
  820. * @sdata: network subif the frame will be sent through
  821. *
  822. * Returns: 0 if the next hop was found. Nonzero otherwise. If no next hop is
  823. * found, the function will start a path discovery and queue the frame so it is
  824. * sent when the path is resolved. This means the caller must not free the skb
  825. * in this case.
  826. */
  827. int mesh_nexthop_lookup(struct sk_buff *skb,
  828. struct ieee80211_sub_if_data *sdata)
  829. {
  830. struct sk_buff *skb_to_free = NULL;
  831. struct mesh_path *mpath;
  832. struct sta_info *next_hop;
  833. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  834. u8 *target_addr = hdr->addr3;
  835. int err = 0;
  836. rcu_read_lock();
  837. mpath = mesh_path_lookup(target_addr, sdata);
  838. if (!mpath) {
  839. mesh_path_add(target_addr, sdata);
  840. mpath = mesh_path_lookup(target_addr, sdata);
  841. if (!mpath) {
  842. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  843. err = -ENOSPC;
  844. goto endlookup;
  845. }
  846. }
  847. if (mpath->flags & MESH_PATH_ACTIVE) {
  848. if (time_after(jiffies,
  849. mpath->exp_time -
  850. msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
  851. !memcmp(sdata->vif.addr, hdr->addr4, ETH_ALEN) &&
  852. !(mpath->flags & MESH_PATH_RESOLVING) &&
  853. !(mpath->flags & MESH_PATH_FIXED)) {
  854. mesh_queue_preq(mpath,
  855. PREQ_Q_F_START | PREQ_Q_F_REFRESH);
  856. }
  857. next_hop = rcu_dereference(mpath->next_hop);
  858. if (next_hop)
  859. memcpy(hdr->addr1, next_hop->sta.addr, ETH_ALEN);
  860. else
  861. err = -ENOENT;
  862. } else {
  863. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  864. if (!(mpath->flags & MESH_PATH_RESOLVING)) {
  865. /* Start discovery only if it is not running yet */
  866. mesh_queue_preq(mpath, PREQ_Q_F_START);
  867. }
  868. if (skb_queue_len(&mpath->frame_queue) >=
  869. MESH_FRAME_QUEUE_LEN)
  870. skb_to_free = skb_dequeue(&mpath->frame_queue);
  871. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  872. skb_queue_tail(&mpath->frame_queue, skb);
  873. if (skb_to_free)
  874. mesh_path_discard_frame(skb_to_free, sdata);
  875. err = -ENOENT;
  876. }
  877. endlookup:
  878. rcu_read_unlock();
  879. return err;
  880. }
  881. void mesh_path_timer(unsigned long data)
  882. {
  883. struct mesh_path *mpath = (void *) data;
  884. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  885. if (sdata->local->quiescing)
  886. return;
  887. spin_lock_bh(&mpath->state_lock);
  888. if (mpath->flags & MESH_PATH_RESOLVED ||
  889. (!(mpath->flags & MESH_PATH_RESOLVING)))
  890. mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
  891. else if (mpath->discovery_retries < max_preq_retries(sdata)) {
  892. ++mpath->discovery_retries;
  893. mpath->discovery_timeout *= 2;
  894. mesh_queue_preq(mpath, 0);
  895. } else {
  896. mpath->flags = 0;
  897. mpath->exp_time = jiffies;
  898. mesh_path_flush_pending(mpath);
  899. }
  900. spin_unlock_bh(&mpath->state_lock);
  901. }
  902. void
  903. mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
  904. {
  905. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  906. mesh_path_sel_frame_tx(MPATH_RANN, 0, sdata->vif.addr,
  907. cpu_to_le32(++ifmsh->sn),
  908. 0, NULL, 0, broadcast_addr,
  909. 0, sdata->u.mesh.mshcfg.element_ttl,
  910. 0, 0, 0, sdata);
  911. }