2 * Copyright (c) 2005-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 * IEEE 802.11 regdomain support.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/socket.h>
41 #include <net/if_media.h>
43 #include <net80211/ieee80211_var.h>
44 #include <net80211/ieee80211_regdomain.h>
47 null_getradiocaps(struct ieee80211com
*ic
, int maxchan
,
48 int *n
, struct ieee80211_channel
*c
)
50 /* just feed back the current channel list */
51 if (maxchan
> ic
->ic_nchans
)
52 maxchan
= ic
->ic_nchans
;
53 memcpy(c
, ic
->ic_channels
, maxchan
*sizeof(struct ieee80211_channel
));
58 null_setregdomain(struct ieee80211com
*ic
,
59 struct ieee80211_regdomain
*rd
,
60 int nchans
, struct ieee80211_channel chans
[])
62 return 0; /* accept anything */
66 ieee80211_regdomain_attach(struct ieee80211com
*ic
)
68 if (ic
->ic_regdomain
.regdomain
== 0 &&
69 ic
->ic_regdomain
.country
== CTRY_DEFAULT
) {
70 ic
->ic_regdomain
.country
= CTRY_UNITED_STATES
; /* XXX */
71 ic
->ic_regdomain
.location
= ' '; /* both */
72 ic
->ic_regdomain
.isocc
[0] = 'U'; /* XXX */
73 ic
->ic_regdomain
.isocc
[1] = 'S'; /* XXX */
74 /* NB: driver calls ieee80211_init_channels or similar */
76 ic
->ic_getradiocaps
= null_getradiocaps
;
77 ic
->ic_setregdomain
= null_setregdomain
;
81 ieee80211_regdomain_detach(struct ieee80211com
*ic
)
83 if (ic
->ic_countryie
!= NULL
) {
84 free(ic
->ic_countryie
, M_80211_NODE_IE
);
85 ic
->ic_countryie
= NULL
;
90 ieee80211_regdomain_vattach(struct ieee80211vap
*vap
)
95 ieee80211_regdomain_vdetach(struct ieee80211vap
*vap
)
100 addchan(struct ieee80211com
*ic
, int ieee
, int flags
)
102 struct ieee80211_channel
*c
;
104 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
105 c
->ic_freq
= ieee80211_ieee2mhz(ieee
, flags
);
112 * Setup the channel list for the specified regulatory domain,
113 * country code, and operating modes. This interface is used
114 * when a driver does not obtain the channel list from another
115 * source (such as firmware).
118 ieee80211_init_channels(struct ieee80211com
*ic
,
119 const struct ieee80211_regdomain
*rd
, const uint8_t bands
[])
123 /* XXX just do something for now */
125 if (isset(bands
, IEEE80211_MODE_11B
) ||
126 isset(bands
, IEEE80211_MODE_11G
)) {
128 if (rd
!= NULL
&& rd
->ecm
)
130 for (i
= 1; i
<= maxchan
; i
++) {
131 if (isset(bands
, IEEE80211_MODE_11B
))
132 addchan(ic
, i
, IEEE80211_CHAN_B
);
133 if (isset(bands
, IEEE80211_MODE_11G
))
134 addchan(ic
, i
, IEEE80211_CHAN_G
);
137 if (isset(bands
, IEEE80211_MODE_11A
)) {
138 for (i
= 36; i
<= 64; i
+= 4)
139 addchan(ic
, i
, IEEE80211_CHAN_A
);
140 for (i
= 100; i
<= 140; i
+= 4)
141 addchan(ic
, i
, IEEE80211_CHAN_A
);
142 for (i
= 149; i
<= 161; i
+= 4)
143 addchan(ic
, i
, IEEE80211_CHAN_A
);
146 ic
->ic_regdomain
= *rd
;
152 chancompar(const void *a
, const void *b
)
154 const struct ieee80211_channel
*ca
= a
;
155 const struct ieee80211_channel
*cb
= b
;
157 return (ca
->ic_freq
== cb
->ic_freq
) ?
158 (ca
->ic_flags
& IEEE80211_CHAN_ALL
) -
159 (cb
->ic_flags
& IEEE80211_CHAN_ALL
) :
160 ca
->ic_freq
- cb
->ic_freq
;
166 #define swap(_a, _b, _size) { \
178 sort_channels(void *a
, size_t n
, size_t size
)
183 KASSERT(n
> 0, ("no channels"));
184 for (ai
= aa
+size
; --n
>= 1; ai
+= size
)
185 for (t
= ai
; t
> aa
; t
-= size
) {
186 uint8_t *u
= t
- size
;
187 if (chancompar(u
, t
) <= 0)
195 * Order channels w/ the same frequency so that
196 * b < g < htg and a < hta. This is used to optimize
197 * channel table lookups and some user applications
198 * may also depend on it (though they should not).
201 ieee80211_sort_channels(struct ieee80211_channel chans
[], int nchans
)
204 sort_channels(chans
, nchans
, sizeof(struct ieee80211_channel
));
208 * Allocate and construct a Country Information IE.
210 struct ieee80211_appie
*
211 ieee80211_alloc_countryie(struct ieee80211com
*ic
)
213 #define CHAN_UNINTERESTING \
214 (IEEE80211_CHAN_TURBO | IEEE80211_CHAN_STURBO | \
215 IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)
216 /* XXX what about auto? */
217 /* flag set of channels to be excluded (band added below) */
218 static const int skipflags
[IEEE80211_MODE_MAX
] = {
219 [IEEE80211_MODE_AUTO
] = CHAN_UNINTERESTING
,
220 [IEEE80211_MODE_11A
] = CHAN_UNINTERESTING
,
221 [IEEE80211_MODE_11B
] = CHAN_UNINTERESTING
,
222 [IEEE80211_MODE_11G
] = CHAN_UNINTERESTING
,
223 [IEEE80211_MODE_FH
] = CHAN_UNINTERESTING
224 | IEEE80211_CHAN_OFDM
226 | IEEE80211_CHAN_DYN
,
227 [IEEE80211_MODE_TURBO_A
] = CHAN_UNINTERESTING
,
228 [IEEE80211_MODE_TURBO_G
] = CHAN_UNINTERESTING
,
229 [IEEE80211_MODE_STURBO_A
] = CHAN_UNINTERESTING
,
230 [IEEE80211_MODE_HALF
] = IEEE80211_CHAN_TURBO
231 | IEEE80211_CHAN_STURBO
,
232 [IEEE80211_MODE_QUARTER
] = IEEE80211_CHAN_TURBO
233 | IEEE80211_CHAN_STURBO
,
234 [IEEE80211_MODE_11NA
] = CHAN_UNINTERESTING
,
235 [IEEE80211_MODE_11NG
] = CHAN_UNINTERESTING
,
237 const struct ieee80211_regdomain
*rd
= &ic
->ic_regdomain
;
238 uint8_t nextchan
, chans
[IEEE80211_CHAN_BYTES
], *frm
;
239 struct ieee80211_appie
*aie
;
240 struct ieee80211_country_ie
*ie
;
243 aie
= malloc(IEEE80211_COUNTRY_MAX_SIZE
, M_80211_NODE_IE
,
246 if_printf(ic
->ic_ifp
,
247 "%s: unable to allocate memory for country ie\n", __func__
);
251 ie
= (struct ieee80211_country_ie
*) aie
->ie_data
;
252 ie
->ie
= IEEE80211_ELEMID_COUNTRY
;
253 if (rd
->isocc
[0] == '\0') {
254 if_printf(ic
->ic_ifp
, "no ISO country string for cc %d; "
255 "using blanks\n", rd
->country
);
256 ie
->cc
[0] = ie
->cc
[1] = ' ';
258 ie
->cc
[0] = rd
->isocc
[0];
259 ie
->cc
[1] = rd
->isocc
[1];
262 * Indoor/Outdoor portion of country string:
265 * ' ' all enviroments
267 ie
->cc
[2] = (rd
->location
== 'I' ? 'I' :
268 rd
->location
== 'O' ? 'O' : ' ');
270 * Run-length encoded channel+max tx power info.
272 frm
= (uint8_t *)&ie
->band
[0];
273 nextchan
= 0; /* NB: impossible channel # */
275 memset(chans
, 0, sizeof(chans
));
276 skip
= skipflags
[ieee80211_chan2mode(ic
->ic_bsschan
)];
277 if (IEEE80211_IS_CHAN_5GHZ(ic
->ic_bsschan
))
278 skip
|= IEEE80211_CHAN_2GHZ
;
279 else if (IEEE80211_IS_CHAN_2GHZ(ic
->ic_bsschan
))
280 skip
|= IEEE80211_CHAN_5GHZ
;
281 for (i
= 0; i
< ic
->ic_nchans
; i
++) {
282 const struct ieee80211_channel
*c
= &ic
->ic_channels
[i
];
284 if (isset(chans
, c
->ic_ieee
)) /* suppress dup's */
286 if (c
->ic_flags
& skip
) /* skip band, etc. */
288 setbit(chans
, c
->ic_ieee
);
289 if (c
->ic_ieee
!= nextchan
||
290 c
->ic_maxregpower
!= frm
[-1]) { /* new run */
291 if (nruns
== IEEE80211_COUNTRY_MAX_BANDS
) {
292 if_printf(ic
->ic_ifp
, "%s: country ie too big, "
293 "runs > max %d, truncating\n",
294 __func__
, IEEE80211_COUNTRY_MAX_BANDS
);
295 /* XXX stat? fail? */
298 frm
[0] = c
->ic_ieee
; /* starting channel # */
299 frm
[1] = 1; /* # channels in run */
300 frm
[2] = c
->ic_maxregpower
; /* tx power cap */
302 nextchan
= c
->ic_ieee
+ 1; /* overflow? */
304 } else { /* extend run */
309 ie
->len
= frm
- ie
->cc
;
310 if (ie
->len
& 1) { /* Zero pad to multiple of 2 */
314 aie
->ie_len
= frm
- aie
->ie_data
;
317 #undef CHAN_UNINTERESTING
321 allvapsdown(struct ieee80211com
*ic
)
323 struct ieee80211vap
*vap
;
325 IEEE80211_LOCK_ASSERT(ic
);
326 TAILQ_FOREACH(vap
, &ic
->ic_vaps
, iv_next
)
327 if (vap
->iv_state
!= IEEE80211_S_INIT
)
333 ieee80211_setregdomain(struct ieee80211vap
*vap
,
334 struct ieee80211_regdomain_req
*reg
)
336 struct ieee80211com
*ic
= vap
->iv_ic
;
337 struct ieee80211_channel
*c
;
338 int desfreq
= 0, desflags
= 0; /* XXX silence gcc complaint */
341 if (reg
->rd
.location
!= 'I' && reg
->rd
.location
!= 'O' &&
342 reg
->rd
.location
!= ' ') {
343 IEEE80211_DPRINTF(vap
, IEEE80211_MSG_IOCTL
,
344 "%s: invalid location 0x%x\n", __func__
, reg
->rd
.location
);
347 if (reg
->rd
.isocc
[0] == '\0' || reg
->rd
.isocc
[1] == '\0') {
348 IEEE80211_DPRINTF(vap
, IEEE80211_MSG_IOCTL
,
349 "%s: invalid iso cc 0x%x:0x%x\n", __func__
,
350 reg
->rd
.isocc
[0], reg
->rd
.isocc
[1]);
353 if (reg
->chaninfo
.ic_nchans
> IEEE80211_CHAN_MAX
) {
354 IEEE80211_DPRINTF(vap
, IEEE80211_MSG_IOCTL
,
355 "%s: too many channels %u, max %u\n", __func__
,
356 reg
->chaninfo
.ic_nchans
, IEEE80211_CHAN_MAX
);
360 * Calculate freq<->IEEE mapping and default max tx power
361 * for channels not setup. The driver can override these
362 * setting to reflect device properties/requirements.
364 for (i
= 0; i
< reg
->chaninfo
.ic_nchans
; i
++) {
365 c
= ®
->chaninfo
.ic_chans
[i
];
366 if (c
->ic_freq
== 0 || c
->ic_flags
== 0) {
367 IEEE80211_DPRINTF(vap
, IEEE80211_MSG_IOCTL
,
368 "%s: invalid channel spec at [%u]\n", __func__
, i
);
371 if (c
->ic_maxregpower
== 0) {
372 IEEE80211_DPRINTF(vap
, IEEE80211_MSG_IOCTL
,
373 "%s: invalid channel spec, zero maxregpower, "
374 "freq %u flags 0x%x\n", __func__
,
375 c
->ic_freq
, c
->ic_flags
);
379 c
->ic_ieee
= ieee80211_mhz2ieee(c
->ic_freq
,c
->ic_flags
);
380 if (IEEE80211_IS_CHAN_HT40(c
) && c
->ic_extieee
== 0)
381 c
->ic_extieee
= ieee80211_mhz2ieee(c
->ic_freq
+
382 (IEEE80211_IS_CHAN_HT40U(c
) ? 20 : -20),
384 if (c
->ic_maxpower
== 0)
385 c
->ic_maxpower
= 2*c
->ic_maxregpower
;
388 /* XXX bandaid; a running vap will likely crash */
389 if (!allvapsdown(ic
)) {
390 IEEE80211_UNLOCK(ic
);
391 IEEE80211_DPRINTF(vap
, IEEE80211_MSG_IOCTL
,
392 "%s: reject: vaps are running\n", __func__
);
395 error
= ic
->ic_setregdomain(ic
, ®
->rd
,
396 reg
->chaninfo
.ic_nchans
, reg
->chaninfo
.ic_chans
);
398 IEEE80211_UNLOCK(ic
);
399 IEEE80211_DPRINTF(vap
, IEEE80211_MSG_IOCTL
,
400 "%s: driver rejected request, error %u\n", __func__
, error
);
404 * Commit: copy in new channel table and reset media state.
405 * On return the state machines will be clocked so all vaps
406 * will reset their state.
408 * XXX ic_bsschan is marked undefined, must have vap's in
409 * INIT state or we blow up forcing stations off
412 * Save any desired channel for restore below. Note this
413 * needs to be done for all vaps but for now we only do
414 * the one where the ioctl is issued.
416 if (vap
->iv_des_chan
!= IEEE80211_CHAN_ANYC
) {
417 desfreq
= vap
->iv_des_chan
->ic_freq
;
418 desflags
= vap
->iv_des_chan
->ic_flags
;
420 /* regdomain parameters */
421 memcpy(&ic
->ic_regdomain
, ®
->rd
, sizeof(reg
->rd
));
423 memcpy(ic
->ic_channels
, reg
->chaninfo
.ic_chans
,
424 reg
->chaninfo
.ic_nchans
* sizeof(struct ieee80211_channel
));
425 ic
->ic_nchans
= reg
->chaninfo
.ic_nchans
;
426 memset(&ic
->ic_channels
[ic
->ic_nchans
], 0,
427 (IEEE80211_CHAN_MAX
- ic
->ic_nchans
) *
428 sizeof(struct ieee80211_channel
));
429 ieee80211_media_init(ic
);
432 * Invalidate channel-related state.
434 if (ic
->ic_countryie
!= NULL
) {
435 free(ic
->ic_countryie
, M_80211_NODE_IE
);
436 ic
->ic_countryie
= NULL
;
438 ieee80211_scan_flush(vap
);
439 ieee80211_dfs_reset(ic
);
440 if (vap
->iv_des_chan
!= IEEE80211_CHAN_ANYC
) {
441 c
= ieee80211_find_channel(ic
, desfreq
, desflags
);
442 /* NB: may be NULL if not present in new channel list */
443 vap
->iv_des_chan
= (c
!= NULL
) ? c
: IEEE80211_CHAN_ANYC
;
445 IEEE80211_UNLOCK(ic
);