ieee80211_regdomain.c revision 188778
1170530Ssam/*- 2178354Ssam * Copyright (c) 2005-2008 Sam Leffler, Errno Consulting 3170530Ssam * All rights reserved. 4170530Ssam * 5170530Ssam * Redistribution and use in source and binary forms, with or without 6170530Ssam * modification, are permitted provided that the following conditions 7170530Ssam * are met: 8170530Ssam * 1. Redistributions of source code must retain the above copyright 9170530Ssam * notice, this list of conditions and the following disclaimer. 10170530Ssam * 2. Redistributions in binary form must reproduce the above copyright 11170530Ssam * notice, this list of conditions and the following disclaimer in the 12170530Ssam * documentation and/or other materials provided with the distribution. 13170530Ssam * 14170530Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15170530Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16170530Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17170530Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18170530Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19170530Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20170530Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21170530Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22170530Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23170530Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24170530Ssam */ 25170530Ssam 26170530Ssam#include <sys/cdefs.h> 27170530Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_regdomain.c 188778 2009-02-19 04:44:18Z sam $"); 28170530Ssam 29170530Ssam/* 30170530Ssam * IEEE 802.11 regdomain support. 31170530Ssam */ 32178354Ssam#include "opt_wlan.h" 33170530Ssam 34170530Ssam#include <sys/param.h> 35170530Ssam#include <sys/systm.h> 36170530Ssam#include <sys/kernel.h> 37170530Ssam 38170530Ssam#include <sys/socket.h> 39170530Ssam 40170530Ssam#include <net/if.h> 41170530Ssam#include <net/if_media.h> 42170530Ssam 43170530Ssam#include <net80211/ieee80211_var.h> 44170530Ssam#include <net80211/ieee80211_regdomain.h> 45170530Ssam 46178354Ssamstatic void 47187800Ssamnull_getradiocaps(struct ieee80211com *ic, int maxchan, 48187800Ssam int *n, struct ieee80211_channel *c) 49178354Ssam{ 50178354Ssam /* just feed back the current channel list */ 51187800Ssam if (maxchan > ic->ic_nchans) 52187800Ssam maxchan = ic->ic_nchans; 53187800Ssam memcpy(c, ic->ic_channels, maxchan*sizeof(struct ieee80211_channel)); 54187834Ssam *n = maxchan; 55178354Ssam} 56178354Ssam 57178354Ssamstatic int 58178354Ssamnull_setregdomain(struct ieee80211com *ic, 59178354Ssam struct ieee80211_regdomain *rd, 60178354Ssam int nchans, struct ieee80211_channel chans[]) 61178354Ssam{ 62178354Ssam return 0; /* accept anything */ 63178354Ssam} 64178354Ssam 65170530Ssamvoid 66170530Ssamieee80211_regdomain_attach(struct ieee80211com *ic) 67170530Ssam{ 68178354Ssam if (ic->ic_regdomain.regdomain == 0 && 69178354Ssam ic->ic_regdomain.country == CTRY_DEFAULT) { 70178354Ssam ic->ic_regdomain.country = CTRY_UNITED_STATES; /* XXX */ 71178354Ssam ic->ic_regdomain.location = ' '; /* both */ 72178354Ssam ic->ic_regdomain.isocc[0] = 'U'; /* XXX */ 73178354Ssam ic->ic_regdomain.isocc[1] = 'S'; /* XXX */ 74187802Ssam /* NB: driver calls ieee80211_init_channels or similar */ 75178354Ssam } 76178354Ssam ic->ic_getradiocaps = null_getradiocaps; 77178354Ssam ic->ic_setregdomain = null_setregdomain; 78170530Ssam} 79170530Ssam 80170530Ssamvoid 81170530Ssamieee80211_regdomain_detach(struct ieee80211com *ic) 82170530Ssam{ 83178354Ssam if (ic->ic_countryie != NULL) { 84178354Ssam free(ic->ic_countryie, M_80211_NODE_IE); 85178354Ssam ic->ic_countryie = NULL; 86178354Ssam } 87170530Ssam} 88170530Ssam 89178354Ssamvoid 90178354Ssamieee80211_regdomain_vattach(struct ieee80211vap *vap) 91178354Ssam{ 92178354Ssam} 93178354Ssam 94178354Ssamvoid 95178354Ssamieee80211_regdomain_vdetach(struct ieee80211vap *vap) 96178354Ssam{ 97178354Ssam} 98178354Ssam 99170530Ssamstatic void 100170530Ssamaddchan(struct ieee80211com *ic, int ieee, int flags) 101170530Ssam{ 102170530Ssam struct ieee80211_channel *c; 103170530Ssam 104170530Ssam c = &ic->ic_channels[ic->ic_nchans++]; 105170530Ssam c->ic_freq = ieee80211_ieee2mhz(ieee, flags); 106170530Ssam c->ic_ieee = ieee; 107170530Ssam c->ic_flags = flags; 108178354Ssam c->ic_extieee = 0; 109170530Ssam} 110170530Ssam 111170530Ssam/* 112170530Ssam * Setup the channel list for the specified regulatory domain, 113170530Ssam * country code, and operating modes. This interface is used 114170530Ssam * when a driver does not obtain the channel list from another 115170530Ssam * source (such as firmware). 116170530Ssam */ 117178354Ssamint 118170530Ssamieee80211_init_channels(struct ieee80211com *ic, 119178354Ssam const struct ieee80211_regdomain *rd, const uint8_t bands[]) 120170530Ssam{ 121170530Ssam int i; 122170530Ssam 123170530Ssam /* XXX just do something for now */ 124170530Ssam ic->ic_nchans = 0; 125178354Ssam if (isset(bands, IEEE80211_MODE_11B) || 126178354Ssam isset(bands, IEEE80211_MODE_11G)) { 127178354Ssam int maxchan = 11; 128178354Ssam if (rd != NULL && rd->ecm) 129178354Ssam maxchan = 14; 130178354Ssam for (i = 1; i <= maxchan; i++) { 131178354Ssam if (isset(bands, IEEE80211_MODE_11B)) 132170530Ssam addchan(ic, i, IEEE80211_CHAN_B); 133178354Ssam if (isset(bands, IEEE80211_MODE_11G)) 134170530Ssam addchan(ic, i, IEEE80211_CHAN_G); 135170530Ssam } 136170530Ssam } 137178354Ssam if (isset(bands, IEEE80211_MODE_11A)) { 138170530Ssam for (i = 36; i <= 64; i += 4) 139170530Ssam addchan(ic, i, IEEE80211_CHAN_A); 140170530Ssam for (i = 100; i <= 140; i += 4) 141170530Ssam addchan(ic, i, IEEE80211_CHAN_A); 142170530Ssam for (i = 149; i <= 161; i += 4) 143170530Ssam addchan(ic, i, IEEE80211_CHAN_A); 144170530Ssam } 145178354Ssam if (rd != NULL) 146178354Ssam ic->ic_regdomain = *rd; 147178354Ssam 148178354Ssam return 0; 149170530Ssam} 150170530Ssam 151178354Ssamstatic __inline int 152178354Ssamchancompar(const void *a, const void *b) 153178354Ssam{ 154178354Ssam const struct ieee80211_channel *ca = a; 155178354Ssam const struct ieee80211_channel *cb = b; 156178354Ssam 157178354Ssam return (ca->ic_freq == cb->ic_freq) ? 158178354Ssam (ca->ic_flags & IEEE80211_CHAN_ALL) - 159178354Ssam (cb->ic_flags & IEEE80211_CHAN_ALL) : 160178354Ssam ca->ic_freq - cb->ic_freq; 161178354Ssam} 162178354Ssam 163170530Ssam/* 164178354Ssam * Insertion sort. 165170530Ssam */ 166178354Ssam#define swap(_a, _b, _size) { \ 167178354Ssam uint8_t *s = _b; \ 168178354Ssam int i = _size; \ 169178354Ssam do { \ 170178354Ssam uint8_t tmp = *_a; \ 171178354Ssam *_a++ = *s; \ 172178354Ssam *s++ = tmp; \ 173178354Ssam } while (--i); \ 174178354Ssam _a -= _size; \ 175178354Ssam} 176178354Ssam 177178354Ssamstatic void 178178354Ssamsort_channels(void *a, size_t n, size_t size) 179170530Ssam{ 180178354Ssam uint8_t *aa = a; 181178354Ssam uint8_t *ai, *t; 182178354Ssam 183178354Ssam KASSERT(n > 0, ("no channels")); 184178354Ssam for (ai = aa+size; --n >= 1; ai += size) 185178354Ssam for (t = ai; t > aa; t -= size) { 186178354Ssam uint8_t *u = t - size; 187178354Ssam if (chancompar(u, t) <= 0) 188178354Ssam break; 189178354Ssam swap(u, t, size); 190178354Ssam } 191178354Ssam} 192178354Ssam#undef swap 193178354Ssam 194178354Ssam/* 195178354Ssam * Order channels w/ the same frequency so that 196178354Ssam * b < g < htg and a < hta. This is used to optimize 197178354Ssam * channel table lookups and some user applications 198178354Ssam * may also depend on it (though they should not). 199178354Ssam */ 200178354Ssamvoid 201178354Ssamieee80211_sort_channels(struct ieee80211_channel chans[], int nchans) 202178354Ssam{ 203178354Ssam if (nchans > 0) 204178354Ssam sort_channels(chans, nchans, sizeof(struct ieee80211_channel)); 205178354Ssam} 206178354Ssam 207178354Ssam/* 208178354Ssam * Allocate and construct a Country Information IE. 209178354Ssam */ 210178354Ssamstruct ieee80211_appie * 211178354Ssamieee80211_alloc_countryie(struct ieee80211com *ic) 212178354Ssam{ 213170530Ssam#define CHAN_UNINTERESTING \ 214170530Ssam (IEEE80211_CHAN_TURBO | IEEE80211_CHAN_STURBO | \ 215170530Ssam IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER) 216170530Ssam /* XXX what about auto? */ 217170530Ssam /* flag set of channels to be excluded */ 218170530Ssam static const int skipflags[IEEE80211_MODE_MAX] = { 219188778Ssam [IEEE80211_MODE_AUTO] = CHAN_UNINTERESTING, 220188778Ssam [IEEE80211_MODE_11A] = CHAN_UNINTERESTING, 221188778Ssam [IEEE80211_MODE_11B] = CHAN_UNINTERESTING, 222188778Ssam [IEEE80211_MODE_11G] = CHAN_UNINTERESTING, 223188778Ssam [IEEE80211_MODE_FH] = CHAN_UNINTERESTING 224188778Ssam | IEEE80211_CHAN_OFDM 225188778Ssam | IEEE80211_CHAN_CCK 226188778Ssam | IEEE80211_CHAN_DYN, 227188778Ssam [IEEE80211_MODE_TURBO_A] = CHAN_UNINTERESTING, 228188778Ssam [IEEE80211_MODE_TURBO_G] = CHAN_UNINTERESTING, 229188778Ssam [IEEE80211_MODE_STURBO_A] = CHAN_UNINTERESTING, 230188778Ssam [IEEE80211_MODE_11NA] = CHAN_UNINTERESTING, 231188778Ssam [IEEE80211_MODE_11NG] = CHAN_UNINTERESTING, 232170530Ssam }; 233178354Ssam const struct ieee80211_regdomain *rd = &ic->ic_regdomain; 234178354Ssam uint8_t nextchan, chans[IEEE80211_CHAN_BYTES], *frm; 235178354Ssam struct ieee80211_appie *aie; 236178354Ssam struct ieee80211_country_ie *ie; 237178354Ssam int i, skip, nruns; 238170530Ssam 239178354Ssam aie = malloc(IEEE80211_COUNTRY_MAX_SIZE, M_80211_NODE_IE, 240178354Ssam M_NOWAIT | M_ZERO); 241178354Ssam if (aie == NULL) { 242178354Ssam if_printf(ic->ic_ifp, 243178354Ssam "%s: unable to allocate memory for country ie\n", __func__); 244178354Ssam /* XXX stat */ 245178354Ssam return NULL; 246178354Ssam } 247178354Ssam ie = (struct ieee80211_country_ie *) aie->ie_data; 248170530Ssam ie->ie = IEEE80211_ELEMID_COUNTRY; 249178354Ssam if (rd->isocc[0] == '\0') { 250178354Ssam if_printf(ic->ic_ifp, "no ISO country string for cc %d; " 251178354Ssam "using blanks\n", rd->country); 252178354Ssam ie->cc[0] = ie->cc[1] = ' '; 253178354Ssam } else { 254178354Ssam ie->cc[0] = rd->isocc[0]; 255178354Ssam ie->cc[1] = rd->isocc[1]; 256170530Ssam } 257170530Ssam /* 258178354Ssam * Indoor/Outdoor portion of country string: 259170530Ssam * 'I' indoor only 260170530Ssam * 'O' outdoor only 261170530Ssam * ' ' all enviroments 262170530Ssam */ 263178354Ssam ie->cc[2] = (rd->location == 'I' ? 'I' : 264178354Ssam rd->location == 'O' ? 'O' : ' '); 265170530Ssam /* 266170530Ssam * Run-length encoded channel+max tx power info. 267170530Ssam */ 268170530Ssam frm = (uint8_t *)&ie->band[0]; 269170530Ssam nextchan = 0; /* NB: impossible channel # */ 270178354Ssam nruns = 0; 271170530Ssam memset(chans, 0, sizeof(chans)); 272178354Ssam skip = skipflags[ieee80211_chan2mode(ic->ic_bsschan)]; 273188778Ssam if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) 274188778Ssam skip |= IEEE80211_CHAN_2GHZ; 275188778Ssam else if (IEEE80211_IS_CHAN_2GHZ(ic->ic_bsschan)) 276188778Ssam skip |= IEEE80211_CHAN_5GHZ; 277170530Ssam for (i = 0; i < ic->ic_nchans; i++) { 278170530Ssam const struct ieee80211_channel *c = &ic->ic_channels[i]; 279170530Ssam 280170530Ssam if (isset(chans, c->ic_ieee)) /* suppress dup's */ 281170530Ssam continue; 282172204Ssam if (c->ic_flags & skip) /* skip band, etc. */ 283170530Ssam continue; 284170530Ssam setbit(chans, c->ic_ieee); 285170530Ssam if (c->ic_ieee != nextchan || 286170530Ssam c->ic_maxregpower != frm[-1]) { /* new run */ 287178354Ssam if (nruns == IEEE80211_COUNTRY_MAX_BANDS) { 288178354Ssam if_printf(ic->ic_ifp, "%s: country ie too big, " 289178354Ssam "runs > max %d, truncating\n", 290178354Ssam __func__, IEEE80211_COUNTRY_MAX_BANDS); 291178354Ssam /* XXX stat? fail? */ 292178354Ssam break; 293178354Ssam } 294170530Ssam frm[0] = c->ic_ieee; /* starting channel # */ 295170530Ssam frm[1] = 1; /* # channels in run */ 296170530Ssam frm[2] = c->ic_maxregpower; /* tx power cap */ 297170530Ssam frm += 3; 298170530Ssam nextchan = c->ic_ieee + 1; /* overflow? */ 299178354Ssam nruns++; 300170530Ssam } else { /* extend run */ 301170530Ssam frm[-2]++; 302170530Ssam nextchan++; 303170530Ssam } 304170530Ssam } 305170530Ssam ie->len = frm - ie->cc; 306171985Ssephe if (ie->len & 1) { /* Zero pad to multiple of 2 */ 307170530Ssam ie->len++; 308171985Ssephe *frm++ = 0; 309171985Ssephe } 310178354Ssam aie->ie_len = frm - aie->ie_data; 311178354Ssam 312178354Ssam return aie; 313170530Ssam#undef CHAN_UNINTERESTING 314170530Ssam} 315170530Ssam 316178354Ssamstatic int 317178354Ssamallvapsdown(struct ieee80211com *ic) 318170530Ssam{ 319178354Ssam struct ieee80211vap *vap; 320170530Ssam 321178354Ssam IEEE80211_LOCK_ASSERT(ic); 322178354Ssam TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 323178354Ssam if (vap->iv_state != IEEE80211_S_INIT) 324178354Ssam return 0; 325178354Ssam return 1; 326170530Ssam} 327170530Ssam 328170530Ssamint 329178354Ssamieee80211_setregdomain(struct ieee80211vap *vap, 330178354Ssam struct ieee80211_regdomain_req *reg) 331170530Ssam{ 332178354Ssam struct ieee80211com *ic = vap->iv_ic; 333178354Ssam struct ieee80211_channel *c; 334178354Ssam int desfreq = 0, desflags = 0; /* XXX silence gcc complaint */ 335178354Ssam int error, i; 336170530Ssam 337178354Ssam if (reg->rd.location != 'I' && reg->rd.location != 'O' && 338184272Ssam reg->rd.location != ' ') { 339184272Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL, 340184272Ssam "%s: invalid location 0x%x\n", __func__, reg->rd.location); 341178354Ssam return EINVAL; 342184272Ssam } 343184272Ssam if (reg->rd.isocc[0] == '\0' || reg->rd.isocc[1] == '\0') { 344184272Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL, 345184272Ssam "%s: invalid iso cc 0x%x:0x%x\n", __func__, 346184272Ssam reg->rd.isocc[0], reg->rd.isocc[1]); 347178354Ssam return EINVAL; 348184272Ssam } 349186107Ssam if (reg->chaninfo.ic_nchans > IEEE80211_CHAN_MAX) { 350184272Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL, 351184272Ssam "%s: too many channels %u, max %u\n", __func__, 352184272Ssam reg->chaninfo.ic_nchans, IEEE80211_CHAN_MAX); 353178354Ssam return EINVAL; 354184272Ssam } 355178354Ssam /* 356178354Ssam * Calculate freq<->IEEE mapping and default max tx power 357178354Ssam * for channels not setup. The driver can override these 358178354Ssam * setting to reflect device properties/requirements. 359178354Ssam */ 360178354Ssam for (i = 0; i < reg->chaninfo.ic_nchans; i++) { 361178354Ssam c = ®->chaninfo.ic_chans[i]; 362184272Ssam if (c->ic_freq == 0 || c->ic_flags == 0) { 363184272Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL, 364184272Ssam "%s: invalid channel spec at [%u]\n", __func__, i); 365178354Ssam return EINVAL; 366184272Ssam } 367184272Ssam if (c->ic_maxregpower == 0) { 368184272Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL, 369184272Ssam "%s: invalid channel spec, zero maxregpower, " 370184272Ssam "freq %u flags 0x%x\n", __func__, 371184272Ssam c->ic_freq, c->ic_flags); 372178354Ssam return EINVAL; 373184272Ssam } 374178354Ssam if (c->ic_ieee == 0) 375178354Ssam c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags); 376178354Ssam if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0) 377178354Ssam c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq + 378178354Ssam (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20), 379178354Ssam c->ic_flags); 380178354Ssam if (c->ic_maxpower == 0) 381178354Ssam c->ic_maxpower = 2*c->ic_maxregpower; 382170530Ssam } 383178354Ssam IEEE80211_LOCK(ic); 384187635Ssam /* XXX bandaid; a running vap will likely crash */ 385187635Ssam if (!allvapsdown(ic)) { 386187635Ssam IEEE80211_UNLOCK(ic); 387187635Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL, 388187635Ssam "%s: reject: vaps are running\n", __func__); 389187635Ssam return EBUSY; 390187635Ssam } 391178354Ssam error = ic->ic_setregdomain(ic, ®->rd, 392178354Ssam reg->chaninfo.ic_nchans, reg->chaninfo.ic_chans); 393178354Ssam if (error != 0) { 394178354Ssam IEEE80211_UNLOCK(ic); 395184272Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL, 396184272Ssam "%s: driver rejected request, error %u\n", __func__, error); 397178354Ssam return error; 398178354Ssam } 399178354Ssam /* 400178354Ssam * Commit: copy in new channel table and reset media state. 401178354Ssam * On return the state machines will be clocked so all vaps 402178354Ssam * will reset their state. 403178354Ssam * 404178354Ssam * XXX ic_bsschan is marked undefined, must have vap's in 405178354Ssam * INIT state or we blow up forcing stations off 406178354Ssam */ 407178354Ssam /* 408178354Ssam * Save any desired channel for restore below. Note this 409178354Ssam * needs to be done for all vaps but for now we only do 410178354Ssam * the one where the ioctl is issued. 411178354Ssam */ 412178354Ssam if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) { 413178354Ssam desfreq = vap->iv_des_chan->ic_freq; 414178354Ssam desflags = vap->iv_des_chan->ic_flags; 415178354Ssam } 416178354Ssam /* regdomain parameters */ 417178354Ssam memcpy(&ic->ic_regdomain, ®->rd, sizeof(reg->rd)); 418178354Ssam /* channel table */ 419178354Ssam memcpy(ic->ic_channels, reg->chaninfo.ic_chans, 420178354Ssam reg->chaninfo.ic_nchans * sizeof(struct ieee80211_channel)); 421178354Ssam ic->ic_nchans = reg->chaninfo.ic_nchans; 422178354Ssam memset(&ic->ic_channels[ic->ic_nchans], 0, 423178354Ssam (IEEE80211_CHAN_MAX - ic->ic_nchans) * 424178354Ssam sizeof(struct ieee80211_channel)); 425178354Ssam ieee80211_media_init(ic); 426178354Ssam 427178354Ssam /* 428178354Ssam * Invalidate channel-related state. 429178354Ssam */ 430178354Ssam if (ic->ic_countryie != NULL) { 431178354Ssam free(ic->ic_countryie, M_80211_NODE_IE); 432178354Ssam ic->ic_countryie = NULL; 433178354Ssam } 434178354Ssam ieee80211_scan_flush(vap); 435178354Ssam ieee80211_dfs_reset(ic); 436178354Ssam if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) { 437188427Ssam c = ieee80211_find_channel(ic, desfreq, desflags); 438178354Ssam /* NB: may be NULL if not present in new channel list */ 439188427Ssam vap->iv_des_chan = (c != NULL) ? c : IEEE80211_CHAN_ANYC; 440178354Ssam } 441178354Ssam IEEE80211_UNLOCK(ic); 442178354Ssam 443178354Ssam return 0; 444170530Ssam} 445