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