ieee80211_regdomain.c revision 187834
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 187834 2009-01-28 18:37:46Z 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] = { 219170530Ssam CHAN_UNINTERESTING, /* MODE_AUTO */ 220170530Ssam CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_11A */ 221170530Ssam CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_11B */ 222170530Ssam CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_11G */ 223170530Ssam CHAN_UNINTERESTING | IEEE80211_CHAN_OFDM | /* MODE_FH */ 224170530Ssam IEEE80211_CHAN_CCK | IEEE80211_CHAN_DYN, 225170530Ssam CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_TURBO_A */ 226170530Ssam CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_TURBO_G */ 227170530Ssam CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_STURBO_A */ 228170530Ssam CHAN_UNINTERESTING | IEEE80211_CHAN_2GHZ, /* MODE_11NA */ 229170530Ssam CHAN_UNINTERESTING | IEEE80211_CHAN_5GHZ, /* MODE_11NG */ 230170530Ssam }; 231178354Ssam const struct ieee80211_regdomain *rd = &ic->ic_regdomain; 232178354Ssam uint8_t nextchan, chans[IEEE80211_CHAN_BYTES], *frm; 233178354Ssam struct ieee80211_appie *aie; 234178354Ssam struct ieee80211_country_ie *ie; 235178354Ssam int i, skip, nruns; 236170530Ssam 237178354Ssam aie = malloc(IEEE80211_COUNTRY_MAX_SIZE, M_80211_NODE_IE, 238178354Ssam M_NOWAIT | M_ZERO); 239178354Ssam if (aie == NULL) { 240178354Ssam if_printf(ic->ic_ifp, 241178354Ssam "%s: unable to allocate memory for country ie\n", __func__); 242178354Ssam /* XXX stat */ 243178354Ssam return NULL; 244178354Ssam } 245178354Ssam ie = (struct ieee80211_country_ie *) aie->ie_data; 246170530Ssam ie->ie = IEEE80211_ELEMID_COUNTRY; 247178354Ssam if (rd->isocc[0] == '\0') { 248178354Ssam if_printf(ic->ic_ifp, "no ISO country string for cc %d; " 249178354Ssam "using blanks\n", rd->country); 250178354Ssam ie->cc[0] = ie->cc[1] = ' '; 251178354Ssam } else { 252178354Ssam ie->cc[0] = rd->isocc[0]; 253178354Ssam ie->cc[1] = rd->isocc[1]; 254170530Ssam } 255170530Ssam /* 256178354Ssam * Indoor/Outdoor portion of country string: 257170530Ssam * 'I' indoor only 258170530Ssam * 'O' outdoor only 259170530Ssam * ' ' all enviroments 260170530Ssam */ 261178354Ssam ie->cc[2] = (rd->location == 'I' ? 'I' : 262178354Ssam rd->location == 'O' ? 'O' : ' '); 263170530Ssam /* 264170530Ssam * Run-length encoded channel+max tx power info. 265170530Ssam */ 266170530Ssam frm = (uint8_t *)&ie->band[0]; 267170530Ssam nextchan = 0; /* NB: impossible channel # */ 268178354Ssam nruns = 0; 269170530Ssam memset(chans, 0, sizeof(chans)); 270178354Ssam skip = skipflags[ieee80211_chan2mode(ic->ic_bsschan)]; 271170530Ssam for (i = 0; i < ic->ic_nchans; i++) { 272170530Ssam const struct ieee80211_channel *c = &ic->ic_channels[i]; 273170530Ssam 274170530Ssam if (isset(chans, c->ic_ieee)) /* suppress dup's */ 275170530Ssam continue; 276172204Ssam if (c->ic_flags & skip) /* skip band, etc. */ 277170530Ssam continue; 278170530Ssam setbit(chans, c->ic_ieee); 279170530Ssam if (c->ic_ieee != nextchan || 280170530Ssam c->ic_maxregpower != frm[-1]) { /* new run */ 281178354Ssam if (nruns == IEEE80211_COUNTRY_MAX_BANDS) { 282178354Ssam if_printf(ic->ic_ifp, "%s: country ie too big, " 283178354Ssam "runs > max %d, truncating\n", 284178354Ssam __func__, IEEE80211_COUNTRY_MAX_BANDS); 285178354Ssam /* XXX stat? fail? */ 286178354Ssam break; 287178354Ssam } 288170530Ssam frm[0] = c->ic_ieee; /* starting channel # */ 289170530Ssam frm[1] = 1; /* # channels in run */ 290170530Ssam frm[2] = c->ic_maxregpower; /* tx power cap */ 291170530Ssam frm += 3; 292170530Ssam nextchan = c->ic_ieee + 1; /* overflow? */ 293178354Ssam nruns++; 294170530Ssam } else { /* extend run */ 295170530Ssam frm[-2]++; 296170530Ssam nextchan++; 297170530Ssam } 298170530Ssam } 299170530Ssam ie->len = frm - ie->cc; 300171985Ssephe if (ie->len & 1) { /* Zero pad to multiple of 2 */ 301170530Ssam ie->len++; 302171985Ssephe *frm++ = 0; 303171985Ssephe } 304178354Ssam aie->ie_len = frm - aie->ie_data; 305178354Ssam 306178354Ssam return aie; 307170530Ssam#undef CHAN_UNINTERESTING 308170530Ssam} 309170530Ssam 310178354Ssamstatic int 311178354Ssamallvapsdown(struct ieee80211com *ic) 312170530Ssam{ 313178354Ssam struct ieee80211vap *vap; 314170530Ssam 315178354Ssam IEEE80211_LOCK_ASSERT(ic); 316178354Ssam TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) 317178354Ssam if (vap->iv_state != IEEE80211_S_INIT) 318178354Ssam return 0; 319178354Ssam return 1; 320170530Ssam} 321170530Ssam 322170530Ssamint 323178354Ssamieee80211_setregdomain(struct ieee80211vap *vap, 324178354Ssam struct ieee80211_regdomain_req *reg) 325170530Ssam{ 326178354Ssam struct ieee80211com *ic = vap->iv_ic; 327178354Ssam struct ieee80211_channel *c; 328178354Ssam int desfreq = 0, desflags = 0; /* XXX silence gcc complaint */ 329178354Ssam int error, i; 330170530Ssam 331178354Ssam if (reg->rd.location != 'I' && reg->rd.location != 'O' && 332184272Ssam reg->rd.location != ' ') { 333184272Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL, 334184272Ssam "%s: invalid location 0x%x\n", __func__, reg->rd.location); 335178354Ssam return EINVAL; 336184272Ssam } 337184272Ssam if (reg->rd.isocc[0] == '\0' || reg->rd.isocc[1] == '\0') { 338184272Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL, 339184272Ssam "%s: invalid iso cc 0x%x:0x%x\n", __func__, 340184272Ssam reg->rd.isocc[0], reg->rd.isocc[1]); 341178354Ssam return EINVAL; 342184272Ssam } 343186107Ssam if (reg->chaninfo.ic_nchans > IEEE80211_CHAN_MAX) { 344184272Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL, 345184272Ssam "%s: too many channels %u, max %u\n", __func__, 346184272Ssam reg->chaninfo.ic_nchans, IEEE80211_CHAN_MAX); 347178354Ssam return EINVAL; 348184272Ssam } 349178354Ssam /* 350178354Ssam * Calculate freq<->IEEE mapping and default max tx power 351178354Ssam * for channels not setup. The driver can override these 352178354Ssam * setting to reflect device properties/requirements. 353178354Ssam */ 354178354Ssam for (i = 0; i < reg->chaninfo.ic_nchans; i++) { 355178354Ssam c = ®->chaninfo.ic_chans[i]; 356184272Ssam if (c->ic_freq == 0 || c->ic_flags == 0) { 357184272Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL, 358184272Ssam "%s: invalid channel spec at [%u]\n", __func__, i); 359178354Ssam return EINVAL; 360184272Ssam } 361184272Ssam if (c->ic_maxregpower == 0) { 362184272Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL, 363184272Ssam "%s: invalid channel spec, zero maxregpower, " 364184272Ssam "freq %u flags 0x%x\n", __func__, 365184272Ssam c->ic_freq, c->ic_flags); 366178354Ssam return EINVAL; 367184272Ssam } 368178354Ssam if (c->ic_ieee == 0) 369178354Ssam c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags); 370178354Ssam if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0) 371178354Ssam c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq + 372178354Ssam (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20), 373178354Ssam c->ic_flags); 374178354Ssam if (c->ic_maxpower == 0) 375178354Ssam c->ic_maxpower = 2*c->ic_maxregpower; 376170530Ssam } 377178354Ssam IEEE80211_LOCK(ic); 378187635Ssam /* XXX bandaid; a running vap will likely crash */ 379187635Ssam if (!allvapsdown(ic)) { 380187635Ssam IEEE80211_UNLOCK(ic); 381187635Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL, 382187635Ssam "%s: reject: vaps are running\n", __func__); 383187635Ssam return EBUSY; 384187635Ssam } 385178354Ssam error = ic->ic_setregdomain(ic, ®->rd, 386178354Ssam reg->chaninfo.ic_nchans, reg->chaninfo.ic_chans); 387178354Ssam if (error != 0) { 388178354Ssam IEEE80211_UNLOCK(ic); 389184272Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL, 390184272Ssam "%s: driver rejected request, error %u\n", __func__, error); 391178354Ssam return error; 392178354Ssam } 393178354Ssam /* 394178354Ssam * Commit: copy in new channel table and reset media state. 395178354Ssam * On return the state machines will be clocked so all vaps 396178354Ssam * will reset their state. 397178354Ssam * 398178354Ssam * XXX ic_bsschan is marked undefined, must have vap's in 399178354Ssam * INIT state or we blow up forcing stations off 400178354Ssam */ 401178354Ssam /* 402178354Ssam * Save any desired channel for restore below. Note this 403178354Ssam * needs to be done for all vaps but for now we only do 404178354Ssam * the one where the ioctl is issued. 405178354Ssam */ 406178354Ssam if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) { 407178354Ssam desfreq = vap->iv_des_chan->ic_freq; 408178354Ssam desflags = vap->iv_des_chan->ic_flags; 409178354Ssam } 410178354Ssam /* regdomain parameters */ 411178354Ssam memcpy(&ic->ic_regdomain, ®->rd, sizeof(reg->rd)); 412178354Ssam /* channel table */ 413178354Ssam memcpy(ic->ic_channels, reg->chaninfo.ic_chans, 414178354Ssam reg->chaninfo.ic_nchans * sizeof(struct ieee80211_channel)); 415178354Ssam ic->ic_nchans = reg->chaninfo.ic_nchans; 416178354Ssam memset(&ic->ic_channels[ic->ic_nchans], 0, 417178354Ssam (IEEE80211_CHAN_MAX - ic->ic_nchans) * 418178354Ssam sizeof(struct ieee80211_channel)); 419178354Ssam ieee80211_media_init(ic); 420178354Ssam 421178354Ssam /* 422178354Ssam * Invalidate channel-related state. 423178354Ssam */ 424178354Ssam if (ic->ic_countryie != NULL) { 425178354Ssam free(ic->ic_countryie, M_80211_NODE_IE); 426178354Ssam ic->ic_countryie = NULL; 427178354Ssam } 428178354Ssam ieee80211_scan_flush(vap); 429178354Ssam ieee80211_dfs_reset(ic); 430178354Ssam if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) { 431178354Ssam /* NB: may be NULL if not present in new channel list */ 432178354Ssam vap->iv_des_chan = ieee80211_find_channel(ic, desfreq, desflags); 433178354Ssam } 434178354Ssam IEEE80211_UNLOCK(ic); 435178354Ssam 436178354Ssam return 0; 437170530Ssam} 438