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 800
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 IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING) != 0;
47}
48
49void mesh_sync_adjust_tsf(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, "TSF : 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, "TSF : 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 if (local->ops->offset_tsf) {
74 drv_offset_tsf(local, sdata, tsfdelta);
75 } else {
76 tsf = drv_get_tsf(local, sdata);
77 if (tsf != -1ULL)
78 drv_set_tsf(local, sdata, tsf + tsfdelta);
79 }
80}
81
82static void mesh_sync_offset_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
83 u16 stype,
84 struct ieee80211_mgmt *mgmt,
85 struct ieee802_11_elems *elems,
86 struct ieee80211_rx_status *rx_status)
87{
88 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
89 struct ieee80211_local *local = sdata->local;
90 struct sta_info *sta;
91 u64 t_t, t_r;
92
93 WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
94
95
96 if (stype != IEEE80211_STYPE_BEACON)
97 return;
98
99
100
101
102
103
104
105 if (ieee80211_have_rx_timestamp(rx_status))
106 t_r = ieee80211_calculate_rx_timestamp(local, rx_status,
107 24 + 12 +
108 elems->total_len +
109 FCS_LEN,
110 24);
111 else
112 t_r = drv_get_tsf(local, sdata);
113
114 rcu_read_lock();
115 sta = sta_info_get(sdata, mgmt->sa);
116 if (!sta)
117 goto no_sync;
118
119
120
121
122
123
124
125 if (elems->mesh_config && mesh_peer_tbtt_adjusting(elems)) {
126 msync_dbg(sdata, "STA %pM : is adjusting TBTT\n",
127 sta->sta.addr);
128 goto no_sync;
129 }
130
131
132 t_t = le64_to_cpu(mgmt->u.beacon.timestamp);
133 sta->mesh->t_offset = t_t - t_r;
134
135 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
136 s64 t_clockdrift = sta->mesh->t_offset_setpoint - sta->mesh->t_offset;
137 msync_dbg(sdata,
138 "STA %pM : t_offset=%lld, t_offset_setpoint=%lld, t_clockdrift=%lld\n",
139 sta->sta.addr, (long long) sta->mesh->t_offset,
140 (long long) sta->mesh->t_offset_setpoint,
141 (long long) t_clockdrift);
142
143 if (t_clockdrift > TOFFSET_MAXIMUM_ADJUSTMENT ||
144 t_clockdrift < -TOFFSET_MAXIMUM_ADJUSTMENT) {
145 msync_dbg(sdata,
146 "STA %pM : t_clockdrift=%lld too large, setpoint reset\n",
147 sta->sta.addr,
148 (long long) t_clockdrift);
149 clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
150 goto no_sync;
151 }
152
153 spin_lock_bh(&ifmsh->sync_offset_lock);
154 if (t_clockdrift > ifmsh->sync_offset_clockdrift_max)
155 ifmsh->sync_offset_clockdrift_max = t_clockdrift;
156 spin_unlock_bh(&ifmsh->sync_offset_lock);
157 } else {
158 sta->mesh->t_offset_setpoint = sta->mesh->t_offset - TOFFSET_SET_MARGIN;
159 set_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
160 msync_dbg(sdata,
161 "STA %pM : offset was invalid, t_offset=%lld\n",
162 sta->sta.addr,
163 (long long) sta->mesh->t_offset);
164 }
165
166no_sync:
167 rcu_read_unlock();
168}
169
170static void mesh_sync_offset_adjust_tsf(struct ieee80211_sub_if_data *sdata,
171 struct beacon_data *beacon)
172{
173 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
174
175 WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
176 WARN_ON(!rcu_read_lock_held());
177
178 spin_lock_bh(&ifmsh->sync_offset_lock);
179
180 if (ifmsh->sync_offset_clockdrift_max > TOFFSET_MINIMUM_ADJUSTMENT) {
181
182
183
184
185
186 msync_dbg(sdata,
187 "TSF : kicking off TSF adjustment with clockdrift_max=%lld\n",
188 ifmsh->sync_offset_clockdrift_max);
189 set_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags);
190 } else {
191 msync_dbg(sdata,
192 "TSF : max clockdrift=%lld; too small to adjust\n",
193 (long long)ifmsh->sync_offset_clockdrift_max);
194 ifmsh->sync_offset_clockdrift_max = 0;
195 }
196 spin_unlock_bh(&ifmsh->sync_offset_lock);
197}
198
199static const struct sync_method sync_methods[] = {
200 {
201 .method = IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET,
202 .ops = {
203 .rx_bcn_presp = &mesh_sync_offset_rx_bcn_presp,
204 .adjust_tsf = &mesh_sync_offset_adjust_tsf,
205 }
206 },
207};
208
209const struct ieee80211_mesh_sync_ops *ieee80211_mesh_sync_ops_get(u8 method)
210{
211 int i;
212
213 for (i = 0 ; i < ARRAY_SIZE(sync_methods); ++i) {
214 if (sync_methods[i].method == method)
215 return &sync_methods[i].ops;
216 }
217 return NULL;
218}
219