blob: 22a5f1e66996cc3bf6761c75a8f00e7e02d3d57d [file] [log] [blame]
Javier Cardonadbf498f2012-03-31 11:31:32 -07001/*
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
11#include "ieee80211_i.h"
12#include "mesh.h"
13#include "driver-ops.h"
14
15#ifdef CONFIG_MAC80211_VERBOSE_MESH_SYNC_DEBUG
16#define msync_dbg(fmt, args...) \
17 printk(KERN_DEBUG "Mesh sync (%s): " fmt "\n", sdata->name, ##args)
18#else
19#define msync_dbg(fmt, args...) do { (void)(0); } while (0)
20#endif
21
22/* This is not in the standard. It represents a tolerable tbtt drift below
23 * which we do no TSF adjustment.
24 */
Javier Cardonaa802a6e2012-04-12 14:32:22 -070025#define TOFFSET_MINIMUM_ADJUSTMENT 10
26
27/* This is not in the standard. It represents the maximum Toffset jump above
28 * which we'll invalidate the Toffset setpoint and choose a new setpoint. This
29 * could be, for instance, in case a neighbor is restarted and its TSF counter
30 * reset.
31 * */
32#define TOFFSET_MAXIMUM_ADJUSTMENT 30000 /* 30 ms */
Javier Cardonadbf498f2012-03-31 11:31:32 -070033
34struct sync_method {
35 u8 method;
36 struct ieee80211_mesh_sync_ops ops;
37};
38
39/**
40 * mesh_peer_tbtt_adjusting - check if an mp is currently adjusting its TBTT
41 *
42 * @ie: information elements of a management frame from the mesh peer
43 */
44static bool mesh_peer_tbtt_adjusting(struct ieee802_11_elems *ie)
45{
46 return (ie->mesh_config->meshconf_cap &
47 MESHCONF_CAPAB_TBTT_ADJUSTING) != 0;
48}
49
50void mesh_sync_adjust_tbtt(struct ieee80211_sub_if_data *sdata)
51{
52 struct ieee80211_local *local = sdata->local;
53 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
54 /* sdata->vif.bss_conf.beacon_int in 1024us units, 0.04% */
55 u64 beacon_int_fraction = sdata->vif.bss_conf.beacon_int * 1024 / 2500;
56 u64 tsf;
57 u64 tsfdelta;
58
59 spin_lock_bh(&ifmsh->sync_offset_lock);
60
61 if (ifmsh->sync_offset_clockdrift_max < beacon_int_fraction) {
62 msync_dbg("TBTT : max clockdrift=%lld; adjusting",
63 (long long) ifmsh->sync_offset_clockdrift_max);
64 tsfdelta = -ifmsh->sync_offset_clockdrift_max;
65 ifmsh->sync_offset_clockdrift_max = 0;
66 } else {
67 msync_dbg("TBTT : max clockdrift=%lld; adjusting by %llu",
68 (long long) ifmsh->sync_offset_clockdrift_max,
69 (unsigned long long) beacon_int_fraction);
70 tsfdelta = -beacon_int_fraction;
71 ifmsh->sync_offset_clockdrift_max -= beacon_int_fraction;
72 }
73
74 tsf = drv_get_tsf(local, sdata);
75 if (tsf != -1ULL)
76 drv_set_tsf(local, sdata, tsf + tsfdelta);
77 spin_unlock_bh(&ifmsh->sync_offset_lock);
78}
79
80static void mesh_sync_offset_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
81 u16 stype,
82 struct ieee80211_mgmt *mgmt,
83 struct ieee802_11_elems *elems,
84 struct ieee80211_rx_status *rx_status)
85{
86 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
87 struct ieee80211_local *local = sdata->local;
88 struct sta_info *sta;
89 u64 t_t, t_r;
90
91 WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
92
93 /* standard mentions only beacons */
94 if (stype != IEEE80211_STYPE_BEACON)
95 return;
96
97 /* The current tsf is a first approximation for the timestamp
98 * for the received beacon. Further down we try to get a
99 * better value from the rx_status->mactime field if
100 * available. Also we have to call drv_get_tsf() before
101 * entering the rcu-read section.*/
102 t_r = drv_get_tsf(local, sdata);
103
104 rcu_read_lock();
105 sta = sta_info_get(sdata, mgmt->sa);
106 if (!sta)
107 goto no_sync;
108
109 /* check offset sync conditions (13.13.2.2.1)
110 *
111 * TODO also sync to
112 * dot11MeshNbrOffsetMaxNeighbor non-peer non-MBSS neighbors
113 */
114
115 if (elems->mesh_config && mesh_peer_tbtt_adjusting(elems)) {
116 clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
117 msync_dbg("STA %pM : is adjusting TBTT", sta->sta.addr);
118 goto no_sync;
119 }
120
121 if (rx_status->flag & RX_FLAG_MACTIME_MPDU && rx_status->mactime) {
122 /*
123 * The mactime is defined as the time the first data symbol
124 * of the frame hits the PHY, and the timestamp of the beacon
125 * is defined as "the time that the data symbol containing the
126 * first bit of the timestamp is transmitted to the PHY plus
127 * the transmitting STA's delays through its local PHY from the
128 * MAC-PHY interface to its interface with the WM" (802.11
129 * 11.1.2)
130 *
131 * T_r, in 13.13.2.2.2, is just defined as "the frame reception
132 * time" but we unless we interpret that time to be the same
133 * time of the beacon timestamp, the offset calculation will be
134 * off. Below we adjust t_r to be "the time at which the first
135 * symbol of the timestamp element in the beacon is received".
136 * This correction depends on the rate.
137 *
138 * Based on similar code in ibss.c
139 */
140 int rate;
141
142 if (rx_status->flag & RX_FLAG_HT) {
143 /* TODO:
144 * In principle there could be HT-beacons (Dual Beacon
145 * HT Operation options), but for now ignore them and
146 * just use the primary (i.e. non-HT) beacons for
147 * synchronization.
148 * */
149 goto no_sync;
150 } else
151 rate = local->hw.wiphy->bands[rx_status->band]->
152 bitrates[rx_status->rate_idx].bitrate;
153
154 /* 24 bytes of header * 8 bits/byte *
155 * 10*(100 Kbps)/Mbps / rate (100 Kbps)*/
156 t_r = rx_status->mactime + (24 * 8 * 10 / rate);
157 }
158
159 /* Timing offset calculation (see 13.13.2.2.2) */
160 t_t = le64_to_cpu(mgmt->u.beacon.timestamp);
161 sta->t_offset = t_t - t_r;
162
163 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
164 s64 t_clockdrift = sta->t_offset_setpoint
165 - sta->t_offset;
Javier Cardonaa802a6e2012-04-12 14:32:22 -0700166 msync_dbg("STA %pM : sta->t_offset=%lld, sta->t_offset_setpoint=%lld, t_clockdrift=%lld",
Javier Cardonadbf498f2012-03-31 11:31:32 -0700167 sta->sta.addr,
168 (long long) sta->t_offset,
169 (long long)
170 sta->t_offset_setpoint,
171 (long long) t_clockdrift);
Javier Cardonaa802a6e2012-04-12 14:32:22 -0700172
173 if (t_clockdrift > TOFFSET_MAXIMUM_ADJUSTMENT ||
174 t_clockdrift < -TOFFSET_MAXIMUM_ADJUSTMENT) {
175 msync_dbg("STA %pM : t_clockdrift=%lld too large, setpoint reset",
176 sta->sta.addr,
177 (long long) t_clockdrift);
178 clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
179 goto no_sync;
180 }
181
Javier Cardonadbf498f2012-03-31 11:31:32 -0700182 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;
193 set_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
194 msync_dbg("STA %pM : offset was invalid, "
195 " sta->t_offset=%lld",
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 >
Javier Cardonaa802a6e2012-04-12 14:32:22 -0700217 TOFFSET_MINIMUM_ADJUSTMENT) {
Javier Cardonadbf498f2012-03-31 11:31:32 -0700218 /* Since ajusting the tsf here would
219 * require a possibly blocking call
220 * to the driver tsf setter, we punt
221 * the tsf adjustment to the mesh tasklet
222 */
223 msync_dbg("TBTT : kicking off TBTT "
224 "adjustment with "
225 "clockdrift_max=%lld",
226 ifmsh->sync_offset_clockdrift_max);
227 set_bit(MESH_WORK_DRIFT_ADJUST,
228 &ifmsh->wrkq_flags);
229 } else {
230 msync_dbg("TBTT : max clockdrift=%lld; "
231 "too small to adjust",
232 (long long)
233 ifmsh->sync_offset_clockdrift_max);
234 ifmsh->sync_offset_clockdrift_max = 0;
235 }
236 spin_unlock_bh(&ifmsh->sync_offset_lock);
237}
238
239static const u8 *mesh_get_vendor_oui(struct ieee80211_sub_if_data *sdata)
240{
241 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
242 u8 offset;
243
244 if (!ifmsh->ie || !ifmsh->ie_len)
245 return NULL;
246
247 offset = ieee80211_ie_split_vendor(ifmsh->ie,
248 ifmsh->ie_len, 0);
249
250 if (!offset)
251 return NULL;
252
253 return ifmsh->ie + offset + 2;
254}
255
256static void mesh_sync_vendor_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
257 u16 stype,
258 struct ieee80211_mgmt *mgmt,
259 struct ieee802_11_elems *elems,
260 struct ieee80211_rx_status *rx_status)
261{
262 const u8 *oui;
263
264 WARN_ON(sdata->u.mesh.mesh_sp_id != IEEE80211_SYNC_METHOD_VENDOR);
265 msync_dbg("called mesh_sync_vendor_rx_bcn_presp");
266 oui = mesh_get_vendor_oui(sdata);
267 /* here you would implement the vendor offset tracking for this oui */
268}
269
270static void mesh_sync_vendor_adjust_tbtt(struct ieee80211_sub_if_data *sdata)
271{
272 const u8 *oui;
273
274 WARN_ON(sdata->u.mesh.mesh_sp_id != IEEE80211_SYNC_METHOD_VENDOR);
275 msync_dbg("called mesh_sync_vendor_adjust_tbtt");
276 oui = mesh_get_vendor_oui(sdata);
277 /* here you would implement the vendor tsf adjustment for this oui */
278}
279
280/* global variable */
281static struct sync_method sync_methods[] = {
282 {
283 .method = IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET,
284 .ops = {
285 .rx_bcn_presp = &mesh_sync_offset_rx_bcn_presp,
286 .adjust_tbtt = &mesh_sync_offset_adjust_tbtt,
287 }
288 },
289 {
290 .method = IEEE80211_SYNC_METHOD_VENDOR,
291 .ops = {
292 .rx_bcn_presp = &mesh_sync_vendor_rx_bcn_presp,
293 .adjust_tbtt = &mesh_sync_vendor_adjust_tbtt,
294 }
295 },
296};
297
298struct ieee80211_mesh_sync_ops *ieee80211_mesh_sync_ops_get(u8 method)
299{
300 struct ieee80211_mesh_sync_ops *ops = NULL;
301 u8 i;
302
303 for (i = 0 ; i < ARRAY_SIZE(sync_methods); ++i) {
304 if (sync_methods[i].method == method) {
305 ops = &sync_methods[i].ops;
306 break;
307 }
308 }
309 return ops;
310}