Line data Source code
1 : /* $OpenBSD: ieee80211.c,v 1.70 2018/09/11 18:16:26 krw Exp $ */
2 : /* $NetBSD: ieee80211.c,v 1.19 2004/06/06 05:45:29 dyoung Exp $ */
3 :
4 : /*-
5 : * Copyright (c) 2001 Atsushi Onoe
6 : * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
7 : * All rights reserved.
8 : *
9 : * Redistribution and use in source and binary forms, with or without
10 : * modification, are permitted provided that the following conditions
11 : * are met:
12 : * 1. Redistributions of source code must retain the above copyright
13 : * notice, this list of conditions and the following disclaimer.
14 : * 2. Redistributions in binary form must reproduce the above copyright
15 : * notice, this list of conditions and the following disclaimer in the
16 : * documentation and/or other materials provided with the distribution.
17 : * 3. The name of the author may not be used to endorse or promote products
18 : * derived from this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 : * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 : * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 : * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 : * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 : * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 : * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 : */
31 :
32 : /*
33 : * IEEE 802.11 generic handler
34 : */
35 :
36 : #include "bpfilter.h"
37 :
38 : #include <sys/param.h>
39 : #include <sys/systm.h>
40 : #include <sys/mbuf.h>
41 : #include <sys/kernel.h>
42 : #include <sys/socket.h>
43 : #include <sys/sockio.h>
44 : #include <sys/endian.h>
45 : #include <sys/errno.h>
46 : #include <sys/sysctl.h>
47 :
48 : #include <net/if.h>
49 : #include <net/if_dl.h>
50 : #include <net/if_media.h>
51 :
52 : #if NBPFILTER > 0
53 : #include <net/bpf.h>
54 : #endif
55 :
56 : #include <netinet/in.h>
57 : #include <netinet/if_ether.h>
58 :
59 : #include <net80211/ieee80211_var.h>
60 : #include <net80211/ieee80211_priv.h>
61 :
62 : #ifdef IEEE80211_DEBUG
63 : int ieee80211_debug = 0;
64 : #endif
65 :
66 : int ieee80211_cache_size = IEEE80211_CACHE_SIZE;
67 :
68 : void ieee80211_setbasicrates(struct ieee80211com *);
69 : int ieee80211_findrate(struct ieee80211com *, enum ieee80211_phymode, int);
70 :
71 : void
72 0 : ieee80211_begin_bgscan(struct ifnet *ifp)
73 : {
74 0 : struct ieee80211com *ic = (void *)ifp;
75 :
76 0 : if ((ic->ic_flags & IEEE80211_F_BGSCAN) ||
77 0 : ic->ic_state != IEEE80211_S_RUN)
78 0 : return;
79 :
80 0 : if (ic->ic_bgscan_start != NULL && ic->ic_bgscan_start(ic) == 0) {
81 : /*
82 : * Free the nodes table to ensure we get an up-to-date view
83 : * of APs around us. In particular, we need to kick out the
84 : * AP we are associated to. Otherwise, our current AP might
85 : * stay cached if it is turned off while we are scanning, and
86 : * we could end up picking a now non-existent AP over and over.
87 : */
88 0 : ieee80211_free_allnodes(ic, 0 /* keep ic->ic_bss */);
89 :
90 0 : ic->ic_flags |= IEEE80211_F_BGSCAN;
91 0 : if (ifp->if_flags & IFF_DEBUG)
92 0 : printf("%s: begin background scan\n", ifp->if_xname);
93 :
94 : /* Driver calls ieee80211_end_scan() when done. */
95 : }
96 0 : }
97 :
98 : void
99 0 : ieee80211_bgscan_timeout(void *arg)
100 : {
101 0 : struct ifnet *ifp = arg;
102 :
103 0 : ieee80211_begin_bgscan(ifp);
104 0 : }
105 :
106 : void
107 0 : ieee80211_channel_init(struct ifnet *ifp)
108 : {
109 0 : struct ieee80211com *ic = (void *)ifp;
110 : struct ieee80211_channel *c;
111 : int i;
112 :
113 : /*
114 : * Fill in 802.11 available channel set, mark
115 : * all available channels as active, and pick
116 : * a default channel if not already specified.
117 : */
118 0 : memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
119 0 : ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
120 0 : for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
121 0 : c = &ic->ic_channels[i];
122 0 : if (c->ic_flags) {
123 : /*
124 : * Verify driver passed us valid data.
125 : */
126 0 : if (i != ieee80211_chan2ieee(ic, c)) {
127 0 : printf("%s: bad channel ignored; "
128 : "freq %u flags %x number %u\n",
129 0 : ifp->if_xname, c->ic_freq, c->ic_flags,
130 : i);
131 0 : c->ic_flags = 0; /* NB: remove */
132 0 : continue;
133 : }
134 0 : setbit(ic->ic_chan_avail, i);
135 : /*
136 : * Identify mode capabilities.
137 : */
138 0 : if (IEEE80211_IS_CHAN_A(c))
139 0 : ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
140 0 : if (IEEE80211_IS_CHAN_B(c))
141 0 : ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
142 0 : if (IEEE80211_IS_CHAN_PUREG(c))
143 0 : ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
144 0 : if (IEEE80211_IS_CHAN_N(c))
145 0 : ic->ic_modecaps |= 1<<IEEE80211_MODE_11N;
146 : }
147 : }
148 : /* validate ic->ic_curmode */
149 0 : if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
150 0 : ic->ic_curmode = IEEE80211_MODE_AUTO;
151 0 : ic->ic_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
152 0 : }
153 :
154 : void
155 0 : ieee80211_ifattach(struct ifnet *ifp)
156 : {
157 0 : struct ieee80211com *ic = (void *)ifp;
158 :
159 0 : memcpy(((struct arpcom *)ifp)->ac_enaddr, ic->ic_myaddr,
160 : ETHER_ADDR_LEN);
161 0 : ether_ifattach(ifp);
162 :
163 0 : ifp->if_output = ieee80211_output;
164 :
165 : #if NBPFILTER > 0
166 0 : bpfattach(&ic->ic_rawbpf, ifp, DLT_IEEE802_11,
167 : sizeof(struct ieee80211_frame_addr4));
168 : #endif
169 0 : ieee80211_crypto_attach(ifp);
170 :
171 0 : ieee80211_channel_init(ifp);
172 :
173 : /* IEEE 802.11 defines a MTU >= 2290 */
174 0 : ifp->if_capabilities |= IFCAP_VLAN_MTU;
175 :
176 0 : ieee80211_setbasicrates(ic);
177 0 : (void)ieee80211_setmode(ic, ic->ic_curmode);
178 :
179 0 : if (ic->ic_lintval == 0)
180 0 : ic->ic_lintval = 100; /* default sleep */
181 0 : ic->ic_bmissthres = 7; /* default 7 beacons */
182 0 : ic->ic_dtim_period = 1; /* all TIMs are DTIMs */
183 :
184 0 : ieee80211_node_attach(ifp);
185 0 : ieee80211_proto_attach(ifp);
186 :
187 0 : if_addgroup(ifp, "wlan");
188 0 : ifp->if_priority = IF_WIRELESS_DEFAULT_PRIORITY;
189 :
190 0 : ieee80211_set_link_state(ic, LINK_STATE_DOWN);
191 :
192 0 : timeout_set(&ic->ic_bgscan_timeout, ieee80211_bgscan_timeout, ifp);
193 0 : }
194 :
195 : void
196 0 : ieee80211_ifdetach(struct ifnet *ifp)
197 : {
198 0 : struct ieee80211com *ic = (void *)ifp;
199 :
200 0 : timeout_del(&ic->ic_bgscan_timeout);
201 0 : ieee80211_proto_detach(ifp);
202 0 : ieee80211_crypto_detach(ifp);
203 0 : ieee80211_node_detach(ifp);
204 0 : ifmedia_delete_instance(&ic->ic_media, IFM_INST_ANY);
205 0 : ether_ifdetach(ifp);
206 0 : }
207 :
208 : /*
209 : * Convert MHz frequency to IEEE channel number.
210 : */
211 : u_int
212 0 : ieee80211_mhz2ieee(u_int freq, u_int flags)
213 : {
214 0 : if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
215 0 : if (freq == 2484)
216 0 : return 14;
217 0 : if (freq < 2484)
218 0 : return (freq - 2407) / 5;
219 : else
220 0 : return 15 + ((freq - 2512) / 20);
221 0 : } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5GHz band */
222 0 : return (freq - 5000) / 5;
223 : } else { /* either, guess */
224 0 : if (freq == 2484)
225 0 : return 14;
226 0 : if (freq < 2484)
227 0 : return (freq - 2407) / 5;
228 0 : if (freq < 5000)
229 0 : return 15 + ((freq - 2512) / 20);
230 0 : return (freq - 5000) / 5;
231 : }
232 0 : }
233 :
234 : /*
235 : * Convert channel to IEEE channel number.
236 : */
237 : u_int
238 0 : ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
239 : {
240 0 : struct ifnet *ifp = &ic->ic_if;
241 0 : if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
242 0 : return c - ic->ic_channels;
243 0 : else if (c == IEEE80211_CHAN_ANYC)
244 0 : return IEEE80211_CHAN_ANY;
245 :
246 0 : panic("%s: bogus channel pointer", ifp->if_xname);
247 0 : }
248 :
249 : /*
250 : * Convert IEEE channel number to MHz frequency.
251 : */
252 : u_int
253 0 : ieee80211_ieee2mhz(u_int chan, u_int flags)
254 : {
255 0 : if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
256 0 : if (chan == 14)
257 0 : return 2484;
258 0 : if (chan < 14)
259 0 : return 2407 + chan*5;
260 : else
261 0 : return 2512 + ((chan-15)*20);
262 0 : } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5GHz band */
263 0 : return 5000 + (chan*5);
264 : } else { /* either, guess */
265 0 : if (chan == 14)
266 0 : return 2484;
267 0 : if (chan < 14) /* 0-13 */
268 0 : return 2407 + chan*5;
269 0 : if (chan < 27) /* 15-26 */
270 0 : return 2512 + ((chan-15)*20);
271 0 : return 5000 + (chan*5);
272 : }
273 0 : }
274 :
275 : /*
276 : * Setup the media data structures according to the channel and
277 : * rate tables. This must be called by the driver after
278 : * ieee80211_attach and before most anything else.
279 : */
280 : void
281 0 : ieee80211_media_init(struct ifnet *ifp,
282 : ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
283 : {
284 : #define ADD(_ic, _s, _o) \
285 : ifmedia_add(&(_ic)->ic_media, \
286 : IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
287 0 : struct ieee80211com *ic = (void *)ifp;
288 0 : struct ifmediareq imr;
289 : int i, j, mode, rate, maxrate, r;
290 : uint64_t mword, mopt;
291 : const struct ieee80211_rateset *rs;
292 0 : struct ieee80211_rateset allrates;
293 :
294 : /*
295 : * Do late attach work that must wait for any subclass
296 : * (i.e. driver) work such as overriding methods.
297 : */
298 0 : ieee80211_node_lateattach(ifp);
299 :
300 : /*
301 : * Fill in media characteristics.
302 : */
303 0 : ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
304 : maxrate = 0;
305 0 : memset(&allrates, 0, sizeof(allrates));
306 0 : for (mode = IEEE80211_MODE_AUTO; mode <= IEEE80211_MODE_11G; mode++) {
307 : static const uint64_t mopts[] = {
308 : IFM_AUTO,
309 : IFM_IEEE80211_11A,
310 : IFM_IEEE80211_11B,
311 : IFM_IEEE80211_11G,
312 : };
313 0 : if ((ic->ic_modecaps & (1<<mode)) == 0)
314 : continue;
315 0 : mopt = mopts[mode];
316 0 : ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */
317 : #ifndef IEEE80211_STA_ONLY
318 0 : if (ic->ic_caps & IEEE80211_C_IBSS)
319 0 : ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_IBSS);
320 0 : if (ic->ic_caps & IEEE80211_C_HOSTAP)
321 0 : ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
322 0 : if (ic->ic_caps & IEEE80211_C_AHDEMO)
323 0 : ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
324 : #endif
325 0 : if (ic->ic_caps & IEEE80211_C_MONITOR)
326 0 : ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
327 0 : if (mode == IEEE80211_MODE_AUTO)
328 : continue;
329 0 : rs = &ic->ic_sup_rates[mode];
330 0 : for (i = 0; i < rs->rs_nrates; i++) {
331 0 : rate = rs->rs_rates[i];
332 0 : mword = ieee80211_rate2media(ic, rate, mode);
333 0 : if (mword == 0)
334 : continue;
335 0 : ADD(ic, mword, mopt);
336 : #ifndef IEEE80211_STA_ONLY
337 0 : if (ic->ic_caps & IEEE80211_C_IBSS)
338 0 : ADD(ic, mword, mopt | IFM_IEEE80211_IBSS);
339 0 : if (ic->ic_caps & IEEE80211_C_HOSTAP)
340 0 : ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
341 0 : if (ic->ic_caps & IEEE80211_C_AHDEMO)
342 0 : ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
343 : #endif
344 0 : if (ic->ic_caps & IEEE80211_C_MONITOR)
345 0 : ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
346 : /*
347 : * Add rate to the collection of all rates.
348 : */
349 0 : r = rate & IEEE80211_RATE_VAL;
350 0 : for (j = 0; j < allrates.rs_nrates; j++)
351 0 : if (allrates.rs_rates[j] == r)
352 : break;
353 0 : if (j == allrates.rs_nrates) {
354 : /* unique, add to the set */
355 0 : allrates.rs_rates[j] = r;
356 0 : allrates.rs_nrates++;
357 0 : }
358 0 : rate = (rate & IEEE80211_RATE_VAL) / 2;
359 0 : if (rate > maxrate)
360 0 : maxrate = rate;
361 : }
362 : }
363 0 : for (i = 0; i < allrates.rs_nrates; i++) {
364 0 : mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
365 : IEEE80211_MODE_AUTO);
366 0 : if (mword == 0)
367 : continue;
368 0 : mword = IFM_SUBTYPE(mword); /* remove media options */
369 0 : ADD(ic, mword, 0);
370 : #ifndef IEEE80211_STA_ONLY
371 0 : if (ic->ic_caps & IEEE80211_C_IBSS)
372 0 : ADD(ic, mword, IFM_IEEE80211_IBSS);
373 0 : if (ic->ic_caps & IEEE80211_C_HOSTAP)
374 0 : ADD(ic, mword, IFM_IEEE80211_HOSTAP);
375 0 : if (ic->ic_caps & IEEE80211_C_AHDEMO)
376 0 : ADD(ic, mword, IFM_IEEE80211_ADHOC);
377 : #endif
378 0 : if (ic->ic_caps & IEEE80211_C_MONITOR)
379 0 : ADD(ic, mword, IFM_IEEE80211_MONITOR);
380 : }
381 :
382 0 : if (ic->ic_modecaps & (1 << IEEE80211_MODE_11N)) {
383 : mopt = IFM_IEEE80211_11N;
384 0 : ADD(ic, IFM_AUTO, mopt);
385 : #ifndef IEEE80211_STA_ONLY
386 0 : if (ic->ic_caps & IEEE80211_C_IBSS)
387 0 : ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_IBSS);
388 0 : if (ic->ic_caps & IEEE80211_C_HOSTAP)
389 0 : ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
390 : #endif
391 0 : if (ic->ic_caps & IEEE80211_C_MONITOR)
392 0 : ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
393 0 : for (i = 0; i < IEEE80211_HT_NUM_MCS; i++) {
394 0 : if (!isset(ic->ic_sup_mcs, i))
395 : continue;
396 0 : ADD(ic, IFM_IEEE80211_HT_MCS0 + i, mopt);
397 : #ifndef IEEE80211_STA_ONLY
398 0 : if (ic->ic_caps & IEEE80211_C_IBSS)
399 0 : ADD(ic, IFM_IEEE80211_HT_MCS0 + i,
400 : mopt | IFM_IEEE80211_IBSS);
401 0 : if (ic->ic_caps & IEEE80211_C_HOSTAP)
402 0 : ADD(ic, IFM_IEEE80211_HT_MCS0 + i,
403 : mopt | IFM_IEEE80211_HOSTAP);
404 : #endif
405 0 : if (ic->ic_caps & IEEE80211_C_MONITOR)
406 0 : ADD(ic, IFM_IEEE80211_HT_MCS0 + i,
407 : mopt | IFM_IEEE80211_MONITOR);
408 : }
409 0 : ic->ic_flags |= IEEE80211_F_HTON; /* enable 11n by default */
410 0 : }
411 :
412 0 : ieee80211_media_status(ifp, &imr);
413 0 : ifmedia_set(&ic->ic_media, imr.ifm_active);
414 :
415 0 : if (maxrate)
416 0 : ifp->if_baudrate = IF_Mbps(maxrate);
417 :
418 : #undef ADD
419 0 : }
420 :
421 : int
422 0 : ieee80211_findrate(struct ieee80211com *ic, enum ieee80211_phymode mode,
423 : int rate)
424 : {
425 : #define IEEERATE(_ic,_m,_i) \
426 : ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
427 0 : int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
428 0 : for (i = 0; i < nrates; i++)
429 0 : if (IEEERATE(ic, mode, i) == rate)
430 0 : return i;
431 0 : return -1;
432 : #undef IEEERATE
433 0 : }
434 :
435 : /*
436 : * Handle a media change request.
437 : */
438 : int
439 0 : ieee80211_media_change(struct ifnet *ifp)
440 : {
441 0 : struct ieee80211com *ic = (void *)ifp;
442 : struct ifmedia_entry *ime;
443 : enum ieee80211_opmode newopmode;
444 : enum ieee80211_phymode newphymode;
445 : int i, j, newrate, error = 0;
446 :
447 0 : ime = ic->ic_media.ifm_cur;
448 : /*
449 : * First, identify the phy mode.
450 : */
451 0 : switch (IFM_MODE(ime->ifm_media)) {
452 : case IFM_IEEE80211_11A:
453 : newphymode = IEEE80211_MODE_11A;
454 0 : break;
455 : case IFM_IEEE80211_11B:
456 : newphymode = IEEE80211_MODE_11B;
457 0 : break;
458 : case IFM_IEEE80211_11G:
459 : newphymode = IEEE80211_MODE_11G;
460 0 : break;
461 : case IFM_IEEE80211_11N:
462 : newphymode = IEEE80211_MODE_11N;
463 0 : break;
464 : case IFM_AUTO:
465 : newphymode = IEEE80211_MODE_AUTO;
466 0 : break;
467 : default:
468 0 : return EINVAL;
469 : }
470 :
471 : /*
472 : * Validate requested mode is available.
473 : */
474 0 : if ((ic->ic_modecaps & (1<<newphymode)) == 0)
475 0 : return EINVAL;
476 :
477 : /*
478 : * Next, the fixed/variable rate.
479 : */
480 : i = -1;
481 0 : if (IFM_SUBTYPE(ime->ifm_media) >= IFM_IEEE80211_HT_MCS0 &&
482 0 : IFM_SUBTYPE(ime->ifm_media) <= IFM_IEEE80211_HT_MCS76) {
483 0 : if ((ic->ic_modecaps & (1 << IEEE80211_MODE_11N)) == 0)
484 0 : return EINVAL;
485 0 : if (newphymode != IEEE80211_MODE_AUTO &&
486 0 : newphymode != IEEE80211_MODE_11N)
487 0 : return EINVAL;
488 0 : i = ieee80211_media2mcs(ime->ifm_media);
489 0 : if (i == -1 || isclr(ic->ic_sup_mcs, i))
490 0 : return EINVAL;
491 0 : } else if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
492 : /*
493 : * Convert media subtype to rate.
494 : */
495 0 : newrate = ieee80211_media2rate(ime->ifm_media);
496 0 : if (newrate == 0)
497 0 : return EINVAL;
498 : /*
499 : * Check the rate table for the specified/current phy.
500 : */
501 0 : if (newphymode == IEEE80211_MODE_AUTO) {
502 : /*
503 : * In autoselect mode search for the rate.
504 : */
505 0 : for (j = IEEE80211_MODE_11A;
506 0 : j < IEEE80211_MODE_MAX; j++) {
507 0 : if ((ic->ic_modecaps & (1<<j)) == 0)
508 : continue;
509 0 : i = ieee80211_findrate(ic, j, newrate);
510 0 : if (i != -1) {
511 : /* lock mode too */
512 : newphymode = j;
513 0 : break;
514 : }
515 : }
516 : } else {
517 0 : i = ieee80211_findrate(ic, newphymode, newrate);
518 : }
519 0 : if (i == -1) /* mode/rate mismatch */
520 0 : return EINVAL;
521 : }
522 : /* NB: defer rate setting to later */
523 :
524 : /*
525 : * Deduce new operating mode but don't install it just yet.
526 : */
527 : #ifndef IEEE80211_STA_ONLY
528 0 : if (ime->ifm_media & IFM_IEEE80211_ADHOC)
529 0 : newopmode = IEEE80211_M_AHDEMO;
530 0 : else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
531 0 : newopmode = IEEE80211_M_HOSTAP;
532 0 : else if (ime->ifm_media & IFM_IEEE80211_IBSS)
533 0 : newopmode = IEEE80211_M_IBSS;
534 : else
535 : #endif
536 0 : if (ime->ifm_media & IFM_IEEE80211_MONITOR)
537 0 : newopmode = IEEE80211_M_MONITOR;
538 : else
539 : newopmode = IEEE80211_M_STA;
540 :
541 : #ifndef IEEE80211_STA_ONLY
542 : /*
543 : * Autoselect doesn't make sense when operating as an AP.
544 : * If no phy mode has been selected, pick one and lock it
545 : * down so rate tables can be used in forming beacon frames
546 : * and the like.
547 : */
548 0 : if (newopmode == IEEE80211_M_HOSTAP &&
549 0 : newphymode == IEEE80211_MODE_AUTO) {
550 0 : if (ic->ic_modecaps & (1 << IEEE80211_MODE_11N))
551 0 : newphymode = IEEE80211_MODE_11N;
552 0 : else if (ic->ic_modecaps & (1 << IEEE80211_MODE_11A))
553 0 : newphymode = IEEE80211_MODE_11A;
554 0 : else if (ic->ic_modecaps & (1 << IEEE80211_MODE_11G))
555 0 : newphymode = IEEE80211_MODE_11G;
556 : else
557 : newphymode = IEEE80211_MODE_11B;
558 : }
559 : #endif
560 :
561 : /*
562 : * Handle phy mode change.
563 : */
564 0 : if (ic->ic_curmode != newphymode) { /* change phy mode */
565 0 : error = ieee80211_setmode(ic, newphymode);
566 0 : if (error != 0)
567 0 : return error;
568 : error = ENETRESET;
569 0 : }
570 :
571 : /*
572 : * Committed to changes, install the MCS/rate setting.
573 : */
574 0 : ic->ic_flags &= ~IEEE80211_F_HTON;
575 0 : if ((ic->ic_modecaps & (1 << IEEE80211_MODE_11N)) &&
576 0 : (newphymode == IEEE80211_MODE_AUTO ||
577 0 : newphymode == IEEE80211_MODE_11N))
578 0 : ic->ic_flags |= IEEE80211_F_HTON;
579 0 : if ((ic->ic_flags & IEEE80211_F_HTON) == 0) {
580 0 : ic->ic_fixed_mcs = -1;
581 0 : if (ic->ic_fixed_rate != i) {
582 0 : ic->ic_fixed_rate = i; /* set fixed tx rate */
583 : error = ENETRESET;
584 0 : }
585 : } else {
586 0 : ic->ic_fixed_rate = -1;
587 0 : if (ic->ic_fixed_mcs != i) {
588 0 : ic->ic_fixed_mcs = i; /* set fixed mcs */
589 : error = ENETRESET;
590 0 : }
591 : }
592 :
593 : /*
594 : * Handle operating mode change.
595 : */
596 0 : if (ic->ic_opmode != newopmode) {
597 0 : ic->ic_opmode = newopmode;
598 : #ifndef IEEE80211_STA_ONLY
599 0 : switch (newopmode) {
600 : case IEEE80211_M_AHDEMO:
601 : case IEEE80211_M_HOSTAP:
602 : case IEEE80211_M_STA:
603 : case IEEE80211_M_MONITOR:
604 0 : ic->ic_flags &= ~IEEE80211_F_IBSSON;
605 0 : break;
606 : case IEEE80211_M_IBSS:
607 0 : ic->ic_flags |= IEEE80211_F_IBSSON;
608 0 : break;
609 : }
610 : #endif
611 : /*
612 : * Yech, slot time may change depending on the
613 : * operating mode so reset it to be sure everything
614 : * is setup appropriately.
615 : */
616 0 : ieee80211_reset_erp(ic);
617 : error = ENETRESET;
618 0 : }
619 : #ifdef notdef
620 : if (error == 0)
621 : ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
622 : #endif
623 0 : return error;
624 0 : }
625 :
626 : void
627 0 : ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
628 : {
629 0 : struct ieee80211com *ic = (void *)ifp;
630 : const struct ieee80211_node *ni = NULL;
631 :
632 0 : imr->ifm_status = IFM_AVALID;
633 0 : imr->ifm_active = IFM_IEEE80211;
634 0 : if (ic->ic_state == IEEE80211_S_RUN &&
635 0 : (ic->ic_opmode != IEEE80211_M_STA ||
636 0 : !(ic->ic_flags & IEEE80211_F_RSNON) ||
637 0 : ic->ic_bss->ni_port_valid))
638 0 : imr->ifm_status |= IFM_ACTIVE;
639 0 : imr->ifm_active |= IFM_AUTO;
640 0 : switch (ic->ic_opmode) {
641 : case IEEE80211_M_STA:
642 0 : ni = ic->ic_bss;
643 0 : if (ic->ic_curmode == IEEE80211_MODE_11N)
644 0 : imr->ifm_active |= ieee80211_mcs2media(ic,
645 0 : ni->ni_txmcs, ic->ic_curmode);
646 : else
647 : /* calculate rate subtype */
648 0 : imr->ifm_active |= ieee80211_rate2media(ic,
649 0 : ni->ni_rates.rs_rates[ni->ni_txrate],
650 : ic->ic_curmode);
651 : break;
652 : #ifndef IEEE80211_STA_ONLY
653 : case IEEE80211_M_IBSS:
654 0 : imr->ifm_active |= IFM_IEEE80211_IBSS;
655 0 : break;
656 : case IEEE80211_M_AHDEMO:
657 0 : imr->ifm_active |= IFM_IEEE80211_ADHOC;
658 0 : break;
659 : case IEEE80211_M_HOSTAP:
660 0 : imr->ifm_active |= IFM_IEEE80211_HOSTAP;
661 0 : break;
662 : #endif
663 : case IEEE80211_M_MONITOR:
664 0 : imr->ifm_active |= IFM_IEEE80211_MONITOR;
665 0 : break;
666 : default:
667 : break;
668 : }
669 0 : switch (ic->ic_curmode) {
670 : case IEEE80211_MODE_11A:
671 0 : imr->ifm_active |= IFM_IEEE80211_11A;
672 0 : break;
673 : case IEEE80211_MODE_11B:
674 0 : imr->ifm_active |= IFM_IEEE80211_11B;
675 0 : break;
676 : case IEEE80211_MODE_11G:
677 0 : imr->ifm_active |= IFM_IEEE80211_11G;
678 0 : break;
679 : case IEEE80211_MODE_11N:
680 0 : imr->ifm_active |= IFM_IEEE80211_11N;
681 0 : break;
682 : }
683 0 : }
684 :
685 : void
686 0 : ieee80211_watchdog(struct ifnet *ifp)
687 : {
688 0 : struct ieee80211com *ic = (void *)ifp;
689 :
690 0 : if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
691 0 : ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
692 :
693 0 : if (ic->ic_mgt_timer != 0)
694 0 : ifp->if_timer = 1;
695 0 : }
696 :
697 : const struct ieee80211_rateset ieee80211_std_rateset_11a =
698 : { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
699 :
700 : const struct ieee80211_rateset ieee80211_std_rateset_11b =
701 : { 4, { 2, 4, 11, 22 } };
702 :
703 : const struct ieee80211_rateset ieee80211_std_rateset_11g =
704 : { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
705 :
706 : /*
707 : * Mark the basic rates for the 11g rate table based on the
708 : * operating mode. For real 11g we mark all the 11b rates
709 : * and 6, 12, and 24 OFDM. For 11b compatibility we mark only
710 : * 11b rates. There's also a pseudo 11a-mode used to mark only
711 : * the basic OFDM rates.
712 : */
713 : void
714 0 : ieee80211_setbasicrates(struct ieee80211com *ic)
715 : {
716 : static const struct ieee80211_rateset basic[] = {
717 : { 0 }, /* IEEE80211_MODE_AUTO */
718 : { 3, { 12, 24, 48 } }, /* IEEE80211_MODE_11A */
719 : { 2, { 2, 4 } }, /* IEEE80211_MODE_11B */
720 : { 4, { 2, 4, 11, 22 } }, /* IEEE80211_MODE_11G */
721 : { 0 }, /* IEEE80211_MODE_11N */
722 : };
723 : enum ieee80211_phymode mode;
724 : struct ieee80211_rateset *rs;
725 : int i, j;
726 :
727 0 : for (mode = 0; mode < IEEE80211_MODE_MAX; mode++) {
728 0 : rs = &ic->ic_sup_rates[mode];
729 0 : for (i = 0; i < rs->rs_nrates; i++) {
730 0 : rs->rs_rates[i] &= IEEE80211_RATE_VAL;
731 0 : for (j = 0; j < basic[mode].rs_nrates; j++) {
732 0 : if (basic[mode].rs_rates[j] ==
733 0 : rs->rs_rates[i]) {
734 0 : rs->rs_rates[i] |=
735 : IEEE80211_RATE_BASIC;
736 0 : break;
737 : }
738 : }
739 : }
740 : }
741 0 : }
742 :
743 : int
744 0 : ieee80211_min_basic_rate(struct ieee80211com *ic)
745 : {
746 0 : struct ieee80211_rateset *rs = &ic->ic_bss->ni_rates;
747 : int i, min, rval;
748 :
749 : min = -1;
750 :
751 0 : for (i = 0; i < rs->rs_nrates; i++) {
752 0 : if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) == 0)
753 : continue;
754 0 : rval = (rs->rs_rates[i] & IEEE80211_RATE_VAL);
755 0 : if (min == -1)
756 0 : min = rval;
757 0 : else if (rval < min)
758 0 : min = rval;
759 : }
760 :
761 : /* Default to 1 Mbit/s on 2GHz and 6 Mbit/s on 5GHz. */
762 0 : if (min == -1)
763 0 : min = IEEE80211_IS_CHAN_2GHZ(ic->ic_bss->ni_chan) ? 2 : 12;
764 :
765 0 : return min;
766 : }
767 :
768 : int
769 0 : ieee80211_max_basic_rate(struct ieee80211com *ic)
770 : {
771 0 : struct ieee80211_rateset *rs = &ic->ic_bss->ni_rates;
772 : int i, max, rval;
773 :
774 : /* Default to 1 Mbit/s on 2GHz and 6 Mbit/s on 5GHz. */
775 0 : max = IEEE80211_IS_CHAN_2GHZ(ic->ic_bss->ni_chan) ? 2 : 12;
776 :
777 0 : for (i = 0; i < rs->rs_nrates; i++) {
778 0 : if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) == 0)
779 : continue;
780 0 : rval = (rs->rs_rates[i] & IEEE80211_RATE_VAL);
781 0 : if (rval > max)
782 0 : max = rval;
783 : }
784 :
785 0 : return max;
786 : }
787 :
788 : /*
789 : * Set the current phy mode and recalculate the active channel
790 : * set based on the available channels for this mode. Also
791 : * select a new default/current channel if the current one is
792 : * inappropriate for this mode.
793 : */
794 : int
795 0 : ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
796 : {
797 0 : struct ifnet *ifp = &ic->ic_if;
798 : static const u_int chanflags[] = {
799 : 0, /* IEEE80211_MODE_AUTO */
800 : IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */
801 : IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */
802 : IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */
803 : IEEE80211_CHAN_HT, /* IEEE80211_MODE_11N */
804 : };
805 : const struct ieee80211_channel *c;
806 : u_int modeflags;
807 : int i;
808 :
809 : /* validate new mode */
810 0 : if ((ic->ic_modecaps & (1<<mode)) == 0) {
811 : DPRINTF(("mode %u not supported (caps 0x%x)\n",
812 : mode, ic->ic_modecaps));
813 0 : return EINVAL;
814 : }
815 :
816 : /*
817 : * Verify at least one channel is present in the available
818 : * channel list before committing to the new mode.
819 : */
820 0 : if (mode >= nitems(chanflags))
821 0 : panic("%s: unexpected mode %u", __func__, mode);
822 0 : modeflags = chanflags[mode];
823 0 : for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
824 0 : c = &ic->ic_channels[i];
825 0 : if (mode == IEEE80211_MODE_AUTO) {
826 0 : if (c->ic_flags != 0)
827 : break;
828 0 : } else if ((c->ic_flags & modeflags) == modeflags)
829 : break;
830 : }
831 0 : if (i > IEEE80211_CHAN_MAX) {
832 : DPRINTF(("no channels found for mode %u\n", mode));
833 0 : return EINVAL;
834 : }
835 :
836 : /*
837 : * Calculate the active channel set.
838 : */
839 0 : memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
840 0 : for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
841 0 : c = &ic->ic_channels[i];
842 0 : if (mode == IEEE80211_MODE_AUTO) {
843 0 : if (c->ic_flags != 0)
844 0 : setbit(ic->ic_chan_active, i);
845 0 : } else if ((c->ic_flags & modeflags) == modeflags)
846 0 : setbit(ic->ic_chan_active, i);
847 : }
848 : /*
849 : * If no current/default channel is setup or the current
850 : * channel is wrong for the mode then pick the first
851 : * available channel from the active list. This is likely
852 : * not the right one.
853 : */
854 0 : if (ic->ic_ibss_chan == NULL || isclr(ic->ic_chan_active,
855 : ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
856 0 : for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
857 0 : if (isset(ic->ic_chan_active, i)) {
858 0 : ic->ic_ibss_chan = &ic->ic_channels[i];
859 0 : break;
860 : }
861 0 : if ((ic->ic_ibss_chan == NULL) || isclr(ic->ic_chan_active,
862 : ieee80211_chan2ieee(ic, ic->ic_ibss_chan)))
863 0 : panic("Bad IBSS channel %u",
864 0 : ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
865 : }
866 :
867 : /*
868 : * Reset the scan state for the new mode. This avoids scanning
869 : * of invalid channels, ie. 5GHz channels in 11b mode.
870 : */
871 0 : ieee80211_reset_scan(ifp);
872 :
873 0 : ic->ic_curmode = mode;
874 0 : ieee80211_reset_erp(ic); /* reset ERP state */
875 :
876 0 : return 0;
877 0 : }
878 :
879 : enum ieee80211_phymode
880 0 : ieee80211_next_mode(struct ifnet *ifp)
881 : {
882 0 : struct ieee80211com *ic = (void *)ifp;
883 :
884 0 : if (IFM_MODE(ic->ic_media.ifm_cur->ifm_media) != IFM_AUTO) {
885 : /*
886 : * Reset the scan state and indicate a wrap around
887 : * if we're running in a fixed, user-specified phy mode.
888 : */
889 0 : ieee80211_reset_scan(ifp);
890 0 : return (IEEE80211_MODE_AUTO);
891 : }
892 :
893 : /*
894 : * Get the next supported mode
895 : */
896 0 : for (++ic->ic_curmode;
897 0 : ic->ic_curmode <= IEEE80211_MODE_MAX;
898 0 : ic->ic_curmode++) {
899 : /*
900 : * Skip over 11n mode. Its set of channels is the superset
901 : * of all channels supported by the other modes.
902 : */
903 0 : if (ic->ic_curmode == IEEE80211_MODE_11N)
904 : continue;
905 :
906 : /* Always scan in AUTO mode if the driver scans all bands. */
907 0 : if (ic->ic_curmode >= IEEE80211_MODE_MAX ||
908 0 : (ic->ic_caps & IEEE80211_C_SCANALLBAND)) {
909 0 : ic->ic_curmode = IEEE80211_MODE_AUTO;
910 0 : break;
911 : }
912 :
913 0 : if (ic->ic_modecaps & (1 << ic->ic_curmode))
914 : break;
915 : }
916 :
917 0 : ieee80211_setmode(ic, ic->ic_curmode);
918 :
919 0 : return (ic->ic_curmode);
920 0 : }
921 :
922 : /*
923 : * Return the phy mode for with the specified channel so the
924 : * caller can select a rate set. This is problematic and the
925 : * work here assumes how things work elsewhere in this code.
926 : *
927 : * Because the result of this function is ultimately used to select a
928 : * rate from the rate set of the returned mode, it must not return
929 : * IEEE80211_MODE_11N, which uses MCS instead of rates for unicast frames.
930 : */
931 : enum ieee80211_phymode
932 0 : ieee80211_chan2mode(struct ieee80211com *ic,
933 : const struct ieee80211_channel *chan)
934 : {
935 : /*
936 : * NB: this assumes the channel would not be supplied to us
937 : * unless it was already compatible with the current mode.
938 : */
939 0 : if (ic->ic_curmode != IEEE80211_MODE_11N &&
940 0 : (ic->ic_curmode != IEEE80211_MODE_AUTO ||
941 0 : chan == IEEE80211_CHAN_ANYC))
942 0 : return ic->ic_curmode;
943 : /*
944 : * In autoselect or 11n mode; deduce a mode based on the channel
945 : * characteristics.
946 : */
947 0 : if (IEEE80211_IS_CHAN_5GHZ(chan))
948 0 : return IEEE80211_MODE_11A;
949 0 : else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN))
950 0 : return IEEE80211_MODE_11G;
951 : else
952 0 : return IEEE80211_MODE_11B;
953 0 : }
954 :
955 : /*
956 : * Convert IEEE80211 MCS index to ifmedia subtype.
957 : */
958 : uint64_t
959 0 : ieee80211_mcs2media(struct ieee80211com *ic, int mcs,
960 : enum ieee80211_phymode mode)
961 : {
962 0 : switch (mode) {
963 : case IEEE80211_MODE_11A:
964 : case IEEE80211_MODE_11B:
965 : case IEEE80211_MODE_11G:
966 : /* these modes use rates, not MCS */
967 0 : panic("%s: unexpected mode %d", __func__, mode);
968 : break;
969 : case IEEE80211_MODE_AUTO:
970 : case IEEE80211_MODE_11N:
971 0 : if (mcs >= 0 && mcs < IEEE80211_HT_NUM_MCS)
972 0 : return (IFM_IEEE80211_11N |
973 0 : (IFM_IEEE80211_HT_MCS0 + mcs));
974 : break;
975 : }
976 :
977 0 : return IFM_AUTO;
978 0 : }
979 :
980 : /*
981 : * Convert ifmedia subtype to IEEE80211 MCS index.
982 : */
983 : int
984 0 : ieee80211_media2mcs(uint64_t mword)
985 : {
986 : uint64_t subtype;
987 :
988 0 : subtype = IFM_SUBTYPE(mword);
989 :
990 0 : if (subtype == IFM_AUTO)
991 0 : return -1;
992 0 : else if (subtype == IFM_MANUAL || subtype == IFM_NONE)
993 0 : return 0;
994 :
995 0 : if (subtype >= IFM_IEEE80211_HT_MCS0 &&
996 0 : subtype <= IFM_IEEE80211_HT_MCS76)
997 0 : return (int)(subtype - IFM_IEEE80211_HT_MCS0);
998 :
999 0 : return -1;
1000 0 : }
1001 :
1002 : /*
1003 : * convert IEEE80211 rate value to ifmedia subtype.
1004 : * ieee80211 rate is in unit of 0.5Mbps.
1005 : */
1006 : uint64_t
1007 0 : ieee80211_rate2media(struct ieee80211com *ic, int rate,
1008 : enum ieee80211_phymode mode)
1009 : {
1010 : static const struct {
1011 : uint64_t m; /* rate + mode */
1012 : uint64_t r; /* if_media rate */
1013 : } rates[] = {
1014 : { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1015 : { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1016 : { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1017 : { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1018 : { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1019 : { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1020 : { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1021 : { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1022 : { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1023 : { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1024 : { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1025 : { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1026 : { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1027 : { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1028 : { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1029 : { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1030 : { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1031 : { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1032 : { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1033 : { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1034 : { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1035 : { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1036 : { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1037 : { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1038 : { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1039 : /* NB: OFDM72 doesn't really exist so we don't handle it */
1040 : };
1041 : uint64_t mask;
1042 : int i;
1043 :
1044 0 : mask = rate & IEEE80211_RATE_VAL;
1045 0 : switch (mode) {
1046 : case IEEE80211_MODE_11A:
1047 0 : mask |= IFM_IEEE80211_11A;
1048 0 : break;
1049 : case IEEE80211_MODE_11B:
1050 0 : mask |= IFM_IEEE80211_11B;
1051 0 : break;
1052 : case IEEE80211_MODE_AUTO:
1053 : /* NB: hack, 11g matches both 11b+11a rates */
1054 : /* FALLTHROUGH */
1055 : case IEEE80211_MODE_11G:
1056 0 : mask |= IFM_IEEE80211_11G;
1057 0 : break;
1058 : case IEEE80211_MODE_11N:
1059 : /* 11n uses MCS, not rates. */
1060 0 : panic("%s: unexpected mode %d", __func__, mode);
1061 : break;
1062 : }
1063 0 : for (i = 0; i < nitems(rates); i++)
1064 0 : if (rates[i].m == mask)
1065 0 : return rates[i].r;
1066 0 : return IFM_AUTO;
1067 0 : }
1068 :
1069 : int
1070 0 : ieee80211_media2rate(uint64_t mword)
1071 : {
1072 : int i;
1073 : static const struct {
1074 : uint64_t subtype;
1075 : int rate;
1076 : } ieeerates[] = {
1077 : { IFM_AUTO, -1 },
1078 : { IFM_MANUAL, 0 },
1079 : { IFM_NONE, 0 },
1080 : { IFM_IEEE80211_DS1, 2 },
1081 : { IFM_IEEE80211_DS2, 4 },
1082 : { IFM_IEEE80211_DS5, 11 },
1083 : { IFM_IEEE80211_DS11, 22 },
1084 : { IFM_IEEE80211_DS22, 44 },
1085 : { IFM_IEEE80211_OFDM6, 12 },
1086 : { IFM_IEEE80211_OFDM9, 18 },
1087 : { IFM_IEEE80211_OFDM12, 24 },
1088 : { IFM_IEEE80211_OFDM18, 36 },
1089 : { IFM_IEEE80211_OFDM24, 48 },
1090 : { IFM_IEEE80211_OFDM36, 72 },
1091 : { IFM_IEEE80211_OFDM48, 96 },
1092 : { IFM_IEEE80211_OFDM54, 108 },
1093 : { IFM_IEEE80211_OFDM72, 144 },
1094 : };
1095 0 : for (i = 0; i < nitems(ieeerates); i++) {
1096 0 : if (ieeerates[i].subtype == IFM_SUBTYPE(mword))
1097 0 : return ieeerates[i].rate;
1098 : }
1099 0 : return 0;
1100 0 : }
1101 :
1102 : /*
1103 : * Convert bit rate (in 0.5Mbps units) to PLCP signal (R4-R1) and vice versa.
1104 : */
1105 : u_int8_t
1106 0 : ieee80211_rate2plcp(u_int8_t rate, enum ieee80211_phymode mode)
1107 : {
1108 0 : rate &= IEEE80211_RATE_VAL;
1109 :
1110 0 : if (mode == IEEE80211_MODE_11B) {
1111 : /* IEEE Std 802.11b-1999 page 15, subclause 18.2.3.3 */
1112 0 : switch (rate) {
1113 0 : case 2: return 10;
1114 0 : case 4: return 20;
1115 0 : case 11: return 55;
1116 0 : case 22: return 110;
1117 : /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */
1118 0 : case 44: return 220;
1119 : }
1120 0 : } else if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11A) {
1121 : /* IEEE Std 802.11a-1999 page 14, subclause 17.3.4.1 */
1122 0 : switch (rate) {
1123 0 : case 12: return 0x0b;
1124 0 : case 18: return 0x0f;
1125 0 : case 24: return 0x0a;
1126 0 : case 36: return 0x0e;
1127 0 : case 48: return 0x09;
1128 0 : case 72: return 0x0d;
1129 0 : case 96: return 0x08;
1130 0 : case 108: return 0x0c;
1131 : }
1132 : } else
1133 0 : panic("%s: unexpected mode %u", __func__, mode);
1134 :
1135 : DPRINTF(("unsupported rate %u\n", rate));
1136 :
1137 0 : return 0;
1138 0 : }
1139 :
1140 : u_int8_t
1141 0 : ieee80211_plcp2rate(u_int8_t plcp, enum ieee80211_phymode mode)
1142 : {
1143 0 : if (mode == IEEE80211_MODE_11B) {
1144 : /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */
1145 0 : switch (plcp) {
1146 0 : case 10: return 2;
1147 0 : case 20: return 4;
1148 0 : case 55: return 11;
1149 0 : case 110: return 22;
1150 : /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */
1151 0 : case 220: return 44;
1152 : }
1153 0 : } else if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11A) {
1154 : /* IEEE Std 802.11a-1999 page 14, subclause 17.3.4.1 */
1155 0 : switch (plcp) {
1156 0 : case 0x0b: return 12;
1157 0 : case 0x0f: return 18;
1158 0 : case 0x0a: return 24;
1159 0 : case 0x0e: return 36;
1160 0 : case 0x09: return 48;
1161 0 : case 0x0d: return 72;
1162 0 : case 0x08: return 96;
1163 0 : case 0x0c: return 108;
1164 : }
1165 : } else
1166 0 : panic("%s: unexpected mode %u", __func__, mode);
1167 :
1168 : DPRINTF(("unsupported plcp %u\n", plcp));
1169 :
1170 0 : return 0;
1171 0 : }
|