mesh_hwmp.c 29 KB

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