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