mesh_hwmp.c 25 KB

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