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