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