mesh_hwmp.c 24 KB

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