mesh_sync.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright 2011-2012, Pavel Zubarev <pavel.zubarev@gmail.com>
  3. * Copyright 2011-2012, Marco Porsch <marco.porsch@s2005.tu-chemnitz.de>
  4. * Copyright 2011-2012, cozybit Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include "ieee80211_i.h"
  11. #include "mesh.h"
  12. #include "driver-ops.h"
  13. #ifdef CONFIG_MAC80211_VERBOSE_MESH_SYNC_DEBUG
  14. #define msync_dbg(fmt, args...) \
  15. printk(KERN_DEBUG "Mesh sync (%s): " fmt "\n", sdata->name, ##args)
  16. #else
  17. #define msync_dbg(fmt, args...) do { (void)(0); } while (0)
  18. #endif
  19. /* This is not in the standard. It represents a tolerable tbtt drift below
  20. * which we do no TSF adjustment.
  21. */
  22. #define TBTT_MINIMUM_ADJUSTMENT 10
  23. struct sync_method {
  24. u8 method;
  25. struct ieee80211_mesh_sync_ops ops;
  26. };
  27. /**
  28. * mesh_peer_tbtt_adjusting - check if an mp is currently adjusting its TBTT
  29. *
  30. * @ie: information elements of a management frame from the mesh peer
  31. */
  32. static bool mesh_peer_tbtt_adjusting(struct ieee802_11_elems *ie)
  33. {
  34. return (ie->mesh_config->meshconf_cap &
  35. MESHCONF_CAPAB_TBTT_ADJUSTING) != 0;
  36. }
  37. void mesh_sync_adjust_tbtt(struct ieee80211_sub_if_data *sdata)
  38. {
  39. struct ieee80211_local *local = sdata->local;
  40. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  41. /* sdata->vif.bss_conf.beacon_int in 1024us units, 0.04% */
  42. u64 beacon_int_fraction = sdata->vif.bss_conf.beacon_int * 1024 / 2500;
  43. u64 tsf;
  44. u64 tsfdelta;
  45. spin_lock_bh(&ifmsh->sync_offset_lock);
  46. if (ifmsh->sync_offset_clockdrift_max < beacon_int_fraction) {
  47. msync_dbg("TBTT : max clockdrift=%lld; adjusting",
  48. (long long) ifmsh->sync_offset_clockdrift_max);
  49. tsfdelta = -ifmsh->sync_offset_clockdrift_max;
  50. ifmsh->sync_offset_clockdrift_max = 0;
  51. } else {
  52. msync_dbg("TBTT : max clockdrift=%lld; adjusting by %llu",
  53. (long long) ifmsh->sync_offset_clockdrift_max,
  54. (unsigned long long) beacon_int_fraction);
  55. tsfdelta = -beacon_int_fraction;
  56. ifmsh->sync_offset_clockdrift_max -= beacon_int_fraction;
  57. }
  58. tsf = drv_get_tsf(local, sdata);
  59. if (tsf != -1ULL)
  60. drv_set_tsf(local, sdata, tsf + tsfdelta);
  61. spin_unlock_bh(&ifmsh->sync_offset_lock);
  62. }
  63. static void mesh_sync_offset_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
  64. u16 stype,
  65. struct ieee80211_mgmt *mgmt,
  66. struct ieee802_11_elems *elems,
  67. struct ieee80211_rx_status *rx_status)
  68. {
  69. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  70. struct ieee80211_local *local = sdata->local;
  71. struct sta_info *sta;
  72. u64 t_t, t_r;
  73. WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
  74. /* standard mentions only beacons */
  75. if (stype != IEEE80211_STYPE_BEACON)
  76. return;
  77. /* The current tsf is a first approximation for the timestamp
  78. * for the received beacon. Further down we try to get a
  79. * better value from the rx_status->mactime field if
  80. * available. Also we have to call drv_get_tsf() before
  81. * entering the rcu-read section.*/
  82. t_r = drv_get_tsf(local, sdata);
  83. rcu_read_lock();
  84. sta = sta_info_get(sdata, mgmt->sa);
  85. if (!sta)
  86. goto no_sync;
  87. /* check offset sync conditions (13.13.2.2.1)
  88. *
  89. * TODO also sync to
  90. * dot11MeshNbrOffsetMaxNeighbor non-peer non-MBSS neighbors
  91. */
  92. if (elems->mesh_config && mesh_peer_tbtt_adjusting(elems)) {
  93. clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
  94. msync_dbg("STA %pM : is adjusting TBTT", sta->sta.addr);
  95. goto no_sync;
  96. }
  97. if (rx_status->flag & RX_FLAG_MACTIME_MPDU && rx_status->mactime) {
  98. /*
  99. * The mactime is defined as the time the first data symbol
  100. * of the frame hits the PHY, and the timestamp of the beacon
  101. * is defined as "the time that the data symbol containing the
  102. * first bit of the timestamp is transmitted to the PHY plus
  103. * the transmitting STA's delays through its local PHY from the
  104. * MAC-PHY interface to its interface with the WM" (802.11
  105. * 11.1.2)
  106. *
  107. * T_r, in 13.13.2.2.2, is just defined as "the frame reception
  108. * time" but we unless we interpret that time to be the same
  109. * time of the beacon timestamp, the offset calculation will be
  110. * off. Below we adjust t_r to be "the time at which the first
  111. * symbol of the timestamp element in the beacon is received".
  112. * This correction depends on the rate.
  113. *
  114. * Based on similar code in ibss.c
  115. */
  116. int rate;
  117. if (rx_status->flag & RX_FLAG_HT) {
  118. /* TODO:
  119. * In principle there could be HT-beacons (Dual Beacon
  120. * HT Operation options), but for now ignore them and
  121. * just use the primary (i.e. non-HT) beacons for
  122. * synchronization.
  123. * */
  124. goto no_sync;
  125. } else
  126. rate = local->hw.wiphy->bands[rx_status->band]->
  127. bitrates[rx_status->rate_idx].bitrate;
  128. /* 24 bytes of header * 8 bits/byte *
  129. * 10*(100 Kbps)/Mbps / rate (100 Kbps)*/
  130. t_r = rx_status->mactime + (24 * 8 * 10 / rate);
  131. }
  132. /* Timing offset calculation (see 13.13.2.2.2) */
  133. t_t = le64_to_cpu(mgmt->u.beacon.timestamp);
  134. sta->t_offset = t_t - t_r;
  135. if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
  136. s64 t_clockdrift = sta->t_offset_setpoint
  137. - sta->t_offset;
  138. msync_dbg("STA %pM : sta->t_offset=%lld,"
  139. " sta->t_offset_setpoint=%lld,"
  140. " t_clockdrift=%lld",
  141. sta->sta.addr,
  142. (long long) sta->t_offset,
  143. (long long)
  144. sta->t_offset_setpoint,
  145. (long long) t_clockdrift);
  146. rcu_read_unlock();
  147. spin_lock_bh(&ifmsh->sync_offset_lock);
  148. if (t_clockdrift >
  149. ifmsh->sync_offset_clockdrift_max)
  150. ifmsh->sync_offset_clockdrift_max
  151. = t_clockdrift;
  152. spin_unlock_bh(&ifmsh->sync_offset_lock);
  153. } else {
  154. sta->t_offset_setpoint = sta->t_offset;
  155. set_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
  156. msync_dbg("STA %pM : offset was invalid, "
  157. " sta->t_offset=%lld",
  158. sta->sta.addr,
  159. (long long) sta->t_offset);
  160. rcu_read_unlock();
  161. }
  162. return;
  163. no_sync:
  164. rcu_read_unlock();
  165. }
  166. static void mesh_sync_offset_adjust_tbtt(struct ieee80211_sub_if_data *sdata)
  167. {
  168. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  169. WARN_ON(ifmsh->mesh_sp_id
  170. != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
  171. BUG_ON(!rcu_read_lock_held());
  172. spin_lock_bh(&ifmsh->sync_offset_lock);
  173. if (ifmsh->sync_offset_clockdrift_max >
  174. TBTT_MINIMUM_ADJUSTMENT) {
  175. /* Since ajusting the tsf here would
  176. * require a possibly blocking call
  177. * to the driver tsf setter, we punt
  178. * the tsf adjustment to the mesh tasklet
  179. */
  180. msync_dbg("TBTT : kicking off TBTT "
  181. "adjustment with "
  182. "clockdrift_max=%lld",
  183. ifmsh->sync_offset_clockdrift_max);
  184. set_bit(MESH_WORK_DRIFT_ADJUST,
  185. &ifmsh->wrkq_flags);
  186. } else {
  187. msync_dbg("TBTT : max clockdrift=%lld; "
  188. "too small to adjust",
  189. (long long)
  190. ifmsh->sync_offset_clockdrift_max);
  191. ifmsh->sync_offset_clockdrift_max = 0;
  192. }
  193. spin_unlock_bh(&ifmsh->sync_offset_lock);
  194. }
  195. static const u8 *mesh_get_vendor_oui(struct ieee80211_sub_if_data *sdata)
  196. {
  197. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  198. u8 offset;
  199. if (!ifmsh->ie || !ifmsh->ie_len)
  200. return NULL;
  201. offset = ieee80211_ie_split_vendor(ifmsh->ie,
  202. ifmsh->ie_len, 0);
  203. if (!offset)
  204. return NULL;
  205. return ifmsh->ie + offset + 2;
  206. }
  207. static void mesh_sync_vendor_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
  208. u16 stype,
  209. struct ieee80211_mgmt *mgmt,
  210. struct ieee802_11_elems *elems,
  211. struct ieee80211_rx_status *rx_status)
  212. {
  213. const u8 *oui;
  214. WARN_ON(sdata->u.mesh.mesh_sp_id != IEEE80211_SYNC_METHOD_VENDOR);
  215. msync_dbg("called mesh_sync_vendor_rx_bcn_presp");
  216. oui = mesh_get_vendor_oui(sdata);
  217. /* here you would implement the vendor offset tracking for this oui */
  218. }
  219. static void mesh_sync_vendor_adjust_tbtt(struct ieee80211_sub_if_data *sdata)
  220. {
  221. const u8 *oui;
  222. WARN_ON(sdata->u.mesh.mesh_sp_id != IEEE80211_SYNC_METHOD_VENDOR);
  223. msync_dbg("called mesh_sync_vendor_adjust_tbtt");
  224. oui = mesh_get_vendor_oui(sdata);
  225. /* here you would implement the vendor tsf adjustment for this oui */
  226. }
  227. /* global variable */
  228. static struct sync_method sync_methods[] = {
  229. {
  230. .method = IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET,
  231. .ops = {
  232. .rx_bcn_presp = &mesh_sync_offset_rx_bcn_presp,
  233. .adjust_tbtt = &mesh_sync_offset_adjust_tbtt,
  234. }
  235. },
  236. {
  237. .method = IEEE80211_SYNC_METHOD_VENDOR,
  238. .ops = {
  239. .rx_bcn_presp = &mesh_sync_vendor_rx_bcn_presp,
  240. .adjust_tbtt = &mesh_sync_vendor_adjust_tbtt,
  241. }
  242. },
  243. };
  244. struct ieee80211_mesh_sync_ops *ieee80211_mesh_sync_ops_get(u8 method)
  245. {
  246. struct ieee80211_mesh_sync_ops *ops = NULL;
  247. u8 i;
  248. for (i = 0 ; i < ARRAY_SIZE(sync_methods); ++i) {
  249. if (sync_methods[i].method == method) {
  250. ops = &sync_methods[i].ops;
  251. break;
  252. }
  253. }
  254. return ops;
  255. }