1
2
3
4
5
6
7
8
9
10
11#include "ieee80211_i.h"
12#include "mesh.h"
13#include "driver-ops.h"
14
15
16
17
18#define TOFFSET_MINIMUM_ADJUSTMENT 10
19
20
21
22
23
24#define TOFFSET_SET_MARGIN 20
25
26
27
28
29
30
31#define TOFFSET_MAXIMUM_ADJUSTMENT 30000
32
33struct sync_method {
34 u8 method;
35 struct ieee80211_mesh_sync_ops ops;
36};
37
38
39
40
41
42
43static bool mesh_peer_tbtt_adjusting(struct ieee802_11_elems *ie)
44{
45 return (ie->mesh_config->meshconf_cap &
46 MESHCONF_CAPAB_TBTT_ADJUSTING) != 0;
47}
48
49void mesh_sync_adjust_tbtt(struct ieee80211_sub_if_data *sdata)
50{
51 struct ieee80211_local *local = sdata->local;
52 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
53
54 u64 beacon_int_fraction = sdata->vif.bss_conf.beacon_int * 1024 / 2500;
55 u64 tsf;
56 u64 tsfdelta;
57
58 spin_lock_bh(&ifmsh->sync_offset_lock);
59 if (ifmsh->sync_offset_clockdrift_max < beacon_int_fraction) {
60 msync_dbg(sdata, "TBTT : max clockdrift=%lld; adjusting\n",
61 (long long) ifmsh->sync_offset_clockdrift_max);
62 tsfdelta = -ifmsh->sync_offset_clockdrift_max;
63 ifmsh->sync_offset_clockdrift_max = 0;
64 } else {
65 msync_dbg(sdata, "TBTT : max clockdrift=%lld; adjusting by %llu\n",
66 (long long) ifmsh->sync_offset_clockdrift_max,
67 (unsigned long long) beacon_int_fraction);
68 tsfdelta = -beacon_int_fraction;
69 ifmsh->sync_offset_clockdrift_max -= beacon_int_fraction;
70 }
71 spin_unlock_bh(&ifmsh->sync_offset_lock);
72
73 tsf = drv_get_tsf(local, sdata);
74 if (tsf != -1ULL)
75 drv_set_tsf(local, sdata, tsf + tsfdelta);
76}
77
78static void mesh_sync_offset_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
79 u16 stype,
80 struct ieee80211_mgmt *mgmt,
81 struct ieee802_11_elems *elems,
82 struct ieee80211_rx_status *rx_status)
83{
84 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
85 struct ieee80211_local *local = sdata->local;
86 struct sta_info *sta;
87 u64 t_t, t_r;
88
89 WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
90
91
92 if (stype != IEEE80211_STYPE_BEACON)
93 return;
94
95
96
97
98
99
100 t_r = drv_get_tsf(local, sdata);
101
102 rcu_read_lock();
103 sta = sta_info_get(sdata, mgmt->sa);
104 if (!sta)
105 goto no_sync;
106
107
108
109
110
111
112
113 if (elems->mesh_config && mesh_peer_tbtt_adjusting(elems)) {
114 clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
115 msync_dbg(sdata, "STA %pM : is adjusting TBTT\n", sta->sta.addr);
116 goto no_sync;
117 }
118
119 if (rx_status->flag & RX_FLAG_MACTIME_MPDU && rx_status->mactime) {
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 int rate;
139
140 if (rx_status->flag & RX_FLAG_HT) {
141
142
143
144
145
146
147 goto no_sync;
148 } else
149 rate = local->hw.wiphy->bands[rx_status->band]->
150 bitrates[rx_status->rate_idx].bitrate;
151
152
153
154 t_r = rx_status->mactime + (24 * 8 * 10 / rate);
155 }
156
157
158 t_t = le64_to_cpu(mgmt->u.beacon.timestamp);
159 sta->t_offset = t_t - t_r;
160
161 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
162 s64 t_clockdrift = sta->t_offset_setpoint
163 - sta->t_offset;
164 msync_dbg(sdata,
165 "STA %pM : sta->t_offset=%lld, sta->t_offset_setpoint=%lld, t_clockdrift=%lld\n",
166 sta->sta.addr,
167 (long long) sta->t_offset,
168 (long long)
169 sta->t_offset_setpoint,
170 (long long) t_clockdrift);
171
172 if (t_clockdrift > TOFFSET_MAXIMUM_ADJUSTMENT ||
173 t_clockdrift < -TOFFSET_MAXIMUM_ADJUSTMENT) {
174 msync_dbg(sdata,
175 "STA %pM : t_clockdrift=%lld too large, setpoint reset\n",
176 sta->sta.addr,
177 (long long) t_clockdrift);
178 clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
179 goto no_sync;
180 }
181
182 rcu_read_unlock();
183
184 spin_lock_bh(&ifmsh->sync_offset_lock);
185 if (t_clockdrift >
186 ifmsh->sync_offset_clockdrift_max)
187 ifmsh->sync_offset_clockdrift_max
188 = t_clockdrift;
189 spin_unlock_bh(&ifmsh->sync_offset_lock);
190
191 } else {
192 sta->t_offset_setpoint = sta->t_offset - TOFFSET_SET_MARGIN;
193 set_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
194 msync_dbg(sdata,
195 "STA %pM : offset was invalid, sta->t_offset=%lld\n",
196 sta->sta.addr,
197 (long long) sta->t_offset);
198 rcu_read_unlock();
199 }
200 return;
201
202no_sync:
203 rcu_read_unlock();
204}
205
206static void mesh_sync_offset_adjust_tbtt(struct ieee80211_sub_if_data *sdata)
207{
208 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
209
210 WARN_ON(ifmsh->mesh_sp_id
211 != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
212 BUG_ON(!rcu_read_lock_held());
213
214 spin_lock_bh(&ifmsh->sync_offset_lock);
215
216 if (ifmsh->sync_offset_clockdrift_max >
217 TOFFSET_MINIMUM_ADJUSTMENT) {
218
219
220
221
222
223 msync_dbg(sdata,
224 "TBTT : kicking off TBTT adjustment with clockdrift_max=%lld\n",
225 ifmsh->sync_offset_clockdrift_max);
226 set_bit(MESH_WORK_DRIFT_ADJUST,
227 &ifmsh->wrkq_flags);
228 } else {
229 msync_dbg(sdata,
230 "TBTT : max clockdrift=%lld; too small to adjust\n",
231 (long long)ifmsh->sync_offset_clockdrift_max);
232 ifmsh->sync_offset_clockdrift_max = 0;
233 }
234 spin_unlock_bh(&ifmsh->sync_offset_lock);
235}
236
237static const u8 *mesh_get_vendor_oui(struct ieee80211_sub_if_data *sdata)
238{
239 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
240 u8 offset;
241
242 if (!ifmsh->ie || !ifmsh->ie_len)
243 return NULL;
244
245 offset = ieee80211_ie_split_vendor(ifmsh->ie,
246 ifmsh->ie_len, 0);
247
248 if (!offset)
249 return NULL;
250
251 return ifmsh->ie + offset + 2;
252}
253
254static void mesh_sync_vendor_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
255 u16 stype,
256 struct ieee80211_mgmt *mgmt,
257 struct ieee802_11_elems *elems,
258 struct ieee80211_rx_status *rx_status)
259{
260 const u8 *oui;
261
262 WARN_ON(sdata->u.mesh.mesh_sp_id != IEEE80211_SYNC_METHOD_VENDOR);
263 msync_dbg(sdata, "called mesh_sync_vendor_rx_bcn_presp\n");
264 oui = mesh_get_vendor_oui(sdata);
265
266}
267
268static void mesh_sync_vendor_adjust_tbtt(struct ieee80211_sub_if_data *sdata)
269{
270 const u8 *oui;
271
272 WARN_ON(sdata->u.mesh.mesh_sp_id != IEEE80211_SYNC_METHOD_VENDOR);
273 msync_dbg(sdata, "called mesh_sync_vendor_adjust_tbtt\n");
274 oui = mesh_get_vendor_oui(sdata);
275
276}
277
278
279static struct sync_method sync_methods[] = {
280 {
281 .method = IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET,
282 .ops = {
283 .rx_bcn_presp = &mesh_sync_offset_rx_bcn_presp,
284 .adjust_tbtt = &mesh_sync_offset_adjust_tbtt,
285 }
286 },
287 {
288 .method = IEEE80211_SYNC_METHOD_VENDOR,
289 .ops = {
290 .rx_bcn_presp = &mesh_sync_vendor_rx_bcn_presp,
291 .adjust_tbtt = &mesh_sync_vendor_adjust_tbtt,
292 }
293 },
294};
295
296struct ieee80211_mesh_sync_ops *ieee80211_mesh_sync_ops_get(u8 method)
297{
298 struct ieee80211_mesh_sync_ops *ops = NULL;
299 u8 i;
300
301 for (i = 0 ; i < ARRAY_SIZE(sync_methods); ++i) {
302 if (sync_methods[i].method == method) {
303 ops = &sync_methods[i].ops;
304 break;
305 }
306 }
307 return ops;
308}
309