1 // SPDX-License-Identifier: GPL-2.0+ 2 /* Copyright (c) 2015-2016 Quantenna Communications. All rights reserved. */ 3 4 #include <linux/kernel.h> 5 #include <linux/etherdevice.h> 6 #include <linux/vmalloc.h> 7 #include <linux/ieee80211.h> 8 #include <net/cfg80211.h> 9 #include <net/netlink.h> 10 11 #include "cfg80211.h" 12 #include "commands.h" 13 #include "core.h" 14 #include "util.h" 15 #include "bus.h" 16 17 /* Supported rates to be advertised to the cfg80211 */ 18 static struct ieee80211_rate qtnf_rates_2g[] = { 19 {.bitrate = 10, .hw_value = 2, }, 20 {.bitrate = 20, .hw_value = 4, }, 21 {.bitrate = 55, .hw_value = 11, }, 22 {.bitrate = 110, .hw_value = 22, }, 23 {.bitrate = 60, .hw_value = 12, }, 24 {.bitrate = 90, .hw_value = 18, }, 25 {.bitrate = 120, .hw_value = 24, }, 26 {.bitrate = 180, .hw_value = 36, }, 27 {.bitrate = 240, .hw_value = 48, }, 28 {.bitrate = 360, .hw_value = 72, }, 29 {.bitrate = 480, .hw_value = 96, }, 30 {.bitrate = 540, .hw_value = 108, }, 31 }; 32 33 /* Supported rates to be advertised to the cfg80211 */ 34 static struct ieee80211_rate qtnf_rates_5g[] = { 35 {.bitrate = 60, .hw_value = 12, }, 36 {.bitrate = 90, .hw_value = 18, }, 37 {.bitrate = 120, .hw_value = 24, }, 38 {.bitrate = 180, .hw_value = 36, }, 39 {.bitrate = 240, .hw_value = 48, }, 40 {.bitrate = 360, .hw_value = 72, }, 41 {.bitrate = 480, .hw_value = 96, }, 42 {.bitrate = 540, .hw_value = 108, }, 43 }; 44 45 /* Supported crypto cipher suits to be advertised to cfg80211 */ 46 static const u32 qtnf_cipher_suites[] = { 47 WLAN_CIPHER_SUITE_TKIP, 48 WLAN_CIPHER_SUITE_CCMP, 49 WLAN_CIPHER_SUITE_AES_CMAC, 50 }; 51 52 /* Supported mgmt frame types to be advertised to cfg80211 */ 53 static const struct ieee80211_txrx_stypes 54 qtnf_mgmt_stypes[NUM_NL80211_IFTYPES] = { 55 [NL80211_IFTYPE_STATION] = { 56 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) | 57 BIT(IEEE80211_STYPE_AUTH >> 4), 58 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) | 59 BIT(IEEE80211_STYPE_PROBE_REQ >> 4) | 60 BIT(IEEE80211_STYPE_AUTH >> 4), 61 }, 62 [NL80211_IFTYPE_AP] = { 63 .tx = BIT(IEEE80211_STYPE_ACTION >> 4), 64 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) | 65 BIT(IEEE80211_STYPE_PROBE_REQ >> 4) | 66 BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) | 67 BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) | 68 BIT(IEEE80211_STYPE_AUTH >> 4), 69 }, 70 }; 71 72 static int 73 qtnf_validate_iface_combinations(struct wiphy *wiphy, 74 struct qtnf_vif *change_vif, 75 enum nl80211_iftype new_type) 76 { 77 struct qtnf_wmac *mac; 78 struct qtnf_vif *vif; 79 int i; 80 int ret = 0; 81 struct iface_combination_params params = { 82 .num_different_channels = 1, 83 }; 84 85 mac = wiphy_priv(wiphy); 86 if (!mac) 87 return -EFAULT; 88 89 for (i = 0; i < QTNF_MAX_INTF; i++) { 90 vif = &mac->iflist[i]; 91 if (vif->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) 92 params.iftype_num[vif->wdev.iftype]++; 93 } 94 95 if (change_vif) { 96 params.iftype_num[new_type]++; 97 params.iftype_num[change_vif->wdev.iftype]--; 98 } else { 99 params.iftype_num[new_type]++; 100 } 101 102 ret = cfg80211_check_combinations(wiphy, ¶ms); 103 104 return ret; 105 } 106 107 static int 108 qtnf_change_virtual_intf(struct wiphy *wiphy, 109 struct net_device *dev, 110 enum nl80211_iftype type, 111 struct vif_params *params) 112 { 113 struct qtnf_vif *vif = qtnf_netdev_get_priv(dev); 114 u8 *mac_addr = NULL; 115 int use4addr = 0; 116 int ret; 117 118 ret = qtnf_validate_iface_combinations(wiphy, vif, type); 119 if (ret) { 120 pr_err("VIF%u.%u combination check: failed to set type %d\n", 121 vif->mac->macid, vif->vifid, type); 122 return ret; 123 } 124 125 if (params) { 126 mac_addr = params->macaddr; 127 use4addr = params->use_4addr; 128 } 129 130 qtnf_scan_done(vif->mac, true); 131 132 ret = qtnf_cmd_send_change_intf_type(vif, type, use4addr, mac_addr); 133 if (ret) { 134 pr_err("VIF%u.%u: failed to change type to %d\n", 135 vif->mac->macid, vif->vifid, type); 136 return ret; 137 } 138 139 vif->wdev.iftype = type; 140 return 0; 141 } 142 143 int qtnf_del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev) 144 { 145 struct net_device *netdev = wdev->netdev; 146 struct qtnf_vif *vif; 147 struct sk_buff *skb; 148 149 if (WARN_ON(!netdev)) 150 return -EFAULT; 151 152 vif = qtnf_netdev_get_priv(wdev->netdev); 153 154 qtnf_scan_done(vif->mac, true); 155 156 /* Stop data */ 157 netif_tx_stop_all_queues(netdev); 158 if (netif_carrier_ok(netdev)) 159 netif_carrier_off(netdev); 160 161 while ((skb = skb_dequeue(&vif->high_pri_tx_queue))) 162 dev_kfree_skb_any(skb); 163 164 cancel_work_sync(&vif->high_pri_tx_work); 165 166 if (netdev->reg_state == NETREG_REGISTERED) 167 unregister_netdevice(netdev); 168 169 if (qtnf_cmd_send_del_intf(vif)) 170 pr_err("VIF%u.%u: failed to delete VIF\n", vif->mac->macid, 171 vif->vifid); 172 173 vif->netdev->ieee80211_ptr = NULL; 174 vif->netdev = NULL; 175 vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED; 176 177 return 0; 178 } 179 180 static struct wireless_dev *qtnf_add_virtual_intf(struct wiphy *wiphy, 181 const char *name, 182 unsigned char name_assign_t, 183 enum nl80211_iftype type, 184 struct vif_params *params) 185 { 186 struct qtnf_wmac *mac; 187 struct qtnf_vif *vif; 188 u8 *mac_addr = NULL; 189 int use4addr = 0; 190 int ret; 191 192 mac = wiphy_priv(wiphy); 193 194 if (!mac) 195 return ERR_PTR(-EFAULT); 196 197 ret = qtnf_validate_iface_combinations(wiphy, NULL, type); 198 if (ret) { 199 pr_err("MAC%u invalid combination: failed to add type %d\n", 200 mac->macid, type); 201 return ERR_PTR(ret); 202 } 203 204 switch (type) { 205 case NL80211_IFTYPE_STATION: 206 case NL80211_IFTYPE_AP: 207 vif = qtnf_mac_get_free_vif(mac); 208 if (!vif) { 209 pr_err("MAC%u: no free VIF available\n", mac->macid); 210 return ERR_PTR(-EFAULT); 211 } 212 213 eth_zero_addr(vif->mac_addr); 214 eth_zero_addr(vif->bssid); 215 vif->bss_priority = QTNF_DEF_BSS_PRIORITY; 216 memset(&vif->wdev, 0, sizeof(vif->wdev)); 217 vif->wdev.wiphy = wiphy; 218 vif->wdev.iftype = type; 219 break; 220 default: 221 pr_err("MAC%u: unsupported IF type %d\n", mac->macid, type); 222 return ERR_PTR(-ENOTSUPP); 223 } 224 225 if (params) { 226 mac_addr = params->macaddr; 227 use4addr = params->use_4addr; 228 } 229 230 ret = qtnf_cmd_send_add_intf(vif, type, use4addr, mac_addr); 231 if (ret) { 232 pr_err("VIF%u.%u: failed to add VIF %pM\n", 233 mac->macid, vif->vifid, mac_addr); 234 goto err_cmd; 235 } 236 237 if (!is_valid_ether_addr(vif->mac_addr)) { 238 pr_err("VIF%u.%u: FW reported bad MAC: %pM\n", 239 mac->macid, vif->vifid, vif->mac_addr); 240 ret = -EINVAL; 241 goto err_mac; 242 } 243 244 ret = qtnf_core_net_attach(mac, vif, name, name_assign_t); 245 if (ret) { 246 pr_err("VIF%u.%u: failed to attach netdev\n", mac->macid, 247 vif->vifid); 248 goto err_net; 249 } 250 251 vif->wdev.netdev = vif->netdev; 252 return &vif->wdev; 253 254 err_net: 255 vif->netdev = NULL; 256 err_mac: 257 qtnf_cmd_send_del_intf(vif); 258 err_cmd: 259 vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED; 260 261 return ERR_PTR(ret); 262 } 263 264 static int qtnf_mgmt_set_appie(struct qtnf_vif *vif, 265 const struct cfg80211_beacon_data *info) 266 { 267 int ret = 0; 268 269 if (!info->beacon_ies || !info->beacon_ies_len) { 270 ret = qtnf_cmd_send_mgmt_set_appie(vif, QLINK_IE_SET_BEACON_IES, 271 NULL, 0); 272 } else { 273 ret = qtnf_cmd_send_mgmt_set_appie(vif, QLINK_IE_SET_BEACON_IES, 274 info->beacon_ies, 275 info->beacon_ies_len); 276 } 277 278 if (ret) 279 goto out; 280 281 if (!info->proberesp_ies || !info->proberesp_ies_len) { 282 ret = qtnf_cmd_send_mgmt_set_appie(vif, 283 QLINK_IE_SET_PROBE_RESP_IES, 284 NULL, 0); 285 } else { 286 ret = qtnf_cmd_send_mgmt_set_appie(vif, 287 QLINK_IE_SET_PROBE_RESP_IES, 288 info->proberesp_ies, 289 info->proberesp_ies_len); 290 } 291 292 if (ret) 293 goto out; 294 295 if (!info->assocresp_ies || !info->assocresp_ies_len) { 296 ret = qtnf_cmd_send_mgmt_set_appie(vif, 297 QLINK_IE_SET_ASSOC_RESP, 298 NULL, 0); 299 } else { 300 ret = qtnf_cmd_send_mgmt_set_appie(vif, 301 QLINK_IE_SET_ASSOC_RESP, 302 info->assocresp_ies, 303 info->assocresp_ies_len); 304 } 305 306 out: 307 return ret; 308 } 309 310 static int qtnf_change_beacon(struct wiphy *wiphy, struct net_device *dev, 311 struct cfg80211_beacon_data *info) 312 { 313 struct qtnf_vif *vif = qtnf_netdev_get_priv(dev); 314 315 return qtnf_mgmt_set_appie(vif, info); 316 } 317 318 static int qtnf_start_ap(struct wiphy *wiphy, struct net_device *dev, 319 struct cfg80211_ap_settings *settings) 320 { 321 struct qtnf_vif *vif = qtnf_netdev_get_priv(dev); 322 int ret; 323 324 ret = qtnf_cmd_send_start_ap(vif, settings); 325 if (ret) 326 pr_err("VIF%u.%u: failed to start AP\n", vif->mac->macid, 327 vif->vifid); 328 329 return ret; 330 } 331 332 static int qtnf_stop_ap(struct wiphy *wiphy, struct net_device *dev) 333 { 334 struct qtnf_vif *vif = qtnf_netdev_get_priv(dev); 335 int ret; 336 337 qtnf_scan_done(vif->mac, true); 338 339 ret = qtnf_cmd_send_stop_ap(vif); 340 if (ret) 341 pr_err("VIF%u.%u: failed to stop AP operation in FW\n", 342 vif->mac->macid, vif->vifid); 343 344 netif_carrier_off(vif->netdev); 345 346 return ret; 347 } 348 349 static int qtnf_set_wiphy_params(struct wiphy *wiphy, u32 changed) 350 { 351 struct qtnf_wmac *mac = wiphy_priv(wiphy); 352 struct qtnf_vif *vif; 353 int ret; 354 355 vif = qtnf_mac_get_base_vif(mac); 356 if (!vif) { 357 pr_err("MAC%u: primary VIF is not configured\n", mac->macid); 358 return -EFAULT; 359 } 360 361 ret = qtnf_cmd_send_update_phy_params(mac, changed); 362 if (ret) 363 pr_err("MAC%u: failed to update PHY params\n", mac->macid); 364 365 return ret; 366 } 367 368 static void 369 qtnf_mgmt_frame_register(struct wiphy *wiphy, struct wireless_dev *wdev, 370 u16 frame_type, bool reg) 371 { 372 struct qtnf_vif *vif = qtnf_netdev_get_priv(wdev->netdev); 373 u16 mgmt_type; 374 u16 new_mask; 375 u16 qlink_frame_type = 0; 376 377 mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4; 378 379 if (reg) 380 new_mask = vif->mgmt_frames_bitmask | BIT(mgmt_type); 381 else 382 new_mask = vif->mgmt_frames_bitmask & ~BIT(mgmt_type); 383 384 if (new_mask == vif->mgmt_frames_bitmask) 385 return; 386 387 switch (frame_type & IEEE80211_FCTL_STYPE) { 388 case IEEE80211_STYPE_REASSOC_REQ: 389 case IEEE80211_STYPE_ASSOC_REQ: 390 qlink_frame_type = QLINK_MGMT_FRAME_ASSOC_REQ; 391 break; 392 case IEEE80211_STYPE_AUTH: 393 qlink_frame_type = QLINK_MGMT_FRAME_AUTH; 394 break; 395 case IEEE80211_STYPE_PROBE_REQ: 396 qlink_frame_type = QLINK_MGMT_FRAME_PROBE_REQ; 397 break; 398 case IEEE80211_STYPE_ACTION: 399 qlink_frame_type = QLINK_MGMT_FRAME_ACTION; 400 break; 401 default: 402 pr_warn("VIF%u.%u: unsupported frame type: %X\n", 403 vif->mac->macid, vif->vifid, 404 (frame_type & IEEE80211_FCTL_STYPE) >> 4); 405 return; 406 } 407 408 if (qtnf_cmd_send_register_mgmt(vif, qlink_frame_type, reg)) { 409 pr_warn("VIF%u.%u: failed to %sregister mgmt frame type 0x%x\n", 410 vif->mac->macid, vif->vifid, reg ? "" : "un", 411 frame_type); 412 return; 413 } 414 415 vif->mgmt_frames_bitmask = new_mask; 416 pr_debug("VIF%u.%u: %sregistered mgmt frame type 0x%x\n", 417 vif->mac->macid, vif->vifid, reg ? "" : "un", frame_type); 418 } 419 420 static int 421 qtnf_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev, 422 struct cfg80211_mgmt_tx_params *params, u64 *cookie) 423 { 424 struct qtnf_vif *vif = qtnf_netdev_get_priv(wdev->netdev); 425 const struct ieee80211_mgmt *mgmt_frame = (void *)params->buf; 426 u32 short_cookie = prandom_u32(); 427 u16 flags = 0; 428 u16 freq; 429 430 *cookie = short_cookie; 431 432 if (params->offchan) 433 flags |= QLINK_FRAME_TX_FLAG_OFFCHAN; 434 435 if (params->no_cck) 436 flags |= QLINK_FRAME_TX_FLAG_NO_CCK; 437 438 if (params->dont_wait_for_ack) 439 flags |= QLINK_FRAME_TX_FLAG_ACK_NOWAIT; 440 441 /* If channel is not specified, pass "freq = 0" to tell device 442 * firmware to use current channel. 443 */ 444 if (params->chan) 445 freq = params->chan->center_freq; 446 else 447 freq = 0; 448 449 pr_debug("%s freq:%u; FC:%.4X; DA:%pM; len:%zu; C:%.8X; FL:%.4X\n", 450 wdev->netdev->name, freq, 451 le16_to_cpu(mgmt_frame->frame_control), mgmt_frame->da, 452 params->len, short_cookie, flags); 453 454 return qtnf_cmd_send_frame(vif, short_cookie, flags, 455 freq, params->buf, params->len); 456 } 457 458 static int 459 qtnf_get_station(struct wiphy *wiphy, struct net_device *dev, 460 const u8 *mac, struct station_info *sinfo) 461 { 462 struct qtnf_vif *vif = qtnf_netdev_get_priv(dev); 463 464 sinfo->generation = vif->generation; 465 return qtnf_cmd_get_sta_info(vif, mac, sinfo); 466 } 467 468 static int 469 qtnf_dump_station(struct wiphy *wiphy, struct net_device *dev, 470 int idx, u8 *mac, struct station_info *sinfo) 471 { 472 struct qtnf_vif *vif = qtnf_netdev_get_priv(dev); 473 const struct qtnf_sta_node *sta_node; 474 int ret; 475 476 switch (vif->wdev.iftype) { 477 case NL80211_IFTYPE_STATION: 478 if (idx != 0 || !vif->wdev.current_bss) 479 return -ENOENT; 480 481 ether_addr_copy(mac, vif->bssid); 482 break; 483 case NL80211_IFTYPE_AP: 484 sta_node = qtnf_sta_list_lookup_index(&vif->sta_list, idx); 485 if (unlikely(!sta_node)) 486 return -ENOENT; 487 488 ether_addr_copy(mac, sta_node->mac_addr); 489 break; 490 default: 491 return -ENOTSUPP; 492 } 493 494 ret = qtnf_cmd_get_sta_info(vif, mac, sinfo); 495 496 if (vif->wdev.iftype == NL80211_IFTYPE_AP) { 497 if (ret == -ENOENT) { 498 cfg80211_del_sta(vif->netdev, mac, GFP_KERNEL); 499 sinfo->filled = 0; 500 } 501 } 502 503 sinfo->generation = vif->generation; 504 505 return ret; 506 } 507 508 static int qtnf_add_key(struct wiphy *wiphy, struct net_device *dev, 509 u8 key_index, bool pairwise, const u8 *mac_addr, 510 struct key_params *params) 511 { 512 struct qtnf_vif *vif = qtnf_netdev_get_priv(dev); 513 int ret; 514 515 ret = qtnf_cmd_send_add_key(vif, key_index, pairwise, mac_addr, params); 516 if (ret) 517 pr_err("VIF%u.%u: failed to add key: cipher=%x idx=%u pw=%u\n", 518 vif->mac->macid, vif->vifid, params->cipher, key_index, 519 pairwise); 520 521 return ret; 522 } 523 524 static int qtnf_del_key(struct wiphy *wiphy, struct net_device *dev, 525 u8 key_index, bool pairwise, const u8 *mac_addr) 526 { 527 struct qtnf_vif *vif = qtnf_netdev_get_priv(dev); 528 int ret; 529 530 ret = qtnf_cmd_send_del_key(vif, key_index, pairwise, mac_addr); 531 if (ret) { 532 if (ret == -ENOENT) { 533 pr_debug("VIF%u.%u: key index %d out of bounds\n", 534 vif->mac->macid, vif->vifid, key_index); 535 } else { 536 pr_err("VIF%u.%u: failed to delete key: idx=%u pw=%u\n", 537 vif->mac->macid, vif->vifid, 538 key_index, pairwise); 539 } 540 } 541 542 return ret; 543 } 544 545 static int qtnf_set_default_key(struct wiphy *wiphy, struct net_device *dev, 546 u8 key_index, bool unicast, bool multicast) 547 { 548 struct qtnf_vif *vif = qtnf_netdev_get_priv(dev); 549 int ret; 550 551 ret = qtnf_cmd_send_set_default_key(vif, key_index, unicast, multicast); 552 if (ret) 553 pr_err("VIF%u.%u: failed to set dflt key: idx=%u uc=%u mc=%u\n", 554 vif->mac->macid, vif->vifid, key_index, unicast, 555 multicast); 556 557 return ret; 558 } 559 560 static int 561 qtnf_set_default_mgmt_key(struct wiphy *wiphy, struct net_device *dev, 562 u8 key_index) 563 { 564 struct qtnf_vif *vif = qtnf_netdev_get_priv(dev); 565 int ret; 566 567 ret = qtnf_cmd_send_set_default_mgmt_key(vif, key_index); 568 if (ret) 569 pr_err("VIF%u.%u: failed to set default MGMT key: idx=%u\n", 570 vif->mac->macid, vif->vifid, key_index); 571 572 return ret; 573 } 574 575 static int 576 qtnf_change_station(struct wiphy *wiphy, struct net_device *dev, 577 const u8 *mac, struct station_parameters *params) 578 { 579 struct qtnf_vif *vif = qtnf_netdev_get_priv(dev); 580 int ret; 581 582 ret = qtnf_cmd_send_change_sta(vif, mac, params); 583 if (ret) 584 pr_err("VIF%u.%u: failed to change STA %pM\n", 585 vif->mac->macid, vif->vifid, mac); 586 587 return ret; 588 } 589 590 static int 591 qtnf_del_station(struct wiphy *wiphy, struct net_device *dev, 592 struct station_del_parameters *params) 593 { 594 struct qtnf_vif *vif = qtnf_netdev_get_priv(dev); 595 int ret; 596 597 if (params->mac && 598 (vif->wdev.iftype == NL80211_IFTYPE_AP) && 599 !is_broadcast_ether_addr(params->mac) && 600 !qtnf_sta_list_lookup(&vif->sta_list, params->mac)) 601 return 0; 602 603 ret = qtnf_cmd_send_del_sta(vif, params); 604 if (ret) 605 pr_err("VIF%u.%u: failed to delete STA %pM\n", 606 vif->mac->macid, vif->vifid, params->mac); 607 608 return ret; 609 } 610 611 static int 612 qtnf_scan(struct wiphy *wiphy, struct cfg80211_scan_request *request) 613 { 614 struct qtnf_wmac *mac = wiphy_priv(wiphy); 615 int ret; 616 617 cancel_delayed_work_sync(&mac->scan_timeout); 618 619 mac->scan_req = request; 620 621 ret = qtnf_cmd_send_scan(mac); 622 if (ret) { 623 pr_err("MAC%u: failed to start scan\n", mac->macid); 624 mac->scan_req = NULL; 625 goto out; 626 } 627 628 pr_debug("MAC%u: scan started\n", mac->macid); 629 queue_delayed_work(mac->bus->workqueue, &mac->scan_timeout, 630 QTNF_SCAN_TIMEOUT_SEC * HZ); 631 632 out: 633 return ret; 634 } 635 636 static int 637 qtnf_connect(struct wiphy *wiphy, struct net_device *dev, 638 struct cfg80211_connect_params *sme) 639 { 640 struct qtnf_vif *vif = qtnf_netdev_get_priv(dev); 641 int ret; 642 643 if (vif->wdev.iftype != NL80211_IFTYPE_STATION) 644 return -EOPNOTSUPP; 645 646 if (sme->auth_type == NL80211_AUTHTYPE_SAE && 647 !(sme->flags & CONNECT_REQ_EXTERNAL_AUTH_SUPPORT)) { 648 pr_err("can not offload authentication to userspace\n"); 649 return -EOPNOTSUPP; 650 } 651 652 if (sme->bssid) 653 ether_addr_copy(vif->bssid, sme->bssid); 654 else 655 eth_zero_addr(vif->bssid); 656 657 ret = qtnf_cmd_send_connect(vif, sme); 658 if (ret) { 659 pr_err("VIF%u.%u: failed to connect\n", 660 vif->mac->macid, vif->vifid); 661 goto out; 662 } 663 664 out: 665 return ret; 666 } 667 668 static int 669 qtnf_external_auth(struct wiphy *wiphy, struct net_device *dev, 670 struct cfg80211_external_auth_params *auth) 671 { 672 struct qtnf_vif *vif = qtnf_netdev_get_priv(dev); 673 int ret; 674 675 if (vif->wdev.iftype != NL80211_IFTYPE_STATION) 676 return -EOPNOTSUPP; 677 678 if (!ether_addr_equal(vif->bssid, auth->bssid)) 679 pr_warn("unexpected bssid: %pM", auth->bssid); 680 681 ret = qtnf_cmd_send_external_auth(vif, auth); 682 if (ret) { 683 pr_err("VIF%u.%u: failed to report external auth\n", 684 vif->mac->macid, vif->vifid); 685 goto out; 686 } 687 688 out: 689 return ret; 690 } 691 692 static int 693 qtnf_disconnect(struct wiphy *wiphy, struct net_device *dev, 694 u16 reason_code) 695 { 696 struct qtnf_wmac *mac = wiphy_priv(wiphy); 697 struct qtnf_vif *vif; 698 int ret = 0; 699 700 vif = qtnf_mac_get_base_vif(mac); 701 if (!vif) { 702 pr_err("MAC%u: primary VIF is not configured\n", mac->macid); 703 return -EFAULT; 704 } 705 706 if (vif->wdev.iftype != NL80211_IFTYPE_STATION) { 707 ret = -EOPNOTSUPP; 708 goto out; 709 } 710 711 ret = qtnf_cmd_send_disconnect(vif, reason_code); 712 if (ret) 713 pr_err("VIF%u.%u: failed to disconnect\n", 714 mac->macid, vif->vifid); 715 716 if (vif->wdev.current_bss) { 717 netif_carrier_off(vif->netdev); 718 cfg80211_disconnected(vif->netdev, reason_code, 719 NULL, 0, true, GFP_KERNEL); 720 } 721 722 out: 723 return ret; 724 } 725 726 static int 727 qtnf_dump_survey(struct wiphy *wiphy, struct net_device *dev, 728 int idx, struct survey_info *survey) 729 { 730 struct qtnf_wmac *mac = wiphy_priv(wiphy); 731 struct wireless_dev *wdev = dev->ieee80211_ptr; 732 struct ieee80211_supported_band *sband; 733 const struct cfg80211_chan_def *chandef = &wdev->chandef; 734 struct ieee80211_channel *chan; 735 struct qtnf_chan_stats stats; 736 int ret; 737 738 sband = wiphy->bands[NL80211_BAND_2GHZ]; 739 if (sband && idx >= sband->n_channels) { 740 idx -= sband->n_channels; 741 sband = NULL; 742 } 743 744 if (!sband) 745 sband = wiphy->bands[NL80211_BAND_5GHZ]; 746 747 if (!sband || idx >= sband->n_channels) 748 return -ENOENT; 749 750 chan = &sband->channels[idx]; 751 memset(&stats, 0, sizeof(stats)); 752 753 survey->channel = chan; 754 survey->filled = 0x0; 755 756 if (chandef->chan) { 757 if (chan->hw_value == chandef->chan->hw_value) 758 survey->filled = SURVEY_INFO_IN_USE; 759 } 760 761 ret = qtnf_cmd_get_chan_stats(mac, chan->hw_value, &stats); 762 switch (ret) { 763 case 0: 764 if (unlikely(stats.chan_num != chan->hw_value)) { 765 pr_err("received stats for channel %d instead of %d\n", 766 stats.chan_num, chan->hw_value); 767 ret = -EINVAL; 768 break; 769 } 770 771 survey->filled |= SURVEY_INFO_TIME | 772 SURVEY_INFO_TIME_SCAN | 773 SURVEY_INFO_TIME_BUSY | 774 SURVEY_INFO_TIME_RX | 775 SURVEY_INFO_TIME_TX | 776 SURVEY_INFO_NOISE_DBM; 777 778 survey->time_scan = stats.cca_try; 779 survey->time = stats.cca_try; 780 survey->time_tx = stats.cca_tx; 781 survey->time_rx = stats.cca_rx; 782 survey->time_busy = stats.cca_busy; 783 survey->noise = stats.chan_noise; 784 break; 785 case -ENOENT: 786 pr_debug("no stats for channel %u\n", chan->hw_value); 787 ret = 0; 788 break; 789 default: 790 pr_debug("failed to get chan(%d) stats from card\n", 791 chan->hw_value); 792 break; 793 } 794 795 return ret; 796 } 797 798 static int 799 qtnf_get_channel(struct wiphy *wiphy, struct wireless_dev *wdev, 800 struct cfg80211_chan_def *chandef) 801 { 802 struct net_device *ndev = wdev->netdev; 803 struct qtnf_vif *vif; 804 int ret; 805 806 if (!ndev) 807 return -ENODEV; 808 809 vif = qtnf_netdev_get_priv(wdev->netdev); 810 811 ret = qtnf_cmd_get_channel(vif, chandef); 812 if (ret) { 813 pr_err("%s: failed to get channel: %d\n", ndev->name, ret); 814 ret = -ENODATA; 815 goto out; 816 } 817 818 if (!cfg80211_chandef_valid(chandef)) { 819 pr_err("%s: bad channel freq=%u cf1=%u cf2=%u bw=%u\n", 820 ndev->name, chandef->chan->center_freq, 821 chandef->center_freq1, chandef->center_freq2, 822 chandef->width); 823 ret = -ENODATA; 824 goto out; 825 } 826 827 out: 828 return ret; 829 } 830 831 static int qtnf_channel_switch(struct wiphy *wiphy, struct net_device *dev, 832 struct cfg80211_csa_settings *params) 833 { 834 struct qtnf_vif *vif = qtnf_netdev_get_priv(dev); 835 int ret; 836 837 pr_debug("%s: chan(%u) count(%u) radar(%u) block_tx(%u)\n", dev->name, 838 params->chandef.chan->hw_value, params->count, 839 params->radar_required, params->block_tx); 840 841 if (!cfg80211_chandef_valid(¶ms->chandef)) { 842 pr_err("%s: invalid channel\n", dev->name); 843 return -EINVAL; 844 } 845 846 ret = qtnf_cmd_send_chan_switch(vif, params); 847 if (ret) 848 pr_warn("%s: failed to switch to channel (%u)\n", 849 dev->name, params->chandef.chan->hw_value); 850 851 return ret; 852 } 853 854 static int qtnf_start_radar_detection(struct wiphy *wiphy, 855 struct net_device *ndev, 856 struct cfg80211_chan_def *chandef, 857 u32 cac_time_ms) 858 { 859 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev); 860 int ret; 861 862 if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD)) 863 return -ENOTSUPP; 864 865 ret = qtnf_cmd_start_cac(vif, chandef, cac_time_ms); 866 if (ret) 867 pr_err("%s: failed to start CAC ret=%d\n", ndev->name, ret); 868 869 return ret; 870 } 871 872 static int qtnf_set_mac_acl(struct wiphy *wiphy, 873 struct net_device *dev, 874 const struct cfg80211_acl_data *params) 875 { 876 struct qtnf_vif *vif = qtnf_netdev_get_priv(dev); 877 int ret; 878 879 ret = qtnf_cmd_set_mac_acl(vif, params); 880 if (ret) 881 pr_err("%s: failed to set mac ACL ret=%d\n", dev->name, ret); 882 883 return ret; 884 } 885 886 static int qtnf_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev, 887 bool enabled, int timeout) 888 { 889 struct qtnf_vif *vif = qtnf_netdev_get_priv(dev); 890 int ret; 891 892 ret = qtnf_cmd_send_pm_set(vif, enabled ? QLINK_PM_AUTO_STANDBY : 893 QLINK_PM_OFF, timeout); 894 if (ret) 895 pr_err("%s: failed to set PM mode ret=%d\n", dev->name, ret); 896 897 return ret; 898 } 899 900 static int qtnf_get_tx_power(struct wiphy *wiphy, struct wireless_dev *wdev, 901 int *dbm) 902 { 903 struct qtnf_vif *vif = qtnf_netdev_get_priv(wdev->netdev); 904 int ret; 905 906 ret = qtnf_cmd_get_tx_power(vif, dbm); 907 if (ret) 908 pr_err("MAC%u: failed to get Tx power\n", vif->mac->macid); 909 910 return ret; 911 } 912 913 static int qtnf_set_tx_power(struct wiphy *wiphy, struct wireless_dev *wdev, 914 enum nl80211_tx_power_setting type, int mbm) 915 { 916 struct qtnf_vif *vif; 917 int ret; 918 919 if (wdev) { 920 vif = qtnf_netdev_get_priv(wdev->netdev); 921 } else { 922 struct qtnf_wmac *mac = wiphy_priv(wiphy); 923 924 vif = qtnf_mac_get_base_vif(mac); 925 if (!vif) { 926 pr_err("MAC%u: primary VIF is not configured\n", 927 mac->macid); 928 return -EFAULT; 929 } 930 } 931 932 ret = qtnf_cmd_set_tx_power(vif, type, mbm); 933 if (ret) 934 pr_err("MAC%u: failed to set Tx power\n", vif->mac->macid); 935 936 return ret; 937 } 938 939 #ifdef CONFIG_PM 940 static int qtnf_suspend(struct wiphy *wiphy, struct cfg80211_wowlan *wowlan) 941 { 942 struct qtnf_wmac *mac = wiphy_priv(wiphy); 943 struct qtnf_vif *vif; 944 int ret = 0; 945 946 vif = qtnf_mac_get_base_vif(mac); 947 if (!vif) { 948 pr_err("MAC%u: primary VIF is not configured\n", mac->macid); 949 ret = -EFAULT; 950 goto exit; 951 } 952 953 if (!wowlan) { 954 pr_debug("WoWLAN triggers are not enabled\n"); 955 qtnf_virtual_intf_cleanup(vif->netdev); 956 goto exit; 957 } 958 959 qtnf_scan_done(vif->mac, true); 960 961 ret = qtnf_cmd_send_wowlan_set(vif, wowlan); 962 if (ret) { 963 pr_err("MAC%u: failed to set WoWLAN triggers\n", 964 mac->macid); 965 goto exit; 966 } 967 968 exit: 969 return ret; 970 } 971 972 static int qtnf_resume(struct wiphy *wiphy) 973 { 974 struct qtnf_wmac *mac = wiphy_priv(wiphy); 975 struct qtnf_vif *vif; 976 int ret = 0; 977 978 vif = qtnf_mac_get_base_vif(mac); 979 if (!vif) { 980 pr_err("MAC%u: primary VIF is not configured\n", mac->macid); 981 ret = -EFAULT; 982 goto exit; 983 } 984 985 ret = qtnf_cmd_send_wowlan_set(vif, NULL); 986 if (ret) { 987 pr_err("MAC%u: failed to reset WoWLAN triggers\n", 988 mac->macid); 989 goto exit; 990 } 991 992 exit: 993 return ret; 994 } 995 996 static void qtnf_set_wakeup(struct wiphy *wiphy, bool enabled) 997 { 998 struct qtnf_wmac *mac = wiphy_priv(wiphy); 999 struct qtnf_bus *bus = mac->bus; 1000 1001 device_set_wakeup_enable(bus->dev, enabled); 1002 } 1003 #endif 1004 1005 static struct cfg80211_ops qtn_cfg80211_ops = { 1006 .add_virtual_intf = qtnf_add_virtual_intf, 1007 .change_virtual_intf = qtnf_change_virtual_intf, 1008 .del_virtual_intf = qtnf_del_virtual_intf, 1009 .start_ap = qtnf_start_ap, 1010 .change_beacon = qtnf_change_beacon, 1011 .stop_ap = qtnf_stop_ap, 1012 .set_wiphy_params = qtnf_set_wiphy_params, 1013 .mgmt_frame_register = qtnf_mgmt_frame_register, 1014 .mgmt_tx = qtnf_mgmt_tx, 1015 .change_station = qtnf_change_station, 1016 .del_station = qtnf_del_station, 1017 .get_station = qtnf_get_station, 1018 .dump_station = qtnf_dump_station, 1019 .add_key = qtnf_add_key, 1020 .del_key = qtnf_del_key, 1021 .set_default_key = qtnf_set_default_key, 1022 .set_default_mgmt_key = qtnf_set_default_mgmt_key, 1023 .scan = qtnf_scan, 1024 .connect = qtnf_connect, 1025 .external_auth = qtnf_external_auth, 1026 .disconnect = qtnf_disconnect, 1027 .dump_survey = qtnf_dump_survey, 1028 .get_channel = qtnf_get_channel, 1029 .channel_switch = qtnf_channel_switch, 1030 .start_radar_detection = qtnf_start_radar_detection, 1031 .set_mac_acl = qtnf_set_mac_acl, 1032 .set_power_mgmt = qtnf_set_power_mgmt, 1033 .get_tx_power = qtnf_get_tx_power, 1034 .set_tx_power = qtnf_set_tx_power, 1035 #ifdef CONFIG_PM 1036 .suspend = qtnf_suspend, 1037 .resume = qtnf_resume, 1038 .set_wakeup = qtnf_set_wakeup, 1039 #endif 1040 }; 1041 1042 static void qtnf_cfg80211_reg_notifier(struct wiphy *wiphy, 1043 struct regulatory_request *req) 1044 { 1045 struct qtnf_wmac *mac = wiphy_priv(wiphy); 1046 enum nl80211_band band; 1047 int ret; 1048 1049 pr_debug("MAC%u: initiator=%d alpha=%c%c\n", mac->macid, req->initiator, 1050 req->alpha2[0], req->alpha2[1]); 1051 1052 ret = qtnf_cmd_reg_notify(mac, req, qtnf_mac_slave_radar_get(wiphy)); 1053 if (ret) { 1054 pr_err("MAC%u: failed to update region to %c%c: %d\n", 1055 mac->macid, req->alpha2[0], req->alpha2[1], ret); 1056 return; 1057 } 1058 1059 for (band = 0; band < NUM_NL80211_BANDS; ++band) { 1060 if (!wiphy->bands[band]) 1061 continue; 1062 1063 ret = qtnf_cmd_band_info_get(mac, wiphy->bands[band]); 1064 if (ret) 1065 pr_err("MAC%u: failed to update band %u\n", 1066 mac->macid, band); 1067 } 1068 } 1069 1070 struct wiphy *qtnf_wiphy_allocate(struct qtnf_bus *bus) 1071 { 1072 struct wiphy *wiphy; 1073 1074 if (bus->hw_info.hw_capab & QLINK_HW_CAPAB_DFS_OFFLOAD) 1075 qtn_cfg80211_ops.start_radar_detection = NULL; 1076 1077 if (!(bus->hw_info.hw_capab & QLINK_HW_CAPAB_PWR_MGMT)) 1078 qtn_cfg80211_ops.set_power_mgmt = NULL; 1079 1080 wiphy = wiphy_new(&qtn_cfg80211_ops, sizeof(struct qtnf_wmac)); 1081 if (!wiphy) 1082 return NULL; 1083 1084 set_wiphy_dev(wiphy, bus->dev); 1085 1086 return wiphy; 1087 } 1088 1089 static int 1090 qtnf_wiphy_setup_if_comb(struct wiphy *wiphy, struct qtnf_mac_info *mac_info) 1091 { 1092 struct ieee80211_iface_combination *if_comb; 1093 size_t n_if_comb; 1094 u16 interface_modes = 0; 1095 size_t i, j; 1096 1097 if_comb = mac_info->if_comb; 1098 n_if_comb = mac_info->n_if_comb; 1099 1100 if (!if_comb || !n_if_comb) 1101 return -ENOENT; 1102 1103 for (i = 0; i < n_if_comb; i++) { 1104 if_comb[i].radar_detect_widths = mac_info->radar_detect_widths; 1105 1106 for (j = 0; j < if_comb[i].n_limits; j++) 1107 interface_modes |= if_comb[i].limits[j].types; 1108 } 1109 1110 wiphy->iface_combinations = if_comb; 1111 wiphy->n_iface_combinations = n_if_comb; 1112 wiphy->interface_modes = interface_modes; 1113 1114 return 0; 1115 } 1116 1117 int qtnf_wiphy_register(struct qtnf_hw_info *hw_info, struct qtnf_wmac *mac) 1118 { 1119 struct wiphy *wiphy = priv_to_wiphy(mac); 1120 struct qtnf_mac_info *macinfo = &mac->macinfo; 1121 int ret; 1122 bool regdomain_is_known; 1123 1124 if (!wiphy) { 1125 pr_err("invalid wiphy pointer\n"); 1126 return -EFAULT; 1127 } 1128 1129 wiphy->frag_threshold = macinfo->frag_thr; 1130 wiphy->rts_threshold = macinfo->rts_thr; 1131 wiphy->retry_short = macinfo->sretry_limit; 1132 wiphy->retry_long = macinfo->lretry_limit; 1133 wiphy->coverage_class = macinfo->coverage_class; 1134 1135 wiphy->max_scan_ssids = 1136 (hw_info->max_scan_ssids) ? hw_info->max_scan_ssids : 1; 1137 wiphy->max_scan_ie_len = QTNF_MAX_VSIE_LEN; 1138 wiphy->mgmt_stypes = qtnf_mgmt_stypes; 1139 wiphy->max_remain_on_channel_duration = 5000; 1140 wiphy->max_acl_mac_addrs = macinfo->max_acl_mac_addrs; 1141 wiphy->max_num_csa_counters = 2; 1142 1143 ret = qtnf_wiphy_setup_if_comb(wiphy, macinfo); 1144 if (ret) 1145 goto out; 1146 1147 /* Initialize cipher suits */ 1148 wiphy->cipher_suites = qtnf_cipher_suites; 1149 wiphy->n_cipher_suites = ARRAY_SIZE(qtnf_cipher_suites); 1150 wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM; 1151 wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME | 1152 WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD | 1153 WIPHY_FLAG_AP_UAPSD | 1154 WIPHY_FLAG_HAS_CHANNEL_SWITCH | 1155 WIPHY_FLAG_4ADDR_STATION | 1156 WIPHY_FLAG_NETNS_OK; 1157 wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT; 1158 1159 if (hw_info->hw_capab & QLINK_HW_CAPAB_DFS_OFFLOAD) 1160 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD); 1161 1162 if (hw_info->hw_capab & QLINK_HW_CAPAB_SCAN_DWELL) 1163 wiphy_ext_feature_set(wiphy, 1164 NL80211_EXT_FEATURE_SET_SCAN_DWELL); 1165 1166 wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS | 1167 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2; 1168 1169 wiphy->available_antennas_tx = macinfo->num_tx_chain; 1170 wiphy->available_antennas_rx = macinfo->num_rx_chain; 1171 1172 wiphy->max_ap_assoc_sta = macinfo->max_ap_assoc_sta; 1173 wiphy->ht_capa_mod_mask = &macinfo->ht_cap_mod_mask; 1174 wiphy->vht_capa_mod_mask = &macinfo->vht_cap_mod_mask; 1175 1176 ether_addr_copy(wiphy->perm_addr, mac->macaddr); 1177 1178 if (hw_info->hw_capab & QLINK_HW_CAPAB_STA_INACT_TIMEOUT) 1179 wiphy->features |= NL80211_FEATURE_INACTIVITY_TIMER; 1180 1181 if (hw_info->hw_capab & QLINK_HW_CAPAB_SCAN_RANDOM_MAC_ADDR) 1182 wiphy->features |= NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR; 1183 1184 if (!(hw_info->hw_capab & QLINK_HW_CAPAB_OBSS_SCAN)) 1185 wiphy->features |= NL80211_FEATURE_NEED_OBSS_SCAN; 1186 1187 if (hw_info->hw_capab & QLINK_HW_CAPAB_SAE) 1188 wiphy->features |= NL80211_FEATURE_SAE; 1189 1190 #ifdef CONFIG_PM 1191 if (macinfo->wowlan) 1192 wiphy->wowlan = macinfo->wowlan; 1193 #endif 1194 1195 regdomain_is_known = isalpha(mac->rd->alpha2[0]) && 1196 isalpha(mac->rd->alpha2[1]); 1197 1198 if (hw_info->hw_capab & QLINK_HW_CAPAB_REG_UPDATE) { 1199 wiphy->reg_notifier = qtnf_cfg80211_reg_notifier; 1200 1201 if (mac->rd->alpha2[0] == '9' && mac->rd->alpha2[1] == '9') { 1202 wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG | 1203 REGULATORY_STRICT_REG; 1204 wiphy_apply_custom_regulatory(wiphy, mac->rd); 1205 } else if (regdomain_is_known) { 1206 wiphy->regulatory_flags |= REGULATORY_STRICT_REG; 1207 } 1208 } else { 1209 wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED; 1210 } 1211 1212 if (mac->macinfo.extended_capabilities_len) { 1213 wiphy->extended_capabilities = 1214 mac->macinfo.extended_capabilities; 1215 wiphy->extended_capabilities_mask = 1216 mac->macinfo.extended_capabilities_mask; 1217 wiphy->extended_capabilities_len = 1218 mac->macinfo.extended_capabilities_len; 1219 } 1220 1221 strlcpy(wiphy->fw_version, hw_info->fw_version, 1222 sizeof(wiphy->fw_version)); 1223 wiphy->hw_version = hw_info->hw_version; 1224 1225 ret = wiphy_register(wiphy); 1226 if (ret < 0) 1227 goto out; 1228 1229 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) 1230 ret = regulatory_set_wiphy_regd(wiphy, mac->rd); 1231 else if (regdomain_is_known) 1232 ret = regulatory_hint(wiphy, mac->rd->alpha2); 1233 1234 out: 1235 return ret; 1236 } 1237 1238 void qtnf_netdev_updown(struct net_device *ndev, bool up) 1239 { 1240 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev); 1241 1242 if (qtnf_cmd_send_updown_intf(vif, up)) 1243 pr_err("failed to send %s command to VIF%u.%u\n", 1244 up ? "UP" : "DOWN", vif->mac->macid, vif->vifid); 1245 } 1246 1247 void qtnf_virtual_intf_cleanup(struct net_device *ndev) 1248 { 1249 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev); 1250 struct qtnf_wmac *mac = wiphy_priv(vif->wdev.wiphy); 1251 1252 if (vif->wdev.iftype == NL80211_IFTYPE_STATION) 1253 qtnf_disconnect(vif->wdev.wiphy, ndev, 1254 WLAN_REASON_DEAUTH_LEAVING); 1255 1256 qtnf_scan_done(mac, true); 1257 } 1258 1259 void qtnf_cfg80211_vif_reset(struct qtnf_vif *vif) 1260 { 1261 if (vif->wdev.iftype == NL80211_IFTYPE_STATION) 1262 cfg80211_disconnected(vif->netdev, WLAN_REASON_DEAUTH_LEAVING, 1263 NULL, 0, 1, GFP_KERNEL); 1264 1265 cfg80211_shutdown_all_interfaces(vif->wdev.wiphy); 1266 } 1267 1268 void qtnf_band_init_rates(struct ieee80211_supported_band *band) 1269 { 1270 switch (band->band) { 1271 case NL80211_BAND_2GHZ: 1272 band->bitrates = qtnf_rates_2g; 1273 band->n_bitrates = ARRAY_SIZE(qtnf_rates_2g); 1274 break; 1275 case NL80211_BAND_5GHZ: 1276 band->bitrates = qtnf_rates_5g; 1277 band->n_bitrates = ARRAY_SIZE(qtnf_rates_5g); 1278 break; 1279 default: 1280 band->bitrates = NULL; 1281 band->n_bitrates = 0; 1282 break; 1283 } 1284 } 1285