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