1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
/*-
* Copyright (c) 2005-2008 Sam Leffler, Errno Consulting
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*
* IEEE 802.11 regdomain support.
*/
#include "opt_wlan.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_media.h>
#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_regdomain.h>
static void
null_getradiocaps(struct ieee80211com *ic, int *n, struct ieee80211_channel *c)
{
/* just feed back the current channel list */
*n = ic->ic_nchans;
memcpy(c, ic->ic_channels,
ic->ic_nchans*sizeof(struct ieee80211_channel));
}
static int
null_setregdomain(struct ieee80211com *ic,
struct ieee80211_regdomain *rd,
int nchans, struct ieee80211_channel chans[])
{
return 0; /* accept anything */
}
void
ieee80211_regdomain_attach(struct ieee80211com *ic)
{
if (ic->ic_regdomain.regdomain == 0 &&
ic->ic_regdomain.country == CTRY_DEFAULT) {
ic->ic_regdomain.country = CTRY_UNITED_STATES; /* XXX */
ic->ic_regdomain.location = ' '; /* both */
ic->ic_regdomain.isocc[0] = 'U'; /* XXX */
ic->ic_regdomain.isocc[1] = 'S'; /* XXX */
/* XXX? too late to setup default channel list */
}
ic->ic_getradiocaps = null_getradiocaps;
ic->ic_setregdomain = null_setregdomain;
}
void
ieee80211_regdomain_detach(struct ieee80211com *ic)
{
if (ic->ic_countryie != NULL) {
free(ic->ic_countryie, M_80211_NODE_IE);
ic->ic_countryie = NULL;
}
}
void
ieee80211_regdomain_vattach(struct ieee80211vap *vap)
{
}
void
ieee80211_regdomain_vdetach(struct ieee80211vap *vap)
{
}
static void
addchan(struct ieee80211com *ic, int ieee, int flags)
{
struct ieee80211_channel *c;
c = &ic->ic_channels[ic->ic_nchans++];
c->ic_freq = ieee80211_ieee2mhz(ieee, flags);
c->ic_ieee = ieee;
c->ic_flags = flags;
c->ic_extieee = 0;
}
/*
* Setup the channel list for the specified regulatory domain,
* country code, and operating modes. This interface is used
* when a driver does not obtain the channel list from another
* source (such as firmware).
*/
int
ieee80211_init_channels(struct ieee80211com *ic,
const struct ieee80211_regdomain *rd, const uint8_t bands[])
{
int i;
/* XXX just do something for now */
ic->ic_nchans = 0;
if (isset(bands, IEEE80211_MODE_11B) ||
isset(bands, IEEE80211_MODE_11G)) {
int maxchan = 11;
if (rd != NULL && rd->ecm)
maxchan = 14;
for (i = 1; i <= maxchan; i++) {
if (isset(bands, IEEE80211_MODE_11B))
addchan(ic, i, IEEE80211_CHAN_B);
if (isset(bands, IEEE80211_MODE_11G))
addchan(ic, i, IEEE80211_CHAN_G);
}
}
if (isset(bands, IEEE80211_MODE_11A)) {
for (i = 36; i <= 64; i += 4)
addchan(ic, i, IEEE80211_CHAN_A);
for (i = 100; i <= 140; i += 4)
addchan(ic, i, IEEE80211_CHAN_A);
for (i = 149; i <= 161; i += 4)
addchan(ic, i, IEEE80211_CHAN_A);
}
if (rd != NULL)
ic->ic_regdomain = *rd;
return 0;
}
static __inline int
chancompar(const void *a, const void *b)
{
const struct ieee80211_channel *ca = a;
const struct ieee80211_channel *cb = b;
return (ca->ic_freq == cb->ic_freq) ?
(ca->ic_flags & IEEE80211_CHAN_ALL) -
(cb->ic_flags & IEEE80211_CHAN_ALL) :
ca->ic_freq - cb->ic_freq;
}
/*
* Insertion sort.
*/
#define swap(_a, _b, _size) { \
uint8_t *s = _b; \
int i = _size; \
do { \
uint8_t tmp = *_a; \
*_a++ = *s; \
*s++ = tmp; \
} while (--i); \
_a -= _size; \
}
static void
sort_channels(void *a, size_t n, size_t size)
{
uint8_t *aa = a;
uint8_t *ai, *t;
KASSERT(n > 0, ("no channels"));
for (ai = aa+size; --n >= 1; ai += size)
for (t = ai; t > aa; t -= size) {
uint8_t *u = t - size;
if (chancompar(u, t) <= 0)
break;
swap(u, t, size);
}
}
#undef swap
/*
* Order channels w/ the same frequency so that
* b < g < htg and a < hta. This is used to optimize
* channel table lookups and some user applications
* may also depend on it (though they should not).
*/
void
ieee80211_sort_channels(struct ieee80211_channel chans[], int nchans)
{
if (nchans > 0)
sort_channels(chans, nchans, sizeof(struct ieee80211_channel));
}
/*
* Allocate and construct a Country Information IE.
*/
struct ieee80211_appie *
ieee80211_alloc_countryie(struct ieee80211com *ic)
{
#define CHAN_UNINTERESTING \
(IEEE80211_CHAN_TURBO | IEEE80211_CHAN_STURBO | \
IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)
/* XXX what about auto? */
/* flag set of channels to be excluded */
static const int skipflags[IEEE80211_MODE_MAX] = {
CHAN_UNINTERESTING, /* MODE_AUTO */
CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_11A */
CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_11B */
CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_11G */
CHAN_UNINTERESTING | IEEE80211_CHAN_OFDM | /* MODE_FH */
IEEE80211_CHAN_CCK | IEEE80211_CHAN_DYN,
CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_TURBO_A */
CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_TURBO_G */
CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_STURBO_A */
CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_11NA */
CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_11NG */
};
const struct ieee80211_regdomain *rd = &ic->ic_regdomain;
uint8_t nextchan, chans[IEEE80211_CHAN_BYTES], *frm;
struct ieee80211_appie *aie;
struct ieee80211_country_ie *ie;
int i, skip, nruns;
aie = malloc(IEEE80211_COUNTRY_MAX_SIZE, M_80211_NODE_IE,
M_NOWAIT | M_ZERO);
if (aie == NULL) {
if_printf(ic->ic_ifp,
"%s: unable to allocate memory for country ie\n", __func__);
/* XXX stat */
return NULL;
}
ie = (struct ieee80211_country_ie *) aie->ie_data;
ie->ie = IEEE80211_ELEMID_COUNTRY;
if (rd->isocc[0] == '\0') {
if_printf(ic->ic_ifp, "no ISO country string for cc %d; "
"using blanks\n", rd->country);
ie->cc[0] = ie->cc[1] = ' ';
} else {
ie->cc[0] = rd->isocc[0];
ie->cc[1] = rd->isocc[1];
}
/*
* Indoor/Outdoor portion of country string:
* 'I' indoor only
* 'O' outdoor only
* ' ' all enviroments
*/
ie->cc[2] = (rd->location == 'I' ? 'I' :
rd->location == 'O' ? 'O' : ' ');
/*
* Run-length encoded channel+max tx power info.
*/
frm = (uint8_t *)&ie->band[0];
nextchan = 0; /* NB: impossible channel # */
nruns = 0;
memset(chans, 0, sizeof(chans));
skip = skipflags[ieee80211_chan2mode(ic->ic_bsschan)];
for (i = 0; i < ic->ic_nchans; i++) {
const struct ieee80211_channel *c = &ic->ic_channels[i];
if (isset(chans, c->ic_ieee)) /* suppress dup's */
continue;
if (c->ic_flags & skip) /* skip band, etc. */
continue;
setbit(chans, c->ic_ieee);
if (c->ic_ieee != nextchan ||
c->ic_maxregpower != frm[-1]) { /* new run */
if (nruns == IEEE80211_COUNTRY_MAX_BANDS) {
if_printf(ic->ic_ifp, "%s: country ie too big, "
"runs > max %d, truncating\n",
__func__, IEEE80211_COUNTRY_MAX_BANDS);
/* XXX stat? fail? */
break;
}
frm[0] = c->ic_ieee; /* starting channel # */
frm[1] = 1; /* # channels in run */
frm[2] = c->ic_maxregpower; /* tx power cap */
frm += 3;
nextchan = c->ic_ieee + 1; /* overflow? */
nruns++;
} else { /* extend run */
frm[-2]++;
nextchan++;
}
}
ie->len = frm - ie->cc;
if (ie->len & 1) { /* Zero pad to multiple of 2 */
ie->len++;
*frm++ = 0;
}
aie->ie_len = frm - aie->ie_data;
return aie;
#undef CHAN_UNINTERESTING
}
static int
allvapsdown(struct ieee80211com *ic)
{
struct ieee80211vap *vap;
IEEE80211_LOCK_ASSERT(ic);
TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
if (vap->iv_state != IEEE80211_S_INIT)
return 0;
return 1;
}
int
ieee80211_setregdomain(struct ieee80211vap *vap,
struct ieee80211_regdomain_req *reg)
{
struct ieee80211com *ic = vap->iv_ic;
struct ieee80211_channel *c;
int desfreq = 0, desflags = 0; /* XXX silence gcc complaint */
int error, i;
if (reg->rd.location != 'I' && reg->rd.location != 'O' &&
reg->rd.location != ' ')
return EINVAL;
if (reg->rd.isocc[0] == '\0' || reg->rd.isocc[1] == '\0')
return EINVAL;
if (reg->chaninfo.ic_nchans >= IEEE80211_CHAN_MAX)
return EINVAL;
/*
* Calculate freq<->IEEE mapping and default max tx power
* for channels not setup. The driver can override these
* setting to reflect device properties/requirements.
*/
for (i = 0; i < reg->chaninfo.ic_nchans; i++) {
c = ®->chaninfo.ic_chans[i];
if (c->ic_freq == 0 || c->ic_flags == 0)
return EINVAL;
if (c->ic_maxregpower == 0)
return EINVAL;
if (c->ic_ieee == 0)
c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
(IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
c->ic_flags);
if (c->ic_maxpower == 0)
c->ic_maxpower = 2*c->ic_maxregpower;
}
IEEE80211_LOCK(ic);
error = ic->ic_setregdomain(ic, ®->rd,
reg->chaninfo.ic_nchans, reg->chaninfo.ic_chans);
if (error != 0) {
IEEE80211_UNLOCK(ic);
return error;
}
/* XXX bandaid; a running vap will likely crash */
if (!allvapsdown(ic)) {
IEEE80211_UNLOCK(ic);
return EBUSY;
}
/*
* Commit: copy in new channel table and reset media state.
* On return the state machines will be clocked so all vaps
* will reset their state.
*
* XXX ic_bsschan is marked undefined, must have vap's in
* INIT state or we blow up forcing stations off
*/
/*
* Save any desired channel for restore below. Note this
* needs to be done for all vaps but for now we only do
* the one where the ioctl is issued.
*/
if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
desfreq = vap->iv_des_chan->ic_freq;
desflags = vap->iv_des_chan->ic_flags;
}
/* regdomain parameters */
memcpy(&ic->ic_regdomain, ®->rd, sizeof(reg->rd));
/* channel table */
memcpy(ic->ic_channels, reg->chaninfo.ic_chans,
reg->chaninfo.ic_nchans * sizeof(struct ieee80211_channel));
ic->ic_nchans = reg->chaninfo.ic_nchans;
memset(&ic->ic_channels[ic->ic_nchans], 0,
(IEEE80211_CHAN_MAX - ic->ic_nchans) *
sizeof(struct ieee80211_channel));
ieee80211_media_init(ic);
/*
* Invalidate channel-related state.
*/
if (ic->ic_countryie != NULL) {
free(ic->ic_countryie, M_80211_NODE_IE);
ic->ic_countryie = NULL;
}
ieee80211_scan_flush(vap);
ieee80211_dfs_reset(ic);
if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
/* NB: may be NULL if not present in new channel list */
vap->iv_des_chan = ieee80211_find_channel(ic, desfreq, desflags);
}
IEEE80211_UNLOCK(ic);
return 0;
}
|