1 /*-
2 * Copyright (c) 2005-2008 Sam Leffler, Errno Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
24 */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD: releng/10.4/sys/net80211/ieee80211_regdomain.c 233845 2012-04-03 17:48:42Z bschmidt $");
28
29 /*
30 * IEEE 802.11 regdomain support.
31 */
32 #include "opt_wlan.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37
38 #include <sys/socket.h>
39
40 #include <net/if.h>
41 #include <net/if_media.h>
42
43 #include <net80211/ieee80211_var.h>
44 #include <net80211/ieee80211_regdomain.h>
45
46 static void
47 null_getradiocaps(struct ieee80211com *ic, int maxchan,
48 int *n, struct ieee80211_channel *c)
49 {
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));
54 *n = maxchan;
55 }
56
57 static int
58 null_setregdomain(struct ieee80211com *ic,
59 struct ieee80211_regdomain *rd,
60 int nchans, struct ieee80211_channel chans[])
61 {
62 return 0; /* accept anything */
63 }
64
65 void
66 ieee80211_regdomain_attach(struct ieee80211com *ic)
67 {
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 */
75 }
76 ic->ic_getradiocaps = null_getradiocaps;
77 ic->ic_setregdomain = null_setregdomain;
78 }
79
80 void
81 ieee80211_regdomain_detach(struct ieee80211com *ic)
82 {
83 if (ic->ic_countryie != NULL) {
84 free(ic->ic_countryie, M_80211_NODE_IE);
85 ic->ic_countryie = NULL;
86 }
87 }
88
89 void
90 ieee80211_regdomain_vattach(struct ieee80211vap *vap)
91 {
92 }
93
94 void
95 ieee80211_regdomain_vdetach(struct ieee80211vap *vap)
96 {
97 }
98
99 static void
100 addchan(struct ieee80211com *ic, int ieee, int flags)
101 {
102 struct ieee80211_channel *c;
103
104 c = &ic->ic_channels[ic->ic_nchans++];
105 c->ic_freq = ieee80211_ieee2mhz(ieee, flags);
106 c->ic_ieee = ieee;
107 c->ic_flags = flags;
108 if (flags & IEEE80211_CHAN_HT40U)
109 c->ic_extieee = ieee + 4;
110 else if (flags & IEEE80211_CHAN_HT40D)
111 c->ic_extieee = ieee - 4;
112 else
113 c->ic_extieee = 0;
114 }
115
116 /*
117 * Setup the channel list for the specified regulatory domain,
118 * country code, and operating modes. This interface is used
119 * when a driver does not obtain the channel list from another
120 * source (such as firmware).
121 */
122 int
123 ieee80211_init_channels(struct ieee80211com *ic,
124 const struct ieee80211_regdomain *rd, const uint8_t bands[])
125 {
126 int i;
127
128 /* XXX just do something for now */
129 ic->ic_nchans = 0;
130 if (isset(bands, IEEE80211_MODE_11B) ||
131 isset(bands, IEEE80211_MODE_11G) ||
132 isset(bands, IEEE80211_MODE_11NG)) {
133 int maxchan = 11;
134 if (rd != NULL && rd->ecm)
135 maxchan = 14;
136 for (i = 1; i <= maxchan; i++) {
137 if (isset(bands, IEEE80211_MODE_11B))
138 addchan(ic, i, IEEE80211_CHAN_B);
139 if (isset(bands, IEEE80211_MODE_11G))
140 addchan(ic, i, IEEE80211_CHAN_G);
141 if (isset(bands, IEEE80211_MODE_11NG)) {
142 addchan(ic, i,
143 IEEE80211_CHAN_G | IEEE80211_CHAN_HT20);
144 }
145 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) == 0)
146 continue;
147 if (i <= 7) {
148 addchan(ic, i,
149 IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U);
150 addchan(ic, i + 4,
151 IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D);
152 }
153 }
154 }
155 if (isset(bands, IEEE80211_MODE_11A) ||
156 isset(bands, IEEE80211_MODE_11NA)) {
157 for (i = 36; i <= 64; i += 4) {
158 addchan(ic, i, IEEE80211_CHAN_A);
159 if (isset(bands, IEEE80211_MODE_11NA)) {
160 addchan(ic, i,
161 IEEE80211_CHAN_A | IEEE80211_CHAN_HT20);
162 }
163 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) == 0)
164 continue;
165 if ((i % 8) == 4) {
166 addchan(ic, i,
167 IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U);
168 addchan(ic, i + 4,
169 IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D);
170 }
171 }
172 for (i = 100; i <= 140; i += 4) {
173 addchan(ic, i, IEEE80211_CHAN_A);
174 if (isset(bands, IEEE80211_MODE_11NA)) {
175 addchan(ic, i,
176 IEEE80211_CHAN_A | IEEE80211_CHAN_HT20);
177 }
178 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) == 0)
179 continue;
180 if ((i % 8) == 4 && i != 140) {
181 addchan(ic, i,
182 IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U);
183 addchan(ic, i + 4,
184 IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D);
185 }
186 }
187 for (i = 149; i <= 161; i += 4) {
188 addchan(ic, i, IEEE80211_CHAN_A);
189 if (isset(bands, IEEE80211_MODE_11NA)) {
190 addchan(ic, i,
191 IEEE80211_CHAN_A | IEEE80211_CHAN_HT20);
192 }
193 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) == 0)
194 continue;
195 if ((i % 8) == 5) {
196 addchan(ic, i,
197 IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U);
198 addchan(ic, i + 4,
199 IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D);
200 }
201 }
202 }
203 if (rd != NULL)
204 ic->ic_regdomain = *rd;
205
206 return 0;
207 }
208
209 static __inline int
210 chancompar(const void *a, const void *b)
211 {
212 const struct ieee80211_channel *ca = a;
213 const struct ieee80211_channel *cb = b;
214
215 return (ca->ic_freq == cb->ic_freq) ?
216 (ca->ic_flags & IEEE80211_CHAN_ALL) -
217 (cb->ic_flags & IEEE80211_CHAN_ALL) :
218 ca->ic_freq - cb->ic_freq;
219 }
220
221 /*
222 * Insertion sort.
223 */
224 #define swap(_a, _b, _size) { \
225 uint8_t *s = _b; \
226 int i = _size; \
227 do { \
228 uint8_t tmp = *_a; \
229 *_a++ = *s; \
230 *s++ = tmp; \
231 } while (--i); \
232 _a -= _size; \
233 }
234
235 static void
236 sort_channels(void *a, size_t n, size_t size)
237 {
238 uint8_t *aa = a;
239 uint8_t *ai, *t;
240
241 KASSERT(n > 0, ("no channels"));
242 for (ai = aa+size; --n >= 1; ai += size)
243 for (t = ai; t > aa; t -= size) {
244 uint8_t *u = t - size;
245 if (chancompar(u, t) <= 0)
246 break;
247 swap(u, t, size);
248 }
249 }
250 #undef swap
251
252 /*
253 * Order channels w/ the same frequency so that
254 * b < g < htg and a < hta. This is used to optimize
255 * channel table lookups and some user applications
256 * may also depend on it (though they should not).
257 */
258 void
259 ieee80211_sort_channels(struct ieee80211_channel chans[], int nchans)
260 {
261 if (nchans > 0)
262 sort_channels(chans, nchans, sizeof(struct ieee80211_channel));
263 }
264
265 /*
266 * Allocate and construct a Country Information IE.
267 */
268 struct ieee80211_appie *
269 ieee80211_alloc_countryie(struct ieee80211com *ic)
270 {
271 #define CHAN_UNINTERESTING \
272 (IEEE80211_CHAN_TURBO | IEEE80211_CHAN_STURBO | \
273 IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)
274 /* XXX what about auto? */
275 /* flag set of channels to be excluded (band added below) */
276 static const int skipflags[IEEE80211_MODE_MAX] = {
277 [IEEE80211_MODE_AUTO] = CHAN_UNINTERESTING,
278 [IEEE80211_MODE_11A] = CHAN_UNINTERESTING,
279 [IEEE80211_MODE_11B] = CHAN_UNINTERESTING,
280 [IEEE80211_MODE_11G] = CHAN_UNINTERESTING,
281 [IEEE80211_MODE_FH] = CHAN_UNINTERESTING
282 | IEEE80211_CHAN_OFDM
283 | IEEE80211_CHAN_CCK
284 | IEEE80211_CHAN_DYN,
285 [IEEE80211_MODE_TURBO_A] = CHAN_UNINTERESTING,
286 [IEEE80211_MODE_TURBO_G] = CHAN_UNINTERESTING,
287 [IEEE80211_MODE_STURBO_A] = CHAN_UNINTERESTING,
288 [IEEE80211_MODE_HALF] = IEEE80211_CHAN_TURBO
289 | IEEE80211_CHAN_STURBO,
290 [IEEE80211_MODE_QUARTER] = IEEE80211_CHAN_TURBO
291 | IEEE80211_CHAN_STURBO,
292 [IEEE80211_MODE_11NA] = CHAN_UNINTERESTING,
293 [IEEE80211_MODE_11NG] = CHAN_UNINTERESTING,
294 };
295 const struct ieee80211_regdomain *rd = &ic->ic_regdomain;
296 uint8_t nextchan, chans[IEEE80211_CHAN_BYTES], *frm;
297 struct ieee80211_appie *aie;
298 struct ieee80211_country_ie *ie;
299 int i, skip, nruns;
300
301 aie = malloc(IEEE80211_COUNTRY_MAX_SIZE, M_80211_NODE_IE,
302 M_NOWAIT | M_ZERO);
303 if (aie == NULL) {
304 if_printf(ic->ic_ifp,
305 "%s: unable to allocate memory for country ie\n", __func__);
306 /* XXX stat */
307 return NULL;
308 }
309 ie = (struct ieee80211_country_ie *) aie->ie_data;
310 ie->ie = IEEE80211_ELEMID_COUNTRY;
311 if (rd->isocc[0] == '\0') {
312 if_printf(ic->ic_ifp, "no ISO country string for cc %d; "
313 "using blanks\n", rd->country);
314 ie->cc[0] = ie->cc[1] = ' ';
315 } else {
316 ie->cc[0] = rd->isocc[0];
317 ie->cc[1] = rd->isocc[1];
318 }
319 /*
320 * Indoor/Outdoor portion of country string:
321 * 'I' indoor only
322 * 'O' outdoor only
323 * ' ' all enviroments
324 */
325 ie->cc[2] = (rd->location == 'I' ? 'I' :
326 rd->location == 'O' ? 'O' : ' ');
327 /*
328 * Run-length encoded channel+max tx power info.
329 */
330 frm = (uint8_t *)&ie->band[0];
331 nextchan = 0; /* NB: impossible channel # */
332 nruns = 0;
333 memset(chans, 0, sizeof(chans));
334 skip = skipflags[ieee80211_chan2mode(ic->ic_bsschan)];
335 if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan))
336 skip |= IEEE80211_CHAN_2GHZ;
337 else if (IEEE80211_IS_CHAN_2GHZ(ic->ic_bsschan))
338 skip |= IEEE80211_CHAN_5GHZ;
339 for (i = 0; i < ic->ic_nchans; i++) {
340 const struct ieee80211_channel *c = &ic->ic_channels[i];
341
342 if (isset(chans, c->ic_ieee)) /* suppress dup's */
343 continue;
344 if (c->ic_flags & skip) /* skip band, etc. */
345 continue;
346 setbit(chans, c->ic_ieee);
347 if (c->ic_ieee != nextchan ||
348 c->ic_maxregpower != frm[-1]) { /* new run */
349 if (nruns == IEEE80211_COUNTRY_MAX_BANDS) {
350 if_printf(ic->ic_ifp, "%s: country ie too big, "
351 "runs > max %d, truncating\n",
352 __func__, IEEE80211_COUNTRY_MAX_BANDS);
353 /* XXX stat? fail? */
354 break;
355 }
356 frm[0] = c->ic_ieee; /* starting channel # */
357 frm[1] = 1; /* # channels in run */
358 frm[2] = c->ic_maxregpower; /* tx power cap */
359 frm += 3;
360 nextchan = c->ic_ieee + 1; /* overflow? */
361 nruns++;
362 } else { /* extend run */
363 frm[-2]++;
364 nextchan++;
365 }
366 }
367 ie->len = frm - ie->cc;
368 if (ie->len & 1) { /* Zero pad to multiple of 2 */
369 ie->len++;
370 *frm++ = 0;
371 }
372 aie->ie_len = frm - aie->ie_data;
373
374 return aie;
375 #undef CHAN_UNINTERESTING
376 }
377
378 static int
379 allvapsdown(struct ieee80211com *ic)
380 {
381 struct ieee80211vap *vap;
382
383 IEEE80211_LOCK_ASSERT(ic);
384 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
385 if (vap->iv_state != IEEE80211_S_INIT)
386 return 0;
387 return 1;
388 }
389
390 int
391 ieee80211_setregdomain(struct ieee80211vap *vap,
392 struct ieee80211_regdomain_req *reg)
393 {
394 struct ieee80211com *ic = vap->iv_ic;
395 struct ieee80211_channel *c;
396 int desfreq = 0, desflags = 0; /* XXX silence gcc complaint */
397 int error, i;
398
399 if (reg->rd.location != 'I' && reg->rd.location != 'O' &&
400 reg->rd.location != ' ') {
401 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
402 "%s: invalid location 0x%x\n", __func__, reg->rd.location);
403 return EINVAL;
404 }
405 if (reg->rd.isocc[0] == '\0' || reg->rd.isocc[1] == '\0') {
406 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
407 "%s: invalid iso cc 0x%x:0x%x\n", __func__,
408 reg->rd.isocc[0], reg->rd.isocc[1]);
409 return EINVAL;
410 }
411 if (reg->chaninfo.ic_nchans > IEEE80211_CHAN_MAX) {
412 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
413 "%s: too many channels %u, max %u\n", __func__,
414 reg->chaninfo.ic_nchans, IEEE80211_CHAN_MAX);
415 return EINVAL;
416 }
417 /*
418 * Calculate freq<->IEEE mapping and default max tx power
419 * for channels not setup. The driver can override these
420 * setting to reflect device properties/requirements.
421 */
422 for (i = 0; i < reg->chaninfo.ic_nchans; i++) {
423 c = ®->chaninfo.ic_chans[i];
424 if (c->ic_freq == 0 || c->ic_flags == 0) {
425 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
426 "%s: invalid channel spec at [%u]\n", __func__, i);
427 return EINVAL;
428 }
429 if (c->ic_maxregpower == 0) {
430 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
431 "%s: invalid channel spec, zero maxregpower, "
432 "freq %u flags 0x%x\n", __func__,
433 c->ic_freq, c->ic_flags);
434 return EINVAL;
435 }
436 if (c->ic_ieee == 0)
437 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
438 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
439 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
440 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
441 c->ic_flags);
442 if (c->ic_maxpower == 0)
443 c->ic_maxpower = 2*c->ic_maxregpower;
444 }
445 IEEE80211_LOCK(ic);
446 /* XXX bandaid; a running vap will likely crash */
447 if (!allvapsdown(ic)) {
448 IEEE80211_UNLOCK(ic);
449 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
450 "%s: reject: vaps are running\n", __func__);
451 return EBUSY;
452 }
453 error = ic->ic_setregdomain(ic, ®->rd,
454 reg->chaninfo.ic_nchans, reg->chaninfo.ic_chans);
455 if (error != 0) {
456 IEEE80211_UNLOCK(ic);
457 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
458 "%s: driver rejected request, error %u\n", __func__, error);
459 return error;
460 }
461 /*
462 * Commit: copy in new channel table and reset media state.
463 * On return the state machines will be clocked so all vaps
464 * will reset their state.
465 *
466 * XXX ic_bsschan is marked undefined, must have vap's in
467 * INIT state or we blow up forcing stations off
468 */
469 /*
470 * Save any desired channel for restore below. Note this
471 * needs to be done for all vaps but for now we only do
472 * the one where the ioctl is issued.
473 */
474 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
475 desfreq = vap->iv_des_chan->ic_freq;
476 desflags = vap->iv_des_chan->ic_flags;
477 }
478 /* regdomain parameters */
479 memcpy(&ic->ic_regdomain, ®->rd, sizeof(reg->rd));
480 /* channel table */
481 memcpy(ic->ic_channels, reg->chaninfo.ic_chans,
482 reg->chaninfo.ic_nchans * sizeof(struct ieee80211_channel));
483 ic->ic_nchans = reg->chaninfo.ic_nchans;
484 memset(&ic->ic_channels[ic->ic_nchans], 0,
485 (IEEE80211_CHAN_MAX - ic->ic_nchans) *
486 sizeof(struct ieee80211_channel));
487 ieee80211_media_init(ic);
488
489 /*
490 * Invalidate channel-related state.
491 */
492 if (ic->ic_countryie != NULL) {
493 free(ic->ic_countryie, M_80211_NODE_IE);
494 ic->ic_countryie = NULL;
495 }
496 ieee80211_scan_flush(vap);
497 ieee80211_dfs_reset(ic);
498 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
499 c = ieee80211_find_channel(ic, desfreq, desflags);
500 /* NB: may be NULL if not present in new channel list */
501 vap->iv_des_chan = (c != NULL) ? c : IEEE80211_CHAN_ANYC;
502 }
503 IEEE80211_UNLOCK(ic);
504
505 return 0;
506 }
Cache object: 074176fc9c7ff16fa07b9e0560751f23
|