1 /* $NetBSD: ieee80211.c,v 1.48 2007/12/01 14:35:51 jmcneill Exp $ */
3 * Copyright (c) 2001 Atsushi Onoe
4 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * Alternatively, this software may be distributed under the terms of the
19 * GNU General Public License ("GPL") version 2 as published by the Free
20 * Software Foundation.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211.c,v 1.22 2005/08/10 16:22:29 sam Exp $");
39 __KERNEL_RCSID(0, "$NetBSD: ieee80211.c,v 1.48 2007/12/01 14:35:51 jmcneill Exp $");
43 * IEEE 802.11 generic handler
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 #include <sys/endian.h>
56 #include <sys/errno.h>
58 #include <sys/sysctl.h>
61 #include <net/if_media.h>
62 #include <net/if_arp.h>
63 #include <net/if_ether.h>
64 #include <net/if_llc.h>
66 #include <net80211/ieee80211_netbsd.h>
67 #include <net80211/ieee80211_var.h>
68 #include <net80211/ieee80211_sysctl.h>
73 #include <netinet/in.h>
74 #include <net/if_ether.h>
77 struct ieee80211com_head ieee80211com_head
=
78 LIST_HEAD_INITIALIZER(ieee80211com_head
);
80 const char *ieee80211_phymode_name
[] = {
81 "auto", /* IEEE80211_MODE_AUTO */
82 "11a", /* IEEE80211_MODE_11A */
83 "11b", /* IEEE80211_MODE_11B */
84 "11g", /* IEEE80211_MODE_11G */
85 "FH", /* IEEE80211_MODE_FH */
86 "turboA", /* IEEE80211_MODE_TURBO_A */
87 "turboG", /* IEEE80211_MODE_TURBO_G */
90 /* list of all instances */
91 SLIST_HEAD(ieee80211_list
, ieee80211com
);
92 static struct ieee80211_list ieee80211_list
=
93 SLIST_HEAD_INITIALIZER(ieee80211_list
);
94 static u_int8_t ieee80211_vapmap
[32]; /* enough for 256 */
96 static void ieee80211_setbasicrates(struct ieee80211com
*);
99 ieee80211_add_vap(struct ieee80211com
*ic
)
101 #define N(a) (sizeof(a)/sizeof(a[0]))
108 for (i
= 0; i
< N(ieee80211_vapmap
) && ieee80211_vapmap
[i
] == 0xff; i
++)
110 if (i
== N(ieee80211_vapmap
))
111 panic("vap table full");
112 for (b
= ieee80211_vapmap
[i
]; b
& 1; b
>>= 1)
114 setbit(ieee80211_vapmap
, ic
->ic_vap
);
115 SLIST_INSERT_HEAD(&ieee80211_list
, ic
, ic_next
);
121 ieee80211_remove_vap(struct ieee80211com
*ic
)
126 SLIST_REMOVE(&ieee80211_list
, ic
, ieee80211com
, ic_next
);
127 IASSERT(ic
->ic_vap
< sizeof(ieee80211_vapmap
)*NBBY
,
128 ("invalid vap id %d", ic
->ic_vap
));
129 IASSERT(isset(ieee80211_vapmap
, ic
->ic_vap
),
130 ("vap id %d not allocated", ic
->ic_vap
));
131 clrbit(ieee80211_vapmap
, ic
->ic_vap
);
136 * Default reset method for use with the ioctl support. This
137 * method is invoked after any state change in the 802.11
138 * layer that should be propagated to the hardware but not
139 * require re-initialization of the 802.11 state machine (e.g
140 * rescanning for an ap). We always return ENETRESET which
141 * should cause the driver to re-initialize the device. Drivers
142 * can override this method to implement more optimized support.
145 ieee80211_default_reset(struct ifnet
*ifp
)
151 ieee80211_ifattach(struct ieee80211com
*ic
)
153 struct ifnet
*ifp
= ic
->ic_ifp
;
154 struct ieee80211_channel
*c
;
159 #endif /* __NetBSD__ */
161 ether_ifattach(ifp
, ic
->ic_myaddr
);
163 bpfattach2(ifp
, DLT_IEEE802_11
,
164 sizeof(struct ieee80211_frame_addr4
), &ic
->ic_rawbpf
);
167 ieee80211_crypto_attach(ic
);
170 * Fill in 802.11 available channel set, mark
171 * all available channels as active, and pick
172 * a default channel if not already specified.
174 memset(ic
->ic_chan_avail
, 0, sizeof(ic
->ic_chan_avail
));
175 ic
->ic_modecaps
|= 1<<IEEE80211_MODE_AUTO
;
176 for (i
= 0; i
<= IEEE80211_CHAN_MAX
; i
++) {
177 c
= &ic
->ic_channels
[i
];
180 * Verify driver passed us valid data.
182 if (i
!= ieee80211_chan2ieee(ic
, c
)) {
183 if_printf(ifp
, "bad channel ignored; "
184 "freq %u flags %x number %u\n",
185 c
->ic_freq
, c
->ic_flags
, i
);
186 c
->ic_flags
= 0; /* NB: remove */
189 setbit(ic
->ic_chan_avail
, i
);
191 * Identify mode capabilities.
193 if (IEEE80211_IS_CHAN_A(c
))
194 ic
->ic_modecaps
|= 1<<IEEE80211_MODE_11A
;
195 if (IEEE80211_IS_CHAN_B(c
))
196 ic
->ic_modecaps
|= 1<<IEEE80211_MODE_11B
;
197 if (IEEE80211_IS_CHAN_PUREG(c
))
198 ic
->ic_modecaps
|= 1<<IEEE80211_MODE_11G
;
199 if (IEEE80211_IS_CHAN_FHSS(c
))
200 ic
->ic_modecaps
|= 1<<IEEE80211_MODE_FH
;
201 if (IEEE80211_IS_CHAN_T(c
))
202 ic
->ic_modecaps
|= 1<<IEEE80211_MODE_TURBO_A
;
203 if (IEEE80211_IS_CHAN_108G(c
))
204 ic
->ic_modecaps
|= 1<<IEEE80211_MODE_TURBO_G
;
205 if (ic
->ic_curchan
== NULL
) {
206 /* arbitrarily pick the first channel */
207 ic
->ic_curchan
= &ic
->ic_channels
[i
];
211 /* validate ic->ic_curmode */
212 if ((ic
->ic_modecaps
& (1<<ic
->ic_curmode
)) == 0)
213 ic
->ic_curmode
= IEEE80211_MODE_AUTO
;
214 ic
->ic_des_chan
= IEEE80211_CHAN_ANYC
; /* any channel is ok */
217 * Enable WME by default if we're capable.
219 if (ic
->ic_caps
& IEEE80211_C_WME
)
220 ic
->ic_flags
|= IEEE80211_F_WME
;
222 ieee80211_setbasicrates(ic
);
223 (void) ieee80211_setmode(ic
, ic
->ic_curmode
);
225 if (ic
->ic_bintval
== 0)
226 ic
->ic_bintval
= IEEE80211_BINTVAL_DEFAULT
;
227 ic
->ic_bmisstimeout
= 7*ic
->ic_bintval
; /* default 7 beacons */
228 ic
->ic_dtim_period
= IEEE80211_DTIM_DEFAULT
;
229 IEEE80211_BEACON_LOCK_INIT(ic
, "beacon");
231 if (ic
->ic_lintval
== 0)
232 ic
->ic_lintval
= ic
->ic_bintval
;
233 ic
->ic_txpowlimit
= IEEE80211_TXPOWER_MAX
;
235 LIST_INSERT_HEAD(&ieee80211com_head
, ic
, ic_list
);
236 ieee80211_node_attach(ic
);
237 ieee80211_proto_attach(ic
);
239 ieee80211_add_vap(ic
);
241 ieee80211_sysctl_attach(ic
); /* NB: requires ic_vap */
244 * Install a default reset method for the ioctl support.
245 * The driver is expected to fill this in before calling us.
247 if (ic
->ic_reset
== NULL
)
248 ic
->ic_reset
= ieee80211_default_reset
;
252 ieee80211_ifdetach(struct ieee80211com
*ic
)
254 struct ifnet
*ifp
= ic
->ic_ifp
;
256 ieee80211_remove_vap(ic
);
258 ieee80211_sysctl_detach(ic
);
259 ieee80211_proto_detach(ic
);
260 ieee80211_crypto_detach(ic
);
261 ieee80211_node_detach(ic
);
262 LIST_REMOVE(ic
, ic_list
);
263 ifmedia_delete_instance(&ic
->ic_media
, IFM_INST_ANY
);
265 IEEE80211_BEACON_LOCK_DESTROY(ic
);
274 * Convert MHz frequency to IEEE channel number.
277 ieee80211_mhz2ieee(u_int freq
, u_int flags
)
279 if (flags
& IEEE80211_CHAN_2GHZ
) { /* 2GHz band */
283 return (freq
- 2407) / 5;
285 return 15 + ((freq
- 2512) / 20);
286 } else if (flags
& IEEE80211_CHAN_5GHZ
) { /* 5 GHz band */
287 return (freq
- 5000) / 5;
288 } else { /* either, guess */
292 return (freq
- 2407) / 5;
294 return 15 + ((freq
- 2512) / 20);
295 return (freq
- 5000) / 5;
300 * Convert channel to IEEE channel number.
303 ieee80211_chan2ieee(struct ieee80211com
*ic
, struct ieee80211_channel
*c
)
305 if (ic
->ic_channels
<= c
&& c
<= &ic
->ic_channels
[IEEE80211_CHAN_MAX
])
306 return c
- ic
->ic_channels
;
307 else if (c
== IEEE80211_CHAN_ANYC
)
308 return IEEE80211_CHAN_ANY
;
309 else if (c
!= NULL
) {
310 if_printf(ic
->ic_ifp
, "invalid channel freq %u flags %x\n",
311 c
->ic_freq
, c
->ic_flags
);
314 if_printf(ic
->ic_ifp
, "invalid channel (NULL)\n");
320 * Convert IEEE channel number to MHz frequency.
323 ieee80211_ieee2mhz(u_int chan
, u_int flags
)
325 if (flags
& IEEE80211_CHAN_2GHZ
) { /* 2GHz band */
329 return 2407 + chan
*5;
331 return 2512 + ((chan
-15)*20);
332 } else if (flags
& IEEE80211_CHAN_5GHZ
) {/* 5 GHz band */
333 return 5000 + (chan
*5);
334 } else { /* either, guess */
337 if (chan
< 14) /* 0-13 */
338 return 2407 + chan
*5;
339 if (chan
< 27) /* 15-26 */
340 return 2512 + ((chan
-15)*20);
341 return 5000 + (chan
*5);
346 * Setup the media data structures according to the channel and
347 * rate tables. This must be called by the driver after
348 * ieee80211_attach and before most anything else.
351 ieee80211_media_init(struct ieee80211com
*ic
,
352 ifm_change_cb_t media_change
, ifm_stat_cb_t media_stat
)
354 #define ADD(_ic, _s, _o) \
355 ifmedia_add(&(_ic)->ic_media, \
356 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
357 struct ifnet
*ifp
= ic
->ic_ifp
;
358 struct ifmediareq imr
;
359 int i
, j
, mode
, rate
, maxrate
, mword
, mopt
, r
;
360 struct ieee80211_rateset
*rs
;
361 struct ieee80211_rateset allrates
;
364 * Do late attach work that must wait for any subclass
365 * (i.e. driver) work such as overriding methods.
367 ieee80211_node_lateattach(ic
);
369 #ifdef IEEE80211_NO_HOSTAP
370 ic
->ic_caps
&= ~IEEE80211_C_HOSTAP
;
371 #endif /* IEEE80211_NO_HOSTAP */
374 * Fill in media characteristics.
376 ifmedia_init(&ic
->ic_media
, 0, media_change
, media_stat
);
378 memset(&allrates
, 0, sizeof(allrates
));
379 for (mode
= IEEE80211_MODE_AUTO
; mode
< IEEE80211_MODE_MAX
; mode
++) {
380 static const u_int mopts
[] = {
386 IFM_IEEE80211_11A
| IFM_IEEE80211_TURBO
,
387 IFM_IEEE80211_11G
| IFM_IEEE80211_TURBO
,
389 if ((ic
->ic_modecaps
& (1<<mode
)) == 0)
392 ADD(ic
, IFM_AUTO
, mopt
); /* e.g. 11a auto */
393 if (ic
->ic_caps
& IEEE80211_C_IBSS
)
394 ADD(ic
, IFM_AUTO
, mopt
| IFM_IEEE80211_ADHOC
);
395 if (ic
->ic_caps
& IEEE80211_C_HOSTAP
)
396 ADD(ic
, IFM_AUTO
, mopt
| IFM_IEEE80211_HOSTAP
);
397 if (ic
->ic_caps
& IEEE80211_C_AHDEMO
)
398 ADD(ic
, IFM_AUTO
, mopt
| IFM_IEEE80211_ADHOC
| IFM_FLAG0
);
399 if (ic
->ic_caps
& IEEE80211_C_MONITOR
)
400 ADD(ic
, IFM_AUTO
, mopt
| IFM_IEEE80211_MONITOR
);
401 if (mode
== IEEE80211_MODE_AUTO
)
403 rs
= &ic
->ic_sup_rates
[mode
];
404 for (i
= 0; i
< rs
->rs_nrates
; i
++) {
405 rate
= rs
->rs_rates
[i
];
406 mword
= ieee80211_rate2media(ic
, rate
, mode
);
409 ADD(ic
, mword
, mopt
);
410 if (ic
->ic_caps
& IEEE80211_C_IBSS
)
411 ADD(ic
, mword
, mopt
| IFM_IEEE80211_ADHOC
);
412 if (ic
->ic_caps
& IEEE80211_C_HOSTAP
)
413 ADD(ic
, mword
, mopt
| IFM_IEEE80211_HOSTAP
);
414 if (ic
->ic_caps
& IEEE80211_C_AHDEMO
)
415 ADD(ic
, mword
, mopt
| IFM_IEEE80211_ADHOC
| IFM_FLAG0
);
416 if (ic
->ic_caps
& IEEE80211_C_MONITOR
)
417 ADD(ic
, mword
, mopt
| IFM_IEEE80211_MONITOR
);
419 * Add rate to the collection of all rates.
421 r
= rate
& IEEE80211_RATE_VAL
;
422 for (j
= 0; j
< allrates
.rs_nrates
; j
++)
423 if (allrates
.rs_rates
[j
] == r
)
425 if (j
== allrates
.rs_nrates
) {
426 /* unique, add to the set */
427 allrates
.rs_rates
[j
] = r
;
428 allrates
.rs_nrates
++;
430 rate
= (rate
& IEEE80211_RATE_VAL
) / 2;
435 for (i
= 0; i
< allrates
.rs_nrates
; i
++) {
436 mword
= ieee80211_rate2media(ic
, allrates
.rs_rates
[i
],
437 IEEE80211_MODE_AUTO
);
440 mword
= IFM_SUBTYPE(mword
); /* remove media options */
442 if (ic
->ic_caps
& IEEE80211_C_IBSS
)
443 ADD(ic
, mword
, IFM_IEEE80211_ADHOC
);
444 if (ic
->ic_caps
& IEEE80211_C_HOSTAP
)
445 ADD(ic
, mword
, IFM_IEEE80211_HOSTAP
);
446 if (ic
->ic_caps
& IEEE80211_C_AHDEMO
)
447 ADD(ic
, mword
, IFM_IEEE80211_ADHOC
| IFM_FLAG0
);
448 if (ic
->ic_caps
& IEEE80211_C_MONITOR
)
449 ADD(ic
, mword
, IFM_IEEE80211_MONITOR
);
451 ieee80211_media_status(ifp
, &imr
);
452 ifmedia_set(&ic
->ic_media
, imr
.ifm_active
);
455 ifp
->if_baudrate
= IF_Mbps(maxrate
);
460 ieee80211_announce(struct ieee80211com
*ic
)
462 struct ifnet
*ifp
= ic
->ic_ifp
;
463 int i
, mode
, rate
, mword
;
464 struct ieee80211_rateset
*rs
;
466 for (mode
= IEEE80211_MODE_11A
; mode
< IEEE80211_MODE_MAX
; mode
++) {
467 if ((ic
->ic_modecaps
& (1<<mode
)) == 0)
469 aprint_normal("%s: %s rates: ", ifp
->if_xname
,
470 ieee80211_phymode_name
[mode
]);
471 rs
= &ic
->ic_sup_rates
[mode
];
472 for (i
= 0; i
< rs
->rs_nrates
; i
++) {
473 rate
= rs
->rs_rates
[i
];
474 mword
= ieee80211_rate2media(ic
, rate
, mode
);
477 aprint_normal("%s%d%sMbps", (i
!= 0 ? " " : ""),
478 (rate
& IEEE80211_RATE_VAL
) / 2,
479 ((rate
& 0x1) != 0 ? ".5" : ""));
486 findrate(struct ieee80211com
*ic
, enum ieee80211_phymode mode
, int rate
)
488 #define IEEERATE(_ic,_m,_i) \
489 ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
490 int i
, nrates
= ic
->ic_sup_rates
[mode
].rs_nrates
;
491 for (i
= 0; i
< nrates
; i
++)
492 if (IEEERATE(ic
, mode
, i
) == rate
)
499 * Find an instance by it's mac address.
501 struct ieee80211com
*
502 ieee80211_find_vap(const u_int8_t mac
[IEEE80211_ADDR_LEN
])
505 struct ieee80211com
*ic
;
508 SLIST_FOREACH(ic
, &ieee80211_list
, ic_next
)
509 if (IEEE80211_ADDR_EQ(mac
, ic
->ic_myaddr
))
515 static struct ieee80211com
*
516 ieee80211_find_instance(struct ifnet
*ifp
)
519 struct ieee80211com
*ic
;
522 /* XXX not right for multiple instances but works for now */
523 SLIST_FOREACH(ic
, &ieee80211_list
, ic_next
)
524 if (ic
->ic_ifp
== ifp
)
531 * Handle a media change request.
534 ieee80211_media_change(struct ifnet
*ifp
)
536 struct ieee80211com
*ic
;
537 struct ifmedia_entry
*ime
;
538 enum ieee80211_opmode newopmode
;
539 enum ieee80211_phymode newphymode
;
540 int i
, j
, newrate
, error
= 0;
542 ic
= ieee80211_find_instance(ifp
);
544 if_printf(ifp
, "%s: no 802.11 instance!\n", __func__
);
547 ime
= ic
->ic_media
.ifm_cur
;
549 * First, identify the phy mode.
551 switch (IFM_MODE(ime
->ifm_media
)) {
552 case IFM_IEEE80211_11A
:
553 newphymode
= IEEE80211_MODE_11A
;
555 case IFM_IEEE80211_11B
:
556 newphymode
= IEEE80211_MODE_11B
;
558 case IFM_IEEE80211_11G
:
559 newphymode
= IEEE80211_MODE_11G
;
561 case IFM_IEEE80211_FH
:
562 newphymode
= IEEE80211_MODE_FH
;
565 newphymode
= IEEE80211_MODE_AUTO
;
571 * Turbo mode is an ``option''.
572 * XXX does not apply to AUTO
574 if (ime
->ifm_media
& IFM_IEEE80211_TURBO
) {
575 if (newphymode
== IEEE80211_MODE_11A
)
576 newphymode
= IEEE80211_MODE_TURBO_A
;
577 else if (newphymode
== IEEE80211_MODE_11G
)
578 newphymode
= IEEE80211_MODE_TURBO_G
;
583 * Validate requested mode is available.
585 if ((ic
->ic_modecaps
& (1<<newphymode
)) == 0)
589 * Next, the fixed/variable rate.
592 if (IFM_SUBTYPE(ime
->ifm_media
) != IFM_AUTO
) {
594 * Convert media subtype to rate.
596 newrate
= ieee80211_media2rate(ime
->ifm_media
);
600 * Check the rate table for the specified/current phy.
602 if (newphymode
== IEEE80211_MODE_AUTO
) {
604 * In autoselect mode search for the rate.
606 for (j
= IEEE80211_MODE_11A
;
607 j
< IEEE80211_MODE_MAX
; j
++) {
608 if ((ic
->ic_modecaps
& (1<<j
)) == 0)
610 i
= findrate(ic
, j
, newrate
);
618 i
= findrate(ic
, newphymode
, newrate
);
620 if (i
== -1) /* mode/rate mismatch */
623 /* NB: defer rate setting to later */
626 * Deduce new operating mode but don't install it just yet.
628 if ((ime
->ifm_media
& (IFM_IEEE80211_ADHOC
|IFM_FLAG0
)) ==
629 (IFM_IEEE80211_ADHOC
|IFM_FLAG0
))
630 newopmode
= IEEE80211_M_AHDEMO
;
631 else if (ime
->ifm_media
& IFM_IEEE80211_HOSTAP
)
632 newopmode
= IEEE80211_M_HOSTAP
;
633 else if (ime
->ifm_media
& IFM_IEEE80211_ADHOC
)
634 newopmode
= IEEE80211_M_IBSS
;
635 else if (ime
->ifm_media
& IFM_IEEE80211_MONITOR
)
636 newopmode
= IEEE80211_M_MONITOR
;
638 newopmode
= IEEE80211_M_STA
;
640 #ifndef IEEE80211_NO_HOSTAP
642 * Autoselect doesn't make sense when operating as an AP.
643 * If no phy mode has been selected, pick one and lock it
644 * down so rate tables can be used in forming beacon frames
647 if (newopmode
== IEEE80211_M_HOSTAP
&&
648 newphymode
== IEEE80211_MODE_AUTO
) {
649 for (j
= IEEE80211_MODE_11A
; j
< IEEE80211_MODE_MAX
; j
++)
650 if (ic
->ic_modecaps
& (1<<j
)) {
655 #endif /* !IEEE80211_NO_HOSTAP */
658 * Handle phy mode change.
660 if (ic
->ic_curmode
!= newphymode
) { /* change phy mode */
661 error
= ieee80211_setmode(ic
, newphymode
);
668 * Committed to changes, install the rate setting.
670 if (ic
->ic_fixed_rate
!= i
) {
671 ic
->ic_fixed_rate
= i
; /* set fixed tx rate */
676 * Handle operating mode change.
678 if (ic
->ic_opmode
!= newopmode
) {
679 ic
->ic_opmode
= newopmode
;
681 case IEEE80211_M_AHDEMO
:
682 case IEEE80211_M_HOSTAP
:
683 case IEEE80211_M_STA
:
684 case IEEE80211_M_MONITOR
:
685 ic
->ic_flags
&= ~IEEE80211_F_IBSSON
;
687 case IEEE80211_M_IBSS
:
688 ic
->ic_flags
|= IEEE80211_F_IBSSON
;
692 * Yech, slot time may change depending on the
693 * operating mode so reset it to be sure everything
694 * is setup appropriately.
696 ieee80211_reset_erp(ic
);
697 ieee80211_wme_initparams(ic
); /* after opmode change */
702 ifp
->if_baudrate
= ifmedia_baudrate(ime
->ifm_media
);
708 ieee80211_media_status(struct ifnet
*ifp
, struct ifmediareq
*imr
)
710 struct ieee80211com
*ic
;
711 struct ieee80211_rateset
*rs
;
713 ic
= ieee80211_find_instance(ifp
);
715 if_printf(ifp
, "%s: no 802.11 instance!\n", __func__
);
718 imr
->ifm_status
= IFM_AVALID
;
719 imr
->ifm_active
= IFM_IEEE80211
;
720 if (ic
->ic_state
== IEEE80211_S_RUN
)
721 imr
->ifm_status
|= IFM_ACTIVE
;
723 * Calculate a current rate if possible.
725 if (ic
->ic_fixed_rate
!= IEEE80211_FIXED_RATE_NONE
) {
727 * A fixed rate is set, report that.
729 rs
= &ic
->ic_sup_rates
[ic
->ic_curmode
];
730 imr
->ifm_active
|= ieee80211_rate2media(ic
,
731 rs
->rs_rates
[ic
->ic_fixed_rate
], ic
->ic_curmode
);
732 } else if (ic
->ic_opmode
== IEEE80211_M_STA
) {
734 * In station mode report the current transmit rate.
736 rs
= &ic
->ic_bss
->ni_rates
;
737 imr
->ifm_active
|= ieee80211_rate2media(ic
,
738 rs
->rs_rates
[ic
->ic_bss
->ni_txrate
], ic
->ic_curmode
);
740 imr
->ifm_active
|= IFM_AUTO
;
741 switch (ic
->ic_opmode
) {
742 case IEEE80211_M_STA
:
744 case IEEE80211_M_IBSS
:
745 imr
->ifm_active
|= IFM_IEEE80211_ADHOC
;
747 case IEEE80211_M_AHDEMO
:
748 /* should not come here */
750 case IEEE80211_M_HOSTAP
:
751 imr
->ifm_active
|= IFM_IEEE80211_HOSTAP
;
753 case IEEE80211_M_MONITOR
:
754 imr
->ifm_active
|= IFM_IEEE80211_MONITOR
;
757 switch (ic
->ic_curmode
) {
758 case IEEE80211_MODE_11A
:
759 imr
->ifm_active
|= IFM_IEEE80211_11A
;
761 case IEEE80211_MODE_11B
:
762 imr
->ifm_active
|= IFM_IEEE80211_11B
;
764 case IEEE80211_MODE_11G
:
765 imr
->ifm_active
|= IFM_IEEE80211_11G
;
767 case IEEE80211_MODE_FH
:
768 imr
->ifm_active
|= IFM_IEEE80211_FH
;
770 case IEEE80211_MODE_TURBO_A
:
771 imr
->ifm_active
|= IFM_IEEE80211_11A
772 | IFM_IEEE80211_TURBO
;
774 case IEEE80211_MODE_TURBO_G
:
775 imr
->ifm_active
|= IFM_IEEE80211_11G
776 | IFM_IEEE80211_TURBO
;
782 ieee80211_watchdog(struct ieee80211com
*ic
)
784 struct ieee80211_node_table
*nt
;
785 int need_inact_timer
= 0;
787 if (ic
->ic_state
!= IEEE80211_S_INIT
) {
788 if (ic
->ic_mgt_timer
&& --ic
->ic_mgt_timer
== 0)
789 ieee80211_new_state(ic
, IEEE80211_S_SCAN
, 0);
791 if (nt
->nt_inact_timer
) {
792 if (--nt
->nt_inact_timer
== 0)
794 need_inact_timer
+= nt
->nt_inact_timer
;
797 if (nt
->nt_inact_timer
) {
798 if (--nt
->nt_inact_timer
== 0)
800 need_inact_timer
+= nt
->nt_inact_timer
;
803 if (ic
->ic_mgt_timer
!= 0 || need_inact_timer
)
804 ic
->ic_ifp
->if_timer
= 1;
807 const struct ieee80211_rateset ieee80211_std_rateset_11a
=
808 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
810 const struct ieee80211_rateset ieee80211_std_rateset_11b
=
811 { 4, { 2, 4, 11, 22 } };
813 const struct ieee80211_rateset ieee80211_std_rateset_11g
=
814 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
817 * Mark the basic rates for the 11g rate table based on the
818 * operating mode. For real 11g we mark all the 11b rates
819 * and 6, 12, and 24 OFDM. For 11b compatibility we mark only
820 * 11b rates. There's also a pseudo 11a-mode used to mark only
821 * the basic OFDM rates.
824 ieee80211_setbasicrates(struct ieee80211com
*ic
)
826 static const struct ieee80211_rateset basic
[] = {
827 { 0, { } }, /* IEEE80211_MODE_AUTO */
828 { 3, { 12, 24, 48 } }, /* IEEE80211_MODE_11A */
829 { 2, { 2, 4 } }, /* IEEE80211_MODE_11B */
830 { 4, { 2, 4, 11, 22 } }, /* IEEE80211_MODE_11G */
831 { 0, { } }, /* IEEE80211_MODE_TURBO */
833 enum ieee80211_phymode mode
;
834 struct ieee80211_rateset
*rs
;
837 for (mode
= 0; mode
< IEEE80211_MODE_MAX
; mode
++) {
838 rs
= &ic
->ic_sup_rates
[mode
];
839 for (i
= 0; i
< rs
->rs_nrates
; i
++) {
840 rs
->rs_rates
[i
] &= IEEE80211_RATE_VAL
;
841 for (j
= 0; j
< basic
[mode
].rs_nrates
; j
++) {
842 if (basic
[mode
].rs_rates
[j
] != rs
->rs_rates
[i
])
844 rs
->rs_rates
[i
] |= IEEE80211_RATE_BASIC
;
852 * Set the current phy mode and recalculate the active channel
853 * set based on the available channels for this mode. Also
854 * select a new default/current channel if the current one is
855 * inappropriate for this mode.
858 ieee80211_setmode(struct ieee80211com
*ic
, enum ieee80211_phymode mode
)
860 #define N(a) (sizeof(a) / sizeof(a[0]))
861 static const u_int chanflags
[] = {
862 0, /* IEEE80211_MODE_AUTO */
863 IEEE80211_CHAN_A
, /* IEEE80211_MODE_11A */
864 IEEE80211_CHAN_B
, /* IEEE80211_MODE_11B */
865 IEEE80211_CHAN_PUREG
, /* IEEE80211_MODE_11G */
866 IEEE80211_CHAN_FHSS
, /* IEEE80211_MODE_FH */
867 IEEE80211_CHAN_T
, /* IEEE80211_MODE_TURBO_A */
868 IEEE80211_CHAN_108G
, /* IEEE80211_MODE_TURBO_G */
870 struct ieee80211_channel
*c
;
874 /* validate new mode */
875 if ((ic
->ic_modecaps
& (1<<mode
)) == 0) {
876 IEEE80211_DPRINTF(ic
, IEEE80211_MSG_ANY
,
877 "%s: mode %u not supported (caps 0x%x)\n",
878 __func__
, mode
, ic
->ic_modecaps
);
883 * Verify at least one channel is present in the available
884 * channel list before committing to the new mode.
886 IASSERT(mode
< N(chanflags
), ("Unexpected mode %u", mode
));
887 modeflags
= chanflags
[mode
];
888 for (i
= 0; i
<= IEEE80211_CHAN_MAX
; i
++) {
889 c
= &ic
->ic_channels
[i
];
890 if (c
->ic_flags
== 0)
892 if (mode
== IEEE80211_MODE_AUTO
) {
893 /* ignore turbo channels for autoselect */
894 if ((c
->ic_flags
& IEEE80211_CHAN_TURBO
) == 0)
897 if ((c
->ic_flags
& modeflags
) == modeflags
)
901 if (i
> IEEE80211_CHAN_MAX
) {
902 IEEE80211_DPRINTF(ic
, IEEE80211_MSG_ANY
,
903 "%s: no channels found for mode %u\n", __func__
, mode
);
908 * Calculate the active channel set.
910 memset(ic
->ic_chan_active
, 0, sizeof(ic
->ic_chan_active
));
911 for (i
= 0; i
<= IEEE80211_CHAN_MAX
; i
++) {
912 c
= &ic
->ic_channels
[i
];
913 if (c
->ic_flags
== 0)
915 if (mode
== IEEE80211_MODE_AUTO
) {
916 /* take anything but pure turbo channels */
917 if ((c
->ic_flags
& IEEE80211_CHAN_TURBO
) == 0)
918 setbit(ic
->ic_chan_active
, i
);
920 if ((c
->ic_flags
& modeflags
) == modeflags
)
921 setbit(ic
->ic_chan_active
, i
);
925 * If no current/default channel is setup or the current
926 * channel is wrong for the mode then pick the first
927 * available channel from the active list. This is likely
930 if (ic
->ic_ibss_chan
== NULL
||
931 isclr(ic
->ic_chan_active
, ieee80211_chan2ieee(ic
, ic
->ic_ibss_chan
))) {
932 for (i
= 0; i
<= IEEE80211_CHAN_MAX
; i
++)
933 if (isset(ic
->ic_chan_active
, i
)) {
934 ic
->ic_ibss_chan
= &ic
->ic_channels
[i
];
937 IASSERT(ic
->ic_ibss_chan
!= NULL
&&
938 isset(ic
->ic_chan_active
,
939 ieee80211_chan2ieee(ic
, ic
->ic_ibss_chan
)),
940 ("Bad IBSS channel %u",
941 ieee80211_chan2ieee(ic
, ic
->ic_ibss_chan
)));
944 * If the desired channel is set but no longer valid then reset it.
946 if (ic
->ic_des_chan
!= IEEE80211_CHAN_ANYC
&&
947 isclr(ic
->ic_chan_active
, ieee80211_chan2ieee(ic
, ic
->ic_des_chan
)))
948 ic
->ic_des_chan
= IEEE80211_CHAN_ANYC
;
951 * Do mode-specific rate setup.
953 if (mode
== IEEE80211_MODE_11G
) {
955 * Use a mixed 11b/11g rate set.
957 ieee80211_set11gbasicrates(&ic
->ic_sup_rates
[mode
],
959 } else if (mode
== IEEE80211_MODE_11B
) {
961 * Force pure 11b rate set.
963 ieee80211_set11gbasicrates(&ic
->ic_sup_rates
[mode
],
967 * Setup an initial rate set according to the
968 * current/default channel selected above. This
969 * will be changed when scanning but must exist
970 * now so driver have a consistent state of ic_ibss_chan.
972 if (ic
->ic_bss
) /* NB: can be called before lateattach */
973 ic
->ic_bss
->ni_rates
= ic
->ic_sup_rates
[mode
];
975 ic
->ic_curmode
= mode
;
976 ieee80211_reset_erp(ic
); /* reset ERP state */
977 ieee80211_wme_initparams(ic
); /* reset WME stat */
984 * Return the phy mode for with the specified channel so the
985 * caller can select a rate set. This is problematic for channels
986 * where multiple operating modes are possible (e.g. 11g+11b).
987 * In those cases we defer to the current operating mode when set.
989 enum ieee80211_phymode
990 ieee80211_chan2mode(struct ieee80211com
*ic
, struct ieee80211_channel
*chan
)
992 if (IEEE80211_IS_CHAN_T(chan
)) {
993 return IEEE80211_MODE_TURBO_A
;
994 } else if (IEEE80211_IS_CHAN_5GHZ(chan
)) {
995 return IEEE80211_MODE_11A
;
996 } else if (IEEE80211_IS_CHAN_FHSS(chan
))
997 return IEEE80211_MODE_FH
;
998 else if (chan
->ic_flags
& (IEEE80211_CHAN_OFDM
|IEEE80211_CHAN_DYN
)) {
1000 * This assumes all 11g channels are also usable
1001 * for 11b, which is currently true.
1003 if (ic
->ic_curmode
== IEEE80211_MODE_TURBO_G
)
1004 return IEEE80211_MODE_TURBO_G
;
1005 if (ic
->ic_curmode
== IEEE80211_MODE_11B
)
1006 return IEEE80211_MODE_11B
;
1007 return IEEE80211_MODE_11G
;
1009 return IEEE80211_MODE_11B
;
1013 * convert IEEE80211 rate value to ifmedia subtype.
1014 * ieee80211 rate is in unit of 0.5Mbps.
1017 ieee80211_rate2media(struct ieee80211com
*ic
, int rate
, enum ieee80211_phymode mode
)
1019 #define N(a) (sizeof(a) / sizeof(a[0]))
1020 static const struct {
1021 u_int m
; /* rate + mode */
1022 u_int r
; /* if_media rate */
1024 { 2 | IFM_IEEE80211_FH
, IFM_IEEE80211_FH1
},
1025 { 4 | IFM_IEEE80211_FH
, IFM_IEEE80211_FH2
},
1026 { 2 | IFM_IEEE80211_11B
, IFM_IEEE80211_DS1
},
1027 { 4 | IFM_IEEE80211_11B
, IFM_IEEE80211_DS2
},
1028 { 11 | IFM_IEEE80211_11B
, IFM_IEEE80211_DS5
},
1029 { 22 | IFM_IEEE80211_11B
, IFM_IEEE80211_DS11
},
1030 { 44 | IFM_IEEE80211_11B
, IFM_IEEE80211_DS22
},
1031 { 12 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM6
},
1032 { 18 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM9
},
1033 { 24 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM12
},
1034 { 36 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM18
},
1035 { 48 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM24
},
1036 { 72 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM36
},
1037 { 96 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM48
},
1038 { 108 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM54
},
1039 { 2 | IFM_IEEE80211_11G
, IFM_IEEE80211_DS1
},
1040 { 4 | IFM_IEEE80211_11G
, IFM_IEEE80211_DS2
},
1041 { 11 | IFM_IEEE80211_11G
, IFM_IEEE80211_DS5
},
1042 { 22 | IFM_IEEE80211_11G
, IFM_IEEE80211_DS11
},
1043 { 12 | IFM_IEEE80211_11G
, IFM_IEEE80211_OFDM6
},
1044 { 18 | IFM_IEEE80211_11G
, IFM_IEEE80211_OFDM9
},
1045 { 24 | IFM_IEEE80211_11G
, IFM_IEEE80211_OFDM12
},
1046 { 36 | IFM_IEEE80211_11G
, IFM_IEEE80211_OFDM18
},
1047 { 48 | IFM_IEEE80211_11G
, IFM_IEEE80211_OFDM24
},
1048 { 72 | IFM_IEEE80211_11G
, IFM_IEEE80211_OFDM36
},
1049 { 96 | IFM_IEEE80211_11G
, IFM_IEEE80211_OFDM48
},
1050 { 108 | IFM_IEEE80211_11G
, IFM_IEEE80211_OFDM54
},
1051 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1055 mask
= rate
& IEEE80211_RATE_VAL
;
1057 case IEEE80211_MODE_11A
:
1058 case IEEE80211_MODE_TURBO_A
:
1059 mask
|= IFM_IEEE80211_11A
;
1061 case IEEE80211_MODE_11B
:
1062 mask
|= IFM_IEEE80211_11B
;
1064 case IEEE80211_MODE_FH
:
1065 mask
|= IFM_IEEE80211_FH
;
1067 case IEEE80211_MODE_AUTO
:
1068 /* NB: ic may be NULL for some drivers */
1069 if (ic
&& ic
->ic_phytype
== IEEE80211_T_FH
) {
1070 mask
|= IFM_IEEE80211_FH
;
1073 /* NB: hack, 11g matches both 11b+11a rates */
1075 case IEEE80211_MODE_11G
:
1076 case IEEE80211_MODE_TURBO_G
:
1077 mask
|= IFM_IEEE80211_11G
;
1080 for (i
= 0; i
< N(rates
); i
++)
1081 if (rates
[i
].m
== mask
)
1088 ieee80211_media2rate(int mword
)
1090 #define N(a) (sizeof(a) / sizeof(a[0]))
1091 static const int ieeerates
[] = {
1095 2, /* IFM_IEEE80211_FH1 */
1096 4, /* IFM_IEEE80211_FH2 */
1097 4, /* IFM_IEEE80211_DS2 */
1098 11, /* IFM_IEEE80211_DS5 */
1099 22, /* IFM_IEEE80211_DS11 */
1100 2, /* IFM_IEEE80211_DS1 */
1101 44, /* IFM_IEEE80211_DS22 */
1102 12, /* IFM_IEEE80211_OFDM6 */
1103 18, /* IFM_IEEE80211_OFDM9 */
1104 24, /* IFM_IEEE80211_OFDM12 */
1105 36, /* IFM_IEEE80211_OFDM18 */
1106 48, /* IFM_IEEE80211_OFDM24 */
1107 72, /* IFM_IEEE80211_OFDM36 */
1108 96, /* IFM_IEEE80211_OFDM48 */
1109 108, /* IFM_IEEE80211_OFDM54 */
1110 144, /* IFM_IEEE80211_OFDM72 */
1112 return IFM_SUBTYPE(mword
) < N(ieeerates
) ?
1113 ieeerates
[IFM_SUBTYPE(mword
)] : 0;