mesh_hwmp.c 24 KB

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