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