mesh_hwmp.c 28 KB

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