2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2008 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * IEEE 802.11 regdomain support.
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/socket.h>
43 #include <net/if_var.h>
44 #include <net/if_media.h>
45 #include <net/ethernet.h>
47 #include <net80211/ieee80211_var.h>
48 #include <net80211/ieee80211_regdomain.h>
51 null_getradiocaps(struct ieee80211com *ic, int maxchan,
52 int *n, struct ieee80211_channel *c)
54 /* just feed back the current channel list */
55 if (maxchan > ic->ic_nchans)
56 maxchan = ic->ic_nchans;
57 memcpy(c, ic->ic_channels, maxchan*sizeof(struct ieee80211_channel));
62 null_setregdomain(struct ieee80211com *ic,
63 struct ieee80211_regdomain *rd,
64 int nchans, struct ieee80211_channel chans[])
66 return 0; /* accept anything */
70 ieee80211_regdomain_attach(struct ieee80211com *ic)
72 if (ic->ic_regdomain.regdomain == 0 &&
73 ic->ic_regdomain.country == CTRY_DEFAULT) {
74 ic->ic_regdomain.location = ' '; /* both */
75 /* NB: driver calls ieee80211_init_channels or similar */
77 ic->ic_getradiocaps = null_getradiocaps;
78 ic->ic_setregdomain = null_setregdomain;
82 ieee80211_regdomain_detach(struct ieee80211com *ic)
84 if (ic->ic_countryie != NULL) {
85 IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
86 ic->ic_countryie = NULL;
91 ieee80211_regdomain_vattach(struct ieee80211vap *vap)
96 ieee80211_regdomain_vdetach(struct ieee80211vap *vap)
100 static const uint8_t def_chan_2ghz[] =
101 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
102 static const uint8_t def_chan_5ghz_band1[] =
103 { 36, 40, 44, 48, 52, 56, 60, 64 };
104 static const uint8_t def_chan_5ghz_band2[] =
105 { 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140 };
106 static const uint8_t def_chan_5ghz_band3[] =
107 { 149, 153, 157, 161 };
110 * Setup the channel list for the specified regulatory domain,
111 * country code, and operating modes. This interface is used
112 * when a driver does not obtain the channel list from another
113 * source (such as firmware).
116 ieee80211_init_channels(struct ieee80211com *ic,
117 const struct ieee80211_regdomain *rd, const uint8_t bands[])
119 struct ieee80211_channel *chans = ic->ic_channels;
120 int *nchans = &ic->ic_nchans;
123 /* XXX just do something for now */
124 ht40 = !!(ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40);
126 if (isset(bands, IEEE80211_MODE_11B) ||
127 isset(bands, IEEE80211_MODE_11G) ||
128 isset(bands, IEEE80211_MODE_11NG)) {
129 int nchan = nitems(def_chan_2ghz);
130 if (!(rd != NULL && rd->ecm))
133 ieee80211_add_channel_list_2ghz(chans, IEEE80211_CHAN_MAX,
134 nchans, def_chan_2ghz, nchan, bands, ht40);
136 if (isset(bands, IEEE80211_MODE_11A) ||
137 isset(bands, IEEE80211_MODE_11NA)) {
138 ieee80211_add_channel_list_5ghz(chans, IEEE80211_CHAN_MAX,
139 nchans, def_chan_5ghz_band1, nitems(def_chan_5ghz_band1),
141 ieee80211_add_channel_list_5ghz(chans, IEEE80211_CHAN_MAX,
142 nchans, def_chan_5ghz_band2, nitems(def_chan_5ghz_band2),
144 ieee80211_add_channel_list_5ghz(chans, IEEE80211_CHAN_MAX,
145 nchans, def_chan_5ghz_band3, nitems(def_chan_5ghz_band3),
149 ic->ic_regdomain = *rd;
155 chancompar(const void *a, const void *b)
157 const struct ieee80211_channel *ca = a;
158 const struct ieee80211_channel *cb = b;
160 return (ca->ic_freq == cb->ic_freq) ?
161 (ca->ic_flags & IEEE80211_CHAN_ALL) -
162 (cb->ic_flags & IEEE80211_CHAN_ALL) :
163 ca->ic_freq - cb->ic_freq;
169 #define swap(_a, _b, _size) { \
181 sort_channels(void *a, size_t n, size_t size)
186 KASSERT(n > 0, ("no channels"));
187 for (ai = aa+size; --n >= 1; ai += size)
188 for (t = ai; t > aa; t -= size) {
189 uint8_t *u = t - size;
190 if (chancompar(u, t) <= 0)
198 * Order channels w/ the same frequency so that
199 * b < g < htg and a < hta. This is used to optimize
200 * channel table lookups and some user applications
201 * may also depend on it (though they should not).
204 ieee80211_sort_channels(struct ieee80211_channel chans[], int nchans)
207 sort_channels(chans, nchans, sizeof(struct ieee80211_channel));
211 * Allocate and construct a Country Information IE.
213 struct ieee80211_appie *
214 ieee80211_alloc_countryie(struct ieee80211com *ic)
216 #define CHAN_UNINTERESTING \
217 (IEEE80211_CHAN_TURBO | IEEE80211_CHAN_STURBO | \
218 IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER)
219 /* XXX what about auto? */
220 /* flag set of channels to be excluded (band added below) */
221 static const int skipflags[IEEE80211_MODE_MAX] = {
222 [IEEE80211_MODE_AUTO] = CHAN_UNINTERESTING,
223 [IEEE80211_MODE_11A] = CHAN_UNINTERESTING,
224 [IEEE80211_MODE_11B] = CHAN_UNINTERESTING,
225 [IEEE80211_MODE_11G] = CHAN_UNINTERESTING,
226 [IEEE80211_MODE_FH] = CHAN_UNINTERESTING
227 | IEEE80211_CHAN_OFDM
229 | IEEE80211_CHAN_DYN,
230 [IEEE80211_MODE_TURBO_A] = CHAN_UNINTERESTING,
231 [IEEE80211_MODE_TURBO_G] = CHAN_UNINTERESTING,
232 [IEEE80211_MODE_STURBO_A] = CHAN_UNINTERESTING,
233 [IEEE80211_MODE_HALF] = IEEE80211_CHAN_TURBO
234 | IEEE80211_CHAN_STURBO,
235 [IEEE80211_MODE_QUARTER] = IEEE80211_CHAN_TURBO
236 | IEEE80211_CHAN_STURBO,
237 [IEEE80211_MODE_11NA] = CHAN_UNINTERESTING,
238 [IEEE80211_MODE_11NG] = CHAN_UNINTERESTING,
240 const struct ieee80211_regdomain *rd = &ic->ic_regdomain;
241 uint8_t nextchan, chans[IEEE80211_CHAN_BYTES], *frm;
242 struct ieee80211_appie *aie;
243 struct ieee80211_country_ie *ie;
246 aie = IEEE80211_MALLOC(IEEE80211_COUNTRY_MAX_SIZE, M_80211_NODE_IE,
247 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
249 ic_printf(ic, "%s: unable to allocate memory for country ie\n",
254 ie = (struct ieee80211_country_ie *) aie->ie_data;
255 ie->ie = IEEE80211_ELEMID_COUNTRY;
256 if (rd->isocc[0] == '\0') {
257 ic_printf(ic, "no ISO country string for cc %d; using blanks\n",
259 ie->cc[0] = ie->cc[1] = ' ';
261 ie->cc[0] = rd->isocc[0];
262 ie->cc[1] = rd->isocc[1];
265 * Indoor/Outdoor portion of country string:
268 * ' ' all environments
270 ie->cc[2] = (rd->location == 'I' ? 'I' :
271 rd->location == 'O' ? 'O' : ' ');
273 * Run-length encoded channel+max tx power info.
275 frm = (uint8_t *)&ie->band[0];
276 nextchan = 0; /* NB: impossible channel # */
278 memset(chans, 0, sizeof(chans));
279 skip = skipflags[ieee80211_chan2mode(ic->ic_bsschan)];
280 if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan))
281 skip |= IEEE80211_CHAN_2GHZ;
282 else if (IEEE80211_IS_CHAN_2GHZ(ic->ic_bsschan))
283 skip |= IEEE80211_CHAN_5GHZ;
284 for (i = 0; i < ic->ic_nchans; i++) {
285 const struct ieee80211_channel *c = &ic->ic_channels[i];
287 if (isset(chans, c->ic_ieee)) /* suppress dup's */
289 if (c->ic_flags & skip) /* skip band, etc. */
291 setbit(chans, c->ic_ieee);
292 if (c->ic_ieee != nextchan ||
293 c->ic_maxregpower != frm[-1]) { /* new run */
294 if (nruns == IEEE80211_COUNTRY_MAX_BANDS) {
295 ic_printf(ic, "%s: country ie too big, "
296 "runs > max %d, truncating\n",
297 __func__, IEEE80211_COUNTRY_MAX_BANDS);
298 /* XXX stat? fail? */
301 frm[0] = c->ic_ieee; /* starting channel # */
302 frm[1] = 1; /* # channels in run */
303 frm[2] = c->ic_maxregpower; /* tx power cap */
305 nextchan = c->ic_ieee + 1; /* overflow? */
307 } else { /* extend run */
312 ie->len = frm - ie->cc;
313 if (ie->len & 1) { /* Zero pad to multiple of 2 */
317 aie->ie_len = frm - aie->ie_data;
320 #undef CHAN_UNINTERESTING
324 allvapsdown(struct ieee80211com *ic)
326 struct ieee80211vap *vap;
328 IEEE80211_LOCK_ASSERT(ic);
329 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
330 if (vap->iv_state != IEEE80211_S_INIT)
336 ieee80211_setregdomain(struct ieee80211vap *vap,
337 struct ieee80211_regdomain_req *reg)
339 struct ieee80211com *ic = vap->iv_ic;
340 struct ieee80211_channel *c;
341 int desfreq = 0, desflags = 0; /* XXX silence gcc complaint */
344 if (reg->rd.location != 'I' && reg->rd.location != 'O' &&
345 reg->rd.location != ' ') {
346 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
347 "%s: invalid location 0x%x\n", __func__, reg->rd.location);
350 if (reg->rd.isocc[0] == '\0' || reg->rd.isocc[1] == '\0') {
351 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
352 "%s: invalid iso cc 0x%x:0x%x\n", __func__,
353 reg->rd.isocc[0], reg->rd.isocc[1]);
356 if (reg->chaninfo.ic_nchans > IEEE80211_CHAN_MAX) {
357 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
358 "%s: too many channels %u, max %u\n", __func__,
359 reg->chaninfo.ic_nchans, IEEE80211_CHAN_MAX);
363 * Calculate freq<->IEEE mapping and default max tx power
364 * for channels not setup. The driver can override these
365 * setting to reflect device properties/requirements.
367 for (i = 0; i < reg->chaninfo.ic_nchans; i++) {
368 c = ®->chaninfo.ic_chans[i];
369 if (c->ic_freq == 0 || c->ic_flags == 0) {
370 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
371 "%s: invalid channel spec at [%u]\n", __func__, i);
374 if (c->ic_maxregpower == 0) {
375 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
376 "%s: invalid channel spec, zero maxregpower, "
377 "freq %u flags 0x%x\n", __func__,
378 c->ic_freq, c->ic_flags);
382 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
383 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
384 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
385 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
387 if (c->ic_maxpower == 0)
388 c->ic_maxpower = 2*c->ic_maxregpower;
391 /* XXX bandaid; a running vap will likely crash */
392 if (!allvapsdown(ic)) {
393 IEEE80211_UNLOCK(ic);
394 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
395 "%s: reject: vaps are running\n", __func__);
398 error = ic->ic_setregdomain(ic, ®->rd,
399 reg->chaninfo.ic_nchans, reg->chaninfo.ic_chans);
401 IEEE80211_UNLOCK(ic);
402 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
403 "%s: driver rejected request, error %u\n", __func__, error);
407 * Commit: copy in new channel table and reset media state.
408 * On return the state machines will be clocked so all vaps
409 * will reset their state.
411 * XXX ic_bsschan is marked undefined, must have vap's in
412 * INIT state or we blow up forcing stations off
415 * Save any desired channel for restore below. Note this
416 * needs to be done for all vaps but for now we only do
417 * the one where the ioctl is issued.
419 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
420 desfreq = vap->iv_des_chan->ic_freq;
421 desflags = vap->iv_des_chan->ic_flags;
423 /* regdomain parameters */
424 memcpy(&ic->ic_regdomain, ®->rd, sizeof(reg->rd));
426 memcpy(ic->ic_channels, reg->chaninfo.ic_chans,
427 reg->chaninfo.ic_nchans * sizeof(struct ieee80211_channel));
428 ic->ic_nchans = reg->chaninfo.ic_nchans;
429 memset(&ic->ic_channels[ic->ic_nchans], 0,
430 (IEEE80211_CHAN_MAX - ic->ic_nchans) *
431 sizeof(struct ieee80211_channel));
432 ieee80211_chan_init(ic);
435 * Invalidate channel-related state.
437 if (ic->ic_countryie != NULL) {
438 IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
439 ic->ic_countryie = NULL;
441 ieee80211_scan_flush(vap);
442 ieee80211_dfs_reset(ic);
443 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
444 c = ieee80211_find_channel(ic, desfreq, desflags);
445 /* NB: may be NULL if not present in new channel list */
446 vap->iv_des_chan = (c != NULL) ? c : IEEE80211_CHAN_ANYC;
448 IEEE80211_UNLOCK(ic);