1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * cfg80211 MLME SAP interface 4 * 5 * Copyright (c) 2009, Jouni Malinen <j@w1.fi> 6 * Copyright (c) 2015 Intel Deutschland GmbH 7 * Copyright (C) 2019-2020, 2022-2026 Intel Corporation 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/etherdevice.h> 13 #include <linux/netdevice.h> 14 #include <linux/nl80211.h> 15 #include <linux/slab.h> 16 #include <linux/wireless.h> 17 #include <net/cfg80211.h> 18 #include <net/iw_handler.h> 19 #include "core.h" 20 #include "nl80211.h" 21 #include "rdev-ops.h" 22 23 24 void cfg80211_rx_assoc_resp(struct net_device *dev, 25 const struct cfg80211_rx_assoc_resp_data *data) 26 { 27 struct wireless_dev *wdev = dev->ieee80211_ptr; 28 struct wiphy *wiphy = wdev->wiphy; 29 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 30 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)data->buf; 31 struct cfg80211_connect_resp_params cr = { 32 .timeout_reason = NL80211_TIMEOUT_UNSPECIFIED, 33 .req_ie = data->req_ies, 34 .req_ie_len = data->req_ies_len, 35 .ap_mld_addr = data->ap_mld_addr, 36 .assoc_encrypted = data->assoc_encrypted, 37 }; 38 unsigned int link_id; 39 bool is_s1g = false; 40 41 for (link_id = 0; link_id < ARRAY_SIZE(data->links); link_id++) { 42 cr.links[link_id].status = data->links[link_id].status; 43 cr.links[link_id].bss = data->links[link_id].bss; 44 45 WARN_ON_ONCE(cr.links[link_id].status != WLAN_STATUS_SUCCESS && 46 (!cr.ap_mld_addr || !cr.links[link_id].bss)); 47 48 if (!cr.links[link_id].bss) 49 continue; 50 cr.links[link_id].bssid = data->links[link_id].bss->bssid; 51 cr.links[link_id].addr = data->links[link_id].addr; 52 /* need to have local link addresses for MLO connections */ 53 WARN_ON(cr.ap_mld_addr && 54 !is_valid_ether_addr(cr.links[link_id].addr)); 55 56 BUG_ON(!cr.links[link_id].bss->channel); 57 58 if (cr.links[link_id].bss->channel->band == NL80211_BAND_S1GHZ) { 59 WARN_ON(link_id); 60 is_s1g = true; 61 } 62 63 if (cr.ap_mld_addr) 64 cr.valid_links |= BIT(link_id); 65 } 66 67 if (is_s1g) { 68 if (data->len < offsetof(struct ieee80211_mgmt, 69 u.s1g_assoc_resp.variable)) 70 goto free_bss; 71 cr.resp_ie = (u8 *)&mgmt->u.s1g_assoc_resp.variable; 72 cr.resp_ie_len = data->len - 73 offsetof(struct ieee80211_mgmt, 74 u.s1g_assoc_resp.variable); 75 } else { 76 if (data->len < offsetof(struct ieee80211_mgmt, 77 u.assoc_resp.variable)) 78 goto free_bss; 79 cr.resp_ie = mgmt->u.assoc_resp.variable; 80 cr.resp_ie_len = data->len - 81 offsetof(struct ieee80211_mgmt, 82 u.assoc_resp.variable); 83 } 84 cr.status = le16_to_cpu(mgmt->u.assoc_resp.status_code); 85 86 trace_cfg80211_send_rx_assoc(dev, data); 87 88 /* 89 * This is a bit of a hack, we don't notify userspace of 90 * a (re-)association reply if we tried to send a reassoc 91 * and got a reject -- we only try again with an assoc 92 * frame instead of reassoc. 93 */ 94 if (cfg80211_sme_rx_assoc_resp(wdev, cr.status)) 95 goto free_bss; 96 97 nl80211_send_rx_assoc(rdev, dev, data); 98 /* update current_bss etc., consumes the bss reference */ 99 __cfg80211_connect_result(dev, &cr, cr.status == WLAN_STATUS_SUCCESS); 100 return; 101 102 free_bss: 103 for (link_id = 0; link_id < ARRAY_SIZE(data->links); link_id++) { 104 struct cfg80211_bss *bss = data->links[link_id].bss; 105 106 if (!bss) 107 continue; 108 109 cfg80211_unhold_bss(bss_from_pub(bss)); 110 cfg80211_put_bss(wiphy, bss); 111 } 112 } 113 EXPORT_SYMBOL(cfg80211_rx_assoc_resp); 114 115 static void cfg80211_process_auth(struct wireless_dev *wdev, 116 const u8 *buf, size_t len) 117 { 118 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 119 120 nl80211_send_rx_auth(rdev, wdev->netdev, buf, len, GFP_KERNEL); 121 cfg80211_sme_rx_auth(wdev, buf, len); 122 } 123 124 static void cfg80211_process_deauth(struct wireless_dev *wdev, 125 const u8 *buf, size_t len, 126 bool reconnect) 127 { 128 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 129 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf; 130 const u8 *bssid = mgmt->bssid; 131 u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code); 132 bool from_ap = !ether_addr_equal(mgmt->sa, wdev->netdev->dev_addr); 133 134 nl80211_send_deauth(rdev, wdev->netdev, buf, len, reconnect, GFP_KERNEL); 135 136 if (!wdev->connected || !ether_addr_equal(wdev->u.client.connected_addr, bssid)) 137 return; 138 139 __cfg80211_disconnected(wdev->netdev, NULL, 0, reason_code, from_ap); 140 cfg80211_sme_deauth(wdev); 141 } 142 143 static void cfg80211_process_disassoc(struct wireless_dev *wdev, 144 const u8 *buf, size_t len, 145 bool reconnect) 146 { 147 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 148 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf; 149 const u8 *bssid = mgmt->bssid; 150 u16 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code); 151 bool from_ap = !ether_addr_equal(mgmt->sa, wdev->netdev->dev_addr); 152 153 nl80211_send_disassoc(rdev, wdev->netdev, buf, len, reconnect, 154 GFP_KERNEL); 155 156 if (WARN_ON(!wdev->connected || 157 !ether_addr_equal(wdev->u.client.connected_addr, bssid))) 158 return; 159 160 __cfg80211_disconnected(wdev->netdev, NULL, 0, reason_code, from_ap); 161 cfg80211_sme_disassoc(wdev); 162 } 163 164 void cfg80211_rx_mlme_mgmt(struct net_device *dev, const u8 *buf, size_t len) 165 { 166 struct wireless_dev *wdev = dev->ieee80211_ptr; 167 struct ieee80211_mgmt *mgmt = (void *)buf; 168 __le16 fc; 169 170 lockdep_assert_wiphy(wdev->wiphy); 171 172 if (len < sizeof(fc)) 173 return; 174 175 fc = mgmt->frame_control; 176 177 if (ieee80211_is_auth(fc)) { 178 if (len < offsetofend(struct ieee80211_mgmt, u.auth.status_code)) 179 return; 180 } else if (ieee80211_is_deauth(fc)) { 181 if (len < offsetofend(struct ieee80211_mgmt, u.deauth.reason_code)) 182 return; 183 } else if (ieee80211_is_disassoc(fc)) { 184 if (len < offsetofend(struct ieee80211_mgmt, u.disassoc.reason_code)) 185 return; 186 } else { 187 return; 188 } 189 190 trace_cfg80211_rx_mlme_mgmt(dev, buf, len); 191 192 if (ieee80211_is_auth(fc)) 193 cfg80211_process_auth(wdev, buf, len); 194 else if (ieee80211_is_deauth(fc)) 195 cfg80211_process_deauth(wdev, buf, len, false); 196 else 197 cfg80211_process_disassoc(wdev, buf, len, false); 198 } 199 EXPORT_SYMBOL(cfg80211_rx_mlme_mgmt); 200 201 void cfg80211_auth_timeout(struct net_device *dev, const u8 *addr) 202 { 203 struct wireless_dev *wdev = dev->ieee80211_ptr; 204 struct wiphy *wiphy = wdev->wiphy; 205 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 206 207 trace_cfg80211_send_auth_timeout(dev, addr); 208 209 nl80211_send_auth_timeout(rdev, dev, addr, GFP_KERNEL); 210 cfg80211_sme_auth_timeout(wdev); 211 } 212 EXPORT_SYMBOL(cfg80211_auth_timeout); 213 214 void cfg80211_assoc_failure(struct net_device *dev, 215 struct cfg80211_assoc_failure *data) 216 { 217 struct wireless_dev *wdev = dev->ieee80211_ptr; 218 struct wiphy *wiphy = wdev->wiphy; 219 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 220 const u8 *addr = data->ap_mld_addr ?: data->bss[0]->bssid; 221 int i; 222 223 trace_cfg80211_send_assoc_failure(dev, data); 224 225 if (data->timeout) { 226 nl80211_send_assoc_timeout(rdev, dev, addr, GFP_KERNEL); 227 cfg80211_sme_assoc_timeout(wdev); 228 } else { 229 cfg80211_sme_abandon_assoc(wdev); 230 } 231 232 for (i = 0; i < ARRAY_SIZE(data->bss); i++) { 233 struct cfg80211_bss *bss = data->bss[i]; 234 235 if (!bss) 236 continue; 237 238 cfg80211_unhold_bss(bss_from_pub(bss)); 239 cfg80211_put_bss(wiphy, bss); 240 } 241 } 242 EXPORT_SYMBOL(cfg80211_assoc_failure); 243 244 void cfg80211_tx_mlme_mgmt(struct net_device *dev, const u8 *buf, size_t len, 245 bool reconnect) 246 { 247 struct wireless_dev *wdev = dev->ieee80211_ptr; 248 struct ieee80211_mgmt *mgmt = (void *)buf; 249 __le16 fc; 250 251 lockdep_assert_wiphy(wdev->wiphy); 252 253 if (len < sizeof(fc)) 254 return; 255 256 fc = mgmt->frame_control; 257 258 if (ieee80211_is_deauth(fc)) { 259 if (len < offsetofend(struct ieee80211_mgmt, u.deauth.reason_code)) 260 return; 261 } else if (ieee80211_is_disassoc(fc)) { 262 if (len < offsetofend(struct ieee80211_mgmt, u.disassoc.reason_code)) 263 return; 264 } else { 265 return; 266 } 267 268 trace_cfg80211_tx_mlme_mgmt(dev, buf, len, reconnect); 269 270 if (ieee80211_is_deauth(fc)) 271 cfg80211_process_deauth(wdev, buf, len, reconnect); 272 else 273 cfg80211_process_disassoc(wdev, buf, len, reconnect); 274 } 275 EXPORT_SYMBOL(cfg80211_tx_mlme_mgmt); 276 277 void cfg80211_michael_mic_failure(struct net_device *dev, const u8 *addr, 278 enum nl80211_key_type key_type, int key_id, 279 const u8 *tsc, gfp_t gfp) 280 { 281 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy; 282 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 283 #ifdef CONFIG_CFG80211_WEXT 284 union iwreq_data wrqu; 285 char *buf = kmalloc(128, gfp); 286 287 if (buf) { 288 memset(&wrqu, 0, sizeof(wrqu)); 289 wrqu.data.length = 290 sprintf(buf, "MLME-MICHAELMICFAILURE." 291 "indication(keyid=%d %scast addr=%pM)", 292 key_id, key_type == NL80211_KEYTYPE_GROUP 293 ? "broad" : "uni", addr); 294 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf); 295 kfree(buf); 296 } 297 #endif 298 299 trace_cfg80211_michael_mic_failure(dev, addr, key_type, key_id, tsc); 300 nl80211_michael_mic_failure(rdev, dev, addr, key_type, key_id, tsc, gfp); 301 } 302 EXPORT_SYMBOL(cfg80211_michael_mic_failure); 303 304 /* some MLME handling for userspace SME */ 305 int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev, 306 struct net_device *dev, 307 struct cfg80211_auth_request *req) 308 { 309 struct wireless_dev *wdev = dev->ieee80211_ptr; 310 311 lockdep_assert_wiphy(wdev->wiphy); 312 313 if (!req->bss) 314 return -ENOENT; 315 316 if (req->link_id >= 0 && 317 !(wdev->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO)) 318 return -EINVAL; 319 320 if (req->auth_type == NL80211_AUTHTYPE_SHARED_KEY) { 321 if (!req->key || !req->key_len || 322 req->key_idx < 0 || req->key_idx > 3) 323 return -EINVAL; 324 } 325 326 if (wdev->connected && 327 ether_addr_equal(req->bss->bssid, wdev->u.client.connected_addr)) 328 return -EALREADY; 329 330 if (ether_addr_equal(req->bss->bssid, dev->dev_addr) || 331 (req->link_id >= 0 && 332 ether_addr_equal(req->ap_mld_addr, dev->dev_addr))) 333 return -EINVAL; 334 335 return rdev_auth(rdev, dev, req); 336 } 337 338 /* Do a logical ht_capa &= ht_capa_mask. */ 339 void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa, 340 const struct ieee80211_ht_cap *ht_capa_mask) 341 { 342 int i; 343 u8 *p1, *p2; 344 if (!ht_capa_mask) { 345 memset(ht_capa, 0, sizeof(*ht_capa)); 346 return; 347 } 348 349 p1 = (u8*)(ht_capa); 350 p2 = (u8*)(ht_capa_mask); 351 for (i = 0; i < sizeof(*ht_capa); i++) 352 p1[i] &= p2[i]; 353 } 354 355 /* Do a logical vht_capa &= vht_capa_mask. */ 356 void cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap *vht_capa, 357 const struct ieee80211_vht_cap *vht_capa_mask) 358 { 359 int i; 360 u8 *p1, *p2; 361 if (!vht_capa_mask) { 362 memset(vht_capa, 0, sizeof(*vht_capa)); 363 return; 364 } 365 366 p1 = (u8*)(vht_capa); 367 p2 = (u8*)(vht_capa_mask); 368 for (i = 0; i < sizeof(*vht_capa); i++) 369 p1[i] &= p2[i]; 370 } 371 372 static int 373 cfg80211_mlme_check_mlo_compat(const struct ieee80211_multi_link_elem *mle_a, 374 const struct ieee80211_multi_link_elem *mle_b, 375 struct netlink_ext_ack *extack) 376 { 377 const struct ieee80211_mle_basic_common_info *common_a, *common_b; 378 379 common_a = (const void *)mle_a->variable; 380 common_b = (const void *)mle_b->variable; 381 382 if (memcmp(common_a->mld_mac_addr, common_b->mld_mac_addr, ETH_ALEN)) { 383 NL_SET_ERR_MSG(extack, "AP MLD address mismatch"); 384 return -EINVAL; 385 } 386 387 if (ieee80211_mle_get_eml_cap((const u8 *)mle_a) != 388 ieee80211_mle_get_eml_cap((const u8 *)mle_b)) { 389 NL_SET_ERR_MSG(extack, "link EML capabilities mismatch"); 390 return -EINVAL; 391 } 392 393 if (ieee80211_mle_get_mld_capa_op((const u8 *)mle_a) != 394 ieee80211_mle_get_mld_capa_op((const u8 *)mle_b)) { 395 NL_SET_ERR_MSG(extack, "link MLD capabilities/ops mismatch"); 396 return -EINVAL; 397 } 398 399 /* 400 * Only verify the values in Extended MLD Capabilities that are 401 * not reserved when transmitted by an AP (and expected to remain the 402 * same over time). 403 * The Recommended Max Simultaneous Links subfield in particular is 404 * reserved when included in a unicast Probe Response frame and may 405 * also change when the AP adds/removes links. The BTM MLD 406 * Recommendation For Multiple APs Support subfield is reserved when 407 * transmitted by an AP. 408 */ 409 if ((ieee80211_mle_get_ext_mld_capa_op((const u8 *)mle_a) & 410 (IEEE80211_EHT_ML_EXT_MLD_CAPA_OP_PARAM_UPDATE | 411 IEEE80211_EHT_ML_EXT_MLD_CAPA_NSTR_UPDATE | 412 IEEE80211_EHT_ML_EXT_MLD_CAPA_EMLSR_ENA_ON_ONE_LINK | 413 IEEE80211_UHR_ML_EXT_MLD_CAPA_ML_PM)) != 414 (ieee80211_mle_get_ext_mld_capa_op((const u8 *)mle_b) & 415 (IEEE80211_EHT_ML_EXT_MLD_CAPA_OP_PARAM_UPDATE | 416 IEEE80211_EHT_ML_EXT_MLD_CAPA_NSTR_UPDATE | 417 IEEE80211_EHT_ML_EXT_MLD_CAPA_EMLSR_ENA_ON_ONE_LINK | 418 IEEE80211_UHR_ML_EXT_MLD_CAPA_ML_PM))) { 419 NL_SET_ERR_MSG(extack, 420 "extended link MLD capabilities/ops mismatch"); 421 return -EINVAL; 422 } 423 424 return 0; 425 } 426 427 static int cfg80211_mlme_check_mlo(struct net_device *dev, 428 struct cfg80211_assoc_request *req, 429 struct netlink_ext_ack *extack) 430 { 431 const struct ieee80211_multi_link_elem *mles[ARRAY_SIZE(req->links)] = {}; 432 int i; 433 434 if (req->link_id < 0) 435 return 0; 436 437 if (!req->links[req->link_id].bss) { 438 NL_SET_ERR_MSG(extack, "no BSS for assoc link"); 439 return -EINVAL; 440 } 441 442 rcu_read_lock(); 443 for (i = 0; i < ARRAY_SIZE(req->links); i++) { 444 const struct cfg80211_bss_ies *ies; 445 const struct element *ml; 446 447 if (!req->links[i].bss) 448 continue; 449 450 if (ether_addr_equal(req->links[i].bss->bssid, dev->dev_addr)) { 451 NL_SET_ERR_MSG(extack, "BSSID must not be our address"); 452 req->links[i].error = -EINVAL; 453 goto error; 454 } 455 456 ies = rcu_dereference(req->links[i].bss->ies); 457 ml = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_MULTI_LINK, 458 ies->data, ies->len); 459 if (!ml) { 460 NL_SET_ERR_MSG(extack, "MLO BSS w/o ML element"); 461 req->links[i].error = -EINVAL; 462 goto error; 463 } 464 465 if (!ieee80211_mle_type_ok(ml->data + 1, 466 IEEE80211_ML_CONTROL_TYPE_BASIC, 467 ml->datalen - 1)) { 468 NL_SET_ERR_MSG(extack, "BSS with invalid ML element"); 469 req->links[i].error = -EINVAL; 470 goto error; 471 } 472 473 mles[i] = (const void *)(ml->data + 1); 474 475 if (ieee80211_mle_get_link_id((const u8 *)mles[i]) != i) { 476 NL_SET_ERR_MSG(extack, "link ID mismatch"); 477 req->links[i].error = -EINVAL; 478 goto error; 479 } 480 } 481 482 if (WARN_ON(!mles[req->link_id])) 483 goto error; 484 485 for (i = 0; i < ARRAY_SIZE(req->links); i++) { 486 if (i == req->link_id || !req->links[i].bss) 487 continue; 488 489 if (WARN_ON(!mles[i])) 490 goto error; 491 492 if (cfg80211_mlme_check_mlo_compat(mles[req->link_id], mles[i], 493 extack)) { 494 req->links[i].error = -EINVAL; 495 goto error; 496 } 497 } 498 499 rcu_read_unlock(); 500 return 0; 501 error: 502 rcu_read_unlock(); 503 return -EINVAL; 504 } 505 506 /* Note: caller must cfg80211_put_bss() regardless of result */ 507 int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev, 508 struct net_device *dev, 509 struct cfg80211_assoc_request *req, 510 struct netlink_ext_ack *extack) 511 { 512 struct wireless_dev *wdev = dev->ieee80211_ptr; 513 int err; 514 515 lockdep_assert_wiphy(wdev->wiphy); 516 517 err = cfg80211_mlme_check_mlo(dev, req, extack); 518 if (err) 519 return err; 520 521 if (wdev->connected && 522 (!req->prev_bssid || 523 !ether_addr_equal(wdev->u.client.connected_addr, req->prev_bssid))) 524 return -EALREADY; 525 526 if ((req->bss && ether_addr_equal(req->bss->bssid, dev->dev_addr)) || 527 (req->link_id >= 0 && 528 ether_addr_equal(req->ap_mld_addr, dev->dev_addr))) 529 return -EINVAL; 530 531 cfg80211_oper_and_ht_capa(&req->ht_capa_mask, 532 rdev->wiphy.ht_capa_mod_mask); 533 cfg80211_oper_and_vht_capa(&req->vht_capa_mask, 534 rdev->wiphy.vht_capa_mod_mask); 535 536 err = rdev_assoc(rdev, dev, req); 537 if (!err) { 538 int link_id; 539 540 if (req->bss) { 541 cfg80211_ref_bss(&rdev->wiphy, req->bss); 542 cfg80211_hold_bss(bss_from_pub(req->bss)); 543 } 544 545 for (link_id = 0; link_id < ARRAY_SIZE(req->links); link_id++) { 546 if (!req->links[link_id].bss) 547 continue; 548 cfg80211_ref_bss(&rdev->wiphy, req->links[link_id].bss); 549 cfg80211_hold_bss(bss_from_pub(req->links[link_id].bss)); 550 } 551 } 552 return err; 553 } 554 555 int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev, 556 struct net_device *dev, const u8 *bssid, 557 const u8 *ie, int ie_len, u16 reason, 558 bool local_state_change) 559 { 560 struct wireless_dev *wdev = dev->ieee80211_ptr; 561 struct cfg80211_deauth_request req = { 562 .bssid = bssid, 563 .reason_code = reason, 564 .ie = ie, 565 .ie_len = ie_len, 566 .local_state_change = local_state_change, 567 }; 568 569 lockdep_assert_wiphy(wdev->wiphy); 570 571 if (local_state_change && 572 (!wdev->connected || 573 !ether_addr_equal(wdev->u.client.connected_addr, bssid))) 574 return 0; 575 576 if (ether_addr_equal(wdev->disconnect_bssid, bssid) || 577 (wdev->connected && 578 ether_addr_equal(wdev->u.client.connected_addr, bssid))) 579 wdev->conn_owner_nlportid = 0; 580 581 return rdev_deauth(rdev, dev, &req); 582 } 583 584 int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev, 585 struct net_device *dev, const u8 *ap_addr, 586 const u8 *ie, int ie_len, u16 reason, 587 bool local_state_change) 588 { 589 struct wireless_dev *wdev = dev->ieee80211_ptr; 590 struct cfg80211_disassoc_request req = { 591 .reason_code = reason, 592 .local_state_change = local_state_change, 593 .ie = ie, 594 .ie_len = ie_len, 595 .ap_addr = ap_addr, 596 }; 597 int err; 598 599 lockdep_assert_wiphy(wdev->wiphy); 600 601 if (!wdev->connected) 602 return -ENOTCONN; 603 604 if (memcmp(wdev->u.client.connected_addr, ap_addr, ETH_ALEN)) 605 return -ENOTCONN; 606 607 err = rdev_disassoc(rdev, dev, &req); 608 if (err) 609 return err; 610 611 /* driver should have reported the disassoc */ 612 WARN_ON(wdev->connected); 613 return 0; 614 } 615 616 void cfg80211_mlme_down(struct cfg80211_registered_device *rdev, 617 struct net_device *dev) 618 { 619 struct wireless_dev *wdev = dev->ieee80211_ptr; 620 u8 bssid[ETH_ALEN]; 621 622 lockdep_assert_wiphy(wdev->wiphy); 623 624 if (!rdev->ops->deauth) 625 return; 626 627 if (!wdev->connected) 628 return; 629 630 memcpy(bssid, wdev->u.client.connected_addr, ETH_ALEN); 631 cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0, 632 WLAN_REASON_DEAUTH_LEAVING, false); 633 } 634 635 struct cfg80211_mgmt_registration { 636 struct list_head list; 637 struct wireless_dev *wdev; 638 639 u32 nlportid; 640 641 int match_len; 642 643 __le16 frame_type; 644 645 bool multicast_rx; 646 647 u8 match[]; 648 }; 649 650 static void cfg80211_mgmt_registrations_update(struct wireless_dev *wdev) 651 { 652 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 653 struct wireless_dev *tmp; 654 struct cfg80211_mgmt_registration *reg; 655 struct mgmt_frame_regs upd = {}; 656 657 lockdep_assert_held(&rdev->wiphy.mtx); 658 659 spin_lock_bh(&rdev->mgmt_registrations_lock); 660 if (!wdev->mgmt_registrations_need_update) { 661 spin_unlock_bh(&rdev->mgmt_registrations_lock); 662 return; 663 } 664 665 rcu_read_lock(); 666 list_for_each_entry_rcu(tmp, &rdev->wiphy.wdev_list, list) { 667 list_for_each_entry(reg, &tmp->mgmt_registrations, list) { 668 u32 mask = BIT(le16_to_cpu(reg->frame_type) >> 4); 669 u32 mcast_mask = 0; 670 671 if (reg->multicast_rx) 672 mcast_mask = mask; 673 674 upd.global_stypes |= mask; 675 upd.global_mcast_stypes |= mcast_mask; 676 677 if (tmp == wdev) { 678 upd.interface_stypes |= mask; 679 upd.interface_mcast_stypes |= mcast_mask; 680 } 681 } 682 } 683 rcu_read_unlock(); 684 685 wdev->mgmt_registrations_need_update = 0; 686 spin_unlock_bh(&rdev->mgmt_registrations_lock); 687 688 rdev_update_mgmt_frame_registrations(rdev, wdev, &upd); 689 } 690 691 void cfg80211_mgmt_registrations_update_wk(struct work_struct *wk) 692 { 693 struct cfg80211_registered_device *rdev; 694 struct wireless_dev *wdev; 695 696 rdev = container_of(wk, struct cfg80211_registered_device, 697 mgmt_registrations_update_wk); 698 699 guard(wiphy)(&rdev->wiphy); 700 701 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) 702 cfg80211_mgmt_registrations_update(wdev); 703 } 704 705 int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_portid, 706 u16 frame_type, const u8 *match_data, 707 int match_len, bool multicast_rx, 708 struct netlink_ext_ack *extack) 709 { 710 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 711 struct cfg80211_mgmt_registration *reg, *nreg; 712 int err = 0; 713 u16 mgmt_type; 714 bool update_multicast = false; 715 716 if (!wdev->wiphy->mgmt_stypes) 717 return -EOPNOTSUPP; 718 719 if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT) { 720 NL_SET_ERR_MSG(extack, "frame type not management"); 721 return -EINVAL; 722 } 723 724 if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) { 725 NL_SET_ERR_MSG(extack, "Invalid frame type"); 726 return -EINVAL; 727 } 728 729 mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4; 730 if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type))) { 731 NL_SET_ERR_MSG(extack, 732 "Registration to specific type not supported"); 733 return -EINVAL; 734 } 735 736 /* 737 * To support Pre Association Security Negotiation (PASN), registration 738 * for authentication frames should be supported. However, as some 739 * versions of the user space daemons wrongly register to all types of 740 * authentication frames (which might result in unexpected behavior) 741 * allow such registration if the request is for a specific 742 * authentication algorithm number. 743 */ 744 if (wdev->iftype == NL80211_IFTYPE_STATION && 745 (frame_type & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_AUTH && 746 !(match_data && match_len >= 2)) { 747 NL_SET_ERR_MSG(extack, 748 "Authentication algorithm number required"); 749 return -EINVAL; 750 } 751 752 nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL); 753 if (!nreg) 754 return -ENOMEM; 755 756 spin_lock_bh(&rdev->mgmt_registrations_lock); 757 758 list_for_each_entry(reg, &wdev->mgmt_registrations, list) { 759 int mlen = min(match_len, reg->match_len); 760 761 if (frame_type != le16_to_cpu(reg->frame_type)) 762 continue; 763 764 if (memcmp(reg->match, match_data, mlen) == 0) { 765 if (reg->multicast_rx != multicast_rx) { 766 update_multicast = true; 767 reg->multicast_rx = multicast_rx; 768 break; 769 } 770 NL_SET_ERR_MSG(extack, "Match already configured"); 771 err = -EALREADY; 772 break; 773 } 774 } 775 776 if (err) 777 goto out; 778 779 if (update_multicast) { 780 kfree(nreg); 781 } else { 782 memcpy(nreg->match, match_data, match_len); 783 nreg->match_len = match_len; 784 nreg->nlportid = snd_portid; 785 nreg->frame_type = cpu_to_le16(frame_type); 786 nreg->wdev = wdev; 787 nreg->multicast_rx = multicast_rx; 788 list_add(&nreg->list, &wdev->mgmt_registrations); 789 } 790 wdev->mgmt_registrations_need_update = 1; 791 spin_unlock_bh(&rdev->mgmt_registrations_lock); 792 793 cfg80211_mgmt_registrations_update(wdev); 794 795 return 0; 796 797 out: 798 kfree(nreg); 799 spin_unlock_bh(&rdev->mgmt_registrations_lock); 800 801 return err; 802 } 803 804 void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlportid) 805 { 806 struct wiphy *wiphy = wdev->wiphy; 807 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 808 struct cfg80211_mgmt_registration *reg, *tmp; 809 810 spin_lock_bh(&rdev->mgmt_registrations_lock); 811 812 list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) { 813 if (reg->nlportid != nlportid) 814 continue; 815 816 list_del(®->list); 817 kfree(reg); 818 819 wdev->mgmt_registrations_need_update = 1; 820 schedule_work(&rdev->mgmt_registrations_update_wk); 821 } 822 823 spin_unlock_bh(&rdev->mgmt_registrations_lock); 824 825 if (nlportid && rdev->crit_proto_nlportid == nlportid) { 826 rdev->crit_proto_nlportid = 0; 827 rdev_crit_proto_stop(rdev, wdev); 828 } 829 830 if (nlportid == wdev->unexpected_nlportid) 831 wdev->unexpected_nlportid = 0; 832 } 833 834 void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev) 835 { 836 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 837 struct cfg80211_mgmt_registration *reg, *tmp; 838 839 spin_lock_bh(&rdev->mgmt_registrations_lock); 840 list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) { 841 list_del(®->list); 842 kfree(reg); 843 } 844 wdev->mgmt_registrations_need_update = 1; 845 spin_unlock_bh(&rdev->mgmt_registrations_lock); 846 847 cfg80211_mgmt_registrations_update(wdev); 848 } 849 850 static bool cfg80211_allowed_address(struct wireless_dev *wdev, const u8 *addr) 851 { 852 int i; 853 854 for_each_valid_link(wdev, i) { 855 if (ether_addr_equal(addr, wdev->links[i].addr)) 856 return true; 857 } 858 859 return ether_addr_equal(addr, wdev_address(wdev)); 860 } 861 862 static bool cfg80211_allowed_random_address(struct wireless_dev *wdev, 863 const struct ieee80211_mgmt *mgmt) 864 { 865 if (ieee80211_is_auth(mgmt->frame_control) || 866 ieee80211_is_deauth(mgmt->frame_control)) { 867 /* Allow random TA to be used with authentication and 868 * deauthentication frames if the driver has indicated support. 869 */ 870 if (wiphy_ext_feature_isset( 871 wdev->wiphy, 872 NL80211_EXT_FEATURE_AUTH_AND_DEAUTH_RANDOM_TA)) 873 return true; 874 } else if (ieee80211_is_action(mgmt->frame_control) && 875 mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) { 876 /* Allow random TA to be used with Public Action frames if the 877 * driver has indicated support. 878 */ 879 if (!wdev->connected && 880 wiphy_ext_feature_isset( 881 wdev->wiphy, 882 NL80211_EXT_FEATURE_MGMT_TX_RANDOM_TA)) 883 return true; 884 885 if (wdev->connected && 886 wiphy_ext_feature_isset( 887 wdev->wiphy, 888 NL80211_EXT_FEATURE_MGMT_TX_RANDOM_TA_CONNECTED)) 889 return true; 890 } 891 892 return false; 893 } 894 895 static int cfg80211_mgmt_tx_check(struct cfg80211_registered_device *rdev, 896 struct wireless_dev *wdev, 897 enum nl80211_iftype iftype, 898 struct cfg80211_mgmt_tx_params *params) 899 { 900 const struct ieee80211_mgmt *mgmt; 901 u16 stype; 902 903 if (!wdev->wiphy->mgmt_stypes) 904 return -EOPNOTSUPP; 905 906 if (!rdev->ops->mgmt_tx) 907 return -EOPNOTSUPP; 908 909 if (params->len < 24 + 1) 910 return -EINVAL; 911 912 mgmt = (const struct ieee80211_mgmt *)params->buf; 913 914 if (!ieee80211_is_mgmt(mgmt->frame_control) || 915 ieee80211_has_order(mgmt->frame_control)) 916 return -EINVAL; 917 918 stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE; 919 if (!(wdev->wiphy->mgmt_stypes[iftype].tx & BIT(stype >> 4))) 920 return -EINVAL; 921 922 if (ieee80211_is_action(mgmt->frame_control) && 923 mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) { 924 int err = 0; 925 926 switch (iftype) { 927 case NL80211_IFTYPE_ADHOC: 928 /* 929 * check for IBSS DA must be done by driver as 930 * cfg80211 doesn't track the stations 931 */ 932 if (!wdev->u.ibss.current_bss || 933 !ether_addr_equal(wdev->u.ibss.current_bss->pub.bssid, 934 mgmt->bssid)) { 935 err = -ENOTCONN; 936 break; 937 } 938 break; 939 case NL80211_IFTYPE_STATION: 940 case NL80211_IFTYPE_P2P_CLIENT: 941 if (!wdev->connected) { 942 err = -ENOTCONN; 943 break; 944 } 945 946 /* FIXME: MLD may address this differently */ 947 948 if (!ether_addr_equal(wdev->u.client.connected_addr, 949 mgmt->bssid)) { 950 err = -ENOTCONN; 951 break; 952 } 953 954 /* for station, check that DA is the AP */ 955 if (!ether_addr_equal(wdev->u.client.connected_addr, 956 mgmt->da)) { 957 err = -ENOTCONN; 958 break; 959 } 960 break; 961 case NL80211_IFTYPE_AP: 962 case NL80211_IFTYPE_P2P_GO: 963 case NL80211_IFTYPE_AP_VLAN: 964 if (!ether_addr_equal(mgmt->bssid, wdev_address(wdev)) && 965 (params->link_id < 0 || 966 !ether_addr_equal(mgmt->bssid, 967 wdev->links[params->link_id].addr))) 968 err = -EINVAL; 969 break; 970 case NL80211_IFTYPE_MESH_POINT: 971 if (!ether_addr_equal(mgmt->sa, mgmt->bssid)) { 972 err = -EINVAL; 973 break; 974 } 975 /* 976 * check for mesh DA must be done by driver as 977 * cfg80211 doesn't track the stations 978 */ 979 break; 980 case NL80211_IFTYPE_NAN: 981 case NL80211_IFTYPE_NAN_DATA: 982 if (mgmt->u.action.category != 983 WLAN_CATEGORY_PROTECTED_DUAL_OF_ACTION) 984 err = -EOPNOTSUPP; 985 break; 986 case NL80211_IFTYPE_P2P_DEVICE: 987 /* 988 * fall through, P2P device only supports 989 * public action frames 990 */ 991 case NL80211_IFTYPE_PD: 992 default: 993 err = -EOPNOTSUPP; 994 break; 995 } 996 997 if (err) 998 return err; 999 } 1000 1001 if (!cfg80211_allowed_address(wdev, mgmt->sa) && 1002 !cfg80211_allowed_random_address(wdev, mgmt)) 1003 return -EINVAL; 1004 1005 return 0; 1006 } 1007 1008 int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev, 1009 struct wireless_dev *wdev, 1010 struct cfg80211_mgmt_tx_params *params, u64 cookie) 1011 { 1012 int err; 1013 1014 lockdep_assert_wiphy(&rdev->wiphy); 1015 1016 err = cfg80211_mgmt_tx_check(rdev, wdev, wdev->iftype, params); 1017 if (err) 1018 return err; 1019 1020 /* Transmit the management frame as requested by user space */ 1021 return rdev_mgmt_tx(rdev, wdev, params, cookie); 1022 } 1023 1024 int cfg80211_mlme_mgmt_tx_unlocked(struct cfg80211_registered_device *rdev, 1025 struct wireless_dev *wdev, 1026 struct cfg80211_mgmt_tx_params *params, 1027 u64 cookie) 1028 { 1029 /* can change concurrently, so read only once */ 1030 enum nl80211_iftype iftype = READ_ONCE(wdev->iftype); 1031 int err; 1032 1033 if (!rdev->ops->mgmt_tx_unlocked) 1034 return -EAGAIN; 1035 1036 switch (iftype) { 1037 case NL80211_IFTYPE_AP: 1038 case NL80211_IFTYPE_P2P_GO: 1039 case NL80211_IFTYPE_NAN: 1040 case NL80211_IFTYPE_NAN_DATA: 1041 break; 1042 default: 1043 /* the checks for other interface types need the wiphy mutex */ 1044 return -EAGAIN; 1045 } 1046 1047 err = cfg80211_mgmt_tx_check(rdev, wdev, iftype, params); 1048 if (err) 1049 return err; 1050 1051 return rdev_mgmt_tx_unlocked(rdev, wdev, params, cookie); 1052 } 1053 1054 bool cfg80211_rx_mgmt_ext(struct wireless_dev *wdev, 1055 struct cfg80211_rx_info *info) 1056 { 1057 struct wiphy *wiphy = wdev->wiphy; 1058 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1059 struct cfg80211_mgmt_registration *reg; 1060 const struct ieee80211_txrx_stypes *stypes = 1061 &wiphy->mgmt_stypes[wdev->iftype]; 1062 struct ieee80211_mgmt *mgmt = (void *)info->buf; 1063 const u8 *data; 1064 int data_len; 1065 bool result = false; 1066 __le16 ftype = mgmt->frame_control & 1067 cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE); 1068 u16 stype; 1069 1070 trace_cfg80211_rx_mgmt(wdev, info); 1071 stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4; 1072 1073 if (!(stypes->rx & BIT(stype))) { 1074 trace_cfg80211_return_bool(false); 1075 return false; 1076 } 1077 1078 data = info->buf + ieee80211_hdrlen(mgmt->frame_control); 1079 data_len = info->len - ieee80211_hdrlen(mgmt->frame_control); 1080 1081 spin_lock_bh(&rdev->mgmt_registrations_lock); 1082 1083 list_for_each_entry(reg, &wdev->mgmt_registrations, list) { 1084 if (reg->frame_type != ftype) 1085 continue; 1086 1087 if (reg->match_len > data_len) 1088 continue; 1089 1090 if (memcmp(reg->match, data, reg->match_len)) 1091 continue; 1092 1093 /* found match! */ 1094 1095 /* Indicate the received Action frame to user space */ 1096 if (nl80211_send_mgmt(rdev, wdev, reg->nlportid, info, 1097 GFP_ATOMIC)) 1098 continue; 1099 1100 result = true; 1101 break; 1102 } 1103 1104 spin_unlock_bh(&rdev->mgmt_registrations_lock); 1105 1106 trace_cfg80211_return_bool(result); 1107 return result; 1108 } 1109 EXPORT_SYMBOL(cfg80211_rx_mgmt_ext); 1110 1111 void cfg80211_sched_dfs_chan_update(struct cfg80211_registered_device *rdev) 1112 { 1113 cancel_delayed_work(&rdev->dfs_update_channels_wk); 1114 queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk, 0); 1115 } 1116 1117 struct cfg80211_dfs_chan_expiry { 1118 struct ieee80211_channel *chan; 1119 unsigned long entered; 1120 enum nl80211_dfs_state state; 1121 enum nl80211_radar_event event; 1122 }; 1123 1124 static bool cfg80211_dfs_chan_expire(struct cfg80211_registered_device *rdev, 1125 struct cfg80211_dfs_chan_expiry *exp, 1126 struct cfg80211_chan_def *chandef) 1127 { 1128 struct ieee80211_channel *c = exp->chan; 1129 1130 guard(wiphy)(&rdev->wiphy); 1131 1132 /* the state may have changed while the wiphy wasn't locked */ 1133 if (c->dfs_state != exp->state || c->dfs_state_entered != exp->entered) 1134 return false; 1135 1136 c->dfs_state = NL80211_DFS_USABLE; 1137 c->dfs_state_entered = jiffies; 1138 1139 cfg80211_chandef_create(chandef, c, NL80211_CHAN_NO_HT); 1140 1141 nl80211_radar_notify(rdev, chandef, exp->event, NULL, GFP_KERNEL); 1142 1143 return true; 1144 } 1145 1146 void cfg80211_dfs_channels_update_work(struct work_struct *work) 1147 { 1148 struct delayed_work *delayed_work = to_delayed_work(work); 1149 struct cfg80211_dfs_chan_expiry *expired; 1150 struct cfg80211_registered_device *rdev; 1151 struct cfg80211_chan_def chandef; 1152 struct ieee80211_supported_band *sband; 1153 struct ieee80211_channel *c; 1154 struct wiphy *wiphy; 1155 bool check_again = false, pre_cac_allowed; 1156 unsigned long timeout, next_time = 0; 1157 unsigned long time_dfs_update; 1158 enum nl80211_radar_event radar_event; 1159 unsigned int n_chans = 0, n_expired = 0; 1160 int bandid, i; 1161 1162 rdev = container_of(delayed_work, struct cfg80211_registered_device, 1163 dfs_update_channels_wk); 1164 wiphy = &rdev->wiphy; 1165 1166 mutex_lock(&cfg80211_mutex); 1167 wiphy_lock(wiphy); 1168 for (bandid = 0; bandid < NUM_NL80211_BANDS; bandid++) { 1169 if (wiphy->bands[bandid]) 1170 n_chans += wiphy->bands[bandid]->n_channels; 1171 } 1172 1173 expired = kvcalloc(n_chans, sizeof(*expired), GFP_KERNEL); 1174 if (!expired) { 1175 /* hmm - retry later */ 1176 queue_delayed_work(cfg80211_wq, 1177 &rdev->dfs_update_channels_wk, 1178 HZ); 1179 wiphy_unlock(wiphy); 1180 mutex_unlock(&cfg80211_mutex); 1181 return; 1182 } 1183 1184 pre_cac_allowed = regulatory_pre_cac_allowed(wiphy); 1185 1186 for (bandid = 0; bandid < NUM_NL80211_BANDS; bandid++) { 1187 sband = wiphy->bands[bandid]; 1188 if (!sband) 1189 continue; 1190 1191 for (i = 0; i < sband->n_channels; i++) { 1192 c = &sband->channels[i]; 1193 1194 if (!(c->flags & IEEE80211_CHAN_RADAR)) 1195 continue; 1196 1197 if (c->dfs_state != NL80211_DFS_UNAVAILABLE && 1198 c->dfs_state != NL80211_DFS_AVAILABLE) 1199 continue; 1200 1201 if (c->dfs_state == NL80211_DFS_UNAVAILABLE) { 1202 time_dfs_update = IEEE80211_DFS_MIN_NOP_TIME_MS; 1203 radar_event = NL80211_RADAR_NOP_FINISHED; 1204 } else { 1205 if (pre_cac_allowed) 1206 continue; 1207 1208 time_dfs_update = REG_PRE_CAC_EXPIRY_GRACE_MS; 1209 radar_event = NL80211_RADAR_PRE_CAC_EXPIRED; 1210 } 1211 1212 timeout = c->dfs_state_entered + 1213 msecs_to_jiffies(time_dfs_update); 1214 1215 if (time_after_eq(jiffies, timeout)) { 1216 expired[n_expired].chan = c; 1217 expired[n_expired].state = c->dfs_state; 1218 expired[n_expired].entered = c->dfs_state_entered; 1219 expired[n_expired].event = radar_event; 1220 n_expired++; 1221 continue; 1222 } 1223 1224 if (!check_again) 1225 next_time = timeout - jiffies; 1226 else 1227 next_time = min(next_time, timeout - jiffies); 1228 check_again = true; 1229 } 1230 } 1231 wiphy_unlock(wiphy); 1232 1233 /* these lock the wiphys, so must be done without holding ours */ 1234 for (i = 0; i < n_expired; i++) { 1235 if (expired[i].event == NL80211_RADAR_PRE_CAC_EXPIRED && 1236 cfg80211_any_wiphy_oper_chan(wiphy, expired[i].chan)) 1237 continue; 1238 1239 if (!cfg80211_dfs_chan_expire(rdev, &expired[i], &chandef)) 1240 continue; 1241 1242 regulatory_propagate_dfs_state(wiphy, &chandef, 1243 NL80211_DFS_USABLE, 1244 expired[i].event); 1245 } 1246 mutex_unlock(&cfg80211_mutex); 1247 1248 kvfree(expired); 1249 1250 /* reschedule if there are other channels waiting to be cleared again */ 1251 if (check_again) 1252 queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk, 1253 next_time); 1254 } 1255 1256 1257 void __cfg80211_radar_event(struct wiphy *wiphy, 1258 struct cfg80211_chan_def *chandef, 1259 bool offchan, gfp_t gfp) 1260 { 1261 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1262 1263 trace_cfg80211_radar_event(wiphy, chandef, offchan); 1264 1265 /* only set the chandef supplied channel to unavailable, in 1266 * case the radar is detected on only one of multiple channels 1267 * spanned by the chandef. 1268 */ 1269 cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_UNAVAILABLE); 1270 1271 if (offchan) { 1272 cancel_delayed_work(&rdev->background_cac_done_wk); 1273 queue_work(cfg80211_wq, &rdev->background_cac_abort_wk); 1274 } 1275 1276 cfg80211_sched_dfs_chan_update(rdev); 1277 1278 nl80211_radar_notify(rdev, chandef, NL80211_RADAR_DETECTED, NULL, gfp); 1279 1280 memcpy(&rdev->radar_chandef, chandef, sizeof(struct cfg80211_chan_def)); 1281 queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk); 1282 } 1283 EXPORT_SYMBOL(__cfg80211_radar_event); 1284 1285 void cfg80211_cac_event(struct net_device *netdev, 1286 const struct cfg80211_chan_def *chandef, 1287 enum nl80211_radar_event event, gfp_t gfp, 1288 unsigned int link_id) 1289 { 1290 struct wireless_dev *wdev = netdev->ieee80211_ptr; 1291 struct wiphy *wiphy = wdev->wiphy; 1292 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1293 unsigned long timeout; 1294 1295 if (WARN_ON(wdev->valid_links && 1296 !(wdev->valid_links & BIT(link_id)))) 1297 return; 1298 1299 trace_cfg80211_cac_event(netdev, event, link_id); 1300 1301 if (WARN_ON(!wdev->links[link_id].cac_started && 1302 event != NL80211_RADAR_CAC_STARTED)) 1303 return; 1304 1305 switch (event) { 1306 case NL80211_RADAR_CAC_FINISHED: 1307 timeout = wdev->links[link_id].cac_start_time + 1308 msecs_to_jiffies(wdev->links[link_id].cac_time_ms); 1309 WARN_ON(!time_after_eq(jiffies, timeout)); 1310 cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_AVAILABLE); 1311 memcpy(&rdev->cac_done_chandef, chandef, 1312 sizeof(struct cfg80211_chan_def)); 1313 queue_work(cfg80211_wq, &rdev->propagate_cac_done_wk); 1314 cfg80211_sched_dfs_chan_update(rdev); 1315 fallthrough; 1316 case NL80211_RADAR_CAC_ABORTED: 1317 wdev->links[link_id].cac_started = false; 1318 cfg80211_set_cac_state(wiphy, chandef, false); 1319 break; 1320 case NL80211_RADAR_CAC_STARTED: 1321 wdev->links[link_id].cac_started = true; 1322 cfg80211_set_cac_state(wiphy, chandef, true); 1323 break; 1324 default: 1325 WARN_ON(1); 1326 return; 1327 } 1328 1329 nl80211_radar_notify(rdev, chandef, event, netdev, gfp); 1330 } 1331 EXPORT_SYMBOL(cfg80211_cac_event); 1332 1333 static void 1334 __cfg80211_background_cac_event(struct cfg80211_registered_device *rdev, 1335 struct wireless_dev *wdev, 1336 const struct cfg80211_chan_def *chandef, 1337 enum nl80211_radar_event event) 1338 { 1339 struct wiphy *wiphy = &rdev->wiphy; 1340 struct net_device *netdev; 1341 1342 lockdep_assert_wiphy(&rdev->wiphy); 1343 1344 if (!cfg80211_chandef_valid(chandef)) 1345 return; 1346 1347 switch (event) { 1348 case NL80211_RADAR_CAC_FINISHED: 1349 cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_AVAILABLE); 1350 cfg80211_set_cac_state(wiphy, chandef, false); 1351 memcpy(&rdev->cac_done_chandef, chandef, sizeof(*chandef)); 1352 queue_work(cfg80211_wq, &rdev->propagate_cac_done_wk); 1353 cfg80211_sched_dfs_chan_update(rdev); 1354 break; 1355 case NL80211_RADAR_CAC_ABORTED: 1356 cfg80211_set_cac_state(wiphy, chandef, false); 1357 if (!cancel_delayed_work(&rdev->background_cac_done_wk)) 1358 return; 1359 break; 1360 case NL80211_RADAR_CAC_STARTED: 1361 cfg80211_set_cac_state(wiphy, chandef, true); 1362 break; 1363 default: 1364 return; 1365 } 1366 1367 netdev = wdev ? wdev->netdev : NULL; 1368 nl80211_radar_notify(rdev, chandef, event, netdev, GFP_KERNEL); 1369 } 1370 1371 void cfg80211_background_cac_done_wk(struct work_struct *work) 1372 { 1373 struct delayed_work *delayed_work = to_delayed_work(work); 1374 struct cfg80211_registered_device *rdev; 1375 1376 rdev = container_of(delayed_work, struct cfg80211_registered_device, 1377 background_cac_done_wk); 1378 1379 guard(wiphy)(&rdev->wiphy); 1380 1381 rdev_set_radar_background(rdev, NULL); 1382 1383 __cfg80211_background_cac_event(rdev, rdev->background_radar_wdev, 1384 &rdev->background_radar_chandef, 1385 NL80211_RADAR_CAC_FINISHED); 1386 1387 rdev->background_radar_wdev = NULL; 1388 } 1389 1390 void cfg80211_background_cac_abort_wk(struct work_struct *work) 1391 { 1392 struct cfg80211_registered_device *rdev; 1393 struct wireless_dev *wdev; 1394 1395 rdev = container_of(work, struct cfg80211_registered_device, 1396 background_cac_abort_wk); 1397 1398 guard(wiphy)(&rdev->wiphy); 1399 1400 wdev = rdev->background_radar_wdev; 1401 if (wdev) 1402 cfg80211_stop_background_radar_detection(wdev); 1403 } 1404 1405 void cfg80211_background_cac_abort(struct wiphy *wiphy) 1406 { 1407 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1408 1409 queue_work(cfg80211_wq, &rdev->background_cac_abort_wk); 1410 } 1411 EXPORT_SYMBOL(cfg80211_background_cac_abort); 1412 1413 int 1414 cfg80211_start_background_radar_detection(struct cfg80211_registered_device *rdev, 1415 struct wireless_dev *wdev, 1416 struct cfg80211_chan_def *chandef) 1417 { 1418 unsigned int cac_time_ms; 1419 int err; 1420 1421 lockdep_assert_wiphy(&rdev->wiphy); 1422 1423 if (!wiphy_ext_feature_isset(&rdev->wiphy, 1424 NL80211_EXT_FEATURE_RADAR_BACKGROUND)) 1425 return -EOPNOTSUPP; 1426 1427 /* Offchannel chain already locked by another wdev */ 1428 if (rdev->background_radar_wdev && rdev->background_radar_wdev != wdev) 1429 return -EBUSY; 1430 1431 /* CAC already in progress on the offchannel chain */ 1432 if (rdev->background_radar_wdev == wdev && 1433 delayed_work_pending(&rdev->background_cac_done_wk)) 1434 return -EBUSY; 1435 1436 err = rdev_set_radar_background(rdev, chandef); 1437 if (err) 1438 return err; 1439 1440 cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, chandef); 1441 if (!cac_time_ms) 1442 cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS; 1443 1444 rdev->background_radar_chandef = *chandef; 1445 rdev->background_radar_wdev = wdev; /* Get offchain ownership */ 1446 1447 __cfg80211_background_cac_event(rdev, wdev, chandef, 1448 NL80211_RADAR_CAC_STARTED); 1449 queue_delayed_work(cfg80211_wq, &rdev->background_cac_done_wk, 1450 msecs_to_jiffies(cac_time_ms)); 1451 1452 return 0; 1453 } 1454 1455 void cfg80211_stop_radar_detection(struct wireless_dev *wdev) 1456 { 1457 struct wiphy *wiphy = wdev->wiphy; 1458 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1459 int link_id; 1460 1461 for_each_valid_link(wdev, link_id) { 1462 struct cfg80211_chan_def chandef; 1463 1464 if (!wdev->links[link_id].cac_started) 1465 continue; 1466 1467 chandef = *wdev_chandef(wdev, link_id); 1468 rdev_end_cac(rdev, wdev->netdev, link_id); 1469 wdev->links[link_id].cac_started = false; 1470 cfg80211_set_cac_state(wiphy, &chandef, false); 1471 nl80211_radar_notify(rdev, &chandef, NL80211_RADAR_CAC_ABORTED, 1472 wdev->netdev, GFP_KERNEL); 1473 } 1474 } 1475 1476 void cfg80211_stop_background_radar_detection(struct wireless_dev *wdev) 1477 { 1478 struct wiphy *wiphy = wdev->wiphy; 1479 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1480 1481 lockdep_assert_wiphy(wiphy); 1482 1483 if (wdev != rdev->background_radar_wdev) 1484 return; 1485 1486 rdev_set_radar_background(rdev, NULL); 1487 1488 __cfg80211_background_cac_event(rdev, wdev, 1489 &rdev->background_radar_chandef, 1490 NL80211_RADAR_CAC_ABORTED); 1491 1492 rdev->background_radar_wdev = NULL; 1493 } 1494 1495 int cfg80211_assoc_ml_reconf(struct cfg80211_registered_device *rdev, 1496 struct net_device *dev, 1497 struct cfg80211_ml_reconf_req *req) 1498 { 1499 struct wireless_dev *wdev = dev->ieee80211_ptr; 1500 int err; 1501 1502 lockdep_assert_wiphy(wdev->wiphy); 1503 1504 err = rdev_assoc_ml_reconf(rdev, dev, req); 1505 if (!err) { 1506 int link_id; 1507 1508 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; 1509 link_id++) { 1510 if (!req->add_links[link_id].bss) 1511 continue; 1512 1513 cfg80211_ref_bss(&rdev->wiphy, req->add_links[link_id].bss); 1514 cfg80211_hold_bss(bss_from_pub(req->add_links[link_id].bss)); 1515 } 1516 } 1517 1518 return err; 1519 } 1520 1521 void cfg80211_mlo_reconf_add_done(struct net_device *dev, 1522 struct cfg80211_mlo_reconf_done_data *data) 1523 { 1524 struct wireless_dev *wdev = dev->ieee80211_ptr; 1525 struct wiphy *wiphy = wdev->wiphy; 1526 int link_id; 1527 1528 lockdep_assert_wiphy(wiphy); 1529 1530 trace_cfg80211_mlo_reconf_add_done(dev, data->added_links, 1531 data->buf, data->len, 1532 data->driver_initiated); 1533 1534 if (WARN_ON(!wdev->valid_links)) 1535 return; 1536 1537 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION && 1538 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) 1539 return; 1540 1541 /* validate that a BSS is given for each added link */ 1542 for (link_id = 0; link_id < ARRAY_SIZE(data->links); link_id++) { 1543 struct cfg80211_bss *bss = data->links[link_id].bss; 1544 1545 if (!(data->added_links & BIT(link_id))) 1546 continue; 1547 1548 if (WARN_ON(!bss)) 1549 return; 1550 } 1551 1552 for (link_id = 0; link_id < ARRAY_SIZE(data->links); link_id++) { 1553 struct cfg80211_bss *bss = data->links[link_id].bss; 1554 1555 if (!bss) 1556 continue; 1557 1558 if (data->added_links & BIT(link_id)) { 1559 wdev->links[link_id].client.current_bss = 1560 bss_from_pub(bss); 1561 1562 if (data->driver_initiated) 1563 cfg80211_hold_bss(bss_from_pub(bss)); 1564 1565 memcpy(wdev->links[link_id].addr, 1566 data->links[link_id].addr, 1567 ETH_ALEN); 1568 } else { 1569 if (!data->driver_initiated) 1570 cfg80211_unhold_bss(bss_from_pub(bss)); 1571 1572 cfg80211_put_bss(wiphy, bss); 1573 } 1574 } 1575 1576 wdev->valid_links |= data->added_links; 1577 nl80211_mlo_reconf_add_done(dev, data); 1578 } 1579 EXPORT_SYMBOL(cfg80211_mlo_reconf_add_done); 1580