mesh_hwmp.c 29 KB

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