1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2001 Atsushi Onoe 5 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 /* 33 * IEEE 802.11 ioctl support (FreeBSD-specific) 34 */ 35 36 #include "opt_inet.h" 37 #include "opt_wlan.h" 38 39 #include <sys/endian.h> 40 #include <sys/param.h> 41 #include <sys/kernel.h> 42 #include <sys/malloc.h> 43 #include <sys/priv.h> 44 #include <sys/socket.h> 45 #include <sys/sockio.h> 46 #include <sys/systm.h> 47 48 #include <net/if.h> 49 #include <net/if_var.h> 50 #include <net/if_dl.h> 51 #include <net/if_media.h> 52 #include <net/ethernet.h> 53 54 #ifdef INET 55 #include <netinet/in.h> 56 #include <netinet/if_ether.h> 57 #endif 58 59 #include <net80211/ieee80211_var.h> 60 #include <net80211/ieee80211_ioctl.h> 61 #include <net80211/ieee80211_regdomain.h> 62 #include <net80211/ieee80211_input.h> 63 64 #define IS_UP_AUTO(_vap) \ 65 (IFNET_IS_UP_RUNNING((_vap)->iv_ifp) && \ 66 (_vap)->iv_roaming == IEEE80211_ROAMING_AUTO) 67 68 static const uint8_t zerobssid[IEEE80211_ADDR_LEN]; 69 static struct ieee80211_channel *findchannel(struct ieee80211com *, 70 int ieee, int mode); 71 static int ieee80211_scanreq(struct ieee80211vap *, 72 struct ieee80211_scan_req *); 73 74 static int 75 ieee80211_ioctl_getkey(struct ieee80211vap *vap, struct ieee80211req *ireq) 76 { 77 struct ieee80211com *ic = vap->iv_ic; 78 struct ieee80211_node *ni; 79 struct ieee80211req_key ik; 80 struct ieee80211_key *wk; 81 const struct ieee80211_cipher *cip; 82 u_int kid; 83 int error; 84 85 if (ireq->i_len != sizeof(ik)) 86 return EINVAL; 87 error = copyin(ireq->i_data, &ik, sizeof(ik)); 88 if (error) 89 return error; 90 kid = ik.ik_keyix; 91 if (kid == IEEE80211_KEYIX_NONE) { 92 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, ik.ik_macaddr); 93 if (ni == NULL) 94 return ENOENT; 95 wk = &ni->ni_ucastkey; 96 } else { 97 if (kid >= IEEE80211_WEP_NKID) 98 return EINVAL; 99 wk = &vap->iv_nw_keys[kid]; 100 IEEE80211_ADDR_COPY(&ik.ik_macaddr, vap->iv_bss->ni_macaddr); 101 ni = NULL; 102 } 103 cip = wk->wk_cipher; 104 ik.ik_type = cip->ic_cipher; 105 ik.ik_keylen = wk->wk_keylen; 106 ik.ik_flags = wk->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV); 107 if (wk->wk_keyix == vap->iv_def_txkey) 108 ik.ik_flags |= IEEE80211_KEY_DEFAULT; 109 /* XXX TODO: move priv check to ieee80211_freebsd.c */ 110 if (priv_check(curthread, PRIV_NET80211_VAP_GETKEY) == 0) { 111 /* NB: only root can read key data */ 112 ik.ik_keyrsc = wk->wk_keyrsc[IEEE80211_NONQOS_TID]; 113 ik.ik_keytsc = wk->wk_keytsc; 114 memcpy(ik.ik_keydata, wk->wk_key, wk->wk_keylen); 115 if (cip->ic_cipher == IEEE80211_CIPHER_TKIP) { 116 memcpy(ik.ik_keydata+wk->wk_keylen, 117 wk->wk_key + IEEE80211_KEYBUF_SIZE, 118 IEEE80211_MICBUF_SIZE); 119 ik.ik_keylen += IEEE80211_MICBUF_SIZE; 120 } 121 } else { 122 ik.ik_keyrsc = 0; 123 ik.ik_keytsc = 0; 124 memset(ik.ik_keydata, 0, sizeof(ik.ik_keydata)); 125 } 126 if (ni != NULL) 127 ieee80211_free_node(ni); 128 return copyout(&ik, ireq->i_data, sizeof(ik)); 129 } 130 131 static int 132 ieee80211_ioctl_getchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq) 133 { 134 struct ieee80211com *ic = vap->iv_ic; 135 136 if (sizeof(ic->ic_chan_active) < ireq->i_len) 137 ireq->i_len = sizeof(ic->ic_chan_active); 138 return copyout(&ic->ic_chan_active, ireq->i_data, ireq->i_len); 139 } 140 141 static int 142 ieee80211_ioctl_getchaninfo(struct ieee80211vap *vap, struct ieee80211req *ireq) 143 { 144 struct ieee80211com *ic = vap->iv_ic; 145 uint32_t space; 146 147 space = __offsetof(struct ieee80211req_chaninfo, 148 ic_chans[ic->ic_nchans]); 149 if (space > ireq->i_len) 150 space = ireq->i_len; 151 /* XXX assumes compatible layout */ 152 return copyout(&ic->ic_nchans, ireq->i_data, space); 153 } 154 155 static int 156 ieee80211_ioctl_getwpaie(struct ieee80211vap *vap, 157 struct ieee80211req *ireq, int req) 158 { 159 struct ieee80211_node *ni; 160 struct ieee80211req_wpaie2 *wpaie; 161 int error; 162 163 if (ireq->i_len < IEEE80211_ADDR_LEN) 164 return EINVAL; 165 wpaie = IEEE80211_MALLOC(sizeof(*wpaie), M_TEMP, 166 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 167 if (wpaie == NULL) 168 return ENOMEM; 169 error = copyin(ireq->i_data, wpaie->wpa_macaddr, IEEE80211_ADDR_LEN); 170 if (error != 0) 171 goto bad; 172 ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, wpaie->wpa_macaddr); 173 if (ni == NULL) { 174 error = ENOENT; 175 goto bad; 176 } 177 if (ni->ni_ies.wpa_ie != NULL) { 178 int ielen = ni->ni_ies.wpa_ie[1] + 2; 179 if (ielen > sizeof(wpaie->wpa_ie)) 180 ielen = sizeof(wpaie->wpa_ie); 181 memcpy(wpaie->wpa_ie, ni->ni_ies.wpa_ie, ielen); 182 } 183 if (req == IEEE80211_IOC_WPAIE2) { 184 if (ni->ni_ies.rsn_ie != NULL) { 185 int ielen = ni->ni_ies.rsn_ie[1] + 2; 186 if (ielen > sizeof(wpaie->rsn_ie)) 187 ielen = sizeof(wpaie->rsn_ie); 188 memcpy(wpaie->rsn_ie, ni->ni_ies.rsn_ie, ielen); 189 } 190 if (ireq->i_len > sizeof(struct ieee80211req_wpaie2)) 191 ireq->i_len = sizeof(struct ieee80211req_wpaie2); 192 } else { 193 /* compatibility op, may overwrite wpa ie */ 194 /* XXX check ic_flags? */ 195 if (ni->ni_ies.rsn_ie != NULL) { 196 int ielen = ni->ni_ies.rsn_ie[1] + 2; 197 if (ielen > sizeof(wpaie->wpa_ie)) 198 ielen = sizeof(wpaie->wpa_ie); 199 memcpy(wpaie->wpa_ie, ni->ni_ies.rsn_ie, ielen); 200 } 201 if (ireq->i_len > sizeof(struct ieee80211req_wpaie)) 202 ireq->i_len = sizeof(struct ieee80211req_wpaie); 203 } 204 ieee80211_free_node(ni); 205 error = copyout(wpaie, ireq->i_data, ireq->i_len); 206 bad: 207 IEEE80211_FREE(wpaie, M_TEMP); 208 return error; 209 } 210 211 static int 212 ieee80211_ioctl_getstastats(struct ieee80211vap *vap, struct ieee80211req *ireq) 213 { 214 struct ieee80211_node *ni; 215 uint8_t macaddr[IEEE80211_ADDR_LEN]; 216 const size_t off = __offsetof(struct ieee80211req_sta_stats, is_stats); 217 int error; 218 219 if (ireq->i_len < off) 220 return EINVAL; 221 error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN); 222 if (error != 0) 223 return error; 224 ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr); 225 if (ni == NULL) 226 return ENOENT; 227 if (ireq->i_len > sizeof(struct ieee80211req_sta_stats)) 228 ireq->i_len = sizeof(struct ieee80211req_sta_stats); 229 /* NB: copy out only the statistics */ 230 error = copyout(&ni->ni_stats, (uint8_t *) ireq->i_data + off, 231 ireq->i_len - off); 232 ieee80211_free_node(ni); 233 return error; 234 } 235 236 struct scanreq { 237 struct ieee80211req_scan_result *sr; 238 size_t space; 239 }; 240 241 static size_t 242 scan_space(const struct ieee80211_scan_entry *se, int *ielen) 243 { 244 size_t len; 245 246 *ielen = se->se_ies.len; 247 /* 248 * NB: ie's can be no more than 255 bytes and the max 802.11 249 * packet is <3Kbytes so we are sure this doesn't overflow 250 * 16-bits; if this is a concern we can drop the ie's. 251 */ 252 len = sizeof(struct ieee80211req_scan_result) + se->se_ssid[1] + 253 se->se_meshid[1] + *ielen; 254 return roundup(len, sizeof(uint32_t)); 255 } 256 257 static void 258 get_scan_space(void *arg, const struct ieee80211_scan_entry *se) 259 { 260 struct scanreq *req = arg; 261 int ielen; 262 263 req->space += scan_space(se, &ielen); 264 } 265 266 static void 267 get_scan_result(void *arg, const struct ieee80211_scan_entry *se) 268 { 269 struct scanreq *req = arg; 270 struct ieee80211req_scan_result *sr; 271 int ielen, len, nr, nxr; 272 uint8_t *cp; 273 274 len = scan_space(se, &ielen); 275 if (len > req->space) 276 return; 277 278 sr = req->sr; 279 KASSERT(len <= 65535 && ielen <= 65535, 280 ("len %u ssid %u ie %u", len, se->se_ssid[1], ielen)); 281 sr->isr_len = len; 282 sr->isr_ie_off = sizeof(struct ieee80211req_scan_result); 283 sr->isr_ie_len = ielen; 284 sr->isr_freq = se->se_chan->ic_freq; 285 sr->isr_flags = se->se_chan->ic_flags; 286 sr->isr_rssi = se->se_rssi; 287 sr->isr_noise = se->se_noise; 288 sr->isr_intval = se->se_intval; 289 sr->isr_capinfo = se->se_capinfo; 290 sr->isr_erp = se->se_erp; 291 IEEE80211_ADDR_COPY(sr->isr_bssid, se->se_bssid); 292 nr = min(se->se_rates[1], IEEE80211_RATE_MAXSIZE); 293 memcpy(sr->isr_rates, se->se_rates+2, nr); 294 nxr = min(se->se_xrates[1], IEEE80211_RATE_MAXSIZE - nr); 295 memcpy(sr->isr_rates+nr, se->se_xrates+2, nxr); 296 sr->isr_nrates = nr + nxr; 297 298 /* copy SSID */ 299 sr->isr_ssid_len = se->se_ssid[1]; 300 cp = ((uint8_t *)sr) + sr->isr_ie_off; 301 memcpy(cp, se->se_ssid+2, sr->isr_ssid_len); 302 303 /* copy mesh id */ 304 cp += sr->isr_ssid_len; 305 sr->isr_meshid_len = se->se_meshid[1]; 306 memcpy(cp, se->se_meshid+2, sr->isr_meshid_len); 307 cp += sr->isr_meshid_len; 308 309 if (ielen) 310 memcpy(cp, se->se_ies.data, ielen); 311 312 req->space -= len; 313 req->sr = (struct ieee80211req_scan_result *)(((uint8_t *)sr) + len); 314 } 315 316 static int 317 ieee80211_ioctl_getscanresults(struct ieee80211vap *vap, 318 struct ieee80211req *ireq) 319 { 320 struct scanreq req; 321 int error; 322 323 if (ireq->i_len < sizeof(struct scanreq)) 324 return EFAULT; 325 326 error = 0; 327 req.space = 0; 328 ieee80211_scan_iterate(vap, get_scan_space, &req); 329 if (req.space > ireq->i_len) 330 req.space = ireq->i_len; 331 if (req.space > 0) { 332 uint32_t space; 333 void *p; 334 335 space = req.space; 336 /* XXX M_WAITOK after driver lock released */ 337 p = IEEE80211_MALLOC(space, M_TEMP, 338 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 339 if (p == NULL) 340 return ENOMEM; 341 req.sr = p; 342 ieee80211_scan_iterate(vap, get_scan_result, &req); 343 ireq->i_len = space - req.space; 344 error = copyout(p, ireq->i_data, ireq->i_len); 345 IEEE80211_FREE(p, M_TEMP); 346 } else 347 ireq->i_len = 0; 348 349 return error; 350 } 351 352 struct stainforeq { 353 struct ieee80211req_sta_info *si; 354 size_t space; 355 }; 356 357 static size_t 358 sta_space(const struct ieee80211_node *ni, size_t *ielen) 359 { 360 *ielen = ni->ni_ies.len; 361 return roundup(sizeof(struct ieee80211req_sta_info) + *ielen, 362 sizeof(uint32_t)); 363 } 364 365 static void 366 get_sta_space(void *arg, struct ieee80211_node *ni) 367 { 368 struct stainforeq *req = arg; 369 size_t ielen; 370 371 if (ni->ni_vap->iv_opmode == IEEE80211_M_HOSTAP && 372 ni->ni_associd == 0) /* only associated stations */ 373 return; 374 req->space += sta_space(ni, &ielen); 375 } 376 377 static void 378 get_sta_info(void *arg, struct ieee80211_node *ni) 379 { 380 struct stainforeq *req = arg; 381 struct ieee80211vap *vap = ni->ni_vap; 382 struct ieee80211req_sta_info *si; 383 size_t ielen, len; 384 uint8_t *cp; 385 386 if (vap->iv_opmode == IEEE80211_M_HOSTAP && 387 ni->ni_associd == 0) /* only associated stations */ 388 return; 389 if (ni->ni_chan == IEEE80211_CHAN_ANYC) /* XXX bogus entry */ 390 return; 391 len = sta_space(ni, &ielen); 392 if (len > req->space) 393 return; 394 si = req->si; 395 si->isi_len = len; 396 si->isi_ie_off = sizeof(struct ieee80211req_sta_info); 397 si->isi_ie_len = ielen; 398 si->isi_freq = ni->ni_chan->ic_freq; 399 si->isi_flags = ni->ni_chan->ic_flags; 400 si->isi_state = ni->ni_flags; 401 si->isi_authmode = ni->ni_authmode; 402 vap->iv_ic->ic_node_getsignal(ni, &si->isi_rssi, &si->isi_noise); 403 vap->iv_ic->ic_node_getmimoinfo(ni, &si->isi_mimo); 404 si->isi_capinfo = ni->ni_capinfo; 405 si->isi_erp = ni->ni_erp; 406 IEEE80211_ADDR_COPY(si->isi_macaddr, ni->ni_macaddr); 407 si->isi_nrates = ni->ni_rates.rs_nrates; 408 if (si->isi_nrates > 15) 409 si->isi_nrates = 15; 410 memcpy(si->isi_rates, ni->ni_rates.rs_rates, si->isi_nrates); 411 si->isi_txrate = ni->ni_txrate; 412 if (si->isi_txrate & IEEE80211_RATE_MCS) { 413 const struct ieee80211_mcs_rates *mcs = 414 &ieee80211_htrates[ni->ni_txrate &~ IEEE80211_RATE_MCS]; 415 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) { 416 if (ni->ni_flags & IEEE80211_NODE_SGI40) 417 si->isi_txmbps = mcs->ht40_rate_800ns; 418 else 419 si->isi_txmbps = mcs->ht40_rate_400ns; 420 } else { 421 if (ni->ni_flags & IEEE80211_NODE_SGI20) 422 si->isi_txmbps = mcs->ht20_rate_800ns; 423 else 424 si->isi_txmbps = mcs->ht20_rate_400ns; 425 } 426 } else 427 si->isi_txmbps = si->isi_txrate; 428 si->isi_associd = ni->ni_associd; 429 si->isi_txpower = ni->ni_txpower; 430 si->isi_vlan = ni->ni_vlan; 431 if (ni->ni_flags & IEEE80211_NODE_QOS) { 432 memcpy(si->isi_txseqs, ni->ni_txseqs, sizeof(ni->ni_txseqs)); 433 memcpy(si->isi_rxseqs, ni->ni_rxseqs, sizeof(ni->ni_rxseqs)); 434 } else { 435 si->isi_txseqs[0] = ni->ni_txseqs[IEEE80211_NONQOS_TID]; 436 si->isi_rxseqs[0] = ni->ni_rxseqs[IEEE80211_NONQOS_TID]; 437 } 438 /* NB: leave all cases in case we relax ni_associd == 0 check */ 439 if (ieee80211_node_is_authorized(ni)) 440 si->isi_inact = vap->iv_inact_run; 441 else if (ni->ni_associd != 0 || 442 (vap->iv_opmode == IEEE80211_M_WDS && 443 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) 444 si->isi_inact = vap->iv_inact_auth; 445 else 446 si->isi_inact = vap->iv_inact_init; 447 si->isi_inact = (si->isi_inact - ni->ni_inact) * IEEE80211_INACT_WAIT; 448 si->isi_localid = ni->ni_mllid; 449 si->isi_peerid = ni->ni_mlpid; 450 si->isi_peerstate = ni->ni_mlstate; 451 452 if (ielen) { 453 cp = ((uint8_t *)si) + si->isi_ie_off; 454 memcpy(cp, ni->ni_ies.data, ielen); 455 } 456 457 req->si = (struct ieee80211req_sta_info *)(((uint8_t *)si) + len); 458 req->space -= len; 459 } 460 461 static int 462 getstainfo_common(struct ieee80211vap *vap, struct ieee80211req *ireq, 463 struct ieee80211_node *ni, size_t off) 464 { 465 struct ieee80211com *ic = vap->iv_ic; 466 struct stainforeq req; 467 size_t space; 468 void *p; 469 int error; 470 471 error = 0; 472 req.space = 0; 473 if (ni == NULL) { 474 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_sta_space, 475 &req); 476 } else 477 get_sta_space(&req, ni); 478 if (req.space > ireq->i_len) 479 req.space = ireq->i_len; 480 if (req.space > 0) { 481 space = req.space; 482 /* XXX M_WAITOK after driver lock released */ 483 p = IEEE80211_MALLOC(space, M_TEMP, 484 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 485 if (p == NULL) { 486 error = ENOMEM; 487 goto bad; 488 } 489 req.si = p; 490 if (ni == NULL) { 491 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, 492 get_sta_info, &req); 493 } else 494 get_sta_info(&req, ni); 495 ireq->i_len = space - req.space; 496 error = copyout(p, (uint8_t *) ireq->i_data+off, ireq->i_len); 497 IEEE80211_FREE(p, M_TEMP); 498 } else 499 ireq->i_len = 0; 500 bad: 501 if (ni != NULL) 502 ieee80211_free_node(ni); 503 return error; 504 } 505 506 static int 507 ieee80211_ioctl_getstainfo(struct ieee80211vap *vap, struct ieee80211req *ireq) 508 { 509 uint8_t macaddr[IEEE80211_ADDR_LEN]; 510 const size_t off = __offsetof(struct ieee80211req_sta_req, info); 511 struct ieee80211_node *ni; 512 int error; 513 514 if (ireq->i_len < sizeof(struct ieee80211req_sta_req)) 515 return EFAULT; 516 error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN); 517 if (error != 0) 518 return error; 519 if (IEEE80211_ADDR_EQ(macaddr, vap->iv_ifp->if_broadcastaddr)) { 520 ni = NULL; 521 } else { 522 ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr); 523 if (ni == NULL) 524 return ENOENT; 525 } 526 return getstainfo_common(vap, ireq, ni, off); 527 } 528 529 static int 530 ieee80211_ioctl_getstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq) 531 { 532 struct ieee80211_node *ni; 533 struct ieee80211req_sta_txpow txpow; 534 int error; 535 536 if (ireq->i_len != sizeof(txpow)) 537 return EINVAL; 538 error = copyin(ireq->i_data, &txpow, sizeof(txpow)); 539 if (error != 0) 540 return error; 541 ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr); 542 if (ni == NULL) 543 return ENOENT; 544 txpow.it_txpow = ni->ni_txpower; 545 error = copyout(&txpow, ireq->i_data, sizeof(txpow)); 546 ieee80211_free_node(ni); 547 return error; 548 } 549 550 static int 551 ieee80211_ioctl_getwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq) 552 { 553 struct ieee80211com *ic = vap->iv_ic; 554 struct ieee80211_wme_state *wme = &ic->ic_wme; 555 struct wmeParams *wmep; 556 int ac; 557 558 if ((ic->ic_caps & IEEE80211_C_WME) == 0) 559 return EINVAL; 560 561 ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL); 562 if (ac >= WME_NUM_AC) 563 ac = WME_AC_BE; 564 if (ireq->i_len & IEEE80211_WMEPARAM_BSS) 565 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac]; 566 else 567 wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac]; 568 switch (ireq->i_type) { 569 case IEEE80211_IOC_WME_CWMIN: /* WME: CWmin */ 570 ireq->i_val = wmep->wmep_logcwmin; 571 break; 572 case IEEE80211_IOC_WME_CWMAX: /* WME: CWmax */ 573 ireq->i_val = wmep->wmep_logcwmax; 574 break; 575 case IEEE80211_IOC_WME_AIFS: /* WME: AIFS */ 576 ireq->i_val = wmep->wmep_aifsn; 577 break; 578 case IEEE80211_IOC_WME_TXOPLIMIT: /* WME: txops limit */ 579 ireq->i_val = wmep->wmep_txopLimit; 580 break; 581 case IEEE80211_IOC_WME_ACM: /* WME: ACM (bss only) */ 582 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac]; 583 ireq->i_val = wmep->wmep_acm; 584 break; 585 case IEEE80211_IOC_WME_ACKPOLICY: /* WME: ACK policy (!bss only)*/ 586 wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac]; 587 ireq->i_val = !wmep->wmep_noackPolicy; 588 break; 589 } 590 return 0; 591 } 592 593 static int 594 ieee80211_ioctl_getmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq) 595 { 596 const struct ieee80211_aclator *acl = vap->iv_acl; 597 598 return (acl == NULL ? EINVAL : acl->iac_getioctl(vap, ireq)); 599 } 600 601 static int 602 ieee80211_ioctl_getcurchan(struct ieee80211vap *vap, struct ieee80211req *ireq) 603 { 604 struct ieee80211com *ic = vap->iv_ic; 605 struct ieee80211_channel *c; 606 607 if (ireq->i_len != sizeof(struct ieee80211_channel)) 608 return EINVAL; 609 /* 610 * vap's may have different operating channels when HT is 611 * in use. When in RUN state report the vap-specific channel. 612 * Otherwise return curchan. 613 */ 614 if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP) 615 c = vap->iv_bss->ni_chan; 616 else 617 c = ic->ic_curchan; 618 return copyout(c, ireq->i_data, sizeof(*c)); 619 } 620 621 static int 622 getappie(const struct ieee80211_appie *aie, struct ieee80211req *ireq) 623 { 624 if (aie == NULL) 625 return EINVAL; 626 /* NB: truncate, caller can check length */ 627 if (ireq->i_len > aie->ie_len) 628 ireq->i_len = aie->ie_len; 629 return copyout(aie->ie_data, ireq->i_data, ireq->i_len); 630 } 631 632 static int 633 ieee80211_ioctl_getappie(struct ieee80211vap *vap, struct ieee80211req *ireq) 634 { 635 uint8_t fc0; 636 637 fc0 = ireq->i_val & 0xff; 638 if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT) 639 return EINVAL; 640 /* NB: could check iv_opmode and reject but hardly worth the effort */ 641 switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) { 642 case IEEE80211_FC0_SUBTYPE_BEACON: 643 return getappie(vap->iv_appie_beacon, ireq); 644 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 645 return getappie(vap->iv_appie_proberesp, ireq); 646 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 647 return getappie(vap->iv_appie_assocresp, ireq); 648 case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 649 return getappie(vap->iv_appie_probereq, ireq); 650 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 651 return getappie(vap->iv_appie_assocreq, ireq); 652 case IEEE80211_FC0_SUBTYPE_BEACON|IEEE80211_FC0_SUBTYPE_PROBE_RESP: 653 return getappie(vap->iv_appie_wpa, ireq); 654 } 655 return EINVAL; 656 } 657 658 static int 659 ieee80211_ioctl_getregdomain(struct ieee80211vap *vap, 660 const struct ieee80211req *ireq) 661 { 662 struct ieee80211com *ic = vap->iv_ic; 663 664 if (ireq->i_len != sizeof(ic->ic_regdomain)) 665 return EINVAL; 666 return copyout(&ic->ic_regdomain, ireq->i_data, 667 sizeof(ic->ic_regdomain)); 668 } 669 670 static int 671 ieee80211_ioctl_getroam(struct ieee80211vap *vap, 672 const struct ieee80211req *ireq) 673 { 674 size_t len = ireq->i_len; 675 /* NB: accept short requests for backwards compat */ 676 if (len > sizeof(vap->iv_roamparms)) 677 len = sizeof(vap->iv_roamparms); 678 return copyout(vap->iv_roamparms, ireq->i_data, len); 679 } 680 681 static int 682 ieee80211_ioctl_gettxparams(struct ieee80211vap *vap, 683 const struct ieee80211req *ireq) 684 { 685 size_t len = ireq->i_len; 686 /* NB: accept short requests for backwards compat */ 687 if (len > sizeof(vap->iv_txparms)) 688 len = sizeof(vap->iv_txparms); 689 return copyout(vap->iv_txparms, ireq->i_data, len); 690 } 691 692 static int 693 ieee80211_ioctl_getdevcaps(struct ieee80211com *ic, 694 const struct ieee80211req *ireq) 695 { 696 struct ieee80211_devcaps_req *dc; 697 struct ieee80211req_chaninfo *ci; 698 int maxchans, error; 699 700 maxchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_devcaps_req)) / 701 sizeof(struct ieee80211_channel)); 702 /* NB: require 1 so we know ic_nchans is accessible */ 703 if (maxchans < 1) 704 return EINVAL; 705 /* constrain max request size, 2K channels is ~24Kbytes */ 706 if (maxchans > 2048) 707 maxchans = 2048; 708 dc = (struct ieee80211_devcaps_req *) 709 IEEE80211_MALLOC(IEEE80211_DEVCAPS_SIZE(maxchans), M_TEMP, 710 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 711 if (dc == NULL) 712 return ENOMEM; 713 dc->dc_drivercaps = ic->ic_caps; 714 dc->dc_cryptocaps = ic->ic_cryptocaps; 715 dc->dc_htcaps = ic->ic_htcaps; 716 dc->dc_vhtcaps = ic->ic_vhtcaps; 717 ci = &dc->dc_chaninfo; 718 ic->ic_getradiocaps(ic, maxchans, &ci->ic_nchans, ci->ic_chans); 719 KASSERT(ci->ic_nchans <= maxchans, 720 ("nchans %d maxchans %d", ci->ic_nchans, maxchans)); 721 ieee80211_sort_channels(ci->ic_chans, ci->ic_nchans); 722 error = copyout(dc, ireq->i_data, IEEE80211_DEVCAPS_SPACE(dc)); 723 IEEE80211_FREE(dc, M_TEMP); 724 return error; 725 } 726 727 static int 728 ieee80211_ioctl_getstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq) 729 { 730 struct ieee80211_node *ni; 731 struct ieee80211req_sta_vlan vlan; 732 int error; 733 734 if (ireq->i_len != sizeof(vlan)) 735 return EINVAL; 736 error = copyin(ireq->i_data, &vlan, sizeof(vlan)); 737 if (error != 0) 738 return error; 739 if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) { 740 ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, 741 vlan.sv_macaddr); 742 if (ni == NULL) 743 return ENOENT; 744 } else 745 ni = ieee80211_ref_node(vap->iv_bss); 746 vlan.sv_vlan = ni->ni_vlan; 747 error = copyout(&vlan, ireq->i_data, sizeof(vlan)); 748 ieee80211_free_node(ni); 749 return error; 750 } 751 752 /* 753 * Dummy ioctl get handler so the linker set is defined. 754 */ 755 static int 756 dummy_ioctl_get(struct ieee80211vap *vap, struct ieee80211req *ireq) 757 { 758 return ENOSYS; 759 } 760 IEEE80211_IOCTL_GET(dummy, dummy_ioctl_get); 761 762 static int 763 ieee80211_ioctl_getdefault(struct ieee80211vap *vap, struct ieee80211req *ireq) 764 { 765 ieee80211_ioctl_getfunc * const *get; 766 int error; 767 768 SET_FOREACH(get, ieee80211_ioctl_getset) { 769 error = (*get)(vap, ireq); 770 if (error != ENOSYS) 771 return error; 772 } 773 return EINVAL; 774 } 775 776 static int 777 ieee80211_ioctl_get80211(struct ieee80211vap *vap, u_long cmd, 778 struct ieee80211req *ireq) 779 { 780 #define MS(_v, _f) (((_v) & _f) >> _f##_S) 781 struct ieee80211com *ic = vap->iv_ic; 782 u_int kid, len; 783 uint8_t tmpkey[IEEE80211_KEYBUF_SIZE]; 784 char tmpssid[IEEE80211_NWID_LEN]; 785 int error = 0; 786 787 switch (ireq->i_type) { 788 case IEEE80211_IOC_SSID: 789 switch (vap->iv_state) { 790 case IEEE80211_S_INIT: 791 case IEEE80211_S_SCAN: 792 ireq->i_len = vap->iv_des_ssid[0].len; 793 memcpy(tmpssid, vap->iv_des_ssid[0].ssid, ireq->i_len); 794 break; 795 default: 796 ireq->i_len = vap->iv_bss->ni_esslen; 797 memcpy(tmpssid, vap->iv_bss->ni_essid, ireq->i_len); 798 break; 799 } 800 error = copyout(tmpssid, ireq->i_data, ireq->i_len); 801 break; 802 case IEEE80211_IOC_NUMSSIDS: 803 ireq->i_val = 1; 804 break; 805 case IEEE80211_IOC_WEP: 806 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0) 807 ireq->i_val = IEEE80211_WEP_OFF; 808 else if (vap->iv_flags & IEEE80211_F_DROPUNENC) 809 ireq->i_val = IEEE80211_WEP_ON; 810 else 811 ireq->i_val = IEEE80211_WEP_MIXED; 812 break; 813 case IEEE80211_IOC_WEPKEY: 814 kid = (u_int) ireq->i_val; 815 if (kid >= IEEE80211_WEP_NKID) 816 return EINVAL; 817 len = (u_int) vap->iv_nw_keys[kid].wk_keylen; 818 /* NB: only root can read WEP keys */ 819 /* XXX TODO: move priv check to ieee80211_freebsd.c */ 820 if (priv_check(curthread, PRIV_NET80211_VAP_GETKEY) == 0) { 821 bcopy(vap->iv_nw_keys[kid].wk_key, tmpkey, len); 822 } else { 823 bzero(tmpkey, len); 824 } 825 ireq->i_len = len; 826 error = copyout(tmpkey, ireq->i_data, len); 827 break; 828 case IEEE80211_IOC_NUMWEPKEYS: 829 ireq->i_val = IEEE80211_WEP_NKID; 830 break; 831 case IEEE80211_IOC_WEPTXKEY: 832 ireq->i_val = vap->iv_def_txkey; 833 break; 834 case IEEE80211_IOC_AUTHMODE: 835 if (vap->iv_flags & IEEE80211_F_WPA) 836 ireq->i_val = IEEE80211_AUTH_WPA; 837 else 838 ireq->i_val = vap->iv_bss->ni_authmode; 839 break; 840 case IEEE80211_IOC_CHANNEL: 841 ireq->i_val = ieee80211_chan2ieee(ic, ic->ic_curchan); 842 break; 843 case IEEE80211_IOC_POWERSAVE: 844 if (vap->iv_flags & IEEE80211_F_PMGTON) 845 ireq->i_val = IEEE80211_POWERSAVE_ON; 846 else 847 ireq->i_val = IEEE80211_POWERSAVE_OFF; 848 break; 849 case IEEE80211_IOC_POWERSAVESLEEP: 850 ireq->i_val = ic->ic_lintval; 851 break; 852 case IEEE80211_IOC_RTSTHRESHOLD: 853 ireq->i_val = vap->iv_rtsthreshold; 854 break; 855 case IEEE80211_IOC_PROTMODE: 856 ireq->i_val = vap->iv_protmode; 857 break; 858 case IEEE80211_IOC_TXPOWER: 859 /* 860 * Tx power limit is the min of max regulatory 861 * power, any user-set limit, and the max the 862 * radio can do. 863 * 864 * TODO: methodize this 865 */ 866 ireq->i_val = 2*ic->ic_curchan->ic_maxregpower; 867 if (ireq->i_val > ic->ic_txpowlimit) 868 ireq->i_val = ic->ic_txpowlimit; 869 if (ireq->i_val > ic->ic_curchan->ic_maxpower) 870 ireq->i_val = ic->ic_curchan->ic_maxpower; 871 break; 872 case IEEE80211_IOC_WPA: 873 switch (vap->iv_flags & IEEE80211_F_WPA) { 874 case IEEE80211_F_WPA1: 875 ireq->i_val = 1; 876 break; 877 case IEEE80211_F_WPA2: 878 ireq->i_val = 2; 879 break; 880 case IEEE80211_F_WPA1 | IEEE80211_F_WPA2: 881 ireq->i_val = 3; 882 break; 883 default: 884 ireq->i_val = 0; 885 break; 886 } 887 break; 888 case IEEE80211_IOC_CHANLIST: 889 error = ieee80211_ioctl_getchanlist(vap, ireq); 890 break; 891 case IEEE80211_IOC_ROAMING: 892 ireq->i_val = vap->iv_roaming; 893 break; 894 case IEEE80211_IOC_PRIVACY: 895 ireq->i_val = (vap->iv_flags & IEEE80211_F_PRIVACY) != 0; 896 break; 897 case IEEE80211_IOC_DROPUNENCRYPTED: 898 ireq->i_val = (vap->iv_flags & IEEE80211_F_DROPUNENC) != 0; 899 break; 900 case IEEE80211_IOC_COUNTERMEASURES: 901 ireq->i_val = (vap->iv_flags & IEEE80211_F_COUNTERM) != 0; 902 break; 903 case IEEE80211_IOC_WME: 904 ireq->i_val = (vap->iv_flags & IEEE80211_F_WME) != 0; 905 break; 906 case IEEE80211_IOC_HIDESSID: 907 ireq->i_val = (vap->iv_flags & IEEE80211_F_HIDESSID) != 0; 908 break; 909 case IEEE80211_IOC_APBRIDGE: 910 ireq->i_val = (vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0; 911 break; 912 case IEEE80211_IOC_WPAKEY: 913 error = ieee80211_ioctl_getkey(vap, ireq); 914 break; 915 case IEEE80211_IOC_CHANINFO: 916 error = ieee80211_ioctl_getchaninfo(vap, ireq); 917 break; 918 case IEEE80211_IOC_BSSID: 919 if (ireq->i_len != IEEE80211_ADDR_LEN) 920 return EINVAL; 921 if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP) { 922 error = copyout(vap->iv_opmode == IEEE80211_M_WDS ? 923 vap->iv_bss->ni_macaddr : vap->iv_bss->ni_bssid, 924 ireq->i_data, ireq->i_len); 925 } else 926 error = copyout(vap->iv_des_bssid, ireq->i_data, 927 ireq->i_len); 928 break; 929 case IEEE80211_IOC_WPAIE: 930 case IEEE80211_IOC_WPAIE2: 931 error = ieee80211_ioctl_getwpaie(vap, ireq, ireq->i_type); 932 break; 933 case IEEE80211_IOC_SCAN_RESULTS: 934 error = ieee80211_ioctl_getscanresults(vap, ireq); 935 break; 936 case IEEE80211_IOC_STA_STATS: 937 error = ieee80211_ioctl_getstastats(vap, ireq); 938 break; 939 case IEEE80211_IOC_TXPOWMAX: 940 ireq->i_val = vap->iv_bss->ni_txpower; 941 break; 942 case IEEE80211_IOC_STA_TXPOW: 943 error = ieee80211_ioctl_getstatxpow(vap, ireq); 944 break; 945 case IEEE80211_IOC_STA_INFO: 946 error = ieee80211_ioctl_getstainfo(vap, ireq); 947 break; 948 case IEEE80211_IOC_WME_CWMIN: /* WME: CWmin */ 949 case IEEE80211_IOC_WME_CWMAX: /* WME: CWmax */ 950 case IEEE80211_IOC_WME_AIFS: /* WME: AIFS */ 951 case IEEE80211_IOC_WME_TXOPLIMIT: /* WME: txops limit */ 952 case IEEE80211_IOC_WME_ACM: /* WME: ACM (bss only) */ 953 case IEEE80211_IOC_WME_ACKPOLICY: /* WME: ACK policy (!bss only) */ 954 error = ieee80211_ioctl_getwmeparam(vap, ireq); 955 break; 956 case IEEE80211_IOC_DTIM_PERIOD: 957 ireq->i_val = vap->iv_dtim_period; 958 break; 959 case IEEE80211_IOC_BEACON_INTERVAL: 960 /* NB: get from ic_bss for station mode */ 961 ireq->i_val = vap->iv_bss->ni_intval; 962 break; 963 case IEEE80211_IOC_PUREG: 964 ireq->i_val = (vap->iv_flags & IEEE80211_F_PUREG) != 0; 965 break; 966 case IEEE80211_IOC_QUIET: 967 ireq->i_val = vap->iv_quiet; 968 break; 969 case IEEE80211_IOC_QUIET_COUNT: 970 ireq->i_val = vap->iv_quiet_count; 971 break; 972 case IEEE80211_IOC_QUIET_PERIOD: 973 ireq->i_val = vap->iv_quiet_period; 974 break; 975 case IEEE80211_IOC_QUIET_DUR: 976 ireq->i_val = vap->iv_quiet_duration; 977 break; 978 case IEEE80211_IOC_QUIET_OFFSET: 979 ireq->i_val = vap->iv_quiet_offset; 980 break; 981 case IEEE80211_IOC_BGSCAN: 982 ireq->i_val = (vap->iv_flags & IEEE80211_F_BGSCAN) != 0; 983 break; 984 case IEEE80211_IOC_BGSCAN_IDLE: 985 ireq->i_val = vap->iv_bgscanidle*hz/1000; /* ms */ 986 break; 987 case IEEE80211_IOC_BGSCAN_INTERVAL: 988 ireq->i_val = vap->iv_bgscanintvl/hz; /* seconds */ 989 break; 990 case IEEE80211_IOC_SCANVALID: 991 ireq->i_val = vap->iv_scanvalid/hz; /* seconds */ 992 break; 993 case IEEE80211_IOC_FRAGTHRESHOLD: 994 ireq->i_val = vap->iv_fragthreshold; 995 break; 996 case IEEE80211_IOC_MACCMD: 997 error = ieee80211_ioctl_getmaccmd(vap, ireq); 998 break; 999 case IEEE80211_IOC_BURST: 1000 ireq->i_val = (vap->iv_flags & IEEE80211_F_BURST) != 0; 1001 break; 1002 case IEEE80211_IOC_BMISSTHRESHOLD: 1003 ireq->i_val = vap->iv_bmissthreshold; 1004 break; 1005 case IEEE80211_IOC_CURCHAN: 1006 error = ieee80211_ioctl_getcurchan(vap, ireq); 1007 break; 1008 case IEEE80211_IOC_SHORTGI: 1009 ireq->i_val = 0; 1010 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) 1011 ireq->i_val |= IEEE80211_HTCAP_SHORTGI20; 1012 if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) 1013 ireq->i_val |= IEEE80211_HTCAP_SHORTGI40; 1014 break; 1015 case IEEE80211_IOC_AMPDU: 1016 ireq->i_val = 0; 1017 if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX) 1018 ireq->i_val |= 1; 1019 if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX) 1020 ireq->i_val |= 2; 1021 break; 1022 case IEEE80211_IOC_AMPDU_LIMIT: 1023 /* XXX TODO: make this a per-node thing; and leave this as global */ 1024 if (vap->iv_opmode == IEEE80211_M_HOSTAP) 1025 ireq->i_val = vap->iv_ampdu_rxmax; 1026 else if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP) 1027 /* 1028 * XXX TODO: this isn't completely correct, as we've 1029 * negotiated the higher of the two. 1030 */ 1031 ireq->i_val = MS(vap->iv_bss->ni_htparam, 1032 IEEE80211_HTCAP_MAXRXAMPDU); 1033 else 1034 ireq->i_val = vap->iv_ampdu_limit; 1035 break; 1036 case IEEE80211_IOC_AMPDU_DENSITY: 1037 /* XXX TODO: make this a per-node thing; and leave this as global */ 1038 if (vap->iv_opmode == IEEE80211_M_STA && 1039 (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)) 1040 /* 1041 * XXX TODO: this isn't completely correct, as we've 1042 * negotiated the higher of the two. 1043 */ 1044 ireq->i_val = MS(vap->iv_bss->ni_htparam, 1045 IEEE80211_HTCAP_MPDUDENSITY); 1046 else 1047 ireq->i_val = vap->iv_ampdu_density; 1048 break; 1049 case IEEE80211_IOC_AMSDU: 1050 ireq->i_val = 0; 1051 if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX) 1052 ireq->i_val |= 1; 1053 if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_RX) 1054 ireq->i_val |= 2; 1055 break; 1056 case IEEE80211_IOC_AMSDU_LIMIT: 1057 ireq->i_val = vap->iv_amsdu_limit; /* XXX truncation? */ 1058 break; 1059 case IEEE80211_IOC_PUREN: 1060 ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_PUREN) != 0; 1061 break; 1062 case IEEE80211_IOC_DOTH: 1063 ireq->i_val = (vap->iv_flags & IEEE80211_F_DOTH) != 0; 1064 break; 1065 case IEEE80211_IOC_REGDOMAIN: 1066 error = ieee80211_ioctl_getregdomain(vap, ireq); 1067 break; 1068 case IEEE80211_IOC_ROAM: 1069 error = ieee80211_ioctl_getroam(vap, ireq); 1070 break; 1071 case IEEE80211_IOC_TXPARAMS: 1072 error = ieee80211_ioctl_gettxparams(vap, ireq); 1073 break; 1074 case IEEE80211_IOC_HTCOMPAT: 1075 ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) != 0; 1076 break; 1077 case IEEE80211_IOC_DWDS: 1078 ireq->i_val = (vap->iv_flags & IEEE80211_F_DWDS) != 0; 1079 break; 1080 case IEEE80211_IOC_INACTIVITY: 1081 ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_INACT) != 0; 1082 break; 1083 case IEEE80211_IOC_APPIE: 1084 error = ieee80211_ioctl_getappie(vap, ireq); 1085 break; 1086 case IEEE80211_IOC_WPS: 1087 ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_WPS) != 0; 1088 break; 1089 case IEEE80211_IOC_TSN: 1090 ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_TSN) != 0; 1091 break; 1092 case IEEE80211_IOC_DFS: 1093 ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DFS) != 0; 1094 break; 1095 case IEEE80211_IOC_DOTD: 1096 ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DOTD) != 0; 1097 break; 1098 case IEEE80211_IOC_DEVCAPS: 1099 error = ieee80211_ioctl_getdevcaps(ic, ireq); 1100 break; 1101 case IEEE80211_IOC_HTPROTMODE: 1102 ireq->i_val = vap->iv_htprotmode; 1103 break; 1104 case IEEE80211_IOC_HTCONF: 1105 if (vap->iv_flags_ht & IEEE80211_FHT_HT) { 1106 ireq->i_val = 1; 1107 if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40) 1108 ireq->i_val |= 2; 1109 } else 1110 ireq->i_val = 0; 1111 break; 1112 case IEEE80211_IOC_STA_VLAN: 1113 error = ieee80211_ioctl_getstavlan(vap, ireq); 1114 break; 1115 case IEEE80211_IOC_SMPS: 1116 if (vap->iv_opmode == IEEE80211_M_STA && 1117 (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)) { 1118 if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_RTS) 1119 ireq->i_val = IEEE80211_HTCAP_SMPS_DYNAMIC; 1120 else if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_PS) 1121 ireq->i_val = IEEE80211_HTCAP_SMPS_ENA; 1122 else 1123 ireq->i_val = IEEE80211_HTCAP_SMPS_OFF; 1124 } else 1125 ireq->i_val = vap->iv_htcaps & IEEE80211_HTCAP_SMPS; 1126 break; 1127 case IEEE80211_IOC_RIFS: 1128 if (vap->iv_opmode == IEEE80211_M_STA && 1129 (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)) 1130 ireq->i_val = 1131 (vap->iv_bss->ni_flags & IEEE80211_NODE_RIFS) != 0; 1132 else 1133 ireq->i_val = 1134 (vap->iv_flags_ht & IEEE80211_FHT_RIFS) != 0; 1135 break; 1136 case IEEE80211_IOC_STBC: 1137 ireq->i_val = 0; 1138 if (vap->iv_flags_ht & IEEE80211_FHT_STBC_TX) 1139 ireq->i_val |= 1; 1140 if (vap->iv_flags_ht & IEEE80211_FHT_STBC_RX) 1141 ireq->i_val |= 2; 1142 break; 1143 case IEEE80211_IOC_LDPC: 1144 ireq->i_val = 0; 1145 if (vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX) 1146 ireq->i_val |= 1; 1147 if (vap->iv_flags_ht & IEEE80211_FHT_LDPC_RX) 1148 ireq->i_val |= 2; 1149 break; 1150 case IEEE80211_IOC_UAPSD: 1151 ireq->i_val = 0; 1152 if (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD) 1153 ireq->i_val = 1; 1154 break; 1155 1156 /* VHT */ 1157 case IEEE80211_IOC_VHTCONF: 1158 ireq->i_val = 0; 1159 if (vap->iv_flags_vht & IEEE80211_FVHT_VHT) 1160 ireq->i_val |= 1; 1161 if (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT40) 1162 ireq->i_val |= 2; 1163 if (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80) 1164 ireq->i_val |= 4; 1165 if (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80P80) 1166 ireq->i_val |= 8; 1167 if (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT160) 1168 ireq->i_val |= 16; 1169 break; 1170 1171 default: 1172 error = ieee80211_ioctl_getdefault(vap, ireq); 1173 break; 1174 } 1175 return error; 1176 #undef MS 1177 } 1178 1179 static int 1180 ieee80211_ioctl_setkey(struct ieee80211vap *vap, struct ieee80211req *ireq) 1181 { 1182 struct ieee80211req_key ik; 1183 struct ieee80211_node *ni; 1184 struct ieee80211_key *wk; 1185 uint16_t kid; 1186 int error, i; 1187 1188 if (ireq->i_len != sizeof(ik)) 1189 return EINVAL; 1190 error = copyin(ireq->i_data, &ik, sizeof(ik)); 1191 if (error) 1192 return error; 1193 /* NB: cipher support is verified by ieee80211_crypt_newkey */ 1194 /* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */ 1195 if (ik.ik_keylen > sizeof(ik.ik_keydata)) 1196 return E2BIG; 1197 kid = ik.ik_keyix; 1198 if (kid == IEEE80211_KEYIX_NONE) { 1199 /* XXX unicast keys currently must be tx/rx */ 1200 if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV)) 1201 return EINVAL; 1202 if (vap->iv_opmode == IEEE80211_M_STA) { 1203 ni = ieee80211_ref_node(vap->iv_bss); 1204 if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) { 1205 ieee80211_free_node(ni); 1206 return EADDRNOTAVAIL; 1207 } 1208 } else { 1209 ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, 1210 ik.ik_macaddr); 1211 if (ni == NULL) 1212 return ENOENT; 1213 } 1214 wk = &ni->ni_ucastkey; 1215 } else { 1216 if (kid >= IEEE80211_WEP_NKID) 1217 return EINVAL; 1218 wk = &vap->iv_nw_keys[kid]; 1219 /* 1220 * Global slots start off w/o any assigned key index. 1221 * Force one here for consistency with IEEE80211_IOC_WEPKEY. 1222 */ 1223 if (wk->wk_keyix == IEEE80211_KEYIX_NONE) 1224 wk->wk_keyix = kid; 1225 ni = NULL; 1226 } 1227 error = 0; 1228 ieee80211_key_update_begin(vap); 1229 if (ieee80211_crypto_newkey(vap, ik.ik_type, ik.ik_flags, wk)) { 1230 wk->wk_keylen = ik.ik_keylen; 1231 /* NB: MIC presence is implied by cipher type */ 1232 if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE) 1233 wk->wk_keylen = IEEE80211_KEYBUF_SIZE; 1234 for (i = 0; i < IEEE80211_TID_SIZE; i++) 1235 wk->wk_keyrsc[i] = ik.ik_keyrsc; 1236 wk->wk_keytsc = 0; /* new key, reset */ 1237 memset(wk->wk_key, 0, sizeof(wk->wk_key)); 1238 memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen); 1239 IEEE80211_ADDR_COPY(wk->wk_macaddr, 1240 ni != NULL ? ni->ni_macaddr : ik.ik_macaddr); 1241 if (!ieee80211_crypto_setkey(vap, wk)) 1242 error = EIO; 1243 else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT)) 1244 /* 1245 * Inform the driver that this is the default 1246 * transmit key. Now, ideally we'd just set 1247 * a flag in the key update that would 1248 * say "yes, we're the default key", but 1249 * that currently isn't the way the ioctl -> 1250 * key interface works. 1251 */ 1252 ieee80211_crypto_set_deftxkey(vap, kid); 1253 } else 1254 error = ENXIO; 1255 ieee80211_key_update_end(vap); 1256 if (ni != NULL) 1257 ieee80211_free_node(ni); 1258 return error; 1259 } 1260 1261 static int 1262 ieee80211_ioctl_delkey(struct ieee80211vap *vap, struct ieee80211req *ireq) 1263 { 1264 struct ieee80211req_del_key dk; 1265 int kid, error; 1266 1267 if (ireq->i_len != sizeof(dk)) 1268 return EINVAL; 1269 error = copyin(ireq->i_data, &dk, sizeof(dk)); 1270 if (error) 1271 return error; 1272 kid = dk.idk_keyix; 1273 /* XXX uint8_t -> uint16_t */ 1274 if (dk.idk_keyix == (uint8_t) IEEE80211_KEYIX_NONE) { 1275 struct ieee80211_node *ni; 1276 1277 if (vap->iv_opmode == IEEE80211_M_STA) { 1278 ni = ieee80211_ref_node(vap->iv_bss); 1279 if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) { 1280 ieee80211_free_node(ni); 1281 return EADDRNOTAVAIL; 1282 } 1283 } else { 1284 ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, 1285 dk.idk_macaddr); 1286 if (ni == NULL) 1287 return ENOENT; 1288 } 1289 /* XXX error return */ 1290 ieee80211_node_delucastkey(ni); 1291 ieee80211_free_node(ni); 1292 } else { 1293 if (kid >= IEEE80211_WEP_NKID) 1294 return EINVAL; 1295 /* XXX error return */ 1296 ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[kid]); 1297 } 1298 return 0; 1299 } 1300 1301 struct mlmeop { 1302 struct ieee80211vap *vap; 1303 int op; 1304 int reason; 1305 }; 1306 1307 static void 1308 mlmedebug(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN], 1309 int op, int reason) 1310 { 1311 #ifdef IEEE80211_DEBUG 1312 static const struct { 1313 int mask; 1314 const char *opstr; 1315 } ops[] = { 1316 { 0, "op#0" }, 1317 { IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE | 1318 IEEE80211_MSG_ASSOC, "assoc" }, 1319 { IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE | 1320 IEEE80211_MSG_ASSOC, "disassoc" }, 1321 { IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE | 1322 IEEE80211_MSG_AUTH, "deauth" }, 1323 { IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE | 1324 IEEE80211_MSG_AUTH, "authorize" }, 1325 { IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE | 1326 IEEE80211_MSG_AUTH, "unauthorize" }, 1327 }; 1328 1329 if (op == IEEE80211_MLME_AUTH) { 1330 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_IOCTL | 1331 IEEE80211_MSG_STATE | IEEE80211_MSG_AUTH, mac, 1332 "station authenticate %s via MLME (reason: %d (%s))", 1333 reason == IEEE80211_STATUS_SUCCESS ? "ACCEPT" : "REJECT", 1334 reason, ieee80211_reason_to_string(reason)); 1335 } else if (!(IEEE80211_MLME_ASSOC <= op && op <= IEEE80211_MLME_AUTH)) { 1336 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, mac, 1337 "unknown MLME request %d (reason: %d (%s))", op, reason, 1338 ieee80211_reason_to_string(reason)); 1339 } else if (reason == IEEE80211_STATUS_SUCCESS) { 1340 IEEE80211_NOTE_MAC(vap, ops[op].mask, mac, 1341 "station %s via MLME", ops[op].opstr); 1342 } else { 1343 IEEE80211_NOTE_MAC(vap, ops[op].mask, mac, 1344 "station %s via MLME (reason: %d (%s))", ops[op].opstr, 1345 reason, ieee80211_reason_to_string(reason)); 1346 } 1347 #endif /* IEEE80211_DEBUG */ 1348 } 1349 1350 static void 1351 domlme(void *arg, struct ieee80211_node *ni) 1352 { 1353 struct mlmeop *mop = arg; 1354 struct ieee80211vap *vap = ni->ni_vap; 1355 1356 if (vap != mop->vap) 1357 return; 1358 /* 1359 * NB: if ni_associd is zero then the node is already cleaned 1360 * up and we don't need to do this (we're safely holding a 1361 * reference but should otherwise not modify it's state). 1362 */ 1363 if (ni->ni_associd == 0) 1364 return; 1365 mlmedebug(vap, ni->ni_macaddr, mop->op, mop->reason); 1366 if (mop->op == IEEE80211_MLME_DEAUTH) { 1367 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH, 1368 mop->reason); 1369 } else { 1370 IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC, 1371 mop->reason); 1372 } 1373 ieee80211_node_leave(ni); 1374 } 1375 1376 static int 1377 setmlme_dropsta(struct ieee80211vap *vap, 1378 const uint8_t mac[IEEE80211_ADDR_LEN], struct mlmeop *mlmeop) 1379 { 1380 struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta; 1381 struct ieee80211_node *ni; 1382 int error = 0; 1383 1384 /* NB: the broadcast address means do 'em all */ 1385 if (!IEEE80211_ADDR_EQ(mac, vap->iv_ifp->if_broadcastaddr)) { 1386 IEEE80211_NODE_LOCK(nt); 1387 ni = ieee80211_find_node_locked(nt, mac); 1388 IEEE80211_NODE_UNLOCK(nt); 1389 /* 1390 * Don't do the node update inside the node 1391 * table lock. This unfortunately causes LORs 1392 * with drivers and their TX paths. 1393 */ 1394 if (ni != NULL) { 1395 domlme(mlmeop, ni); 1396 ieee80211_free_node(ni); 1397 } else 1398 error = ENOENT; 1399 } else { 1400 ieee80211_iterate_nodes(nt, domlme, mlmeop); 1401 } 1402 return error; 1403 } 1404 1405 static int 1406 setmlme_common(struct ieee80211vap *vap, int op, 1407 const uint8_t mac[IEEE80211_ADDR_LEN], int reason) 1408 { 1409 struct ieee80211com *ic = vap->iv_ic; 1410 struct ieee80211_node_table *nt = &ic->ic_sta; 1411 struct ieee80211_node *ni; 1412 struct mlmeop mlmeop; 1413 int error; 1414 1415 error = 0; 1416 switch (op) { 1417 case IEEE80211_MLME_DISASSOC: 1418 case IEEE80211_MLME_DEAUTH: 1419 switch (vap->iv_opmode) { 1420 case IEEE80211_M_STA: 1421 mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason); 1422 /* XXX not quite right */ 1423 ieee80211_new_state(vap, IEEE80211_S_INIT, reason); 1424 break; 1425 case IEEE80211_M_HOSTAP: 1426 mlmeop.vap = vap; 1427 mlmeop.op = op; 1428 mlmeop.reason = reason; 1429 error = setmlme_dropsta(vap, mac, &mlmeop); 1430 break; 1431 case IEEE80211_M_WDS: 1432 /* XXX user app should send raw frame? */ 1433 if (op != IEEE80211_MLME_DEAUTH) { 1434 error = EINVAL; 1435 break; 1436 } 1437 #if 0 1438 /* XXX accept any address, simplifies user code */ 1439 if (!IEEE80211_ADDR_EQ(mac, vap->iv_bss->ni_macaddr)) { 1440 error = EINVAL; 1441 break; 1442 } 1443 #endif 1444 mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason); 1445 ni = ieee80211_ref_node(vap->iv_bss); 1446 IEEE80211_SEND_MGMT(ni, 1447 IEEE80211_FC0_SUBTYPE_DEAUTH, reason); 1448 ieee80211_free_node(ni); 1449 break; 1450 case IEEE80211_M_MBSS: 1451 IEEE80211_NODE_LOCK(nt); 1452 ni = ieee80211_find_node_locked(nt, mac); 1453 /* 1454 * Don't do the node update inside the node 1455 * table lock. This unfortunately causes LORs 1456 * with drivers and their TX paths. 1457 */ 1458 IEEE80211_NODE_UNLOCK(nt); 1459 if (ni != NULL) { 1460 ieee80211_node_leave(ni); 1461 ieee80211_free_node(ni); 1462 } else { 1463 error = ENOENT; 1464 } 1465 break; 1466 default: 1467 error = EINVAL; 1468 break; 1469 } 1470 break; 1471 case IEEE80211_MLME_AUTHORIZE: 1472 case IEEE80211_MLME_UNAUTHORIZE: 1473 if (vap->iv_opmode != IEEE80211_M_HOSTAP && 1474 vap->iv_opmode != IEEE80211_M_WDS) { 1475 error = EINVAL; 1476 break; 1477 } 1478 IEEE80211_NODE_LOCK(nt); 1479 ni = ieee80211_find_vap_node_locked(nt, vap, mac); 1480 /* 1481 * Don't do the node update inside the node 1482 * table lock. This unfortunately causes LORs 1483 * with drivers and their TX paths. 1484 */ 1485 IEEE80211_NODE_UNLOCK(nt); 1486 if (ni != NULL) { 1487 mlmedebug(vap, mac, op, reason); 1488 if (op == IEEE80211_MLME_AUTHORIZE) 1489 ieee80211_node_authorize(ni); 1490 else 1491 ieee80211_node_unauthorize(ni); 1492 ieee80211_free_node(ni); 1493 } else 1494 error = ENOENT; 1495 break; 1496 case IEEE80211_MLME_AUTH: 1497 if (vap->iv_opmode != IEEE80211_M_HOSTAP) { 1498 error = EINVAL; 1499 break; 1500 } 1501 IEEE80211_NODE_LOCK(nt); 1502 ni = ieee80211_find_vap_node_locked(nt, vap, mac); 1503 /* 1504 * Don't do the node update inside the node 1505 * table lock. This unfortunately causes LORs 1506 * with drivers and their TX paths. 1507 */ 1508 IEEE80211_NODE_UNLOCK(nt); 1509 if (ni != NULL) { 1510 mlmedebug(vap, mac, op, reason); 1511 if (reason == IEEE80211_STATUS_SUCCESS) { 1512 IEEE80211_SEND_MGMT(ni, 1513 IEEE80211_FC0_SUBTYPE_AUTH, 2); 1514 /* 1515 * For shared key auth, just continue the 1516 * exchange. Otherwise when 802.1x is not in 1517 * use mark the port authorized at this point 1518 * so traffic can flow. 1519 */ 1520 if (ni->ni_authmode != IEEE80211_AUTH_8021X && 1521 ni->ni_challenge == NULL) 1522 ieee80211_node_authorize(ni); 1523 } else { 1524 vap->iv_stats.is_rx_acl++; 1525 ieee80211_send_error(ni, ni->ni_macaddr, 1526 IEEE80211_FC0_SUBTYPE_AUTH, 2|(reason<<16)); 1527 ieee80211_node_leave(ni); 1528 } 1529 ieee80211_free_node(ni); 1530 } else 1531 error = ENOENT; 1532 break; 1533 default: 1534 error = EINVAL; 1535 break; 1536 } 1537 return error; 1538 } 1539 1540 struct scanlookup { 1541 const uint8_t *mac; 1542 int esslen; 1543 const uint8_t *essid; 1544 const struct ieee80211_scan_entry *se; 1545 }; 1546 1547 /* 1548 * Match mac address and any ssid. 1549 */ 1550 static void 1551 mlmelookup(void *arg, const struct ieee80211_scan_entry *se) 1552 { 1553 struct scanlookup *look = arg; 1554 1555 if (!IEEE80211_ADDR_EQ(look->mac, se->se_macaddr)) 1556 return; 1557 if (look->esslen != 0) { 1558 if (se->se_ssid[1] != look->esslen) 1559 return; 1560 if (memcmp(look->essid, se->se_ssid+2, look->esslen)) 1561 return; 1562 } 1563 look->se = se; 1564 } 1565 1566 static int 1567 setmlme_assoc_sta(struct ieee80211vap *vap, 1568 const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len, 1569 const uint8_t ssid[IEEE80211_NWID_LEN]) 1570 { 1571 struct scanlookup lookup; 1572 1573 KASSERT(vap->iv_opmode == IEEE80211_M_STA, 1574 ("expected opmode STA not %s", 1575 ieee80211_opmode_name[vap->iv_opmode])); 1576 1577 /* NB: this is racey if roaming is !manual */ 1578 lookup.se = NULL; 1579 lookup.mac = mac; 1580 lookup.esslen = ssid_len; 1581 lookup.essid = ssid; 1582 ieee80211_scan_iterate(vap, mlmelookup, &lookup); 1583 if (lookup.se == NULL) 1584 return ENOENT; 1585 mlmedebug(vap, mac, IEEE80211_MLME_ASSOC, 0); 1586 if (!ieee80211_sta_join(vap, lookup.se->se_chan, lookup.se)) 1587 return EIO; /* XXX unique but could be better */ 1588 return 0; 1589 } 1590 1591 static int 1592 setmlme_assoc_adhoc(struct ieee80211vap *vap, 1593 const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len, 1594 const uint8_t ssid[IEEE80211_NWID_LEN]) 1595 { 1596 struct ieee80211_scan_req *sr; 1597 int error; 1598 1599 KASSERT(vap->iv_opmode == IEEE80211_M_IBSS || 1600 vap->iv_opmode == IEEE80211_M_AHDEMO, 1601 ("expected opmode IBSS or AHDEMO not %s", 1602 ieee80211_opmode_name[vap->iv_opmode])); 1603 1604 if (ssid_len == 0) 1605 return EINVAL; 1606 1607 sr = IEEE80211_MALLOC(sizeof(*sr), M_TEMP, 1608 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 1609 if (sr == NULL) 1610 return ENOMEM; 1611 1612 /* NB: IEEE80211_IOC_SSID call missing for ap_scan=2. */ 1613 memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN); 1614 vap->iv_des_ssid[0].len = ssid_len; 1615 memcpy(vap->iv_des_ssid[0].ssid, ssid, ssid_len); 1616 vap->iv_des_nssid = 1; 1617 1618 sr->sr_flags = IEEE80211_IOC_SCAN_ACTIVE | IEEE80211_IOC_SCAN_ONCE; 1619 sr->sr_duration = IEEE80211_IOC_SCAN_FOREVER; 1620 memcpy(sr->sr_ssid[0].ssid, ssid, ssid_len); 1621 sr->sr_ssid[0].len = ssid_len; 1622 sr->sr_nssid = 1; 1623 1624 error = ieee80211_scanreq(vap, sr); 1625 1626 IEEE80211_FREE(sr, M_TEMP); 1627 return error; 1628 } 1629 1630 static int 1631 ieee80211_ioctl_setmlme(struct ieee80211vap *vap, struct ieee80211req *ireq) 1632 { 1633 struct ieee80211req_mlme mlme; 1634 int error; 1635 1636 if (ireq->i_len != sizeof(mlme)) 1637 return EINVAL; 1638 error = copyin(ireq->i_data, &mlme, sizeof(mlme)); 1639 if (error) 1640 return error; 1641 if (vap->iv_opmode == IEEE80211_M_STA && 1642 mlme.im_op == IEEE80211_MLME_ASSOC) 1643 return setmlme_assoc_sta(vap, mlme.im_macaddr, 1644 vap->iv_des_ssid[0].len, vap->iv_des_ssid[0].ssid); 1645 else if ((vap->iv_opmode == IEEE80211_M_IBSS || 1646 vap->iv_opmode == IEEE80211_M_AHDEMO) && 1647 mlme.im_op == IEEE80211_MLME_ASSOC) 1648 return setmlme_assoc_adhoc(vap, mlme.im_macaddr, 1649 mlme.im_ssid_len, mlme.im_ssid); 1650 else 1651 return setmlme_common(vap, mlme.im_op, 1652 mlme.im_macaddr, mlme.im_reason); 1653 } 1654 1655 static int 1656 ieee80211_ioctl_macmac(struct ieee80211vap *vap, struct ieee80211req *ireq) 1657 { 1658 uint8_t mac[IEEE80211_ADDR_LEN]; 1659 const struct ieee80211_aclator *acl = vap->iv_acl; 1660 int error; 1661 1662 if (ireq->i_len != sizeof(mac)) 1663 return EINVAL; 1664 error = copyin(ireq->i_data, mac, ireq->i_len); 1665 if (error) 1666 return error; 1667 if (acl == NULL) { 1668 acl = ieee80211_aclator_get("mac"); 1669 if (acl == NULL || !acl->iac_attach(vap)) 1670 return EINVAL; 1671 vap->iv_acl = acl; 1672 } 1673 if (ireq->i_type == IEEE80211_IOC_ADDMAC) 1674 acl->iac_add(vap, mac); 1675 else 1676 acl->iac_remove(vap, mac); 1677 return 0; 1678 } 1679 1680 static int 1681 ieee80211_ioctl_setmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq) 1682 { 1683 const struct ieee80211_aclator *acl = vap->iv_acl; 1684 1685 switch (ireq->i_val) { 1686 case IEEE80211_MACCMD_POLICY_OPEN: 1687 case IEEE80211_MACCMD_POLICY_ALLOW: 1688 case IEEE80211_MACCMD_POLICY_DENY: 1689 case IEEE80211_MACCMD_POLICY_RADIUS: 1690 if (acl == NULL) { 1691 acl = ieee80211_aclator_get("mac"); 1692 if (acl == NULL || !acl->iac_attach(vap)) 1693 return EINVAL; 1694 vap->iv_acl = acl; 1695 } 1696 acl->iac_setpolicy(vap, ireq->i_val); 1697 break; 1698 case IEEE80211_MACCMD_FLUSH: 1699 if (acl != NULL) 1700 acl->iac_flush(vap); 1701 /* NB: silently ignore when not in use */ 1702 break; 1703 case IEEE80211_MACCMD_DETACH: 1704 if (acl != NULL) { 1705 vap->iv_acl = NULL; 1706 acl->iac_detach(vap); 1707 } 1708 break; 1709 default: 1710 if (acl == NULL) 1711 return EINVAL; 1712 else 1713 return acl->iac_setioctl(vap, ireq); 1714 } 1715 return 0; 1716 } 1717 1718 static int 1719 ieee80211_ioctl_setchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq) 1720 { 1721 struct ieee80211com *ic = vap->iv_ic; 1722 uint8_t *chanlist, *list; 1723 int i, nchan, maxchan, error; 1724 1725 if (ireq->i_len > sizeof(ic->ic_chan_active)) 1726 ireq->i_len = sizeof(ic->ic_chan_active); 1727 list = IEEE80211_MALLOC(ireq->i_len + IEEE80211_CHAN_BYTES, M_TEMP, 1728 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 1729 if (list == NULL) 1730 return ENOMEM; 1731 error = copyin(ireq->i_data, list, ireq->i_len); 1732 if (error) { 1733 IEEE80211_FREE(list, M_TEMP); 1734 return error; 1735 } 1736 nchan = 0; 1737 chanlist = list + ireq->i_len; /* NB: zero'd already */ 1738 maxchan = ireq->i_len * NBBY; 1739 for (i = 0; i < ic->ic_nchans; i++) { 1740 const struct ieee80211_channel *c = &ic->ic_channels[i]; 1741 /* 1742 * Calculate the intersection of the user list and the 1743 * available channels so users can do things like specify 1744 * 1-255 to get all available channels. 1745 */ 1746 if (c->ic_ieee < maxchan && isset(list, c->ic_ieee)) { 1747 setbit(chanlist, c->ic_ieee); 1748 nchan++; 1749 } 1750 } 1751 if (nchan == 0) { 1752 IEEE80211_FREE(list, M_TEMP); 1753 return EINVAL; 1754 } 1755 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC && /* XXX */ 1756 isclr(chanlist, ic->ic_bsschan->ic_ieee)) 1757 ic->ic_bsschan = IEEE80211_CHAN_ANYC; 1758 memcpy(ic->ic_chan_active, chanlist, IEEE80211_CHAN_BYTES); 1759 ieee80211_scan_flush(vap); 1760 IEEE80211_FREE(list, M_TEMP); 1761 return ENETRESET; 1762 } 1763 1764 static int 1765 ieee80211_ioctl_setstastats(struct ieee80211vap *vap, struct ieee80211req *ireq) 1766 { 1767 struct ieee80211_node *ni; 1768 uint8_t macaddr[IEEE80211_ADDR_LEN]; 1769 int error; 1770 1771 /* 1772 * NB: we could copyin ieee80211req_sta_stats so apps 1773 * could make selective changes but that's overkill; 1774 * just clear all stats for now. 1775 */ 1776 if (ireq->i_len < IEEE80211_ADDR_LEN) 1777 return EINVAL; 1778 error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN); 1779 if (error != 0) 1780 return error; 1781 ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr); 1782 if (ni == NULL) 1783 return ENOENT; 1784 /* XXX require ni_vap == vap? */ 1785 memset(&ni->ni_stats, 0, sizeof(ni->ni_stats)); 1786 ieee80211_free_node(ni); 1787 return 0; 1788 } 1789 1790 static int 1791 ieee80211_ioctl_setstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq) 1792 { 1793 struct ieee80211_node *ni; 1794 struct ieee80211req_sta_txpow txpow; 1795 int error; 1796 1797 if (ireq->i_len != sizeof(txpow)) 1798 return EINVAL; 1799 error = copyin(ireq->i_data, &txpow, sizeof(txpow)); 1800 if (error != 0) 1801 return error; 1802 ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr); 1803 if (ni == NULL) 1804 return ENOENT; 1805 ni->ni_txpower = txpow.it_txpow; 1806 ieee80211_free_node(ni); 1807 return error; 1808 } 1809 1810 static int 1811 ieee80211_ioctl_setwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq) 1812 { 1813 struct ieee80211com *ic = vap->iv_ic; 1814 struct ieee80211_wme_state *wme = &ic->ic_wme; 1815 struct wmeParams *wmep, *chanp; 1816 int isbss, ac, aggrmode; 1817 1818 if ((ic->ic_caps & IEEE80211_C_WME) == 0) 1819 return EOPNOTSUPP; 1820 1821 isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS); 1822 ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL); 1823 aggrmode = (wme->wme_flags & WME_F_AGGRMODE); 1824 if (ac >= WME_NUM_AC) 1825 ac = WME_AC_BE; 1826 if (isbss) { 1827 chanp = &wme->wme_bssChanParams.cap_wmeParams[ac]; 1828 wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac]; 1829 } else { 1830 chanp = &wme->wme_chanParams.cap_wmeParams[ac]; 1831 wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac]; 1832 } 1833 switch (ireq->i_type) { 1834 case IEEE80211_IOC_WME_CWMIN: /* WME: CWmin */ 1835 wmep->wmep_logcwmin = ireq->i_val; 1836 if (!isbss || !aggrmode) 1837 chanp->wmep_logcwmin = ireq->i_val; 1838 break; 1839 case IEEE80211_IOC_WME_CWMAX: /* WME: CWmax */ 1840 wmep->wmep_logcwmax = ireq->i_val; 1841 if (!isbss || !aggrmode) 1842 chanp->wmep_logcwmax = ireq->i_val; 1843 break; 1844 case IEEE80211_IOC_WME_AIFS: /* WME: AIFS */ 1845 wmep->wmep_aifsn = ireq->i_val; 1846 if (!isbss || !aggrmode) 1847 chanp->wmep_aifsn = ireq->i_val; 1848 break; 1849 case IEEE80211_IOC_WME_TXOPLIMIT: /* WME: txops limit */ 1850 wmep->wmep_txopLimit = ireq->i_val; 1851 if (!isbss || !aggrmode) 1852 chanp->wmep_txopLimit = ireq->i_val; 1853 break; 1854 case IEEE80211_IOC_WME_ACM: /* WME: ACM (bss only) */ 1855 wmep->wmep_acm = ireq->i_val; 1856 if (!aggrmode) 1857 chanp->wmep_acm = ireq->i_val; 1858 break; 1859 case IEEE80211_IOC_WME_ACKPOLICY: /* WME: ACK policy (!bss only)*/ 1860 wmep->wmep_noackPolicy = chanp->wmep_noackPolicy = 1861 (ireq->i_val) == 0; 1862 break; 1863 } 1864 ieee80211_wme_updateparams(vap); 1865 return 0; 1866 } 1867 1868 static int 1869 find11gchannel(struct ieee80211com *ic, int start, int freq) 1870 { 1871 const struct ieee80211_channel *c; 1872 int i; 1873 1874 for (i = start+1; i < ic->ic_nchans; i++) { 1875 c = &ic->ic_channels[i]; 1876 if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c)) 1877 return 1; 1878 } 1879 /* NB: should not be needed but in case things are mis-sorted */ 1880 for (i = 0; i < start; i++) { 1881 c = &ic->ic_channels[i]; 1882 if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c)) 1883 return 1; 1884 } 1885 return 0; 1886 } 1887 1888 static struct ieee80211_channel * 1889 findchannel(struct ieee80211com *ic, int ieee, int mode) 1890 { 1891 static const u_int chanflags[IEEE80211_MODE_MAX] = { 1892 [IEEE80211_MODE_AUTO] = 0, 1893 [IEEE80211_MODE_11A] = IEEE80211_CHAN_A, 1894 [IEEE80211_MODE_11B] = IEEE80211_CHAN_B, 1895 [IEEE80211_MODE_11G] = IEEE80211_CHAN_G, 1896 [IEEE80211_MODE_FH] = IEEE80211_CHAN_FHSS, 1897 [IEEE80211_MODE_TURBO_A] = IEEE80211_CHAN_108A, 1898 [IEEE80211_MODE_TURBO_G] = IEEE80211_CHAN_108G, 1899 [IEEE80211_MODE_STURBO_A] = IEEE80211_CHAN_STURBO, 1900 [IEEE80211_MODE_HALF] = IEEE80211_CHAN_HALF, 1901 [IEEE80211_MODE_QUARTER] = IEEE80211_CHAN_QUARTER, 1902 /* NB: handled specially below */ 1903 [IEEE80211_MODE_11NA] = IEEE80211_CHAN_A, 1904 [IEEE80211_MODE_11NG] = IEEE80211_CHAN_G, 1905 [IEEE80211_MODE_VHT_5GHZ] = IEEE80211_CHAN_A, 1906 [IEEE80211_MODE_VHT_2GHZ] = IEEE80211_CHAN_G, 1907 }; 1908 u_int modeflags; 1909 int i; 1910 1911 modeflags = chanflags[mode]; 1912 for (i = 0; i < ic->ic_nchans; i++) { 1913 struct ieee80211_channel *c = &ic->ic_channels[i]; 1914 1915 if (c->ic_ieee != ieee) 1916 continue; 1917 if (mode == IEEE80211_MODE_AUTO) { 1918 /* ignore turbo channels for autoselect */ 1919 if (IEEE80211_IS_CHAN_TURBO(c)) 1920 continue; 1921 /* 1922 * XXX special-case 11b/g channels so we 1923 * always select the g channel if both 1924 * are present. 1925 * XXX prefer HT to non-HT? 1926 */ 1927 if (!IEEE80211_IS_CHAN_B(c) || 1928 !find11gchannel(ic, i, c->ic_freq)) 1929 return c; 1930 } else { 1931 /* must check VHT specifically */ 1932 if ((mode == IEEE80211_MODE_VHT_5GHZ || 1933 mode == IEEE80211_MODE_VHT_2GHZ) && 1934 !IEEE80211_IS_CHAN_VHT(c)) 1935 continue; 1936 1937 /* 1938 * Must check HT specially - only match on HT, 1939 * not HT+VHT channels 1940 */ 1941 if ((mode == IEEE80211_MODE_11NA || 1942 mode == IEEE80211_MODE_11NG) && 1943 !IEEE80211_IS_CHAN_HT(c)) 1944 continue; 1945 1946 if ((mode == IEEE80211_MODE_11NA || 1947 mode == IEEE80211_MODE_11NG) && 1948 IEEE80211_IS_CHAN_VHT(c)) 1949 continue; 1950 1951 /* Check that the modeflags above match */ 1952 if ((c->ic_flags & modeflags) == modeflags) 1953 return c; 1954 } 1955 } 1956 return NULL; 1957 } 1958 1959 /* 1960 * Check the specified against any desired mode (aka netband). 1961 * This is only used (presently) when operating in hostap mode 1962 * to enforce consistency. 1963 */ 1964 static int 1965 check_mode_consistency(const struct ieee80211_channel *c, int mode) 1966 { 1967 KASSERT(c != IEEE80211_CHAN_ANYC, ("oops, no channel")); 1968 1969 switch (mode) { 1970 case IEEE80211_MODE_11B: 1971 return (IEEE80211_IS_CHAN_B(c)); 1972 case IEEE80211_MODE_11G: 1973 return (IEEE80211_IS_CHAN_ANYG(c) && !IEEE80211_IS_CHAN_HT(c)); 1974 case IEEE80211_MODE_11A: 1975 return (IEEE80211_IS_CHAN_A(c) && !IEEE80211_IS_CHAN_HT(c)); 1976 case IEEE80211_MODE_STURBO_A: 1977 return (IEEE80211_IS_CHAN_STURBO(c)); 1978 case IEEE80211_MODE_11NA: 1979 return (IEEE80211_IS_CHAN_HTA(c)); 1980 case IEEE80211_MODE_11NG: 1981 return (IEEE80211_IS_CHAN_HTG(c)); 1982 } 1983 return 1; 1984 1985 } 1986 1987 /* 1988 * Common code to set the current channel. If the device 1989 * is up and running this may result in an immediate channel 1990 * change or a kick of the state machine. 1991 */ 1992 static int 1993 setcurchan(struct ieee80211vap *vap, struct ieee80211_channel *c) 1994 { 1995 struct ieee80211com *ic = vap->iv_ic; 1996 int error; 1997 1998 if (c != IEEE80211_CHAN_ANYC) { 1999 if (IEEE80211_IS_CHAN_RADAR(c)) 2000 return EBUSY; /* XXX better code? */ 2001 if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 2002 if (IEEE80211_IS_CHAN_NOHOSTAP(c)) 2003 return EINVAL; 2004 if (!check_mode_consistency(c, vap->iv_des_mode)) 2005 return EINVAL; 2006 } else if (vap->iv_opmode == IEEE80211_M_IBSS) { 2007 if (IEEE80211_IS_CHAN_NOADHOC(c)) 2008 return EINVAL; 2009 } 2010 if ((vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP) && 2011 vap->iv_bss->ni_chan == c) 2012 return 0; /* NB: nothing to do */ 2013 } 2014 vap->iv_des_chan = c; 2015 2016 error = 0; 2017 if (vap->iv_opmode == IEEE80211_M_MONITOR && 2018 vap->iv_des_chan != IEEE80211_CHAN_ANYC) { 2019 /* 2020 * Monitor mode can switch directly. 2021 */ 2022 if (IFNET_IS_UP_RUNNING(vap->iv_ifp)) { 2023 /* XXX need state machine for other vap's to follow */ 2024 ieee80211_setcurchan(ic, vap->iv_des_chan); 2025 vap->iv_bss->ni_chan = ic->ic_curchan; 2026 } else { 2027 ic->ic_curchan = vap->iv_des_chan; 2028 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan); 2029 } 2030 } else { 2031 /* 2032 * Need to go through the state machine in case we 2033 * need to reassociate or the like. The state machine 2034 * will pickup the desired channel and avoid scanning. 2035 */ 2036 if (IS_UP_AUTO(vap)) 2037 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0); 2038 else if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) { 2039 /* 2040 * When not up+running and a real channel has 2041 * been specified fix the current channel so 2042 * there is immediate feedback; e.g. via ifconfig. 2043 */ 2044 ic->ic_curchan = vap->iv_des_chan; 2045 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan); 2046 } 2047 } 2048 return error; 2049 } 2050 2051 /* 2052 * Old api for setting the current channel; this is 2053 * deprecated because channel numbers are ambiguous. 2054 */ 2055 static int 2056 ieee80211_ioctl_setchannel(struct ieee80211vap *vap, 2057 const struct ieee80211req *ireq) 2058 { 2059 struct ieee80211com *ic = vap->iv_ic; 2060 struct ieee80211_channel *c; 2061 2062 /* XXX 0xffff overflows 16-bit signed */ 2063 if (ireq->i_val == 0 || 2064 ireq->i_val == (int16_t) IEEE80211_CHAN_ANY) { 2065 c = IEEE80211_CHAN_ANYC; 2066 } else { 2067 struct ieee80211_channel *c2; 2068 2069 c = findchannel(ic, ireq->i_val, vap->iv_des_mode); 2070 if (c == NULL) { 2071 c = findchannel(ic, ireq->i_val, 2072 IEEE80211_MODE_AUTO); 2073 if (c == NULL) 2074 return EINVAL; 2075 } 2076 2077 /* 2078 * Fine tune channel selection based on desired mode: 2079 * if 11b is requested, find the 11b version of any 2080 * 11g channel returned, 2081 * if static turbo, find the turbo version of any 2082 * 11a channel return, 2083 * if 11na is requested, find the ht version of any 2084 * 11a channel returned, 2085 * if 11ng is requested, find the ht version of any 2086 * 11g channel returned, 2087 * if 11ac is requested, find the 11ac version 2088 * of any 11a/11na channel returned, 2089 * (TBD) 11acg (2GHz VHT) 2090 * otherwise we should be ok with what we've got. 2091 */ 2092 switch (vap->iv_des_mode) { 2093 case IEEE80211_MODE_11B: 2094 if (IEEE80211_IS_CHAN_ANYG(c)) { 2095 c2 = findchannel(ic, ireq->i_val, 2096 IEEE80211_MODE_11B); 2097 /* NB: should not happen, =>'s 11g w/o 11b */ 2098 if (c2 != NULL) 2099 c = c2; 2100 } 2101 break; 2102 case IEEE80211_MODE_TURBO_A: 2103 if (IEEE80211_IS_CHAN_A(c)) { 2104 c2 = findchannel(ic, ireq->i_val, 2105 IEEE80211_MODE_TURBO_A); 2106 if (c2 != NULL) 2107 c = c2; 2108 } 2109 break; 2110 case IEEE80211_MODE_11NA: 2111 if (IEEE80211_IS_CHAN_A(c)) { 2112 c2 = findchannel(ic, ireq->i_val, 2113 IEEE80211_MODE_11NA); 2114 if (c2 != NULL) 2115 c = c2; 2116 } 2117 break; 2118 case IEEE80211_MODE_11NG: 2119 if (IEEE80211_IS_CHAN_ANYG(c)) { 2120 c2 = findchannel(ic, ireq->i_val, 2121 IEEE80211_MODE_11NG); 2122 if (c2 != NULL) 2123 c = c2; 2124 } 2125 break; 2126 case IEEE80211_MODE_VHT_2GHZ: 2127 printf("%s: TBD\n", __func__); 2128 break; 2129 case IEEE80211_MODE_VHT_5GHZ: 2130 if (IEEE80211_IS_CHAN_A(c)) { 2131 c2 = findchannel(ic, ireq->i_val, 2132 IEEE80211_MODE_VHT_5GHZ); 2133 if (c2 != NULL) 2134 c = c2; 2135 } 2136 break; 2137 default: /* NB: no static turboG */ 2138 break; 2139 } 2140 } 2141 return setcurchan(vap, c); 2142 } 2143 2144 /* 2145 * New/current api for setting the current channel; a complete 2146 * channel description is provide so there is no ambiguity in 2147 * identifying the channel. 2148 */ 2149 static int 2150 ieee80211_ioctl_setcurchan(struct ieee80211vap *vap, 2151 const struct ieee80211req *ireq) 2152 { 2153 struct ieee80211com *ic = vap->iv_ic; 2154 struct ieee80211_channel chan, *c; 2155 int error; 2156 2157 if (ireq->i_len != sizeof(chan)) 2158 return EINVAL; 2159 error = copyin(ireq->i_data, &chan, sizeof(chan)); 2160 if (error != 0) 2161 return error; 2162 2163 /* XXX 0xffff overflows 16-bit signed */ 2164 if (chan.ic_freq == 0 || chan.ic_freq == IEEE80211_CHAN_ANY) { 2165 c = IEEE80211_CHAN_ANYC; 2166 } else { 2167 c = ieee80211_find_channel(ic, chan.ic_freq, chan.ic_flags); 2168 if (c == NULL) 2169 return EINVAL; 2170 } 2171 return setcurchan(vap, c); 2172 } 2173 2174 static int 2175 ieee80211_ioctl_setregdomain(struct ieee80211vap *vap, 2176 const struct ieee80211req *ireq) 2177 { 2178 struct ieee80211_regdomain_req *reg; 2179 int nchans, error; 2180 2181 nchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_regdomain_req)) / 2182 sizeof(struct ieee80211_channel)); 2183 if (!(1 <= nchans && nchans <= IEEE80211_CHAN_MAX)) { 2184 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL, 2185 "%s: bad # chans, i_len %d nchans %d\n", __func__, 2186 ireq->i_len, nchans); 2187 return EINVAL; 2188 } 2189 reg = (struct ieee80211_regdomain_req *) 2190 IEEE80211_MALLOC(IEEE80211_REGDOMAIN_SIZE(nchans), M_TEMP, 2191 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 2192 if (reg == NULL) { 2193 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL, 2194 "%s: no memory, nchans %d\n", __func__, nchans); 2195 return ENOMEM; 2196 } 2197 error = copyin(ireq->i_data, reg, IEEE80211_REGDOMAIN_SIZE(nchans)); 2198 if (error == 0) { 2199 /* NB: validate inline channel count against storage size */ 2200 if (reg->chaninfo.ic_nchans != nchans) { 2201 IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL, 2202 "%s: chan cnt mismatch, %d != %d\n", __func__, 2203 reg->chaninfo.ic_nchans, nchans); 2204 error = EINVAL; 2205 } else 2206 error = ieee80211_setregdomain(vap, reg); 2207 } 2208 IEEE80211_FREE(reg, M_TEMP); 2209 2210 return (error == 0 ? ENETRESET : error); 2211 } 2212 2213 static int 2214 checkrate(const struct ieee80211_rateset *rs, int rate) 2215 { 2216 int i; 2217 2218 if (rate == IEEE80211_FIXED_RATE_NONE) 2219 return 1; 2220 for (i = 0; i < rs->rs_nrates; i++) 2221 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate) 2222 return 1; 2223 return 0; 2224 } 2225 2226 static int 2227 checkmcs(const struct ieee80211_htrateset *rs, int mcs) 2228 { 2229 int rate_val = IEEE80211_RV(mcs); 2230 int i; 2231 2232 if (mcs == IEEE80211_FIXED_RATE_NONE) 2233 return 1; 2234 if ((mcs & IEEE80211_RATE_MCS) == 0) /* MCS always have 0x80 set */ 2235 return 0; 2236 for (i = 0; i < rs->rs_nrates; i++) 2237 if (IEEE80211_RV(rs->rs_rates[i]) == rate_val) 2238 return 1; 2239 return 0; 2240 } 2241 2242 static int 2243 ieee80211_ioctl_setroam(struct ieee80211vap *vap, 2244 const struct ieee80211req *ireq) 2245 { 2246 struct ieee80211com *ic = vap->iv_ic; 2247 struct ieee80211_roamparams_req *parms; 2248 struct ieee80211_roamparam *src, *dst; 2249 const struct ieee80211_htrateset *rs_ht; 2250 const struct ieee80211_rateset *rs; 2251 int changed, error, mode, is11n, nmodes; 2252 2253 if (ireq->i_len != sizeof(vap->iv_roamparms)) 2254 return EINVAL; 2255 2256 parms = IEEE80211_MALLOC(sizeof(*parms), M_TEMP, 2257 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 2258 if (parms == NULL) 2259 return ENOMEM; 2260 2261 error = copyin(ireq->i_data, parms, ireq->i_len); 2262 if (error != 0) 2263 goto fail; 2264 2265 changed = 0; 2266 nmodes = IEEE80211_MODE_MAX; 2267 2268 /* validate parameters and check if anything changed */ 2269 for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) { 2270 if (isclr(ic->ic_modecaps, mode)) 2271 continue; 2272 src = &parms->params[mode]; 2273 dst = &vap->iv_roamparms[mode]; 2274 rs = &ic->ic_sup_rates[mode]; /* NB: 11n maps to legacy */ 2275 rs_ht = &ic->ic_sup_htrates; 2276 is11n = (mode == IEEE80211_MODE_11NA || 2277 mode == IEEE80211_MODE_11NG); 2278 /* XXX TODO: 11ac */ 2279 if (src->rate != dst->rate) { 2280 if (!checkrate(rs, src->rate) && 2281 (!is11n || !checkmcs(rs_ht, src->rate))) { 2282 error = EINVAL; 2283 goto fail; 2284 } 2285 changed++; 2286 } 2287 if (src->rssi != dst->rssi) 2288 changed++; 2289 } 2290 if (changed) { 2291 /* 2292 * Copy new parameters in place and notify the 2293 * driver so it can push state to the device. 2294 */ 2295 /* XXX locking? */ 2296 for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) { 2297 if (isset(ic->ic_modecaps, mode)) 2298 vap->iv_roamparms[mode] = parms->params[mode]; 2299 } 2300 2301 if (vap->iv_roaming == IEEE80211_ROAMING_DEVICE) 2302 error = ERESTART; 2303 } 2304 2305 fail: IEEE80211_FREE(parms, M_TEMP); 2306 return error; 2307 } 2308 2309 static int 2310 ieee80211_ioctl_settxparams(struct ieee80211vap *vap, 2311 const struct ieee80211req *ireq) 2312 { 2313 struct ieee80211com *ic = vap->iv_ic; 2314 struct ieee80211_txparams_req parms; /* XXX stack use? */ 2315 struct ieee80211_txparam *src, *dst; 2316 const struct ieee80211_htrateset *rs_ht; 2317 const struct ieee80211_rateset *rs; 2318 int error, mode, changed, is11n, nmodes; 2319 2320 /* NB: accept short requests for backwards compat */ 2321 if (ireq->i_len > sizeof(parms)) 2322 return EINVAL; 2323 error = copyin(ireq->i_data, &parms, ireq->i_len); 2324 if (error != 0) 2325 return error; 2326 nmodes = ireq->i_len / sizeof(struct ieee80211_txparam); 2327 changed = 0; 2328 /* validate parameters and check if anything changed */ 2329 for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) { 2330 if (isclr(ic->ic_modecaps, mode)) 2331 continue; 2332 src = &parms.params[mode]; 2333 dst = &vap->iv_txparms[mode]; 2334 rs = &ic->ic_sup_rates[mode]; /* NB: 11n maps to legacy */ 2335 rs_ht = &ic->ic_sup_htrates; 2336 is11n = (mode == IEEE80211_MODE_11NA || 2337 mode == IEEE80211_MODE_11NG); 2338 if (src->ucastrate != dst->ucastrate) { 2339 if (!checkrate(rs, src->ucastrate) && 2340 (!is11n || !checkmcs(rs_ht, src->ucastrate))) 2341 return EINVAL; 2342 changed++; 2343 } 2344 if (src->mcastrate != dst->mcastrate) { 2345 if (!checkrate(rs, src->mcastrate) && 2346 (!is11n || !checkmcs(rs_ht, src->mcastrate))) 2347 return EINVAL; 2348 changed++; 2349 } 2350 if (src->mgmtrate != dst->mgmtrate) { 2351 if (!checkrate(rs, src->mgmtrate) && 2352 (!is11n || !checkmcs(rs_ht, src->mgmtrate))) 2353 return EINVAL; 2354 changed++; 2355 } 2356 if (src->maxretry != dst->maxretry) /* NB: no bounds */ 2357 changed++; 2358 } 2359 if (changed) { 2360 /* 2361 * Copy new parameters in place and notify the 2362 * driver so it can push state to the device. 2363 */ 2364 for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) { 2365 if (isset(ic->ic_modecaps, mode)) 2366 vap->iv_txparms[mode] = parms.params[mode]; 2367 } 2368 /* XXX could be more intelligent, 2369 e.g. don't reset if setting not being used */ 2370 return ENETRESET; 2371 } 2372 return 0; 2373 } 2374 2375 /* 2376 * Application Information Element support. 2377 */ 2378 static int 2379 setappie(struct ieee80211_appie **aie, const struct ieee80211req *ireq) 2380 { 2381 struct ieee80211_appie *app = *aie; 2382 struct ieee80211_appie *napp; 2383 int error; 2384 2385 if (ireq->i_len == 0) { /* delete any existing ie */ 2386 if (app != NULL) { 2387 *aie = NULL; /* XXX racey */ 2388 IEEE80211_FREE(app, M_80211_NODE_IE); 2389 } 2390 return 0; 2391 } 2392 if (!(2 <= ireq->i_len && ireq->i_len <= IEEE80211_MAX_APPIE)) 2393 return EINVAL; 2394 /* 2395 * Allocate a new appie structure and copy in the user data. 2396 * When done swap in the new structure. Note that we do not 2397 * guard against users holding a ref to the old structure; 2398 * this must be handled outside this code. 2399 * 2400 * XXX bad bad bad 2401 */ 2402 napp = (struct ieee80211_appie *) IEEE80211_MALLOC( 2403 sizeof(struct ieee80211_appie) + ireq->i_len, M_80211_NODE_IE, 2404 IEEE80211_M_NOWAIT); 2405 if (napp == NULL) 2406 return ENOMEM; 2407 /* XXX holding ic lock */ 2408 error = copyin(ireq->i_data, napp->ie_data, ireq->i_len); 2409 if (error) { 2410 IEEE80211_FREE(napp, M_80211_NODE_IE); 2411 return error; 2412 } 2413 napp->ie_len = ireq->i_len; 2414 *aie = napp; 2415 if (app != NULL) 2416 IEEE80211_FREE(app, M_80211_NODE_IE); 2417 return 0; 2418 } 2419 2420 static void 2421 setwparsnie(struct ieee80211vap *vap, uint8_t *ie, int space) 2422 { 2423 /* validate data is present as best we can */ 2424 if (space == 0 || 2+ie[1] > space) 2425 return; 2426 if (ie[0] == IEEE80211_ELEMID_VENDOR) 2427 vap->iv_wpa_ie = ie; 2428 else if (ie[0] == IEEE80211_ELEMID_RSN) 2429 vap->iv_rsn_ie = ie; 2430 } 2431 2432 static int 2433 ieee80211_ioctl_setappie_locked(struct ieee80211vap *vap, 2434 const struct ieee80211req *ireq, int fc0) 2435 { 2436 int error; 2437 2438 IEEE80211_LOCK_ASSERT(vap->iv_ic); 2439 2440 switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) { 2441 case IEEE80211_FC0_SUBTYPE_BEACON: 2442 if (vap->iv_opmode != IEEE80211_M_HOSTAP && 2443 vap->iv_opmode != IEEE80211_M_IBSS) { 2444 error = EINVAL; 2445 break; 2446 } 2447 error = setappie(&vap->iv_appie_beacon, ireq); 2448 if (error == 0) 2449 ieee80211_beacon_notify(vap, IEEE80211_BEACON_APPIE); 2450 break; 2451 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 2452 error = setappie(&vap->iv_appie_proberesp, ireq); 2453 break; 2454 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 2455 if (vap->iv_opmode == IEEE80211_M_HOSTAP) 2456 error = setappie(&vap->iv_appie_assocresp, ireq); 2457 else 2458 error = EINVAL; 2459 break; 2460 case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 2461 error = setappie(&vap->iv_appie_probereq, ireq); 2462 break; 2463 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 2464 if (vap->iv_opmode == IEEE80211_M_STA) 2465 error = setappie(&vap->iv_appie_assocreq, ireq); 2466 else 2467 error = EINVAL; 2468 break; 2469 case (IEEE80211_APPIE_WPA & IEEE80211_FC0_SUBTYPE_MASK): 2470 error = setappie(&vap->iv_appie_wpa, ireq); 2471 if (error == 0) { 2472 /* 2473 * Must split single blob of data into separate 2474 * WPA and RSN ie's because they go in different 2475 * locations in the mgt frames. 2476 * XXX use IEEE80211_IOC_WPA2 so user code does split 2477 */ 2478 vap->iv_wpa_ie = NULL; 2479 vap->iv_rsn_ie = NULL; 2480 if (vap->iv_appie_wpa != NULL) { 2481 struct ieee80211_appie *appie = 2482 vap->iv_appie_wpa; 2483 uint8_t *data = appie->ie_data; 2484 2485 /* XXX ie length validate is painful, cheat */ 2486 setwparsnie(vap, data, appie->ie_len); 2487 setwparsnie(vap, data + 2 + data[1], 2488 appie->ie_len - (2 + data[1])); 2489 } 2490 if (vap->iv_opmode == IEEE80211_M_HOSTAP || 2491 vap->iv_opmode == IEEE80211_M_IBSS) { 2492 /* 2493 * Must rebuild beacon frame as the update 2494 * mechanism doesn't handle WPA/RSN ie's. 2495 * Could extend it but it doesn't normally 2496 * change; this is just to deal with hostapd 2497 * plumbing the ie after the interface is up. 2498 */ 2499 error = ENETRESET; 2500 } 2501 } 2502 break; 2503 default: 2504 error = EINVAL; 2505 break; 2506 } 2507 return error; 2508 } 2509 2510 static int 2511 ieee80211_ioctl_setappie(struct ieee80211vap *vap, 2512 const struct ieee80211req *ireq) 2513 { 2514 struct ieee80211com *ic = vap->iv_ic; 2515 int error; 2516 uint8_t fc0; 2517 2518 fc0 = ireq->i_val & 0xff; 2519 if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT) 2520 return EINVAL; 2521 /* NB: could check iv_opmode and reject but hardly worth the effort */ 2522 IEEE80211_LOCK(ic); 2523 error = ieee80211_ioctl_setappie_locked(vap, ireq, fc0); 2524 IEEE80211_UNLOCK(ic); 2525 return error; 2526 } 2527 2528 static int 2529 ieee80211_ioctl_chanswitch(struct ieee80211vap *vap, struct ieee80211req *ireq) 2530 { 2531 struct ieee80211com *ic = vap->iv_ic; 2532 struct ieee80211_chanswitch_req csr; 2533 struct ieee80211_channel *c; 2534 int error; 2535 2536 if (ireq->i_len != sizeof(csr)) 2537 return EINVAL; 2538 error = copyin(ireq->i_data, &csr, sizeof(csr)); 2539 if (error != 0) 2540 return error; 2541 /* XXX adhoc mode not supported */ 2542 if (vap->iv_opmode != IEEE80211_M_HOSTAP || 2543 (vap->iv_flags & IEEE80211_F_DOTH) == 0) 2544 return EOPNOTSUPP; 2545 c = ieee80211_find_channel(ic, 2546 csr.csa_chan.ic_freq, csr.csa_chan.ic_flags); 2547 if (c == NULL) 2548 return ENOENT; 2549 IEEE80211_LOCK(ic); 2550 if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0) 2551 ieee80211_csa_startswitch(ic, c, csr.csa_mode, csr.csa_count); 2552 else if (csr.csa_count == 0) 2553 ieee80211_csa_cancelswitch(ic); 2554 else 2555 error = EBUSY; 2556 IEEE80211_UNLOCK(ic); 2557 return error; 2558 } 2559 2560 static int 2561 ieee80211_scanreq(struct ieee80211vap *vap, struct ieee80211_scan_req *sr) 2562 { 2563 #define IEEE80211_IOC_SCAN_FLAGS \ 2564 (IEEE80211_IOC_SCAN_NOPICK | IEEE80211_IOC_SCAN_ACTIVE | \ 2565 IEEE80211_IOC_SCAN_PICK1ST | IEEE80211_IOC_SCAN_BGSCAN | \ 2566 IEEE80211_IOC_SCAN_ONCE | IEEE80211_IOC_SCAN_NOBCAST | \ 2567 IEEE80211_IOC_SCAN_NOJOIN | IEEE80211_IOC_SCAN_FLUSH | \ 2568 IEEE80211_IOC_SCAN_CHECK) 2569 struct ieee80211com *ic = vap->iv_ic; 2570 int error, i; 2571 2572 /* convert duration */ 2573 if (sr->sr_duration == IEEE80211_IOC_SCAN_FOREVER) 2574 sr->sr_duration = IEEE80211_SCAN_FOREVER; 2575 else { 2576 if (sr->sr_duration < IEEE80211_IOC_SCAN_DURATION_MIN || 2577 sr->sr_duration > IEEE80211_IOC_SCAN_DURATION_MAX) 2578 return EINVAL; 2579 sr->sr_duration = msecs_to_ticks(sr->sr_duration); 2580 } 2581 /* convert min/max channel dwell */ 2582 if (sr->sr_mindwell != 0) 2583 sr->sr_mindwell = msecs_to_ticks(sr->sr_mindwell); 2584 if (sr->sr_maxdwell != 0) 2585 sr->sr_maxdwell = msecs_to_ticks(sr->sr_maxdwell); 2586 /* NB: silently reduce ssid count to what is supported */ 2587 if (sr->sr_nssid > IEEE80211_SCAN_MAX_SSID) 2588 sr->sr_nssid = IEEE80211_SCAN_MAX_SSID; 2589 for (i = 0; i < sr->sr_nssid; i++) 2590 if (sr->sr_ssid[i].len > IEEE80211_NWID_LEN) 2591 return EINVAL; 2592 /* cleanse flags just in case, could reject if invalid flags */ 2593 sr->sr_flags &= IEEE80211_IOC_SCAN_FLAGS; 2594 /* 2595 * Add an implicit NOPICK if the vap is not marked UP. This 2596 * allows applications to scan without joining a bss (or picking 2597 * a channel and setting up a bss) and without forcing manual 2598 * roaming mode--you just need to mark the parent device UP. 2599 */ 2600 if ((vap->iv_ifp->if_flags & IFF_UP) == 0) 2601 sr->sr_flags |= IEEE80211_IOC_SCAN_NOPICK; 2602 2603 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 2604 "%s: flags 0x%x%s duration 0x%x mindwell %u maxdwell %u nssid %d\n", 2605 __func__, sr->sr_flags, 2606 (vap->iv_ifp->if_flags & IFF_UP) == 0 ? " (!IFF_UP)" : "", 2607 sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell, sr->sr_nssid); 2608 /* 2609 * If we are in INIT state then the driver has never had a chance 2610 * to setup hardware state to do a scan; we must use the state 2611 * machine to get us up to the SCAN state but once we reach SCAN 2612 * state we then want to use the supplied params. Stash the 2613 * parameters in the vap and mark IEEE80211_FEXT_SCANREQ; the 2614 * state machines will recognize this and use the stashed params 2615 * to issue the scan request. 2616 * 2617 * Otherwise just invoke the scan machinery directly. 2618 */ 2619 IEEE80211_LOCK(ic); 2620 if (ic->ic_nrunning == 0) { 2621 IEEE80211_UNLOCK(ic); 2622 return ENXIO; 2623 } 2624 2625 if (vap->iv_state == IEEE80211_S_INIT) { 2626 /* NB: clobbers previous settings */ 2627 vap->iv_scanreq_flags = sr->sr_flags; 2628 vap->iv_scanreq_duration = sr->sr_duration; 2629 vap->iv_scanreq_nssid = sr->sr_nssid; 2630 for (i = 0; i < sr->sr_nssid; i++) { 2631 vap->iv_scanreq_ssid[i].len = sr->sr_ssid[i].len; 2632 memcpy(vap->iv_scanreq_ssid[i].ssid, 2633 sr->sr_ssid[i].ssid, sr->sr_ssid[i].len); 2634 } 2635 vap->iv_flags_ext |= IEEE80211_FEXT_SCANREQ; 2636 IEEE80211_UNLOCK(ic); 2637 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0); 2638 } else { 2639 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ; 2640 IEEE80211_UNLOCK(ic); 2641 if (sr->sr_flags & IEEE80211_IOC_SCAN_CHECK) { 2642 error = ieee80211_check_scan(vap, sr->sr_flags, 2643 sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell, 2644 sr->sr_nssid, 2645 /* NB: cheat, we assume structures are compatible */ 2646 (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]); 2647 } else { 2648 error = ieee80211_start_scan(vap, sr->sr_flags, 2649 sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell, 2650 sr->sr_nssid, 2651 /* NB: cheat, we assume structures are compatible */ 2652 (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]); 2653 } 2654 if (error == 0) 2655 return EINPROGRESS; 2656 } 2657 return 0; 2658 #undef IEEE80211_IOC_SCAN_FLAGS 2659 } 2660 2661 static int 2662 ieee80211_ioctl_scanreq(struct ieee80211vap *vap, struct ieee80211req *ireq) 2663 { 2664 struct ieee80211_scan_req *sr; 2665 int error; 2666 2667 if (ireq->i_len != sizeof(*sr)) 2668 return EINVAL; 2669 sr = IEEE80211_MALLOC(sizeof(*sr), M_TEMP, 2670 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 2671 if (sr == NULL) 2672 return ENOMEM; 2673 error = copyin(ireq->i_data, sr, sizeof(*sr)); 2674 if (error != 0) 2675 goto bad; 2676 error = ieee80211_scanreq(vap, sr); 2677 bad: 2678 IEEE80211_FREE(sr, M_TEMP); 2679 return error; 2680 } 2681 2682 static int 2683 ieee80211_ioctl_setstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq) 2684 { 2685 struct ieee80211_node *ni; 2686 struct ieee80211req_sta_vlan vlan; 2687 int error; 2688 2689 if (ireq->i_len != sizeof(vlan)) 2690 return EINVAL; 2691 error = copyin(ireq->i_data, &vlan, sizeof(vlan)); 2692 if (error != 0) 2693 return error; 2694 if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) { 2695 ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, 2696 vlan.sv_macaddr); 2697 if (ni == NULL) 2698 return ENOENT; 2699 } else 2700 ni = ieee80211_ref_node(vap->iv_bss); 2701 ni->ni_vlan = vlan.sv_vlan; 2702 ieee80211_free_node(ni); 2703 return error; 2704 } 2705 2706 static int 2707 isvap11g(const struct ieee80211vap *vap) 2708 { 2709 const struct ieee80211_node *bss = vap->iv_bss; 2710 return bss->ni_chan != IEEE80211_CHAN_ANYC && 2711 IEEE80211_IS_CHAN_ANYG(bss->ni_chan); 2712 } 2713 2714 static int 2715 isvapht(const struct ieee80211vap *vap) 2716 { 2717 const struct ieee80211_node *bss = vap->iv_bss; 2718 return bss->ni_chan != IEEE80211_CHAN_ANYC && 2719 IEEE80211_IS_CHAN_HT(bss->ni_chan); 2720 } 2721 2722 /* 2723 * Dummy ioctl set handler so the linker set is defined. 2724 */ 2725 static int 2726 dummy_ioctl_set(struct ieee80211vap *vap, struct ieee80211req *ireq) 2727 { 2728 return ENOSYS; 2729 } 2730 IEEE80211_IOCTL_SET(dummy, dummy_ioctl_set); 2731 2732 static int 2733 ieee80211_ioctl_setdefault(struct ieee80211vap *vap, struct ieee80211req *ireq) 2734 { 2735 ieee80211_ioctl_setfunc * const *set; 2736 int error; 2737 2738 SET_FOREACH(set, ieee80211_ioctl_setset) { 2739 error = (*set)(vap, ireq); 2740 if (error != ENOSYS) 2741 return error; 2742 } 2743 return EINVAL; 2744 } 2745 2746 static int 2747 ieee80211_ioctl_set80211(struct ieee80211vap *vap, u_long cmd, struct ieee80211req *ireq) 2748 { 2749 struct ieee80211com *ic = vap->iv_ic; 2750 int error; 2751 const struct ieee80211_authenticator *auth; 2752 uint8_t tmpkey[IEEE80211_KEYBUF_SIZE]; 2753 char tmpssid[IEEE80211_NWID_LEN]; 2754 uint8_t tmpbssid[IEEE80211_ADDR_LEN]; 2755 struct ieee80211_key *k; 2756 u_int kid; 2757 uint32_t flags; 2758 2759 error = 0; 2760 switch (ireq->i_type) { 2761 case IEEE80211_IOC_SSID: 2762 if (ireq->i_val != 0 || 2763 ireq->i_len > IEEE80211_NWID_LEN) 2764 return EINVAL; 2765 error = copyin(ireq->i_data, tmpssid, ireq->i_len); 2766 if (error) 2767 break; 2768 memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN); 2769 vap->iv_des_ssid[0].len = ireq->i_len; 2770 memcpy(vap->iv_des_ssid[0].ssid, tmpssid, ireq->i_len); 2771 vap->iv_des_nssid = (ireq->i_len > 0); 2772 error = ENETRESET; 2773 break; 2774 case IEEE80211_IOC_WEP: 2775 switch (ireq->i_val) { 2776 case IEEE80211_WEP_OFF: 2777 vap->iv_flags &= ~IEEE80211_F_PRIVACY; 2778 vap->iv_flags &= ~IEEE80211_F_DROPUNENC; 2779 break; 2780 case IEEE80211_WEP_ON: 2781 vap->iv_flags |= IEEE80211_F_PRIVACY; 2782 vap->iv_flags |= IEEE80211_F_DROPUNENC; 2783 break; 2784 case IEEE80211_WEP_MIXED: 2785 vap->iv_flags |= IEEE80211_F_PRIVACY; 2786 vap->iv_flags &= ~IEEE80211_F_DROPUNENC; 2787 break; 2788 } 2789 error = ENETRESET; 2790 break; 2791 case IEEE80211_IOC_WEPKEY: 2792 kid = (u_int) ireq->i_val; 2793 if (kid >= IEEE80211_WEP_NKID) 2794 return EINVAL; 2795 k = &vap->iv_nw_keys[kid]; 2796 if (ireq->i_len == 0) { 2797 /* zero-len =>'s delete any existing key */ 2798 (void) ieee80211_crypto_delkey(vap, k); 2799 break; 2800 } 2801 if (ireq->i_len > sizeof(tmpkey)) 2802 return EINVAL; 2803 memset(tmpkey, 0, sizeof(tmpkey)); 2804 error = copyin(ireq->i_data, tmpkey, ireq->i_len); 2805 if (error) 2806 break; 2807 ieee80211_key_update_begin(vap); 2808 k->wk_keyix = kid; /* NB: force fixed key id */ 2809 if (ieee80211_crypto_newkey(vap, IEEE80211_CIPHER_WEP, 2810 IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) { 2811 k->wk_keylen = ireq->i_len; 2812 memcpy(k->wk_key, tmpkey, sizeof(tmpkey)); 2813 IEEE80211_ADDR_COPY(k->wk_macaddr, vap->iv_myaddr); 2814 if (!ieee80211_crypto_setkey(vap, k)) 2815 error = EINVAL; 2816 } else 2817 error = EINVAL; 2818 ieee80211_key_update_end(vap); 2819 break; 2820 case IEEE80211_IOC_WEPTXKEY: 2821 kid = (u_int) ireq->i_val; 2822 if (kid >= IEEE80211_WEP_NKID && 2823 (uint16_t) kid != IEEE80211_KEYIX_NONE) 2824 return EINVAL; 2825 /* 2826 * Firmware devices may need to be told about an explicit 2827 * key index here, versus just inferring it from the 2828 * key set / change. Since we may also need to pause 2829 * things like transmit before the key is updated, 2830 * give the driver a chance to flush things by tying 2831 * into key update begin/end. 2832 */ 2833 ieee80211_key_update_begin(vap); 2834 ieee80211_crypto_set_deftxkey(vap, kid); 2835 ieee80211_key_update_end(vap); 2836 break; 2837 case IEEE80211_IOC_AUTHMODE: 2838 switch (ireq->i_val) { 2839 case IEEE80211_AUTH_WPA: 2840 case IEEE80211_AUTH_8021X: /* 802.1x */ 2841 case IEEE80211_AUTH_OPEN: /* open */ 2842 case IEEE80211_AUTH_SHARED: /* shared-key */ 2843 case IEEE80211_AUTH_AUTO: /* auto */ 2844 auth = ieee80211_authenticator_get(ireq->i_val); 2845 if (auth == NULL) 2846 return EINVAL; 2847 break; 2848 default: 2849 return EINVAL; 2850 } 2851 switch (ireq->i_val) { 2852 case IEEE80211_AUTH_WPA: /* WPA w/ 802.1x */ 2853 vap->iv_flags |= IEEE80211_F_PRIVACY; 2854 ireq->i_val = IEEE80211_AUTH_8021X; 2855 break; 2856 case IEEE80211_AUTH_OPEN: /* open */ 2857 vap->iv_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY); 2858 break; 2859 case IEEE80211_AUTH_SHARED: /* shared-key */ 2860 case IEEE80211_AUTH_8021X: /* 802.1x */ 2861 vap->iv_flags &= ~IEEE80211_F_WPA; 2862 /* both require a key so mark the PRIVACY capability */ 2863 vap->iv_flags |= IEEE80211_F_PRIVACY; 2864 break; 2865 case IEEE80211_AUTH_AUTO: /* auto */ 2866 vap->iv_flags &= ~IEEE80211_F_WPA; 2867 /* XXX PRIVACY handling? */ 2868 /* XXX what's the right way to do this? */ 2869 break; 2870 } 2871 /* NB: authenticator attach/detach happens on state change */ 2872 vap->iv_bss->ni_authmode = ireq->i_val; 2873 /* XXX mixed/mode/usage? */ 2874 vap->iv_auth = auth; 2875 error = ENETRESET; 2876 break; 2877 case IEEE80211_IOC_CHANNEL: 2878 error = ieee80211_ioctl_setchannel(vap, ireq); 2879 break; 2880 case IEEE80211_IOC_POWERSAVE: 2881 switch (ireq->i_val) { 2882 case IEEE80211_POWERSAVE_OFF: 2883 if (vap->iv_flags & IEEE80211_F_PMGTON) { 2884 ieee80211_syncflag(vap, -IEEE80211_F_PMGTON); 2885 error = ERESTART; 2886 } 2887 break; 2888 case IEEE80211_POWERSAVE_ON: 2889 if ((vap->iv_caps & IEEE80211_C_PMGT) == 0) 2890 error = EOPNOTSUPP; 2891 else if ((vap->iv_flags & IEEE80211_F_PMGTON) == 0) { 2892 ieee80211_syncflag(vap, IEEE80211_F_PMGTON); 2893 error = ERESTART; 2894 } 2895 break; 2896 default: 2897 error = EINVAL; 2898 break; 2899 } 2900 break; 2901 case IEEE80211_IOC_POWERSAVESLEEP: 2902 if (ireq->i_val < 0) 2903 return EINVAL; 2904 ic->ic_lintval = ireq->i_val; 2905 error = ERESTART; 2906 break; 2907 case IEEE80211_IOC_RTSTHRESHOLD: 2908 if (!(IEEE80211_RTS_MIN <= ireq->i_val && 2909 ireq->i_val <= IEEE80211_RTS_MAX)) 2910 return EINVAL; 2911 vap->iv_rtsthreshold = ireq->i_val; 2912 error = ERESTART; 2913 break; 2914 case IEEE80211_IOC_PROTMODE: 2915 if (ireq->i_val > IEEE80211_PROT_RTSCTS) 2916 return EINVAL; 2917 vap->iv_protmode = (enum ieee80211_protmode)ireq->i_val; 2918 /* NB: if not operating in 11g this can wait */ 2919 if (ic->ic_bsschan != IEEE80211_CHAN_ANYC && 2920 IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan)) 2921 error = ERESTART; 2922 /* driver callback for protection mode update */ 2923 ieee80211_vap_update_erp_protmode(vap); 2924 break; 2925 case IEEE80211_IOC_TXPOWER: 2926 if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0) 2927 return EOPNOTSUPP; 2928 if (!(IEEE80211_TXPOWER_MIN <= ireq->i_val && 2929 ireq->i_val <= IEEE80211_TXPOWER_MAX)) 2930 return EINVAL; 2931 ic->ic_txpowlimit = ireq->i_val; 2932 error = ERESTART; 2933 break; 2934 case IEEE80211_IOC_ROAMING: 2935 if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val && 2936 ireq->i_val <= IEEE80211_ROAMING_MANUAL)) 2937 return EINVAL; 2938 vap->iv_roaming = (enum ieee80211_roamingmode)ireq->i_val; 2939 /* XXXX reset? */ 2940 break; 2941 case IEEE80211_IOC_PRIVACY: 2942 if (ireq->i_val) { 2943 /* XXX check for key state? */ 2944 vap->iv_flags |= IEEE80211_F_PRIVACY; 2945 } else 2946 vap->iv_flags &= ~IEEE80211_F_PRIVACY; 2947 /* XXX ERESTART? */ 2948 break; 2949 case IEEE80211_IOC_DROPUNENCRYPTED: 2950 if (ireq->i_val) 2951 vap->iv_flags |= IEEE80211_F_DROPUNENC; 2952 else 2953 vap->iv_flags &= ~IEEE80211_F_DROPUNENC; 2954 /* XXX ERESTART? */ 2955 break; 2956 case IEEE80211_IOC_WPAKEY: 2957 error = ieee80211_ioctl_setkey(vap, ireq); 2958 break; 2959 case IEEE80211_IOC_DELKEY: 2960 error = ieee80211_ioctl_delkey(vap, ireq); 2961 break; 2962 case IEEE80211_IOC_MLME: 2963 error = ieee80211_ioctl_setmlme(vap, ireq); 2964 break; 2965 case IEEE80211_IOC_COUNTERMEASURES: 2966 if (ireq->i_val) { 2967 if ((vap->iv_flags & IEEE80211_F_WPA) == 0) 2968 return EOPNOTSUPP; 2969 vap->iv_flags |= IEEE80211_F_COUNTERM; 2970 } else 2971 vap->iv_flags &= ~IEEE80211_F_COUNTERM; 2972 /* XXX ERESTART? */ 2973 break; 2974 case IEEE80211_IOC_WPA: 2975 if (ireq->i_val > 3) 2976 return EINVAL; 2977 /* XXX verify ciphers available */ 2978 flags = vap->iv_flags & ~IEEE80211_F_WPA; 2979 switch (ireq->i_val) { 2980 case 0: 2981 /* wpa_supplicant calls this to clear the WPA config */ 2982 break; 2983 case 1: 2984 if (!(vap->iv_caps & IEEE80211_C_WPA1)) 2985 return EOPNOTSUPP; 2986 flags |= IEEE80211_F_WPA1; 2987 break; 2988 case 2: 2989 if (!(vap->iv_caps & IEEE80211_C_WPA2)) 2990 return EOPNOTSUPP; 2991 flags |= IEEE80211_F_WPA2; 2992 break; 2993 case 3: 2994 if ((vap->iv_caps & IEEE80211_C_WPA) != IEEE80211_C_WPA) 2995 return EOPNOTSUPP; 2996 flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2; 2997 break; 2998 default: /* Can't set any -> error */ 2999 return EOPNOTSUPP; 3000 } 3001 vap->iv_flags = flags; 3002 error = ERESTART; /* NB: can change beacon frame */ 3003 break; 3004 case IEEE80211_IOC_WME: 3005 if (ireq->i_val) { 3006 if ((vap->iv_caps & IEEE80211_C_WME) == 0) 3007 return EOPNOTSUPP; 3008 ieee80211_syncflag(vap, IEEE80211_F_WME); 3009 } else 3010 ieee80211_syncflag(vap, -IEEE80211_F_WME); 3011 error = ERESTART; /* NB: can change beacon frame */ 3012 break; 3013 case IEEE80211_IOC_HIDESSID: 3014 if (ireq->i_val) 3015 vap->iv_flags |= IEEE80211_F_HIDESSID; 3016 else 3017 vap->iv_flags &= ~IEEE80211_F_HIDESSID; 3018 error = ERESTART; /* XXX ENETRESET? */ 3019 break; 3020 case IEEE80211_IOC_APBRIDGE: 3021 if (ireq->i_val == 0) 3022 vap->iv_flags |= IEEE80211_F_NOBRIDGE; 3023 else 3024 vap->iv_flags &= ~IEEE80211_F_NOBRIDGE; 3025 break; 3026 case IEEE80211_IOC_BSSID: 3027 if (ireq->i_len != sizeof(tmpbssid)) 3028 return EINVAL; 3029 error = copyin(ireq->i_data, tmpbssid, ireq->i_len); 3030 if (error) 3031 break; 3032 IEEE80211_ADDR_COPY(vap->iv_des_bssid, tmpbssid); 3033 if (IEEE80211_ADDR_EQ(vap->iv_des_bssid, zerobssid)) 3034 vap->iv_flags &= ~IEEE80211_F_DESBSSID; 3035 else 3036 vap->iv_flags |= IEEE80211_F_DESBSSID; 3037 error = ENETRESET; 3038 break; 3039 case IEEE80211_IOC_CHANLIST: 3040 error = ieee80211_ioctl_setchanlist(vap, ireq); 3041 break; 3042 #define OLD_IEEE80211_IOC_SCAN_REQ 23 3043 #ifdef OLD_IEEE80211_IOC_SCAN_REQ 3044 case OLD_IEEE80211_IOC_SCAN_REQ: 3045 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 3046 "%s: active scan request\n", __func__); 3047 /* 3048 * If we are in INIT state then the driver has never 3049 * had a chance to setup hardware state to do a scan; 3050 * use the state machine to get us up the SCAN state. 3051 * Otherwise just invoke the scan machinery to start 3052 * a one-time scan. 3053 */ 3054 if (vap->iv_state == IEEE80211_S_INIT) 3055 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0); 3056 else 3057 (void) ieee80211_start_scan(vap, 3058 IEEE80211_SCAN_ACTIVE | 3059 IEEE80211_SCAN_NOPICK | 3060 IEEE80211_SCAN_ONCE, 3061 IEEE80211_SCAN_FOREVER, 0, 0, 3062 /* XXX use ioctl params */ 3063 vap->iv_des_nssid, vap->iv_des_ssid); 3064 break; 3065 #endif /* OLD_IEEE80211_IOC_SCAN_REQ */ 3066 case IEEE80211_IOC_SCAN_REQ: 3067 error = ieee80211_ioctl_scanreq(vap, ireq); 3068 break; 3069 case IEEE80211_IOC_SCAN_CANCEL: 3070 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, 3071 "%s: cancel scan\n", __func__); 3072 ieee80211_cancel_scan(vap); 3073 break; 3074 case IEEE80211_IOC_HTCONF: 3075 if (ireq->i_val & 1) 3076 ieee80211_syncflag_ht(vap, IEEE80211_FHT_HT); 3077 else 3078 ieee80211_syncflag_ht(vap, -IEEE80211_FHT_HT); 3079 if (ireq->i_val & 2) 3080 ieee80211_syncflag_ht(vap, IEEE80211_FHT_USEHT40); 3081 else 3082 ieee80211_syncflag_ht(vap, -IEEE80211_FHT_USEHT40); 3083 error = ENETRESET; 3084 break; 3085 case IEEE80211_IOC_ADDMAC: 3086 case IEEE80211_IOC_DELMAC: 3087 error = ieee80211_ioctl_macmac(vap, ireq); 3088 break; 3089 case IEEE80211_IOC_MACCMD: 3090 error = ieee80211_ioctl_setmaccmd(vap, ireq); 3091 break; 3092 case IEEE80211_IOC_STA_STATS: 3093 error = ieee80211_ioctl_setstastats(vap, ireq); 3094 break; 3095 case IEEE80211_IOC_STA_TXPOW: 3096 error = ieee80211_ioctl_setstatxpow(vap, ireq); 3097 break; 3098 case IEEE80211_IOC_WME_CWMIN: /* WME: CWmin */ 3099 case IEEE80211_IOC_WME_CWMAX: /* WME: CWmax */ 3100 case IEEE80211_IOC_WME_AIFS: /* WME: AIFS */ 3101 case IEEE80211_IOC_WME_TXOPLIMIT: /* WME: txops limit */ 3102 case IEEE80211_IOC_WME_ACM: /* WME: ACM (bss only) */ 3103 case IEEE80211_IOC_WME_ACKPOLICY: /* WME: ACK policy (!bss only) */ 3104 error = ieee80211_ioctl_setwmeparam(vap, ireq); 3105 break; 3106 case IEEE80211_IOC_DTIM_PERIOD: 3107 if (vap->iv_opmode != IEEE80211_M_HOSTAP && 3108 vap->iv_opmode != IEEE80211_M_MBSS && 3109 vap->iv_opmode != IEEE80211_M_IBSS) 3110 return EINVAL; 3111 if (IEEE80211_DTIM_MIN <= ireq->i_val && 3112 ireq->i_val <= IEEE80211_DTIM_MAX) { 3113 vap->iv_dtim_period = ireq->i_val; 3114 error = ENETRESET; /* requires restart */ 3115 } else 3116 error = EINVAL; 3117 break; 3118 case IEEE80211_IOC_BEACON_INTERVAL: 3119 if (vap->iv_opmode != IEEE80211_M_HOSTAP && 3120 vap->iv_opmode != IEEE80211_M_MBSS && 3121 vap->iv_opmode != IEEE80211_M_IBSS) 3122 return EINVAL; 3123 if (IEEE80211_BINTVAL_MIN <= ireq->i_val && 3124 ireq->i_val <= IEEE80211_BINTVAL_MAX) { 3125 ic->ic_bintval = ireq->i_val; 3126 error = ENETRESET; /* requires restart */ 3127 } else 3128 error = EINVAL; 3129 break; 3130 case IEEE80211_IOC_PUREG: 3131 if (ireq->i_val) 3132 vap->iv_flags |= IEEE80211_F_PUREG; 3133 else 3134 vap->iv_flags &= ~IEEE80211_F_PUREG; 3135 /* NB: reset only if we're operating on an 11g channel */ 3136 if (isvap11g(vap)) 3137 error = ENETRESET; 3138 break; 3139 case IEEE80211_IOC_QUIET: 3140 vap->iv_quiet= ireq->i_val; 3141 break; 3142 case IEEE80211_IOC_QUIET_COUNT: 3143 vap->iv_quiet_count=ireq->i_val; 3144 break; 3145 case IEEE80211_IOC_QUIET_PERIOD: 3146 vap->iv_quiet_period=ireq->i_val; 3147 break; 3148 case IEEE80211_IOC_QUIET_OFFSET: 3149 vap->iv_quiet_offset=ireq->i_val; 3150 break; 3151 case IEEE80211_IOC_QUIET_DUR: 3152 if(ireq->i_val < vap->iv_bss->ni_intval) 3153 vap->iv_quiet_duration = ireq->i_val; 3154 else 3155 error = EINVAL; 3156 break; 3157 case IEEE80211_IOC_BGSCAN: 3158 if (ireq->i_val) { 3159 if ((vap->iv_caps & IEEE80211_C_BGSCAN) == 0) 3160 return EOPNOTSUPP; 3161 vap->iv_flags |= IEEE80211_F_BGSCAN; 3162 } else 3163 vap->iv_flags &= ~IEEE80211_F_BGSCAN; 3164 break; 3165 case IEEE80211_IOC_BGSCAN_IDLE: 3166 if (ireq->i_val >= IEEE80211_BGSCAN_IDLE_MIN) 3167 vap->iv_bgscanidle = ireq->i_val*hz/1000; 3168 else 3169 error = EINVAL; 3170 break; 3171 case IEEE80211_IOC_BGSCAN_INTERVAL: 3172 if (ireq->i_val >= IEEE80211_BGSCAN_INTVAL_MIN) 3173 vap->iv_bgscanintvl = ireq->i_val*hz; 3174 else 3175 error = EINVAL; 3176 break; 3177 case IEEE80211_IOC_SCANVALID: 3178 if (ireq->i_val >= IEEE80211_SCAN_VALID_MIN) 3179 vap->iv_scanvalid = ireq->i_val*hz; 3180 else 3181 error = EINVAL; 3182 break; 3183 case IEEE80211_IOC_FRAGTHRESHOLD: 3184 if ((vap->iv_caps & IEEE80211_C_TXFRAG) == 0 && 3185 ireq->i_val != IEEE80211_FRAG_MAX) 3186 return EOPNOTSUPP; 3187 if (!(IEEE80211_FRAG_MIN <= ireq->i_val && 3188 ireq->i_val <= IEEE80211_FRAG_MAX)) 3189 return EINVAL; 3190 vap->iv_fragthreshold = ireq->i_val; 3191 error = ERESTART; 3192 break; 3193 case IEEE80211_IOC_BURST: 3194 if (ireq->i_val) { 3195 if ((vap->iv_caps & IEEE80211_C_BURST) == 0) 3196 return EOPNOTSUPP; 3197 ieee80211_syncflag(vap, IEEE80211_F_BURST); 3198 } else 3199 ieee80211_syncflag(vap, -IEEE80211_F_BURST); 3200 error = ERESTART; 3201 break; 3202 case IEEE80211_IOC_BMISSTHRESHOLD: 3203 if (!(IEEE80211_HWBMISS_MIN <= ireq->i_val && 3204 ireq->i_val <= IEEE80211_HWBMISS_MAX)) 3205 return EINVAL; 3206 vap->iv_bmissthreshold = ireq->i_val; 3207 error = ERESTART; 3208 break; 3209 case IEEE80211_IOC_CURCHAN: 3210 error = ieee80211_ioctl_setcurchan(vap, ireq); 3211 break; 3212 case IEEE80211_IOC_SHORTGI: 3213 if (ireq->i_val) { 3214 #define IEEE80211_HTCAP_SHORTGI \ 3215 (IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40) 3216 if (((ireq->i_val ^ vap->iv_htcaps) & IEEE80211_HTCAP_SHORTGI) != 0) 3217 return EINVAL; 3218 if (ireq->i_val & IEEE80211_HTCAP_SHORTGI20) 3219 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20; 3220 if (ireq->i_val & IEEE80211_HTCAP_SHORTGI40) 3221 vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40; 3222 #undef IEEE80211_HTCAP_SHORTGI 3223 } else 3224 vap->iv_flags_ht &= 3225 ~(IEEE80211_FHT_SHORTGI20 | IEEE80211_FHT_SHORTGI40); 3226 error = ERESTART; 3227 break; 3228 case IEEE80211_IOC_AMPDU: 3229 if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMPDU) == 0) 3230 return EINVAL; 3231 if (ireq->i_val & 1) 3232 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX; 3233 else 3234 vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX; 3235 if (ireq->i_val & 2) 3236 vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX; 3237 else 3238 vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX; 3239 /* NB: reset only if we're operating on an 11n channel */ 3240 if (isvapht(vap)) 3241 error = ERESTART; 3242 break; 3243 case IEEE80211_IOC_AMPDU_LIMIT: 3244 /* XXX TODO: figure out ampdu_limit versus ampdu_rxmax */ 3245 if (!(IEEE80211_HTCAP_MAXRXAMPDU_8K <= ireq->i_val && 3246 ireq->i_val <= IEEE80211_HTCAP_MAXRXAMPDU_64K)) 3247 return EINVAL; 3248 if (vap->iv_opmode == IEEE80211_M_HOSTAP) 3249 vap->iv_ampdu_rxmax = ireq->i_val; 3250 else 3251 vap->iv_ampdu_limit = ireq->i_val; 3252 error = ERESTART; 3253 break; 3254 case IEEE80211_IOC_AMPDU_DENSITY: 3255 if (!(IEEE80211_HTCAP_MPDUDENSITY_NA <= ireq->i_val && 3256 ireq->i_val <= IEEE80211_HTCAP_MPDUDENSITY_16)) 3257 return EINVAL; 3258 vap->iv_ampdu_density = ireq->i_val; 3259 error = ERESTART; 3260 break; 3261 case IEEE80211_IOC_AMSDU: 3262 if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMSDU) == 0) 3263 return EINVAL; 3264 if (ireq->i_val & 1) 3265 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX; 3266 else 3267 vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_TX; 3268 if (ireq->i_val & 2) 3269 vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX; 3270 else 3271 vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_RX; 3272 /* NB: reset only if we're operating on an 11n channel */ 3273 if (isvapht(vap)) 3274 error = ERESTART; 3275 break; 3276 case IEEE80211_IOC_AMSDU_LIMIT: 3277 /* XXX validate */ 3278 vap->iv_amsdu_limit = ireq->i_val; /* XXX truncation? */ 3279 break; 3280 case IEEE80211_IOC_PUREN: 3281 if (ireq->i_val) { 3282 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0) 3283 return EINVAL; 3284 vap->iv_flags_ht |= IEEE80211_FHT_PUREN; 3285 } else 3286 vap->iv_flags_ht &= ~IEEE80211_FHT_PUREN; 3287 /* NB: reset only if we're operating on an 11n channel */ 3288 if (isvapht(vap)) 3289 error = ERESTART; 3290 break; 3291 case IEEE80211_IOC_DOTH: 3292 if (ireq->i_val) { 3293 #if 0 3294 /* XXX no capability */ 3295 if ((vap->iv_caps & IEEE80211_C_DOTH) == 0) 3296 return EOPNOTSUPP; 3297 #endif 3298 vap->iv_flags |= IEEE80211_F_DOTH; 3299 } else 3300 vap->iv_flags &= ~IEEE80211_F_DOTH; 3301 error = ENETRESET; 3302 break; 3303 case IEEE80211_IOC_REGDOMAIN: 3304 error = ieee80211_ioctl_setregdomain(vap, ireq); 3305 break; 3306 case IEEE80211_IOC_ROAM: 3307 error = ieee80211_ioctl_setroam(vap, ireq); 3308 break; 3309 case IEEE80211_IOC_TXPARAMS: 3310 error = ieee80211_ioctl_settxparams(vap, ireq); 3311 break; 3312 case IEEE80211_IOC_HTCOMPAT: 3313 if (ireq->i_val) { 3314 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0) 3315 return EOPNOTSUPP; 3316 vap->iv_flags_ht |= IEEE80211_FHT_HTCOMPAT; 3317 } else 3318 vap->iv_flags_ht &= ~IEEE80211_FHT_HTCOMPAT; 3319 /* NB: reset only if we're operating on an 11n channel */ 3320 if (isvapht(vap)) 3321 error = ERESTART; 3322 break; 3323 case IEEE80211_IOC_DWDS: 3324 if (ireq->i_val) { 3325 /* NB: DWDS only makes sense for WDS-capable devices */ 3326 if ((ic->ic_caps & IEEE80211_C_WDS) == 0) 3327 return EOPNOTSUPP; 3328 /* NB: DWDS is used only with ap+sta vaps */ 3329 if (vap->iv_opmode != IEEE80211_M_HOSTAP && 3330 vap->iv_opmode != IEEE80211_M_STA) 3331 return EINVAL; 3332 vap->iv_flags |= IEEE80211_F_DWDS; 3333 if (vap->iv_opmode == IEEE80211_M_STA) 3334 vap->iv_flags_ext |= IEEE80211_FEXT_4ADDR; 3335 } else { 3336 vap->iv_flags &= ~IEEE80211_F_DWDS; 3337 if (vap->iv_opmode == IEEE80211_M_STA) 3338 vap->iv_flags_ext &= ~IEEE80211_FEXT_4ADDR; 3339 } 3340 break; 3341 case IEEE80211_IOC_INACTIVITY: 3342 if (ireq->i_val) 3343 vap->iv_flags_ext |= IEEE80211_FEXT_INACT; 3344 else 3345 vap->iv_flags_ext &= ~IEEE80211_FEXT_INACT; 3346 break; 3347 case IEEE80211_IOC_APPIE: 3348 error = ieee80211_ioctl_setappie(vap, ireq); 3349 break; 3350 case IEEE80211_IOC_WPS: 3351 if (ireq->i_val) { 3352 if ((vap->iv_caps & IEEE80211_C_WPA) == 0) 3353 return EOPNOTSUPP; 3354 vap->iv_flags_ext |= IEEE80211_FEXT_WPS; 3355 } else 3356 vap->iv_flags_ext &= ~IEEE80211_FEXT_WPS; 3357 break; 3358 case IEEE80211_IOC_TSN: 3359 if (ireq->i_val) { 3360 if ((vap->iv_caps & IEEE80211_C_WPA) == 0) 3361 return EOPNOTSUPP; 3362 vap->iv_flags_ext |= IEEE80211_FEXT_TSN; 3363 } else 3364 vap->iv_flags_ext &= ~IEEE80211_FEXT_TSN; 3365 break; 3366 case IEEE80211_IOC_CHANSWITCH: 3367 error = ieee80211_ioctl_chanswitch(vap, ireq); 3368 break; 3369 case IEEE80211_IOC_DFS: 3370 if (ireq->i_val) { 3371 if ((vap->iv_caps & IEEE80211_C_DFS) == 0) 3372 return EOPNOTSUPP; 3373 /* NB: DFS requires 11h support */ 3374 if ((vap->iv_flags & IEEE80211_F_DOTH) == 0) 3375 return EINVAL; 3376 vap->iv_flags_ext |= IEEE80211_FEXT_DFS; 3377 } else 3378 vap->iv_flags_ext &= ~IEEE80211_FEXT_DFS; 3379 break; 3380 case IEEE80211_IOC_DOTD: 3381 if (ireq->i_val) 3382 vap->iv_flags_ext |= IEEE80211_FEXT_DOTD; 3383 else 3384 vap->iv_flags_ext &= ~IEEE80211_FEXT_DOTD; 3385 if (vap->iv_opmode == IEEE80211_M_STA) 3386 error = ENETRESET; 3387 break; 3388 case IEEE80211_IOC_HTPROTMODE: 3389 if (ireq->i_val > IEEE80211_PROT_RTSCTS) 3390 return EINVAL; 3391 vap->iv_htprotmode = ireq->i_val ? 3392 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_NONE; 3393 /* NB: if not operating in 11n this can wait */ 3394 if (isvapht(vap)) 3395 error = ERESTART; 3396 /* Notify driver layer of HT protmode changes */ 3397 ieee80211_vap_update_ht_protmode(vap); 3398 break; 3399 case IEEE80211_IOC_STA_VLAN: 3400 error = ieee80211_ioctl_setstavlan(vap, ireq); 3401 break; 3402 case IEEE80211_IOC_SMPS: 3403 if ((ireq->i_val &~ IEEE80211_HTCAP_SMPS) != 0 || 3404 ireq->i_val == 0x0008) /* value of 2 is reserved */ 3405 return EINVAL; 3406 if (ireq->i_val != IEEE80211_HTCAP_SMPS_OFF && 3407 (vap->iv_htcaps & IEEE80211_HTC_SMPS) == 0) 3408 return EOPNOTSUPP; 3409 vap->iv_htcaps = (vap->iv_htcaps &~ IEEE80211_HTCAP_SMPS) | 3410 ireq->i_val; 3411 /* NB: if not operating in 11n this can wait */ 3412 if (isvapht(vap)) 3413 error = ERESTART; 3414 break; 3415 case IEEE80211_IOC_RIFS: 3416 if (ireq->i_val != 0) { 3417 if ((vap->iv_htcaps & IEEE80211_HTC_RIFS) == 0) 3418 return EOPNOTSUPP; 3419 vap->iv_flags_ht |= IEEE80211_FHT_RIFS; 3420 } else 3421 vap->iv_flags_ht &= ~IEEE80211_FHT_RIFS; 3422 /* NB: if not operating in 11n this can wait */ 3423 if (isvapht(vap)) 3424 error = ERESTART; 3425 break; 3426 case IEEE80211_IOC_STBC: 3427 /* Check if we can do STBC TX/RX before changing the setting */ 3428 if ((ireq->i_val & 1) && 3429 ((vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC) == 0)) 3430 return EOPNOTSUPP; 3431 if ((ireq->i_val & 2) && 3432 ((vap->iv_htcaps & IEEE80211_HTCAP_RXSTBC) == 0)) 3433 return EOPNOTSUPP; 3434 3435 /* TX */ 3436 if (ireq->i_val & 1) 3437 vap->iv_flags_ht |= IEEE80211_FHT_STBC_TX; 3438 else 3439 vap->iv_flags_ht &= ~IEEE80211_FHT_STBC_TX; 3440 3441 /* RX */ 3442 if (ireq->i_val & 2) 3443 vap->iv_flags_ht |= IEEE80211_FHT_STBC_RX; 3444 else 3445 vap->iv_flags_ht &= ~IEEE80211_FHT_STBC_RX; 3446 3447 /* NB: reset only if we're operating on an 11n channel */ 3448 if (isvapht(vap)) 3449 error = ERESTART; 3450 break; 3451 case IEEE80211_IOC_LDPC: 3452 /* Check if we can do LDPC TX/RX before changing the setting */ 3453 if ((ireq->i_val & 1) && 3454 (vap->iv_htcaps & IEEE80211_HTC_TXLDPC) == 0) 3455 return EOPNOTSUPP; 3456 if ((ireq->i_val & 2) && 3457 (vap->iv_htcaps & IEEE80211_HTCAP_LDPC) == 0) 3458 return EOPNOTSUPP; 3459 3460 /* TX */ 3461 if (ireq->i_val & 1) 3462 vap->iv_flags_ht |= IEEE80211_FHT_LDPC_TX; 3463 else 3464 vap->iv_flags_ht &= ~IEEE80211_FHT_LDPC_TX; 3465 3466 /* RX */ 3467 if (ireq->i_val & 2) 3468 vap->iv_flags_ht |= IEEE80211_FHT_LDPC_RX; 3469 else 3470 vap->iv_flags_ht &= ~IEEE80211_FHT_LDPC_RX; 3471 3472 /* NB: reset only if we're operating on an 11n channel */ 3473 if (isvapht(vap)) 3474 error = ERESTART; 3475 break; 3476 case IEEE80211_IOC_UAPSD: 3477 if ((vap->iv_caps & IEEE80211_C_UAPSD) == 0) 3478 return EOPNOTSUPP; 3479 if (ireq->i_val == 0) 3480 vap->iv_flags_ext &= ~IEEE80211_FEXT_UAPSD; 3481 else if (ireq->i_val == 1) 3482 vap->iv_flags_ext |= IEEE80211_FEXT_UAPSD; 3483 else 3484 return EINVAL; 3485 break; 3486 3487 /* VHT */ 3488 case IEEE80211_IOC_VHTCONF: 3489 if (ireq->i_val & 1) 3490 ieee80211_syncflag_vht(vap, IEEE80211_FVHT_VHT); 3491 else 3492 ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_VHT); 3493 3494 if (ireq->i_val & 2) 3495 ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT40); 3496 else 3497 ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT40); 3498 3499 if (ireq->i_val & 4) 3500 ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT80); 3501 else 3502 ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT80); 3503 3504 if (ireq->i_val & 8) 3505 ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT80P80); 3506 else 3507 ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT80P80); 3508 3509 if (ireq->i_val & 16) 3510 ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT160); 3511 else 3512 ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT160); 3513 3514 error = ENETRESET; 3515 break; 3516 3517 default: 3518 error = ieee80211_ioctl_setdefault(vap, ireq); 3519 break; 3520 } 3521 /* 3522 * The convention is that ENETRESET means an operation 3523 * requires a complete re-initialization of the device (e.g. 3524 * changing something that affects the association state). 3525 * ERESTART means the request may be handled with only a 3526 * reload of the hardware state. We hand ERESTART requests 3527 * to the iv_reset callback so the driver can decide. If 3528 * a device does not fillin iv_reset then it defaults to one 3529 * that returns ENETRESET. Otherwise a driver may return 3530 * ENETRESET (in which case a full reset will be done) or 3531 * 0 to mean there's no need to do anything (e.g. when the 3532 * change has no effect on the driver/device). 3533 */ 3534 if (error == ERESTART) 3535 error = IFNET_IS_UP_RUNNING(vap->iv_ifp) ? 3536 vap->iv_reset(vap, ireq->i_type) : 0; 3537 if (error == ENETRESET) { 3538 /* XXX need to re-think AUTO handling */ 3539 if (IS_UP_AUTO(vap)) 3540 ieee80211_init(vap); 3541 error = 0; 3542 } 3543 return error; 3544 } 3545 3546 int 3547 ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 3548 { 3549 struct ieee80211vap *vap = ifp->if_softc; 3550 struct ieee80211com *ic = vap->iv_ic; 3551 int error = 0, wait = 0, ic_used; 3552 struct ifreq *ifr; 3553 struct ifaddr *ifa; /* XXX */ 3554 3555 ic_used = (cmd != SIOCSIFMTU && cmd != SIOCG80211STATS); 3556 if (ic_used && (error = ieee80211_com_vincref(vap)) != 0) 3557 return (error); 3558 3559 switch (cmd) { 3560 case SIOCSIFFLAGS: 3561 IEEE80211_LOCK(ic); 3562 if ((ifp->if_flags ^ vap->iv_ifflags) & IFF_PROMISC) { 3563 /* 3564 * Enable promiscuous mode when: 3565 * 1. Interface is not a member of bridge, or 3566 * 2. Requested by user, or 3567 * 3. In monitor (or adhoc-demo) mode. 3568 */ 3569 if (ifp->if_bridge == NULL || 3570 (ifp->if_flags & IFF_PPROMISC) != 0 || 3571 vap->iv_opmode == IEEE80211_M_MONITOR || 3572 (vap->iv_opmode == IEEE80211_M_AHDEMO && 3573 (vap->iv_caps & IEEE80211_C_TDMA) == 0)) { 3574 ieee80211_promisc(vap, 3575 ifp->if_flags & IFF_PROMISC); 3576 vap->iv_ifflags ^= IFF_PROMISC; 3577 } 3578 } 3579 if ((ifp->if_flags ^ vap->iv_ifflags) & IFF_ALLMULTI) { 3580 ieee80211_allmulti(vap, ifp->if_flags & IFF_ALLMULTI); 3581 vap->iv_ifflags ^= IFF_ALLMULTI; 3582 } 3583 if (ifp->if_flags & IFF_UP) { 3584 /* 3585 * Bring ourself up unless we're already operational. 3586 * If we're the first vap and the parent is not up 3587 * then it will automatically be brought up as a 3588 * side-effect of bringing ourself up. 3589 */ 3590 if (vap->iv_state == IEEE80211_S_INIT) { 3591 if (ic->ic_nrunning == 0) 3592 wait = 1; 3593 ieee80211_start_locked(vap); 3594 } 3595 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 3596 /* 3597 * Stop ourself. If we are the last vap to be 3598 * marked down the parent will also be taken down. 3599 */ 3600 if (ic->ic_nrunning == 1) 3601 wait = 1; 3602 ieee80211_stop_locked(vap); 3603 } 3604 IEEE80211_UNLOCK(ic); 3605 /* Wait for parent ioctl handler if it was queued */ 3606 if (wait) { 3607 struct epoch_tracker et; 3608 3609 ieee80211_waitfor_parent(ic); 3610 3611 /* 3612 * Check if the MAC address was changed 3613 * via SIOCSIFLLADDR ioctl. 3614 * 3615 * NB: device may be detached during initialization; 3616 * use if_ioctl for existence check. 3617 */ 3618 NET_EPOCH_ENTER(et); 3619 if (ifp->if_ioctl == ieee80211_ioctl && 3620 (ifp->if_flags & IFF_UP) == 0 && 3621 !IEEE80211_ADDR_EQ(vap->iv_myaddr, IF_LLADDR(ifp))) 3622 IEEE80211_ADDR_COPY(vap->iv_myaddr, 3623 IF_LLADDR(ifp)); 3624 NET_EPOCH_EXIT(et); 3625 } 3626 break; 3627 case SIOCADDMULTI: 3628 case SIOCDELMULTI: 3629 ieee80211_runtask(ic, &ic->ic_mcast_task); 3630 break; 3631 case SIOCSIFMEDIA: 3632 case SIOCGIFMEDIA: 3633 ifr = (struct ifreq *)data; 3634 error = ifmedia_ioctl(ifp, ifr, &vap->iv_media, cmd); 3635 break; 3636 case SIOCG80211: 3637 error = ieee80211_ioctl_get80211(vap, cmd, 3638 (struct ieee80211req *) data); 3639 break; 3640 case SIOCS80211: 3641 /* XXX TODO: move priv check to ieee80211_freebsd.c */ 3642 error = priv_check(curthread, PRIV_NET80211_VAP_MANAGE); 3643 if (error == 0) 3644 error = ieee80211_ioctl_set80211(vap, cmd, 3645 (struct ieee80211req *) data); 3646 break; 3647 case SIOCG80211STATS: 3648 ifr = (struct ifreq *)data; 3649 copyout(&vap->iv_stats, ifr_data_get_ptr(ifr), 3650 sizeof (vap->iv_stats)); 3651 break; 3652 case SIOCSIFMTU: 3653 ifr = (struct ifreq *)data; 3654 if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu && 3655 ifr->ifr_mtu <= IEEE80211_MTU_MAX)) 3656 error = EINVAL; 3657 else 3658 ifp->if_mtu = ifr->ifr_mtu; 3659 break; 3660 case SIOCSIFADDR: 3661 /* 3662 * XXX Handle this directly so we can suppress if_init calls. 3663 * XXX This should be done in ether_ioctl but for the moment 3664 * XXX there are too many other parts of the system that 3665 * XXX set IFF_UP and so suppress if_init being called when 3666 * XXX it should be. 3667 */ 3668 ifa = (struct ifaddr *) data; 3669 switch (ifa->ifa_addr->sa_family) { 3670 #ifdef INET 3671 case AF_INET: 3672 if ((ifp->if_flags & IFF_UP) == 0) { 3673 ifp->if_flags |= IFF_UP; 3674 ifp->if_init(ifp->if_softc); 3675 } 3676 arp_ifinit(ifp, ifa); 3677 break; 3678 #endif 3679 default: 3680 if ((ifp->if_flags & IFF_UP) == 0) { 3681 ifp->if_flags |= IFF_UP; 3682 ifp->if_init(ifp->if_softc); 3683 } 3684 break; 3685 } 3686 break; 3687 case SIOCSIFLLADDR: 3688 /* XXX TODO: move priv check to ieee80211_freebsd.c */ 3689 error = priv_check(curthread, PRIV_NET80211_VAP_SETMAC); 3690 if (error == 0) 3691 break; 3692 /* Fallthrough */ 3693 default: 3694 /* 3695 * Pass unknown ioctls first to the driver, and if it 3696 * returns ENOTTY, then to the generic Ethernet handler. 3697 */ 3698 if (ic->ic_ioctl != NULL && 3699 (error = ic->ic_ioctl(ic, cmd, data)) != ENOTTY) 3700 break; 3701 error = ether_ioctl(ifp, cmd, data); 3702 break; 3703 } 3704 3705 if (ic_used) 3706 ieee80211_com_vdecref(vap); 3707 3708 return (error); 3709 } 3710