mesh_hwmp.c 28 KB

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