Deleted Added
full compact
ieee80211_regdomain.c (187800) ieee80211_regdomain.c (187802)
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>
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: head/sys/net80211/ieee80211_regdomain.c 187800 2009-01-27 23:19:36Z sam $");
27__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_regdomain.c 187802 2009-01-27 23:43:20Z sam $");
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
46static void
47null_getradiocaps(struct ieee80211com *ic, int maxchan,
48 int *n, struct ieee80211_channel *c)
49{
50 /* just feed back the current channel list */
51 *n = ic->ic_nchans; /* XXX return count copied? */
52 if (maxchan > ic->ic_nchans)
53 maxchan = ic->ic_nchans;
54 memcpy(c, ic->ic_channels, maxchan*sizeof(struct ieee80211_channel));
55}
56
57static int
58null_setregdomain(struct ieee80211com *ic,
59 struct ieee80211_regdomain *rd,
60 int nchans, struct ieee80211_channel chans[])
61{
62 return 0; /* accept anything */
63}
64
65void
66ieee80211_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 */
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
46static void
47null_getradiocaps(struct ieee80211com *ic, int maxchan,
48 int *n, struct ieee80211_channel *c)
49{
50 /* just feed back the current channel list */
51 *n = ic->ic_nchans; /* XXX return count copied? */
52 if (maxchan > ic->ic_nchans)
53 maxchan = ic->ic_nchans;
54 memcpy(c, ic->ic_channels, maxchan*sizeof(struct ieee80211_channel));
55}
56
57static int
58null_setregdomain(struct ieee80211com *ic,
59 struct ieee80211_regdomain *rd,
60 int nchans, struct ieee80211_channel chans[])
61{
62 return 0; /* accept anything */
63}
64
65void
66ieee80211_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 /* XXX? too late to setup default channel list */
74 /* NB: driver calls ieee80211_init_channels or similar */
75 }
76 ic->ic_getradiocaps = null_getradiocaps;
77 ic->ic_setregdomain = null_setregdomain;
78}
79
80void
81ieee80211_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
89void
90ieee80211_regdomain_vattach(struct ieee80211vap *vap)
91{
92}
93
94void
95ieee80211_regdomain_vdetach(struct ieee80211vap *vap)
96{
97}
98
99static void
100addchan(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 c->ic_extieee = 0;
109}
110
111/*
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).
116 */
117int
118ieee80211_init_channels(struct ieee80211com *ic,
119 const struct ieee80211_regdomain *rd, const uint8_t bands[])
120{
121 int i;
122
123 /* XXX just do something for now */
124 ic->ic_nchans = 0;
125 if (isset(bands, IEEE80211_MODE_11B) ||
126 isset(bands, IEEE80211_MODE_11G)) {
127 int maxchan = 11;
128 if (rd != NULL && rd->ecm)
129 maxchan = 14;
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);
135 }
136 }
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);
144 }
145 if (rd != NULL)
146 ic->ic_regdomain = *rd;
147
148 return 0;
149}
150
151static __inline int
152chancompar(const void *a, const void *b)
153{
154 const struct ieee80211_channel *ca = a;
155 const struct ieee80211_channel *cb = b;
156
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;
161}
162
163/*
164 * Insertion sort.
165 */
166#define swap(_a, _b, _size) { \
167 uint8_t *s = _b; \
168 int i = _size; \
169 do { \
170 uint8_t tmp = *_a; \
171 *_a++ = *s; \
172 *s++ = tmp; \
173 } while (--i); \
174 _a -= _size; \
175}
176
177static void
178sort_channels(void *a, size_t n, size_t size)
179{
180 uint8_t *aa = a;
181 uint8_t *ai, *t;
182
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)
188 break;
189 swap(u, t, size);
190 }
191}
192#undef swap
193
194/*
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).
199 */
200void
201ieee80211_sort_channels(struct ieee80211_channel chans[], int nchans)
202{
203 if (nchans > 0)
204 sort_channels(chans, nchans, sizeof(struct ieee80211_channel));
205}
206
207/*
208 * Allocate and construct a Country Information IE.
209 */
210struct ieee80211_appie *
211ieee80211_alloc_countryie(struct ieee80211com *ic)
212{
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 */
218 static const int skipflags[IEEE80211_MODE_MAX] = {
219 CHAN_UNINTERESTING, /* MODE_AUTO */
220 CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_11A */
221 CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_11B */
222 CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_11G */
223 CHAN_UNINTERESTING | IEEE80211_CHAN_OFDM | /* MODE_FH */
224 IEEE80211_CHAN_CCK | IEEE80211_CHAN_DYN,
225 CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_TURBO_A */
226 CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_TURBO_G */
227 CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_STURBO_A */
228 CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_11NA */
229 CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_11NG */
230 };
231 const struct ieee80211_regdomain *rd = &ic->ic_regdomain;
232 uint8_t nextchan, chans[IEEE80211_CHAN_BYTES], *frm;
233 struct ieee80211_appie *aie;
234 struct ieee80211_country_ie *ie;
235 int i, skip, nruns;
236
237 aie = malloc(IEEE80211_COUNTRY_MAX_SIZE, M_80211_NODE_IE,
238 M_NOWAIT | M_ZERO);
239 if (aie == NULL) {
240 if_printf(ic->ic_ifp,
241 "%s: unable to allocate memory for country ie\n", __func__);
242 /* XXX stat */
243 return NULL;
244 }
245 ie = (struct ieee80211_country_ie *) aie->ie_data;
246 ie->ie = IEEE80211_ELEMID_COUNTRY;
247 if (rd->isocc[0] == '\0') {
248 if_printf(ic->ic_ifp, "no ISO country string for cc %d; "
249 "using blanks\n", rd->country);
250 ie->cc[0] = ie->cc[1] = ' ';
251 } else {
252 ie->cc[0] = rd->isocc[0];
253 ie->cc[1] = rd->isocc[1];
254 }
255 /*
256 * Indoor/Outdoor portion of country string:
257 * 'I' indoor only
258 * 'O' outdoor only
259 * ' ' all enviroments
260 */
261 ie->cc[2] = (rd->location == 'I' ? 'I' :
262 rd->location == 'O' ? 'O' : ' ');
263 /*
264 * Run-length encoded channel+max tx power info.
265 */
266 frm = (uint8_t *)&ie->band[0];
267 nextchan = 0; /* NB: impossible channel # */
268 nruns = 0;
269 memset(chans, 0, sizeof(chans));
270 skip = skipflags[ieee80211_chan2mode(ic->ic_bsschan)];
271 for (i = 0; i < ic->ic_nchans; i++) {
272 const struct ieee80211_channel *c = &ic->ic_channels[i];
273
274 if (isset(chans, c->ic_ieee)) /* suppress dup's */
275 continue;
276 if (c->ic_flags & skip) /* skip band, etc. */
277 continue;
278 setbit(chans, c->ic_ieee);
279 if (c->ic_ieee != nextchan ||
280 c->ic_maxregpower != frm[-1]) { /* new run */
281 if (nruns == IEEE80211_COUNTRY_MAX_BANDS) {
282 if_printf(ic->ic_ifp, "%s: country ie too big, "
283 "runs > max %d, truncating\n",
284 __func__, IEEE80211_COUNTRY_MAX_BANDS);
285 /* XXX stat? fail? */
286 break;
287 }
288 frm[0] = c->ic_ieee; /* starting channel # */
289 frm[1] = 1; /* # channels in run */
290 frm[2] = c->ic_maxregpower; /* tx power cap */
291 frm += 3;
292 nextchan = c->ic_ieee + 1; /* overflow? */
293 nruns++;
294 } else { /* extend run */
295 frm[-2]++;
296 nextchan++;
297 }
298 }
299 ie->len = frm - ie->cc;
300 if (ie->len & 1) { /* Zero pad to multiple of 2 */
301 ie->len++;
302 *frm++ = 0;
303 }
304 aie->ie_len = frm - aie->ie_data;
305
306 return aie;
307#undef CHAN_UNINTERESTING
308}
309
310static int
311allvapsdown(struct ieee80211com *ic)
312{
313 struct ieee80211vap *vap;
314
315 IEEE80211_LOCK_ASSERT(ic);
316 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
317 if (vap->iv_state != IEEE80211_S_INIT)
318 return 0;
319 return 1;
320}
321
322int
323ieee80211_setregdomain(struct ieee80211vap *vap,
324 struct ieee80211_regdomain_req *reg)
325{
326 struct ieee80211com *ic = vap->iv_ic;
327 struct ieee80211_channel *c;
328 int desfreq = 0, desflags = 0; /* XXX silence gcc complaint */
329 int error, i;
330
331 if (reg->rd.location != 'I' && reg->rd.location != 'O' &&
332 reg->rd.location != ' ') {
333 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
334 "%s: invalid location 0x%x\n", __func__, reg->rd.location);
335 return EINVAL;
336 }
337 if (reg->rd.isocc[0] == '\0' || reg->rd.isocc[1] == '\0') {
338 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
339 "%s: invalid iso cc 0x%x:0x%x\n", __func__,
340 reg->rd.isocc[0], reg->rd.isocc[1]);
341 return EINVAL;
342 }
343 if (reg->chaninfo.ic_nchans > IEEE80211_CHAN_MAX) {
344 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
345 "%s: too many channels %u, max %u\n", __func__,
346 reg->chaninfo.ic_nchans, IEEE80211_CHAN_MAX);
347 return EINVAL;
348 }
349 /*
350 * Calculate freq<->IEEE mapping and default max tx power
351 * for channels not setup. The driver can override these
352 * setting to reflect device properties/requirements.
353 */
354 for (i = 0; i < reg->chaninfo.ic_nchans; i++) {
355 c = &reg->chaninfo.ic_chans[i];
356 if (c->ic_freq == 0 || c->ic_flags == 0) {
357 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
358 "%s: invalid channel spec at [%u]\n", __func__, i);
359 return EINVAL;
360 }
361 if (c->ic_maxregpower == 0) {
362 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
363 "%s: invalid channel spec, zero maxregpower, "
364 "freq %u flags 0x%x\n", __func__,
365 c->ic_freq, c->ic_flags);
366 return EINVAL;
367 }
368 if (c->ic_ieee == 0)
369 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
370 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
371 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
372 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
373 c->ic_flags);
374 if (c->ic_maxpower == 0)
375 c->ic_maxpower = 2*c->ic_maxregpower;
376 }
377 IEEE80211_LOCK(ic);
378 /* XXX bandaid; a running vap will likely crash */
379 if (!allvapsdown(ic)) {
380 IEEE80211_UNLOCK(ic);
381 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
382 "%s: reject: vaps are running\n", __func__);
383 return EBUSY;
384 }
385 error = ic->ic_setregdomain(ic, &reg->rd,
386 reg->chaninfo.ic_nchans, reg->chaninfo.ic_chans);
387 if (error != 0) {
388 IEEE80211_UNLOCK(ic);
389 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
390 "%s: driver rejected request, error %u\n", __func__, error);
391 return error;
392 }
393 /*
394 * Commit: copy in new channel table and reset media state.
395 * On return the state machines will be clocked so all vaps
396 * will reset their state.
397 *
398 * XXX ic_bsschan is marked undefined, must have vap's in
399 * INIT state or we blow up forcing stations off
400 */
401 /*
402 * Save any desired channel for restore below. Note this
403 * needs to be done for all vaps but for now we only do
404 * the one where the ioctl is issued.
405 */
406 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
407 desfreq = vap->iv_des_chan->ic_freq;
408 desflags = vap->iv_des_chan->ic_flags;
409 }
410 /* regdomain parameters */
411 memcpy(&ic->ic_regdomain, &reg->rd, sizeof(reg->rd));
412 /* channel table */
413 memcpy(ic->ic_channels, reg->chaninfo.ic_chans,
414 reg->chaninfo.ic_nchans * sizeof(struct ieee80211_channel));
415 ic->ic_nchans = reg->chaninfo.ic_nchans;
416 memset(&ic->ic_channels[ic->ic_nchans], 0,
417 (IEEE80211_CHAN_MAX - ic->ic_nchans) *
418 sizeof(struct ieee80211_channel));
419 ieee80211_media_init(ic);
420
421 /*
422 * Invalidate channel-related state.
423 */
424 if (ic->ic_countryie != NULL) {
425 free(ic->ic_countryie, M_80211_NODE_IE);
426 ic->ic_countryie = NULL;
427 }
428 ieee80211_scan_flush(vap);
429 ieee80211_dfs_reset(ic);
430 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
431 /* NB: may be NULL if not present in new channel list */
432 vap->iv_des_chan = ieee80211_find_channel(ic, desfreq, desflags);
433 }
434 IEEE80211_UNLOCK(ic);
435
436 return 0;
437}
75 }
76 ic->ic_getradiocaps = null_getradiocaps;
77 ic->ic_setregdomain = null_setregdomain;
78}
79
80void
81ieee80211_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
89void
90ieee80211_regdomain_vattach(struct ieee80211vap *vap)
91{
92}
93
94void
95ieee80211_regdomain_vdetach(struct ieee80211vap *vap)
96{
97}
98
99static void
100addchan(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 c->ic_extieee = 0;
109}
110
111/*
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).
116 */
117int
118ieee80211_init_channels(struct ieee80211com *ic,
119 const struct ieee80211_regdomain *rd, const uint8_t bands[])
120{
121 int i;
122
123 /* XXX just do something for now */
124 ic->ic_nchans = 0;
125 if (isset(bands, IEEE80211_MODE_11B) ||
126 isset(bands, IEEE80211_MODE_11G)) {
127 int maxchan = 11;
128 if (rd != NULL && rd->ecm)
129 maxchan = 14;
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);
135 }
136 }
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);
144 }
145 if (rd != NULL)
146 ic->ic_regdomain = *rd;
147
148 return 0;
149}
150
151static __inline int
152chancompar(const void *a, const void *b)
153{
154 const struct ieee80211_channel *ca = a;
155 const struct ieee80211_channel *cb = b;
156
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;
161}
162
163/*
164 * Insertion sort.
165 */
166#define swap(_a, _b, _size) { \
167 uint8_t *s = _b; \
168 int i = _size; \
169 do { \
170 uint8_t tmp = *_a; \
171 *_a++ = *s; \
172 *s++ = tmp; \
173 } while (--i); \
174 _a -= _size; \
175}
176
177static void
178sort_channels(void *a, size_t n, size_t size)
179{
180 uint8_t *aa = a;
181 uint8_t *ai, *t;
182
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)
188 break;
189 swap(u, t, size);
190 }
191}
192#undef swap
193
194/*
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).
199 */
200void
201ieee80211_sort_channels(struct ieee80211_channel chans[], int nchans)
202{
203 if (nchans > 0)
204 sort_channels(chans, nchans, sizeof(struct ieee80211_channel));
205}
206
207/*
208 * Allocate and construct a Country Information IE.
209 */
210struct ieee80211_appie *
211ieee80211_alloc_countryie(struct ieee80211com *ic)
212{
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 */
218 static const int skipflags[IEEE80211_MODE_MAX] = {
219 CHAN_UNINTERESTING, /* MODE_AUTO */
220 CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_11A */
221 CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_11B */
222 CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_11G */
223 CHAN_UNINTERESTING | IEEE80211_CHAN_OFDM | /* MODE_FH */
224 IEEE80211_CHAN_CCK | IEEE80211_CHAN_DYN,
225 CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_TURBO_A */
226 CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_TURBO_G */
227 CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_STURBO_A */
228 CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_11NA */
229 CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_11NG */
230 };
231 const struct ieee80211_regdomain *rd = &ic->ic_regdomain;
232 uint8_t nextchan, chans[IEEE80211_CHAN_BYTES], *frm;
233 struct ieee80211_appie *aie;
234 struct ieee80211_country_ie *ie;
235 int i, skip, nruns;
236
237 aie = malloc(IEEE80211_COUNTRY_MAX_SIZE, M_80211_NODE_IE,
238 M_NOWAIT | M_ZERO);
239 if (aie == NULL) {
240 if_printf(ic->ic_ifp,
241 "%s: unable to allocate memory for country ie\n", __func__);
242 /* XXX stat */
243 return NULL;
244 }
245 ie = (struct ieee80211_country_ie *) aie->ie_data;
246 ie->ie = IEEE80211_ELEMID_COUNTRY;
247 if (rd->isocc[0] == '\0') {
248 if_printf(ic->ic_ifp, "no ISO country string for cc %d; "
249 "using blanks\n", rd->country);
250 ie->cc[0] = ie->cc[1] = ' ';
251 } else {
252 ie->cc[0] = rd->isocc[0];
253 ie->cc[1] = rd->isocc[1];
254 }
255 /*
256 * Indoor/Outdoor portion of country string:
257 * 'I' indoor only
258 * 'O' outdoor only
259 * ' ' all enviroments
260 */
261 ie->cc[2] = (rd->location == 'I' ? 'I' :
262 rd->location == 'O' ? 'O' : ' ');
263 /*
264 * Run-length encoded channel+max tx power info.
265 */
266 frm = (uint8_t *)&ie->band[0];
267 nextchan = 0; /* NB: impossible channel # */
268 nruns = 0;
269 memset(chans, 0, sizeof(chans));
270 skip = skipflags[ieee80211_chan2mode(ic->ic_bsschan)];
271 for (i = 0; i < ic->ic_nchans; i++) {
272 const struct ieee80211_channel *c = &ic->ic_channels[i];
273
274 if (isset(chans, c->ic_ieee)) /* suppress dup's */
275 continue;
276 if (c->ic_flags & skip) /* skip band, etc. */
277 continue;
278 setbit(chans, c->ic_ieee);
279 if (c->ic_ieee != nextchan ||
280 c->ic_maxregpower != frm[-1]) { /* new run */
281 if (nruns == IEEE80211_COUNTRY_MAX_BANDS) {
282 if_printf(ic->ic_ifp, "%s: country ie too big, "
283 "runs > max %d, truncating\n",
284 __func__, IEEE80211_COUNTRY_MAX_BANDS);
285 /* XXX stat? fail? */
286 break;
287 }
288 frm[0] = c->ic_ieee; /* starting channel # */
289 frm[1] = 1; /* # channels in run */
290 frm[2] = c->ic_maxregpower; /* tx power cap */
291 frm += 3;
292 nextchan = c->ic_ieee + 1; /* overflow? */
293 nruns++;
294 } else { /* extend run */
295 frm[-2]++;
296 nextchan++;
297 }
298 }
299 ie->len = frm - ie->cc;
300 if (ie->len & 1) { /* Zero pad to multiple of 2 */
301 ie->len++;
302 *frm++ = 0;
303 }
304 aie->ie_len = frm - aie->ie_data;
305
306 return aie;
307#undef CHAN_UNINTERESTING
308}
309
310static int
311allvapsdown(struct ieee80211com *ic)
312{
313 struct ieee80211vap *vap;
314
315 IEEE80211_LOCK_ASSERT(ic);
316 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
317 if (vap->iv_state != IEEE80211_S_INIT)
318 return 0;
319 return 1;
320}
321
322int
323ieee80211_setregdomain(struct ieee80211vap *vap,
324 struct ieee80211_regdomain_req *reg)
325{
326 struct ieee80211com *ic = vap->iv_ic;
327 struct ieee80211_channel *c;
328 int desfreq = 0, desflags = 0; /* XXX silence gcc complaint */
329 int error, i;
330
331 if (reg->rd.location != 'I' && reg->rd.location != 'O' &&
332 reg->rd.location != ' ') {
333 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
334 "%s: invalid location 0x%x\n", __func__, reg->rd.location);
335 return EINVAL;
336 }
337 if (reg->rd.isocc[0] == '\0' || reg->rd.isocc[1] == '\0') {
338 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
339 "%s: invalid iso cc 0x%x:0x%x\n", __func__,
340 reg->rd.isocc[0], reg->rd.isocc[1]);
341 return EINVAL;
342 }
343 if (reg->chaninfo.ic_nchans > IEEE80211_CHAN_MAX) {
344 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
345 "%s: too many channels %u, max %u\n", __func__,
346 reg->chaninfo.ic_nchans, IEEE80211_CHAN_MAX);
347 return EINVAL;
348 }
349 /*
350 * Calculate freq<->IEEE mapping and default max tx power
351 * for channels not setup. The driver can override these
352 * setting to reflect device properties/requirements.
353 */
354 for (i = 0; i < reg->chaninfo.ic_nchans; i++) {
355 c = &reg->chaninfo.ic_chans[i];
356 if (c->ic_freq == 0 || c->ic_flags == 0) {
357 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
358 "%s: invalid channel spec at [%u]\n", __func__, i);
359 return EINVAL;
360 }
361 if (c->ic_maxregpower == 0) {
362 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
363 "%s: invalid channel spec, zero maxregpower, "
364 "freq %u flags 0x%x\n", __func__,
365 c->ic_freq, c->ic_flags);
366 return EINVAL;
367 }
368 if (c->ic_ieee == 0)
369 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
370 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
371 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
372 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
373 c->ic_flags);
374 if (c->ic_maxpower == 0)
375 c->ic_maxpower = 2*c->ic_maxregpower;
376 }
377 IEEE80211_LOCK(ic);
378 /* XXX bandaid; a running vap will likely crash */
379 if (!allvapsdown(ic)) {
380 IEEE80211_UNLOCK(ic);
381 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
382 "%s: reject: vaps are running\n", __func__);
383 return EBUSY;
384 }
385 error = ic->ic_setregdomain(ic, &reg->rd,
386 reg->chaninfo.ic_nchans, reg->chaninfo.ic_chans);
387 if (error != 0) {
388 IEEE80211_UNLOCK(ic);
389 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
390 "%s: driver rejected request, error %u\n", __func__, error);
391 return error;
392 }
393 /*
394 * Commit: copy in new channel table and reset media state.
395 * On return the state machines will be clocked so all vaps
396 * will reset their state.
397 *
398 * XXX ic_bsschan is marked undefined, must have vap's in
399 * INIT state or we blow up forcing stations off
400 */
401 /*
402 * Save any desired channel for restore below. Note this
403 * needs to be done for all vaps but for now we only do
404 * the one where the ioctl is issued.
405 */
406 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
407 desfreq = vap->iv_des_chan->ic_freq;
408 desflags = vap->iv_des_chan->ic_flags;
409 }
410 /* regdomain parameters */
411 memcpy(&ic->ic_regdomain, &reg->rd, sizeof(reg->rd));
412 /* channel table */
413 memcpy(ic->ic_channels, reg->chaninfo.ic_chans,
414 reg->chaninfo.ic_nchans * sizeof(struct ieee80211_channel));
415 ic->ic_nchans = reg->chaninfo.ic_nchans;
416 memset(&ic->ic_channels[ic->ic_nchans], 0,
417 (IEEE80211_CHAN_MAX - ic->ic_nchans) *
418 sizeof(struct ieee80211_channel));
419 ieee80211_media_init(ic);
420
421 /*
422 * Invalidate channel-related state.
423 */
424 if (ic->ic_countryie != NULL) {
425 free(ic->ic_countryie, M_80211_NODE_IE);
426 ic->ic_countryie = NULL;
427 }
428 ieee80211_scan_flush(vap);
429 ieee80211_dfs_reset(ic);
430 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
431 /* NB: may be NULL if not present in new channel list */
432 vap->iv_des_chan = ieee80211_find_channel(ic, desfreq, desflags);
433 }
434 IEEE80211_UNLOCK(ic);
435
436 return 0;
437}