mesh_hwmp.c 24 KB

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