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