xref: /freebsd/sys/net80211/ieee80211_tdma.c (revision 5463c4a4)
1 /*-
2  * Copyright (c) 2007-2009 Sam Leffler, Errno Consulting
3  * Copyright (c) 2007-2009 Intel Corporation
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #ifdef __FreeBSD__
29 __FBSDID("$FreeBSD$");
30 #endif
31 
32 /*
33  * IEEE 802.11 TDMA mode support.
34  */
35 #include "opt_inet.h"
36 #include "opt_tdma.h"
37 #include "opt_wlan.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/mbuf.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/endian.h>
48 #include <sys/errno.h>
49 #include <sys/proc.h>
50 #include <sys/sysctl.h>
51 
52 #include <net/if.h>
53 #include <net/if_media.h>
54 #include <net/if_llc.h>
55 #include <net/ethernet.h>
56 
57 #include <net/bpf.h>
58 
59 #include <net80211/ieee80211_var.h>
60 #include <net80211/ieee80211_tdma.h>
61 #include <net80211/ieee80211_input.h>
62 
63 #ifndef TDMA_SLOTLEN_DEFAULT
64 #define	TDMA_SLOTLEN_DEFAULT	10*1000		/* 10ms */
65 #endif
66 #ifndef TDMA_SLOTCNT_DEFAULT
67 #define	TDMA_SLOTCNT_DEFAULT	2		/* 2x (pt-to-pt) */
68 #endif
69 #ifndef TDMA_BINTVAL_DEFAULT
70 #define	TDMA_BINTVAL_DEFAULT	5		/* 5x ~= 100TU beacon intvl */
71 #endif
72 #ifndef TDMA_TXRATE_11B_DEFAULT
73 #define	TDMA_TXRATE_11B_DEFAULT	2*11
74 #endif
75 #ifndef TDMA_TXRATE_11G_DEFAULT
76 #define	TDMA_TXRATE_11G_DEFAULT	2*24
77 #endif
78 #ifndef TDMA_TXRATE_11A_DEFAULT
79 #define	TDMA_TXRATE_11A_DEFAULT	2*24
80 #endif
81 #ifndef TDMA_TXRATE_TURBO_DEFAULT
82 #define	TDMA_TXRATE_TURBO_DEFAULT	2*24
83 #endif
84 #ifndef TDMA_TXRATE_HALF_DEFAULT
85 #define	TDMA_TXRATE_HALF_DEFAULT	2*12
86 #endif
87 #ifndef TDMA_TXRATE_QUARTER_DEFAULT
88 #define	TDMA_TXRATE_QUARTER_DEFAULT	2*6
89 #endif
90 #ifndef TDMA_TXRATE_11NA_DEFAULT
91 #define	TDMA_TXRATE_11NA_DEFAULT	(4 | IEEE80211_RATE_MCS)
92 #endif
93 #ifndef TDMA_TXRATE_11NG_DEFAULT
94 #define	TDMA_TXRATE_11NG_DEFAULT	(4 | IEEE80211_RATE_MCS)
95 #endif
96 
97 #define	TDMA_VERSION_VALID(_version) \
98 	(TDMA_VERSION_V2 <= (_version) && (_version) <= TDMA_VERSION)
99 #define	TDMA_SLOTCNT_VALID(_slotcnt) \
100 	(2 <= (_slotcnt) && (_slotcnt) <= TDMA_MAXSLOTS)
101 /* XXX magic constants */
102 #define	TDMA_SLOTLEN_VALID(_slotlen) \
103 	(2*100 <= (_slotlen) && (unsigned)(_slotlen) <= 0xfffff)
104 /* XXX probably should set a max */
105 #define	TDMA_BINTVAL_VALID(_bintval)	(1 <= (_bintval))
106 
107 static void tdma_vdetach(struct ieee80211vap *vap);
108 static int tdma_newstate(struct ieee80211vap *, enum ieee80211_state, int);
109 static void tdma_beacon_miss(struct ieee80211vap *vap);
110 static void tdma_recv_mgmt(struct ieee80211_node *, struct mbuf *,
111 	int subtype, int rssi, int nf);
112 static int tdma_update(struct ieee80211vap *vap,
113 	const struct ieee80211_tdma_param *tdma, struct ieee80211_node *ni,
114 	int pickslot);
115 static int tdma_process_params(struct ieee80211_node *ni,
116 	const u_int8_t *ie, int rssi, int nf, const struct ieee80211_frame *wh);
117 
118 static void
119 settxparms(struct ieee80211vap *vap, enum ieee80211_phymode mode, int rate)
120 {
121 	vap->iv_txparms[mode].ucastrate = rate;
122 	vap->iv_txparms[mode].mcastrate = rate;
123 }
124 
125 static void
126 setackpolicy(struct ieee80211com *ic, int noack)
127 {
128 	struct ieee80211_wme_state *wme = &ic->ic_wme;
129 	int ac;
130 
131 	for (ac = 0; ac < WME_NUM_AC; ac++) {
132 		wme->wme_chanParams.cap_wmeParams[ac].wmep_noackPolicy = noack;
133 		wme->wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy = noack;
134 	}
135 }
136 
137 void
138 ieee80211_tdma_vattach(struct ieee80211vap *vap)
139 {
140 	struct ieee80211_tdma_state *ts;
141 
142 	KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
143 	     ("not a tdma vap, caps 0x%x", vap->iv_caps));
144 
145 	ts = (struct ieee80211_tdma_state *) malloc(
146 	     sizeof(struct ieee80211_tdma_state), M_80211_VAP, M_NOWAIT | M_ZERO);
147 	if (ts == NULL) {
148 		printf("%s: cannot allocate TDMA state block\n", __func__);
149 		/* NB: fall back to adhdemo mode */
150 		vap->iv_caps &= ~IEEE80211_C_TDMA;
151 		return;
152 	}
153 	/* NB: default configuration is passive so no beacons */
154 	ts->tdma_version = TDMA_VERSION;
155 	ts->tdma_slotlen = TDMA_SLOTLEN_DEFAULT;
156 	ts->tdma_slotcnt = TDMA_SLOTCNT_DEFAULT;
157 	ts->tdma_bintval = TDMA_BINTVAL_DEFAULT;
158 	ts->tdma_slot = 1;			/* passive operation */
159 
160 	/* setup default fixed rates */
161 	settxparms(vap, IEEE80211_MODE_11A, TDMA_TXRATE_11A_DEFAULT);
162 	settxparms(vap, IEEE80211_MODE_11B, TDMA_TXRATE_11B_DEFAULT);
163 	settxparms(vap, IEEE80211_MODE_11G, TDMA_TXRATE_11G_DEFAULT);
164 	settxparms(vap, IEEE80211_MODE_TURBO_A, TDMA_TXRATE_TURBO_DEFAULT);
165 	settxparms(vap, IEEE80211_MODE_TURBO_G, TDMA_TXRATE_TURBO_DEFAULT);
166 	settxparms(vap, IEEE80211_MODE_STURBO_A, TDMA_TXRATE_TURBO_DEFAULT);
167 	settxparms(vap, IEEE80211_MODE_11NA, TDMA_TXRATE_11NA_DEFAULT);
168 	settxparms(vap, IEEE80211_MODE_11NG, TDMA_TXRATE_11NG_DEFAULT);
169 	settxparms(vap, IEEE80211_MODE_HALF, TDMA_TXRATE_HALF_DEFAULT);
170 	settxparms(vap, IEEE80211_MODE_QUARTER, TDMA_TXRATE_QUARTER_DEFAULT);
171 
172 	setackpolicy(vap->iv_ic, 1);	/* disable ACK's */
173 
174 	ts->tdma_opdetach = vap->iv_opdetach;
175 	vap->iv_opdetach = tdma_vdetach;
176 	ts->tdma_newstate = vap->iv_newstate;
177 	vap->iv_newstate = tdma_newstate;
178 	vap->iv_bmiss = tdma_beacon_miss;
179 	ts->tdma_recv_mgmt = vap->iv_recv_mgmt;
180 	vap->iv_recv_mgmt = tdma_recv_mgmt;
181 
182 	vap->iv_tdma = ts;
183 }
184 
185 static void
186 tdma_vdetach(struct ieee80211vap *vap)
187 {
188 	struct ieee80211_tdma_state *ts = vap->iv_tdma;
189 
190 	ts->tdma_opdetach(vap);
191 	free(vap->iv_tdma, M_80211_VAP);
192 
193 	setackpolicy(vap->iv_ic, 0);	/* enable ACK's */
194 }
195 
196 static void
197 sta_leave(void *arg, struct ieee80211_node *ni)
198 {
199 	struct ieee80211vap *vap = arg;
200 
201 	if (ni->ni_vap == vap && ni != vap->iv_bss)
202 		ieee80211_node_leave(ni);
203 }
204 
205 /*
206  * TDMA state machine handler.
207  */
208 static int
209 tdma_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
210 {
211 	struct ieee80211_tdma_state *ts = vap->iv_tdma;
212 	struct ieee80211com *ic = vap->iv_ic;
213 	enum ieee80211_state ostate;
214 	int status;
215 
216 	IEEE80211_LOCK_ASSERT(ic);
217 
218 	ostate = vap->iv_state;
219 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
220 	    __func__, ieee80211_state_name[ostate],
221 	    ieee80211_state_name[nstate], arg);
222 
223 	if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
224 		callout_stop(&vap->iv_swbmiss);
225 	if (nstate == IEEE80211_S_SCAN &&
226 	    (ostate == IEEE80211_S_INIT || ostate == IEEE80211_S_RUN) &&
227 	    ts->tdma_slot != 0) {
228 		/*
229 		 * Override adhoc behaviour when operating as a slave;
230 		 * we need to scan even if the channel is locked.
231 		 */
232 		vap->iv_state = nstate;			/* state transition */
233 		ieee80211_cancel_scan(vap);		/* background scan */
234 		if (ostate == IEEE80211_S_RUN) {
235 			/* purge station table; entries are stale */
236 			ieee80211_iterate_nodes(&ic->ic_sta, sta_leave, vap);
237 		}
238 		if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
239 			ieee80211_check_scan(vap,
240 			    vap->iv_scanreq_flags,
241 			    vap->iv_scanreq_duration,
242 			    vap->iv_scanreq_mindwell,
243 			    vap->iv_scanreq_maxdwell,
244 			    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
245 			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
246 		} else
247 			ieee80211_check_scan_current(vap);
248 		status = 0;
249 	} else {
250 		status = ts->tdma_newstate(vap, nstate, arg);
251 	}
252 	if (status == 0 &&
253 	    nstate == IEEE80211_S_RUN && ostate != IEEE80211_S_RUN &&
254 	    (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) &&
255 	    ts->tdma_slot != 0 &&
256 	    vap->iv_des_chan == IEEE80211_CHAN_ANYC) {
257 		/*
258 		 * Start s/w beacon miss timer for slave devices w/o
259 		 * hardware support.  Note we do this only if we're
260 		 * not locked to a channel (i.e. roam to follow the
261 		 * master). The 2x is a fudge for our doing this in
262 		 * software.
263 		 */
264 		vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
265 		    2 * vap->iv_bmissthreshold * ts->tdma_bintval *
266 		    ((ts->tdma_slotcnt * ts->tdma_slotlen) / 1024));
267 		vap->iv_swbmiss_count = 0;
268 		callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
269 			ieee80211_swbmiss, vap);
270 	}
271 	return status;
272 }
273 
274 static void
275 tdma_beacon_miss(struct ieee80211vap *vap)
276 {
277 	struct ieee80211_tdma_state *ts = vap->iv_tdma;
278 
279 	KASSERT((vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
280 	KASSERT(vap->iv_state == IEEE80211_S_RUN,
281 	    ("wrong state %d", vap->iv_state));
282 
283 	IEEE80211_DPRINTF(vap,
284 		IEEE80211_MSG_STATE | IEEE80211_MSG_TDMA | IEEE80211_MSG_DEBUG,
285 		"beacon miss, mode %u state %s\n",
286 		vap->iv_opmode, ieee80211_state_name[vap->iv_state]);
287 
288 	if (ts->tdma_peer != NULL) {	/* XXX? can this be null? */
289 		ieee80211_notify_node_leave(vap->iv_bss);
290 		ts->tdma_peer = NULL;
291 		/*
292 		 * Treat beacon miss like an associate failure wrt the
293 		 * scan policy; this forces the entry in the scan cache
294 		 * to be ignored after several tries.
295 		 */
296 		ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr,
297 		    IEEE80211_STATUS_TIMEOUT);
298 	}
299 #if 0
300 	ts->tdma_inuse = 0;		/* clear slot usage */
301 #endif
302 	ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
303 }
304 
305 static void
306 tdma_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
307 	int subtype, int rssi, int nf)
308 {
309 	struct ieee80211com *ic = ni->ni_ic;
310 	struct ieee80211vap *vap = ni->ni_vap;
311 	struct ieee80211_tdma_state *ts = vap->iv_tdma;
312 
313 	if (subtype == IEEE80211_FC0_SUBTYPE_BEACON &&
314 	    (ic->ic_flags & IEEE80211_F_SCAN) == 0) {
315 		struct ieee80211_frame *wh = mtod(m0, struct ieee80211_frame *);
316 		struct ieee80211_scanparams scan;
317 
318 		if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
319 			return;
320 		if (scan.tdma == NULL) {
321 			/*
322 			 * TDMA stations must beacon a TDMA ie; ignore
323 			 * any other station.
324 			 * XXX detect overlapping bss and change channel
325 			 */
326 			IEEE80211_DISCARD(vap,
327 			    IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
328 			    wh, ieee80211_mgt_subtype_name[subtype >>
329 				IEEE80211_FC0_SUBTYPE_SHIFT],
330 			    "%s", "no TDMA ie");
331 			vap->iv_stats.is_rx_mgtdiscard++;
332 			return;
333 		}
334 		if (ni == vap->iv_bss &&
335 		    !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
336 			/*
337 			 * Fake up a node for this newly
338 			 * discovered member of the IBSS.
339 			 */
340 			ni = ieee80211_add_neighbor(vap, wh, &scan);
341 			if (ni == NULL) {
342 				/* NB: stat kept for alloc failure */
343 				return;
344 			}
345 		}
346 		/*
347 		 * Check for state updates.
348 		 */
349 		if (IEEE80211_ADDR_EQ(wh->i_addr3, ni->ni_bssid)) {
350 			/*
351 			 * Count frame now that we know it's to be processed.
352 			 */
353 			vap->iv_stats.is_rx_beacon++;
354 			IEEE80211_NODE_STAT(ni, rx_beacons);
355 			/*
356 			 * Record tsf of last beacon.  NB: this must be
357 			 * done before calling tdma_process_params
358 			 * as deeper routines reference it.
359 			 */
360 			memcpy(&ni->ni_tstamp.data, scan.tstamp,
361 				sizeof(ni->ni_tstamp.data));
362 			/*
363 			 * Count beacon frame for s/w bmiss handling.
364 			 */
365 			vap->iv_swbmiss_count++;
366 			/*
367 			 * Process tdma ie.  The contents are used to sync
368 			 * the slot timing, reconfigure the bss, etc.
369 			 */
370 			(void) tdma_process_params(ni, scan.tdma, rssi, nf, wh);
371 			return;
372 		}
373 		/*
374 		 * NB: defer remaining work to the adhoc code; this causes
375 		 *     2x parsing of the frame but should happen infrequently
376 		 */
377 	}
378 	ts->tdma_recv_mgmt(ni, m0, subtype, rssi, nf);
379 }
380 
381 /*
382  * Update TDMA state on receipt of a beacon frame with
383  * a TDMA information element.  The sender's identity
384  * is provided so we can track who our peer is.  If pickslot
385  * is non-zero we scan the slot allocation state in the ie
386  * to locate a free slot for our use.
387  */
388 static int
389 tdma_update(struct ieee80211vap *vap, const struct ieee80211_tdma_param *tdma,
390 	struct ieee80211_node *ni, int pickslot)
391 {
392 	struct ieee80211_tdma_state *ts = vap->iv_tdma;
393 	int slot, slotlen, update;
394 
395 	KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
396 	     ("not a tdma vap, caps 0x%x", vap->iv_caps));
397 
398 	update = 0;
399 	if (tdma->tdma_slotcnt != ts->tdma_slotcnt) {
400 		if (!TDMA_SLOTCNT_VALID(tdma->tdma_slotcnt)) {
401 			if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
402 				printf("%s: bad slot cnt %u\n",
403 				    __func__, tdma->tdma_slotcnt);
404 			return 0;
405 		}
406 		update |= TDMA_UPDATE_SLOTCNT;
407  	}
408 	slotlen = le16toh(tdma->tdma_slotlen) * 100;
409 	if (slotlen != ts->tdma_slotlen) {
410 		if (!TDMA_SLOTLEN_VALID(slotlen)) {
411 			if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
412 				printf("%s: bad slot len %u\n",
413 				    __func__, slotlen);
414 			return 0;
415 		}
416 		update |= TDMA_UPDATE_SLOTLEN;
417  	}
418 	if (tdma->tdma_bintval != ts->tdma_bintval) {
419 		if (!TDMA_BINTVAL_VALID(tdma->tdma_bintval)) {
420 			if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
421 				printf("%s: bad beacon interval %u\n",
422 				    __func__, tdma->tdma_bintval);
423 			return 0;
424 		}
425 		update |= TDMA_UPDATE_BINTVAL;
426  	}
427 	slot = ts->tdma_slot;
428 	if (pickslot) {
429 		/*
430 		 * Pick unoccupied slot.  Note we never choose slot 0.
431 		 */
432 		for (slot = tdma->tdma_slotcnt-1; slot > 0; slot--)
433 			if (isclr(tdma->tdma_inuse, slot))
434 				break;
435 		if (slot <= 0) {
436 			printf("%s: no free slot, slotcnt %u inuse: 0x%x\n",
437 				__func__, tdma->tdma_slotcnt,
438 				tdma->tdma_inuse[0]);
439 			/* XXX need to do something better */
440 			return 0;
441 		}
442 		if (slot != ts->tdma_slot)
443 			update |= TDMA_UPDATE_SLOT;
444 	}
445 	if (ni != ts->tdma_peer) {
446 		/* update everything */
447 		update = TDMA_UPDATE_SLOT
448 		       | TDMA_UPDATE_SLOTCNT
449 		       | TDMA_UPDATE_SLOTLEN
450 		       | TDMA_UPDATE_BINTVAL;
451 	}
452 
453 	if (update) {
454 		/*
455 		 * New/changed parameters; update runtime state.
456 		 */
457 		/* XXX overwrites user parameters */
458 		if (update & TDMA_UPDATE_SLOTCNT)
459 			ts->tdma_slotcnt = tdma->tdma_slotcnt;
460 		if (update & TDMA_UPDATE_SLOTLEN)
461 			ts->tdma_slotlen = slotlen;
462 		if (update & TDMA_UPDATE_SLOT)
463 			ts->tdma_slot = slot;
464 		if (update & TDMA_UPDATE_BINTVAL)
465 			ts->tdma_bintval = tdma->tdma_bintval;
466 		/* mark beacon to be updated before next xmit */
467 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_TDMA);
468 
469 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_TDMA,
470 		    "%s: slot %u slotcnt %u slotlen %u us bintval %u\n",
471 		    __func__, ts->tdma_slot, ts->tdma_slotcnt,
472 		    ts->tdma_slotlen, ts->tdma_bintval);
473 	}
474 	/*
475 	 * Notify driver.  Note we can be called before
476 	 * entering RUN state if we scanned and are
477 	 * joining an existing bss.  In that case do not
478 	 * call the driver because not all necessary state
479 	 * has been setup.  The next beacon will dtrt.
480 	 */
481 	if (vap->iv_state == IEEE80211_S_RUN)
482 		vap->iv_ic->ic_tdma_update(ni, tdma, update);
483 	/*
484 	 * Dispatch join event on first beacon from new master.
485 	 */
486 	if (ts->tdma_peer != ni) {
487 		if (ts->tdma_peer != NULL)
488 			ieee80211_notify_node_leave(vap->iv_bss);
489 		ieee80211_notify_node_join(ni, 1);
490 		/* NB: no reference, we just use the address */
491 		ts->tdma_peer = ni;
492 	}
493 	return 1;
494 }
495 
496 /*
497  * Process received TDMA parameters.
498  */
499 static int
500 tdma_process_params(struct ieee80211_node *ni, const u_int8_t *ie,
501 	int rssi, int nf, const struct ieee80211_frame *wh)
502 {
503 	struct ieee80211vap *vap = ni->ni_vap;
504 	struct ieee80211_tdma_state *ts = vap->iv_tdma;
505 	const struct ieee80211_tdma_param *tdma =
506 		(const struct ieee80211_tdma_param *) ie;
507 	u_int len = ie[1];
508 
509 	KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
510 	     ("not a tdma vap, caps 0x%x", vap->iv_caps));
511 
512 	if (len < sizeof(*tdma) - 2) {
513 		IEEE80211_DISCARD_IE(vap,
514 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
515 		    wh, "tdma", "too short, len %u", len);
516 		return IEEE80211_REASON_IE_INVALID;
517 	}
518 	if (tdma->tdma_version != ts->tdma_version) {
519 		IEEE80211_DISCARD_IE(vap,
520 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
521 		    wh, "tdma", "bad version %u (ours %u)",
522 		    tdma->tdma_version, ts->tdma_version);
523 		return IEEE80211_REASON_IE_INVALID;
524 	}
525  	/*
526 	 * NB: ideally we'd check against tdma_slotcnt, but that
527 	 * would require extra effort so do this easy check that
528 	 * covers the work below; more stringent checks are done
529 	 * before we make more extensive use of the ie contents.
530 	 */
531 	if (tdma->tdma_slot >= TDMA_MAXSLOTS) {
532 		IEEE80211_DISCARD_IE(vap,
533 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
534 		    wh, "tdma", "invalid slot %u", tdma->tdma_slot);
535 		return IEEE80211_REASON_IE_INVALID;
536 	}
537 	/*
538 	 * Can reach here while scanning, update
539 	 * operational state only in RUN state.
540 	 */
541 	if (vap->iv_state == IEEE80211_S_RUN) {
542 		if (tdma->tdma_slot != ts->tdma_slot &&
543 		    isclr(ts->tdma_inuse, tdma->tdma_slot)) {
544 			IEEE80211_NOTE(vap, IEEE80211_MSG_TDMA, ni,
545 			    "discovered in slot %u", tdma->tdma_slot);
546 			setbit(ts->tdma_inuse, tdma->tdma_slot);
547 			/* XXX dispatch event only when operating as master */
548 			if (ts->tdma_slot == 0)
549 				ieee80211_notify_node_join(ni, 1);
550 		}
551 		setbit(ts->tdma_active, tdma->tdma_slot);
552 		if (tdma->tdma_slot == ts->tdma_slot-1) {
553 			/*
554 			 * Slave tsf synchronization to station
555 			 * just before us in the schedule. The driver
556 			 * is responsible for copying the timestamp
557 			 * of the received beacon into our beacon
558 			 * frame so the sender can calculate round
559 			 * trip time.  We cannot do that here because
560 			 * we don't know how to update our beacon frame.
561 			 */
562 			(void) tdma_update(vap, tdma, ni, 0);
563 			/* XXX reschedule swbmiss timer on parameter change */
564 		} else if (tdma->tdma_slot == ts->tdma_slot+1) {
565 			uint64_t tstamp;
566 #if 0
567 			uint32_t rstamp = (uint32_t) le64toh(rs->tsf);
568 			int32_t rtt;
569 #endif
570 			/*
571 			 * Use returned timstamp to calculate the
572 			 * roundtrip time.
573 			 */
574 			memcpy(&tstamp, tdma->tdma_tstamp, 8);
575 #if 0
576 			/* XXX use only 15 bits of rstamp */
577 			rtt = rstamp - (le64toh(tstamp) & 0x7fff);
578 			if (rtt < 0)
579 				rtt += 0x7fff;
580 			/* XXX hack to quiet normal use */
581 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_DOT1X,
582 			    "tdma rtt %5u [rstamp %5u tstamp %llu]\n",
583 			    rtt, rstamp,
584 			    (unsigned long long) le64toh(tstamp));
585 #endif
586 		} else if (tdma->tdma_slot == ts->tdma_slot &&
587 		    le64toh(ni->ni_tstamp.tsf) > vap->iv_bss->ni_tstamp.tsf) {
588 			/*
589 			 * Station using the same slot as us and has
590 			 * been around longer than us; we must move.
591 			 * Note this can happen if stations do not
592 			 * see each other while scanning.
593 			 */
594 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_TDMA,
595 			    "slot %u collision rxtsf %llu tsf %llu\n",
596 			    tdma->tdma_slot,
597 			    (unsigned long long) le64toh(ni->ni_tstamp.tsf),
598 			    vap->iv_bss->ni_tstamp.tsf);
599 			setbit(ts->tdma_inuse, tdma->tdma_slot);
600 
601 			(void) tdma_update(vap, tdma, ni, 1);
602 		}
603 	}
604 	return 0;
605 }
606 
607 int
608 ieee80211_tdma_getslot(struct ieee80211vap *vap)
609 {
610 	struct ieee80211_tdma_state *ts = vap->iv_tdma;
611 
612 	KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
613 	     ("not a tdma vap, caps 0x%x", vap->iv_caps));
614 	return ts->tdma_slot;
615 }
616 
617 /*
618  * Parse a TDMA ie on station join and use it to setup node state.
619  */
620 void
621 ieee80211_parse_tdma(struct ieee80211_node *ni, const uint8_t *ie)
622 {
623 	struct ieee80211vap *vap = ni->ni_vap;
624 
625 	if (vap->iv_caps & IEEE80211_C_TDMA) {
626 		const struct ieee80211_tdma_param *tdma =
627 		    (const struct ieee80211_tdma_param *)ie;
628 		struct ieee80211_tdma_state *ts = vap->iv_tdma;
629 		/*
630 		 * Adopt TDMA configuration when joining an
631 		 * existing network.
632 		 */
633 		setbit(ts->tdma_inuse, tdma->tdma_slot);
634 		(void) tdma_update(vap, tdma, ni, 1);
635 		/*
636 		 * Propagate capabilities based on the local
637 		 * configuration and the remote station's advertised
638 		 * capabilities. In particular this permits us to
639 		 * enable use of QoS to disable ACK's.
640 		 */
641 		if ((vap->iv_flags & IEEE80211_F_WME) &&
642 		    ni->ni_ies.wme_ie != NULL)
643 			ni->ni_flags |= IEEE80211_NODE_QOS;
644 	}
645 }
646 
647 #define	TDMA_OUI_BYTES		0x00, 0x03, 0x7f
648 /*
649  * Add a TDMA parameters element to a frame.
650  */
651 uint8_t *
652 ieee80211_add_tdma(uint8_t *frm, struct ieee80211vap *vap)
653 {
654 #define	ADDSHORT(frm, v) do {			\
655 	frm[0] = (v) & 0xff;			\
656 	frm[1] = (v) >> 8;			\
657 	frm += 2;				\
658 } while (0)
659 	static const struct ieee80211_tdma_param param = {
660 		.tdma_id	= IEEE80211_ELEMID_VENDOR,
661 		.tdma_len	= sizeof(struct ieee80211_tdma_param) - 2,
662 		.tdma_oui	= { TDMA_OUI_BYTES },
663 		.tdma_type	= TDMA_OUI_TYPE,
664 		.tdma_subtype	= TDMA_SUBTYPE_PARAM,
665 		.tdma_version	= TDMA_VERSION,
666 	};
667 	const struct ieee80211_tdma_state *ts = vap->iv_tdma;
668 	uint16_t slotlen;
669 
670 	KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
671 	     ("not a tdma vap, caps 0x%x", vap->iv_caps));
672 
673 	memcpy(frm, &param, sizeof(param));
674 	frm += __offsetof(struct ieee80211_tdma_param, tdma_slot);
675 	*frm++ = ts->tdma_slot;
676 	*frm++ = ts->tdma_slotcnt;
677 	/* NB: convert units to fit in 16-bits */
678 	slotlen = ts->tdma_slotlen / 100;	/* 100us units */
679 	ADDSHORT(frm, slotlen);
680 	*frm++ = ts->tdma_bintval;
681 	*frm++ = ts->tdma_inuse[0];
682 	frm += 10;				/* pad+timestamp */
683 	return frm;
684 #undef ADDSHORT
685 }
686 #undef TDMA_OUI_BYTES
687 
688 /*
689  * Update TDMA state at TBTT.
690  */
691 void
692 ieee80211_tdma_update_beacon(struct ieee80211vap *vap,
693 	struct ieee80211_beacon_offsets *bo)
694 {
695 	struct ieee80211_tdma_state *ts = vap->iv_tdma;
696 
697 	KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
698 	     ("not a tdma vap, caps 0x%x", vap->iv_caps));
699 
700 	if (isset(bo->bo_flags,  IEEE80211_BEACON_TDMA)) {
701 		(void) ieee80211_add_tdma(bo->bo_tdma, vap);
702 		clrbit(bo->bo_flags, IEEE80211_BEACON_TDMA);
703 	}
704 	if (ts->tdma_slot != 0)		/* only on master */
705 		return;
706 	if (ts->tdma_count <= 0) {
707 		/*
708 		 * Time to update the mask of active/inuse stations.
709 		 * We track stations that we've received a beacon
710 		 * frame from and update this mask periodically.
711 		 * This allows us to miss a few beacons before marking
712 		 * a slot free for re-use.
713 		 */
714 		ts->tdma_inuse[0] = ts->tdma_active[0];
715 		ts->tdma_active[0] = 0x01;
716 		/* update next time 'round */
717 		/* XXX use notify framework */
718 		setbit(bo->bo_flags, IEEE80211_BEACON_TDMA);
719 		/* NB: use s/w beacon miss threshold; may be too high */
720 		ts->tdma_count = vap->iv_bmissthreshold-1;
721 	} else
722 		ts->tdma_count--;
723 }
724 
725 static int
726 tdma_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
727 {
728 	struct ieee80211_tdma_state *ts = vap->iv_tdma;
729 
730 	if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
731 		return EOPNOTSUPP;
732 
733 	switch (ireq->i_type) {
734 	case IEEE80211_IOC_TDMA_SLOT:
735 		ireq->i_val = ts->tdma_slot;
736 		break;
737 	case IEEE80211_IOC_TDMA_SLOTCNT:
738 		ireq->i_val = ts->tdma_slotcnt;
739 		break;
740 	case IEEE80211_IOC_TDMA_SLOTLEN:
741 		ireq->i_val = ts->tdma_slotlen;
742 		break;
743 	case IEEE80211_IOC_TDMA_BINTERVAL:
744 		ireq->i_val = ts->tdma_bintval;
745 		break;
746 	default:
747 		return ENOSYS;
748 	}
749 	return 0;
750 }
751 IEEE80211_IOCTL_GET(tdma, tdma_ioctl_get80211);
752 
753 static int
754 tdma_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
755 {
756 	struct ieee80211_tdma_state *ts = vap->iv_tdma;
757 
758 	if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
759 		return EOPNOTSUPP;
760 
761 	switch (ireq->i_type) {
762 	case IEEE80211_IOC_TDMA_SLOT:
763 		if (!(0 <= ireq->i_val && ireq->i_val <= ts->tdma_slotcnt))
764 			return EINVAL;
765 		if (ireq->i_val != ts->tdma_slot) {
766 			ts->tdma_slot = ireq->i_val;
767 			return ERESTART;
768 		}
769 		break;
770 	case IEEE80211_IOC_TDMA_SLOTCNT:
771 		if (!TDMA_SLOTCNT_VALID(ireq->i_val))
772 			return EINVAL;
773 		if (ireq->i_val != ts->tdma_slotcnt) {
774 			ts->tdma_slotcnt = ireq->i_val;
775 			return ERESTART;
776 		}
777 		break;
778 	case IEEE80211_IOC_TDMA_SLOTLEN:
779 		/*
780 		 * XXX
781 		 * 150 insures at least 1/8 TU
782 		 * 0xfffff is the max duration for bursting
783 		 * (implict by way of 16-bit data type for i_val)
784 		 */
785 		if (!TDMA_SLOTLEN_VALID(ireq->i_val))
786 			return EINVAL;
787 		if (ireq->i_val != ts->tdma_slotlen) {
788 			ts->tdma_slotlen = ireq->i_val;
789 			return ERESTART;
790 		}
791 		break;
792 	case IEEE80211_IOC_TDMA_BINTERVAL:
793 		if (!TDMA_BINTVAL_VALID(ireq->i_val))
794 			return EINVAL;
795 		if (ireq->i_val != ts->tdma_bintval) {
796 			ts->tdma_bintval = ireq->i_val;
797 			return ERESTART;
798 		}
799 		break;
800 	default:
801 		return ENOSYS;
802 	}
803 	return 0;
804 }
805 IEEE80211_IOCTL_SET(tdma, tdma_ioctl_set80211);
806