mesh_hwmp.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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 = WLAN_CATEGORY_MESH_PATH_SEL;
  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 = WLAN_CATEGORY_MESH_PATH_SEL;
  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++ = 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. new_metric >= mpath->metric)) {
  355. process = false;
  356. fresh_info = false;
  357. }
  358. }
  359. } else {
  360. mesh_path_add(orig_addr, sdata);
  361. mpath = mesh_path_lookup(orig_addr, sdata);
  362. if (!mpath) {
  363. rcu_read_unlock();
  364. return 0;
  365. }
  366. spin_lock_bh(&mpath->state_lock);
  367. }
  368. if (fresh_info) {
  369. mesh_path_assign_nexthop(mpath, sta);
  370. mpath->flags |= MESH_PATH_SN_VALID;
  371. mpath->metric = new_metric;
  372. mpath->sn = orig_sn;
  373. mpath->exp_time = time_after(mpath->exp_time, exp_time)
  374. ? mpath->exp_time : exp_time;
  375. mesh_path_activate(mpath);
  376. spin_unlock_bh(&mpath->state_lock);
  377. mesh_path_tx_pending(mpath);
  378. /* draft says preq_id should be saved to, but there does
  379. * not seem to be any use for it, skipping by now
  380. */
  381. } else
  382. spin_unlock_bh(&mpath->state_lock);
  383. }
  384. /* Update and check transmitter routing info */
  385. ta = mgmt->sa;
  386. if (memcmp(orig_addr, ta, ETH_ALEN) == 0)
  387. fresh_info = false;
  388. else {
  389. fresh_info = true;
  390. mpath = mesh_path_lookup(ta, sdata);
  391. if (mpath) {
  392. spin_lock_bh(&mpath->state_lock);
  393. if ((mpath->flags & MESH_PATH_FIXED) ||
  394. ((mpath->flags & MESH_PATH_ACTIVE) &&
  395. (last_hop_metric > mpath->metric)))
  396. fresh_info = false;
  397. } else {
  398. mesh_path_add(ta, sdata);
  399. mpath = mesh_path_lookup(ta, sdata);
  400. if (!mpath) {
  401. rcu_read_unlock();
  402. return 0;
  403. }
  404. spin_lock_bh(&mpath->state_lock);
  405. }
  406. if (fresh_info) {
  407. mesh_path_assign_nexthop(mpath, sta);
  408. mpath->flags &= ~MESH_PATH_SN_VALID;
  409. mpath->metric = last_hop_metric;
  410. mpath->exp_time = time_after(mpath->exp_time, exp_time)
  411. ? mpath->exp_time : exp_time;
  412. mesh_path_activate(mpath);
  413. spin_unlock_bh(&mpath->state_lock);
  414. mesh_path_tx_pending(mpath);
  415. } else
  416. spin_unlock_bh(&mpath->state_lock);
  417. }
  418. rcu_read_unlock();
  419. return process ? new_metric : 0;
  420. }
  421. static void hwmp_preq_frame_process(struct ieee80211_sub_if_data *sdata,
  422. struct ieee80211_mgmt *mgmt,
  423. u8 *preq_elem, u32 metric)
  424. {
  425. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  426. struct mesh_path *mpath;
  427. u8 *target_addr, *orig_addr;
  428. u8 target_flags, ttl;
  429. u32 orig_sn, target_sn, lifetime;
  430. bool reply = false;
  431. bool forward = true;
  432. /* Update target SN, if present */
  433. target_addr = PREQ_IE_TARGET_ADDR(preq_elem);
  434. orig_addr = PREQ_IE_ORIG_ADDR(preq_elem);
  435. target_sn = PREQ_IE_TARGET_SN(preq_elem);
  436. orig_sn = PREQ_IE_ORIG_SN(preq_elem);
  437. target_flags = PREQ_IE_TARGET_F(preq_elem);
  438. mhwmp_dbg("received PREQ from %pM\n", orig_addr);
  439. if (memcmp(target_addr, sdata->vif.addr, ETH_ALEN) == 0) {
  440. mhwmp_dbg("PREQ is for us\n");
  441. forward = false;
  442. reply = true;
  443. metric = 0;
  444. if (time_after(jiffies, ifmsh->last_sn_update +
  445. net_traversal_jiffies(sdata)) ||
  446. time_before(jiffies, ifmsh->last_sn_update)) {
  447. target_sn = ++ifmsh->sn;
  448. ifmsh->last_sn_update = jiffies;
  449. }
  450. } else {
  451. rcu_read_lock();
  452. mpath = mesh_path_lookup(target_addr, sdata);
  453. if (mpath) {
  454. if ((!(mpath->flags & MESH_PATH_SN_VALID)) ||
  455. SN_LT(mpath->sn, target_sn)) {
  456. mpath->sn = target_sn;
  457. mpath->flags |= MESH_PATH_SN_VALID;
  458. } else if ((!(target_flags & MP_F_DO)) &&
  459. (mpath->flags & MESH_PATH_ACTIVE)) {
  460. reply = true;
  461. metric = mpath->metric;
  462. target_sn = mpath->sn;
  463. if (target_flags & MP_F_RF)
  464. target_flags |= MP_F_DO;
  465. else
  466. forward = false;
  467. }
  468. }
  469. rcu_read_unlock();
  470. }
  471. if (reply) {
  472. lifetime = PREQ_IE_LIFETIME(preq_elem);
  473. ttl = ifmsh->mshcfg.element_ttl;
  474. if (ttl != 0) {
  475. mhwmp_dbg("replying to the PREQ\n");
  476. mesh_path_sel_frame_tx(MPATH_PREP, 0, target_addr,
  477. cpu_to_le32(target_sn), 0, orig_addr,
  478. cpu_to_le32(orig_sn), mgmt->sa, 0, ttl,
  479. cpu_to_le32(lifetime), cpu_to_le32(metric),
  480. 0, sdata);
  481. } else
  482. ifmsh->mshstats.dropped_frames_ttl++;
  483. }
  484. if (forward) {
  485. u32 preq_id;
  486. u8 hopcount, flags;
  487. ttl = PREQ_IE_TTL(preq_elem);
  488. lifetime = PREQ_IE_LIFETIME(preq_elem);
  489. if (ttl <= 1) {
  490. ifmsh->mshstats.dropped_frames_ttl++;
  491. return;
  492. }
  493. mhwmp_dbg("forwarding the PREQ from %pM\n", orig_addr);
  494. --ttl;
  495. flags = PREQ_IE_FLAGS(preq_elem);
  496. preq_id = PREQ_IE_PREQ_ID(preq_elem);
  497. hopcount = PREQ_IE_HOPCOUNT(preq_elem) + 1;
  498. mesh_path_sel_frame_tx(MPATH_PREQ, flags, orig_addr,
  499. cpu_to_le32(orig_sn), target_flags, target_addr,
  500. cpu_to_le32(target_sn), broadcast_addr,
  501. hopcount, ttl, cpu_to_le32(lifetime),
  502. cpu_to_le32(metric), cpu_to_le32(preq_id),
  503. sdata);
  504. ifmsh->mshstats.fwded_mcast++;
  505. ifmsh->mshstats.fwded_frames++;
  506. }
  507. }
  508. static void hwmp_prep_frame_process(struct ieee80211_sub_if_data *sdata,
  509. struct ieee80211_mgmt *mgmt,
  510. u8 *prep_elem, u32 metric)
  511. {
  512. struct mesh_path *mpath;
  513. u8 *target_addr, *orig_addr;
  514. u8 ttl, hopcount, flags;
  515. u8 next_hop[ETH_ALEN];
  516. u32 target_sn, orig_sn, lifetime;
  517. mhwmp_dbg("received PREP from %pM\n", PREP_IE_ORIG_ADDR(prep_elem));
  518. /* Note that we divert from the draft nomenclature and denominate
  519. * destination to what the draft refers to as origininator. So in this
  520. * function destnation refers to the final destination of the PREP,
  521. * which corresponds with the originator of the PREQ which this PREP
  522. * replies
  523. */
  524. target_addr = PREP_IE_TARGET_ADDR(prep_elem);
  525. if (memcmp(target_addr, sdata->vif.addr, ETH_ALEN) == 0)
  526. /* destination, no forwarding required */
  527. return;
  528. ttl = PREP_IE_TTL(prep_elem);
  529. if (ttl <= 1) {
  530. sdata->u.mesh.mshstats.dropped_frames_ttl++;
  531. return;
  532. }
  533. rcu_read_lock();
  534. mpath = mesh_path_lookup(target_addr, sdata);
  535. if (mpath)
  536. spin_lock_bh(&mpath->state_lock);
  537. else
  538. goto fail;
  539. if (!(mpath->flags & MESH_PATH_ACTIVE)) {
  540. spin_unlock_bh(&mpath->state_lock);
  541. goto fail;
  542. }
  543. memcpy(next_hop, mpath->next_hop->sta.addr, ETH_ALEN);
  544. spin_unlock_bh(&mpath->state_lock);
  545. --ttl;
  546. flags = PREP_IE_FLAGS(prep_elem);
  547. lifetime = PREP_IE_LIFETIME(prep_elem);
  548. hopcount = PREP_IE_HOPCOUNT(prep_elem) + 1;
  549. orig_addr = PREP_IE_ORIG_ADDR(prep_elem);
  550. target_sn = PREP_IE_TARGET_SN(prep_elem);
  551. orig_sn = PREP_IE_ORIG_SN(prep_elem);
  552. mesh_path_sel_frame_tx(MPATH_PREP, flags, orig_addr,
  553. cpu_to_le32(orig_sn), 0, target_addr,
  554. cpu_to_le32(target_sn), next_hop, hopcount,
  555. ttl, cpu_to_le32(lifetime), cpu_to_le32(metric),
  556. 0, sdata);
  557. rcu_read_unlock();
  558. sdata->u.mesh.mshstats.fwded_unicast++;
  559. sdata->u.mesh.mshstats.fwded_frames++;
  560. return;
  561. fail:
  562. rcu_read_unlock();
  563. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  564. }
  565. static void hwmp_perr_frame_process(struct ieee80211_sub_if_data *sdata,
  566. struct ieee80211_mgmt *mgmt, u8 *perr_elem)
  567. {
  568. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  569. struct mesh_path *mpath;
  570. u8 ttl;
  571. u8 *ta, *target_addr;
  572. u32 target_sn;
  573. u16 target_rcode;
  574. ta = mgmt->sa;
  575. ttl = PERR_IE_TTL(perr_elem);
  576. if (ttl <= 1) {
  577. ifmsh->mshstats.dropped_frames_ttl++;
  578. return;
  579. }
  580. ttl--;
  581. target_addr = PERR_IE_TARGET_ADDR(perr_elem);
  582. target_sn = PERR_IE_TARGET_SN(perr_elem);
  583. target_rcode = PERR_IE_TARGET_RCODE(perr_elem);
  584. rcu_read_lock();
  585. mpath = mesh_path_lookup(target_addr, sdata);
  586. if (mpath) {
  587. spin_lock_bh(&mpath->state_lock);
  588. if (mpath->flags & MESH_PATH_ACTIVE &&
  589. memcmp(ta, mpath->next_hop->sta.addr, ETH_ALEN) == 0 &&
  590. (!(mpath->flags & MESH_PATH_SN_VALID) ||
  591. SN_GT(target_sn, mpath->sn))) {
  592. mpath->flags &= ~MESH_PATH_ACTIVE;
  593. mpath->sn = target_sn;
  594. spin_unlock_bh(&mpath->state_lock);
  595. mesh_path_error_tx(ttl, target_addr, cpu_to_le32(target_sn),
  596. cpu_to_le16(target_rcode),
  597. broadcast_addr, sdata);
  598. } else
  599. spin_unlock_bh(&mpath->state_lock);
  600. }
  601. rcu_read_unlock();
  602. }
  603. static void hwmp_rann_frame_process(struct ieee80211_sub_if_data *sdata,
  604. struct ieee80211_mgmt *mgmt,
  605. struct ieee80211_rann_ie *rann)
  606. {
  607. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  608. struct mesh_path *mpath;
  609. u8 ttl, flags, hopcount;
  610. u8 *orig_addr;
  611. u32 orig_sn, metric;
  612. ttl = rann->rann_ttl;
  613. if (ttl <= 1) {
  614. ifmsh->mshstats.dropped_frames_ttl++;
  615. return;
  616. }
  617. ttl--;
  618. flags = rann->rann_flags;
  619. orig_addr = rann->rann_addr;
  620. orig_sn = rann->rann_seq;
  621. hopcount = rann->rann_hopcount;
  622. hopcount++;
  623. metric = rann->rann_metric;
  624. mhwmp_dbg("received RANN from %pM\n", orig_addr);
  625. rcu_read_lock();
  626. mpath = mesh_path_lookup(orig_addr, sdata);
  627. if (!mpath) {
  628. mesh_path_add(orig_addr, sdata);
  629. mpath = mesh_path_lookup(orig_addr, sdata);
  630. if (!mpath) {
  631. rcu_read_unlock();
  632. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  633. return;
  634. }
  635. mesh_queue_preq(mpath,
  636. PREQ_Q_F_START | PREQ_Q_F_REFRESH);
  637. }
  638. if (mpath->sn < orig_sn) {
  639. mesh_path_sel_frame_tx(MPATH_RANN, flags, orig_addr,
  640. cpu_to_le32(orig_sn),
  641. 0, NULL, 0, broadcast_addr,
  642. hopcount, ttl, 0,
  643. cpu_to_le32(metric + mpath->metric),
  644. 0, sdata);
  645. mpath->sn = orig_sn;
  646. }
  647. rcu_read_unlock();
  648. }
  649. void mesh_rx_path_sel_frame(struct ieee80211_sub_if_data *sdata,
  650. struct ieee80211_mgmt *mgmt,
  651. size_t len)
  652. {
  653. struct ieee802_11_elems elems;
  654. size_t baselen;
  655. u32 last_hop_metric;
  656. /* need action_code */
  657. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  658. return;
  659. baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
  660. ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
  661. len - baselen, &elems);
  662. if (elems.preq) {
  663. if (elems.preq_len != 37)
  664. /* Right now we support just 1 destination and no AE */
  665. return;
  666. last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.preq,
  667. MPATH_PREQ);
  668. if (last_hop_metric)
  669. hwmp_preq_frame_process(sdata, mgmt, elems.preq,
  670. last_hop_metric);
  671. }
  672. if (elems.prep) {
  673. if (elems.prep_len != 31)
  674. /* Right now we support no AE */
  675. return;
  676. last_hop_metric = hwmp_route_info_get(sdata, mgmt, elems.prep,
  677. MPATH_PREP);
  678. if (last_hop_metric)
  679. hwmp_prep_frame_process(sdata, mgmt, elems.prep,
  680. last_hop_metric);
  681. }
  682. if (elems.perr) {
  683. if (elems.perr_len != 15)
  684. /* Right now we support only one destination per PERR */
  685. return;
  686. hwmp_perr_frame_process(sdata, mgmt, elems.perr);
  687. }
  688. if (elems.rann)
  689. hwmp_rann_frame_process(sdata, mgmt, elems.rann);
  690. }
  691. /**
  692. * mesh_queue_preq - queue a PREQ to a given destination
  693. *
  694. * @mpath: mesh path to discover
  695. * @flags: special attributes of the PREQ to be sent
  696. *
  697. * Locking: the function must be called from within a rcu read lock block.
  698. *
  699. */
  700. static void mesh_queue_preq(struct mesh_path *mpath, u8 flags)
  701. {
  702. struct ieee80211_sub_if_data *sdata = mpath->sdata;
  703. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  704. struct mesh_preq_queue *preq_node;
  705. preq_node = kmalloc(sizeof(struct mesh_preq_queue), GFP_ATOMIC);
  706. if (!preq_node) {
  707. mhwmp_dbg("could not allocate PREQ node\n");
  708. return;
  709. }
  710. spin_lock(&ifmsh->mesh_preq_queue_lock);
  711. if (ifmsh->preq_queue_len == MAX_PREQ_QUEUE_LEN) {
  712. spin_unlock(&ifmsh->mesh_preq_queue_lock);
  713. kfree(preq_node);
  714. if (printk_ratelimit())
  715. mhwmp_dbg("PREQ node queue full\n");
  716. return;
  717. }
  718. memcpy(preq_node->dst, mpath->dst, ETH_ALEN);
  719. preq_node->flags = flags;
  720. list_add_tail(&preq_node->list, &ifmsh->preq_queue.list);
  721. ++ifmsh->preq_queue_len;
  722. spin_unlock(&ifmsh->mesh_preq_queue_lock);
  723. if (time_after(jiffies, ifmsh->last_preq + min_preq_int_jiff(sdata)))
  724. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  725. else if (time_before(jiffies, ifmsh->last_preq)) {
  726. /* avoid long wait if did not send preqs for a long time
  727. * and jiffies wrapped around
  728. */
  729. ifmsh->last_preq = jiffies - min_preq_int_jiff(sdata) - 1;
  730. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  731. } else
  732. mod_timer(&ifmsh->mesh_path_timer, ifmsh->last_preq +
  733. min_preq_int_jiff(sdata));
  734. }
  735. /**
  736. * mesh_path_start_discovery - launch a path discovery from the PREQ queue
  737. *
  738. * @sdata: local mesh subif
  739. */
  740. void mesh_path_start_discovery(struct ieee80211_sub_if_data *sdata)
  741. {
  742. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  743. struct mesh_preq_queue *preq_node;
  744. struct mesh_path *mpath;
  745. u8 ttl, target_flags;
  746. u32 lifetime;
  747. spin_lock_bh(&ifmsh->mesh_preq_queue_lock);
  748. if (!ifmsh->preq_queue_len ||
  749. time_before(jiffies, ifmsh->last_preq +
  750. min_preq_int_jiff(sdata))) {
  751. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  752. return;
  753. }
  754. preq_node = list_first_entry(&ifmsh->preq_queue.list,
  755. struct mesh_preq_queue, list);
  756. list_del(&preq_node->list);
  757. --ifmsh->preq_queue_len;
  758. spin_unlock_bh(&ifmsh->mesh_preq_queue_lock);
  759. rcu_read_lock();
  760. mpath = mesh_path_lookup(preq_node->dst, sdata);
  761. if (!mpath)
  762. goto enddiscovery;
  763. spin_lock_bh(&mpath->state_lock);
  764. if (preq_node->flags & PREQ_Q_F_START) {
  765. if (mpath->flags & MESH_PATH_RESOLVING) {
  766. spin_unlock_bh(&mpath->state_lock);
  767. goto enddiscovery;
  768. } else {
  769. mpath->flags &= ~MESH_PATH_RESOLVED;
  770. mpath->flags |= MESH_PATH_RESOLVING;
  771. mpath->discovery_retries = 0;
  772. mpath->discovery_timeout = disc_timeout_jiff(sdata);
  773. }
  774. } else if (!(mpath->flags & MESH_PATH_RESOLVING) ||
  775. mpath->flags & MESH_PATH_RESOLVED) {
  776. mpath->flags &= ~MESH_PATH_RESOLVING;
  777. spin_unlock_bh(&mpath->state_lock);
  778. goto enddiscovery;
  779. }
  780. ifmsh->last_preq = jiffies;
  781. if (time_after(jiffies, ifmsh->last_sn_update +
  782. net_traversal_jiffies(sdata)) ||
  783. time_before(jiffies, ifmsh->last_sn_update)) {
  784. ++ifmsh->sn;
  785. sdata->u.mesh.last_sn_update = jiffies;
  786. }
  787. lifetime = default_lifetime(sdata);
  788. ttl = sdata->u.mesh.mshcfg.element_ttl;
  789. if (ttl == 0) {
  790. sdata->u.mesh.mshstats.dropped_frames_ttl++;
  791. spin_unlock_bh(&mpath->state_lock);
  792. goto enddiscovery;
  793. }
  794. if (preq_node->flags & PREQ_Q_F_REFRESH)
  795. target_flags = MP_F_DO;
  796. else
  797. target_flags = MP_F_RF;
  798. spin_unlock_bh(&mpath->state_lock);
  799. mesh_path_sel_frame_tx(MPATH_PREQ, 0, sdata->vif.addr,
  800. cpu_to_le32(ifmsh->sn), target_flags, mpath->dst,
  801. cpu_to_le32(mpath->sn), broadcast_addr, 0,
  802. ttl, cpu_to_le32(lifetime), 0,
  803. cpu_to_le32(ifmsh->preq_id++), sdata);
  804. mod_timer(&mpath->timer, jiffies + mpath->discovery_timeout);
  805. enddiscovery:
  806. rcu_read_unlock();
  807. kfree(preq_node);
  808. }
  809. /**
  810. * mesh_nexthop_lookup - put the appropriate next hop on a mesh frame
  811. *
  812. * @skb: 802.11 frame to be sent
  813. * @sdata: network subif the frame will be sent through
  814. *
  815. * Returns: 0 if the next hop was found. Nonzero otherwise. If no next hop is
  816. * found, the function will start a path discovery and queue the frame so it is
  817. * sent when the path is resolved. This means the caller must not free the skb
  818. * in this case.
  819. */
  820. int mesh_nexthop_lookup(struct sk_buff *skb,
  821. struct ieee80211_sub_if_data *sdata)
  822. {
  823. struct sk_buff *skb_to_free = NULL;
  824. struct mesh_path *mpath;
  825. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  826. u8 *target_addr = hdr->addr3;
  827. int err = 0;
  828. rcu_read_lock();
  829. mpath = mesh_path_lookup(target_addr, sdata);
  830. if (!mpath) {
  831. mesh_path_add(target_addr, sdata);
  832. mpath = mesh_path_lookup(target_addr, sdata);
  833. if (!mpath) {
  834. sdata->u.mesh.mshstats.dropped_frames_no_route++;
  835. err = -ENOSPC;
  836. goto endlookup;
  837. }
  838. }
  839. if (mpath->flags & MESH_PATH_ACTIVE) {
  840. if (time_after(jiffies,
  841. mpath->exp_time -
  842. msecs_to_jiffies(sdata->u.mesh.mshcfg.path_refresh_time)) &&
  843. !memcmp(sdata->vif.addr, hdr->addr4, ETH_ALEN) &&
  844. !(mpath->flags & MESH_PATH_RESOLVING) &&
  845. !(mpath->flags & MESH_PATH_FIXED)) {
  846. mesh_queue_preq(mpath,
  847. PREQ_Q_F_START | PREQ_Q_F_REFRESH);
  848. }
  849. memcpy(hdr->addr1, mpath->next_hop->sta.addr, ETH_ALEN);
  850. } else {
  851. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  852. if (!(mpath->flags & MESH_PATH_RESOLVING)) {
  853. /* Start discovery only if it is not running yet */
  854. mesh_queue_preq(mpath, PREQ_Q_F_START);
  855. }
  856. if (skb_queue_len(&mpath->frame_queue) >=
  857. MESH_FRAME_QUEUE_LEN)
  858. skb_to_free = skb_dequeue(&mpath->frame_queue);
  859. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  860. skb_queue_tail(&mpath->frame_queue, skb);
  861. if (skb_to_free)
  862. mesh_path_discard_frame(skb_to_free, sdata);
  863. err = -ENOENT;
  864. }
  865. endlookup:
  866. rcu_read_unlock();
  867. return err;
  868. }
  869. void mesh_path_timer(unsigned long data)
  870. {
  871. struct ieee80211_sub_if_data *sdata;
  872. struct mesh_path *mpath;
  873. rcu_read_lock();
  874. mpath = (struct mesh_path *) data;
  875. mpath = rcu_dereference(mpath);
  876. if (!mpath)
  877. goto endmpathtimer;
  878. sdata = mpath->sdata;
  879. if (sdata->local->quiescing) {
  880. rcu_read_unlock();
  881. return;
  882. }
  883. spin_lock_bh(&mpath->state_lock);
  884. if (mpath->flags & MESH_PATH_RESOLVED ||
  885. (!(mpath->flags & MESH_PATH_RESOLVING)))
  886. mpath->flags &= ~(MESH_PATH_RESOLVING | MESH_PATH_RESOLVED);
  887. else if (mpath->discovery_retries < max_preq_retries(sdata)) {
  888. ++mpath->discovery_retries;
  889. mpath->discovery_timeout *= 2;
  890. mesh_queue_preq(mpath, 0);
  891. } else {
  892. mpath->flags = 0;
  893. mpath->exp_time = jiffies;
  894. mesh_path_flush_pending(mpath);
  895. }
  896. spin_unlock_bh(&mpath->state_lock);
  897. endmpathtimer:
  898. rcu_read_unlock();
  899. }
  900. void
  901. mesh_path_tx_root_frame(struct ieee80211_sub_if_data *sdata)
  902. {
  903. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  904. mesh_path_sel_frame_tx(MPATH_RANN, 0, sdata->vif.addr,
  905. cpu_to_le32(++ifmsh->sn),
  906. 0, NULL, 0, broadcast_addr,
  907. 0, sdata->u.mesh.mshcfg.element_ttl,
  908. 0, 0, 0, sdata);
  909. }