mesh_hwmp.c 25 KB

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