linux/net/wireless/nl80211.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * This is the new netlink-based wireless configuration interface.
   4 *
   5 * Copyright 2006-2010  Johannes Berg <johannes@sipsolutions.net>
   6 * Copyright 2013-2014  Intel Mobile Communications GmbH
   7 * Copyright 2015-2017  Intel Deutschland GmbH
   8 * Copyright (C) 2018-2019 Intel Corporation
   9 */
  10
  11#include <linux/if.h>
  12#include <linux/module.h>
  13#include <linux/err.h>
  14#include <linux/slab.h>
  15#include <linux/list.h>
  16#include <linux/if_ether.h>
  17#include <linux/ieee80211.h>
  18#include <linux/nl80211.h>
  19#include <linux/rtnetlink.h>
  20#include <linux/netlink.h>
  21#include <linux/nospec.h>
  22#include <linux/etherdevice.h>
  23#include <net/net_namespace.h>
  24#include <net/genetlink.h>
  25#include <net/cfg80211.h>
  26#include <net/sock.h>
  27#include <net/inet_connection_sock.h>
  28#include "core.h"
  29#include "nl80211.h"
  30#include "reg.h"
  31#include "rdev-ops.h"
  32
  33static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
  34                                   struct genl_info *info,
  35                                   struct cfg80211_crypto_settings *settings,
  36                                   int cipher_limit);
  37
  38/* the netlink family */
  39static struct genl_family nl80211_fam;
  40
  41/* multicast groups */
  42enum nl80211_multicast_groups {
  43        NL80211_MCGRP_CONFIG,
  44        NL80211_MCGRP_SCAN,
  45        NL80211_MCGRP_REGULATORY,
  46        NL80211_MCGRP_MLME,
  47        NL80211_MCGRP_VENDOR,
  48        NL80211_MCGRP_NAN,
  49        NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
  50};
  51
  52static const struct genl_multicast_group nl80211_mcgrps[] = {
  53        [NL80211_MCGRP_CONFIG] = { .name = NL80211_MULTICAST_GROUP_CONFIG },
  54        [NL80211_MCGRP_SCAN] = { .name = NL80211_MULTICAST_GROUP_SCAN },
  55        [NL80211_MCGRP_REGULATORY] = { .name = NL80211_MULTICAST_GROUP_REG },
  56        [NL80211_MCGRP_MLME] = { .name = NL80211_MULTICAST_GROUP_MLME },
  57        [NL80211_MCGRP_VENDOR] = { .name = NL80211_MULTICAST_GROUP_VENDOR },
  58        [NL80211_MCGRP_NAN] = { .name = NL80211_MULTICAST_GROUP_NAN },
  59#ifdef CONFIG_NL80211_TESTMODE
  60        [NL80211_MCGRP_TESTMODE] = { .name = NL80211_MULTICAST_GROUP_TESTMODE }
  61#endif
  62};
  63
  64/* returns ERR_PTR values */
  65static struct wireless_dev *
  66__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
  67{
  68        struct cfg80211_registered_device *rdev;
  69        struct wireless_dev *result = NULL;
  70        bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
  71        bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
  72        u64 wdev_id;
  73        int wiphy_idx = -1;
  74        int ifidx = -1;
  75
  76        ASSERT_RTNL();
  77
  78        if (!have_ifidx && !have_wdev_id)
  79                return ERR_PTR(-EINVAL);
  80
  81        if (have_ifidx)
  82                ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
  83        if (have_wdev_id) {
  84                wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
  85                wiphy_idx = wdev_id >> 32;
  86        }
  87
  88        list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  89                struct wireless_dev *wdev;
  90
  91                if (wiphy_net(&rdev->wiphy) != netns)
  92                        continue;
  93
  94                if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
  95                        continue;
  96
  97                list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
  98                        if (have_ifidx && wdev->netdev &&
  99                            wdev->netdev->ifindex == ifidx) {
 100                                result = wdev;
 101                                break;
 102                        }
 103                        if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
 104                                result = wdev;
 105                                break;
 106                        }
 107                }
 108
 109                if (result)
 110                        break;
 111        }
 112
 113        if (result)
 114                return result;
 115        return ERR_PTR(-ENODEV);
 116}
 117
 118static struct cfg80211_registered_device *
 119__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
 120{
 121        struct cfg80211_registered_device *rdev = NULL, *tmp;
 122        struct net_device *netdev;
 123
 124        ASSERT_RTNL();
 125
 126        if (!attrs[NL80211_ATTR_WIPHY] &&
 127            !attrs[NL80211_ATTR_IFINDEX] &&
 128            !attrs[NL80211_ATTR_WDEV])
 129                return ERR_PTR(-EINVAL);
 130
 131        if (attrs[NL80211_ATTR_WIPHY])
 132                rdev = cfg80211_rdev_by_wiphy_idx(
 133                                nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
 134
 135        if (attrs[NL80211_ATTR_WDEV]) {
 136                u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
 137                struct wireless_dev *wdev;
 138                bool found = false;
 139
 140                tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
 141                if (tmp) {
 142                        /* make sure wdev exists */
 143                        list_for_each_entry(wdev, &tmp->wiphy.wdev_list, list) {
 144                                if (wdev->identifier != (u32)wdev_id)
 145                                        continue;
 146                                found = true;
 147                                break;
 148                        }
 149
 150                        if (!found)
 151                                tmp = NULL;
 152
 153                        if (rdev && tmp != rdev)
 154                                return ERR_PTR(-EINVAL);
 155                        rdev = tmp;
 156                }
 157        }
 158
 159        if (attrs[NL80211_ATTR_IFINDEX]) {
 160                int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
 161
 162                netdev = __dev_get_by_index(netns, ifindex);
 163                if (netdev) {
 164                        if (netdev->ieee80211_ptr)
 165                                tmp = wiphy_to_rdev(
 166                                        netdev->ieee80211_ptr->wiphy);
 167                        else
 168                                tmp = NULL;
 169
 170                        /* not wireless device -- return error */
 171                        if (!tmp)
 172                                return ERR_PTR(-EINVAL);
 173
 174                        /* mismatch -- return error */
 175                        if (rdev && tmp != rdev)
 176                                return ERR_PTR(-EINVAL);
 177
 178                        rdev = tmp;
 179                }
 180        }
 181
 182        if (!rdev)
 183                return ERR_PTR(-ENODEV);
 184
 185        if (netns != wiphy_net(&rdev->wiphy))
 186                return ERR_PTR(-ENODEV);
 187
 188        return rdev;
 189}
 190
 191/*
 192 * This function returns a pointer to the driver
 193 * that the genl_info item that is passed refers to.
 194 *
 195 * The result of this can be a PTR_ERR and hence must
 196 * be checked with IS_ERR() for errors.
 197 */
 198static struct cfg80211_registered_device *
 199cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
 200{
 201        return __cfg80211_rdev_from_attrs(netns, info->attrs);
 202}
 203
 204static int validate_beacon_head(const struct nlattr *attr,
 205                                struct netlink_ext_ack *extack)
 206{
 207        const u8 *data = nla_data(attr);
 208        unsigned int len = nla_len(attr);
 209        const struct element *elem;
 210        const struct ieee80211_mgmt *mgmt = (void *)data;
 211        unsigned int fixedlen = offsetof(struct ieee80211_mgmt,
 212                                         u.beacon.variable);
 213
 214        if (len < fixedlen)
 215                goto err;
 216
 217        if (ieee80211_hdrlen(mgmt->frame_control) !=
 218            offsetof(struct ieee80211_mgmt, u.beacon))
 219                goto err;
 220
 221        data += fixedlen;
 222        len -= fixedlen;
 223
 224        for_each_element(elem, data, len) {
 225                /* nothing */
 226        }
 227
 228        if (for_each_element_completed(elem, data, len))
 229                return 0;
 230
 231err:
 232        NL_SET_ERR_MSG_ATTR(extack, attr, "malformed beacon head");
 233        return -EINVAL;
 234}
 235
 236static int validate_ie_attr(const struct nlattr *attr,
 237                            struct netlink_ext_ack *extack)
 238{
 239        const u8 *data = nla_data(attr);
 240        unsigned int len = nla_len(attr);
 241        const struct element *elem;
 242
 243        for_each_element(elem, data, len) {
 244                /* nothing */
 245        }
 246
 247        if (for_each_element_completed(elem, data, len))
 248                return 0;
 249
 250        NL_SET_ERR_MSG_ATTR(extack, attr, "malformed information elements");
 251        return -EINVAL;
 252}
 253
 254/* policy for the attributes */
 255static const struct nla_policy
 256nl80211_ftm_responder_policy[NL80211_FTM_RESP_ATTR_MAX + 1] = {
 257        [NL80211_FTM_RESP_ATTR_ENABLED] = { .type = NLA_FLAG, },
 258        [NL80211_FTM_RESP_ATTR_LCI] = { .type = NLA_BINARY,
 259                                        .len = U8_MAX },
 260        [NL80211_FTM_RESP_ATTR_CIVICLOC] = { .type = NLA_BINARY,
 261                                             .len = U8_MAX },
 262};
 263
 264static const struct nla_policy
 265nl80211_pmsr_ftm_req_attr_policy[NL80211_PMSR_FTM_REQ_ATTR_MAX + 1] = {
 266        [NL80211_PMSR_FTM_REQ_ATTR_ASAP] = { .type = NLA_FLAG },
 267        [NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE] = { .type = NLA_U32 },
 268        [NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP] =
 269                NLA_POLICY_MAX(NLA_U8, 15),
 270        [NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD] = { .type = NLA_U16 },
 271        [NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION] =
 272                NLA_POLICY_MAX(NLA_U8, 15),
 273        [NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST] =
 274                NLA_POLICY_MAX(NLA_U8, 31),
 275        [NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES] = { .type = NLA_U8 },
 276        [NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI] = { .type = NLA_FLAG },
 277        [NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC] = { .type = NLA_FLAG },
 278};
 279
 280static const struct nla_policy
 281nl80211_pmsr_req_data_policy[NL80211_PMSR_TYPE_MAX + 1] = {
 282        [NL80211_PMSR_TYPE_FTM] =
 283                NLA_POLICY_NESTED(nl80211_pmsr_ftm_req_attr_policy),
 284};
 285
 286static const struct nla_policy
 287nl80211_pmsr_req_attr_policy[NL80211_PMSR_REQ_ATTR_MAX + 1] = {
 288        [NL80211_PMSR_REQ_ATTR_DATA] =
 289                NLA_POLICY_NESTED(nl80211_pmsr_req_data_policy),
 290        [NL80211_PMSR_REQ_ATTR_GET_AP_TSF] = { .type = NLA_FLAG },
 291};
 292
 293static const struct nla_policy
 294nl80211_psmr_peer_attr_policy[NL80211_PMSR_PEER_ATTR_MAX + 1] = {
 295        [NL80211_PMSR_PEER_ATTR_ADDR] = NLA_POLICY_ETH_ADDR,
 296        /*
 297         * we could specify this again to be the top-level policy,
 298         * but that would open us up to recursion problems ...
 299         */
 300        [NL80211_PMSR_PEER_ATTR_CHAN] = { .type = NLA_NESTED },
 301        [NL80211_PMSR_PEER_ATTR_REQ] =
 302                NLA_POLICY_NESTED(nl80211_pmsr_req_attr_policy),
 303        [NL80211_PMSR_PEER_ATTR_RESP] = { .type = NLA_REJECT },
 304};
 305
 306static const struct nla_policy
 307nl80211_pmsr_attr_policy[NL80211_PMSR_ATTR_MAX + 1] = {
 308        [NL80211_PMSR_ATTR_MAX_PEERS] = { .type = NLA_REJECT },
 309        [NL80211_PMSR_ATTR_REPORT_AP_TSF] = { .type = NLA_REJECT },
 310        [NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR] = { .type = NLA_REJECT },
 311        [NL80211_PMSR_ATTR_TYPE_CAPA] = { .type = NLA_REJECT },
 312        [NL80211_PMSR_ATTR_PEERS] =
 313                NLA_POLICY_NESTED_ARRAY(nl80211_psmr_peer_attr_policy),
 314};
 315
 316static const struct nla_policy
 317he_obss_pd_policy[NL80211_HE_OBSS_PD_ATTR_MAX + 1] = {
 318        [NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] =
 319                NLA_POLICY_RANGE(NLA_U8, 1, 20),
 320        [NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET] =
 321                NLA_POLICY_RANGE(NLA_U8, 1, 20),
 322};
 323
 324const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
 325        [0] = { .strict_start_type = NL80211_ATTR_HE_OBSS_PD },
 326        [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
 327        [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
 328                                      .len = 20-1 },
 329        [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
 330
 331        [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
 332        [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
 333        [NL80211_ATTR_WIPHY_EDMG_CHANNELS] = NLA_POLICY_RANGE(NLA_U8,
 334                                                NL80211_EDMG_CHANNELS_MIN,
 335                                                NL80211_EDMG_CHANNELS_MAX),
 336        [NL80211_ATTR_WIPHY_EDMG_BW_CONFIG] = NLA_POLICY_RANGE(NLA_U8,
 337                                                NL80211_EDMG_BW_CONFIG_MIN,
 338                                                NL80211_EDMG_BW_CONFIG_MAX),
 339
 340        [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
 341        [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
 342        [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
 343
 344        [NL80211_ATTR_WIPHY_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
 345        [NL80211_ATTR_WIPHY_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
 346        [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
 347        [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
 348        [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
 349        [NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
 350
 351        [NL80211_ATTR_IFTYPE] = NLA_POLICY_MAX(NLA_U32, NL80211_IFTYPE_MAX),
 352        [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
 353        [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
 354
 355        [NL80211_ATTR_MAC] = { .type = NLA_EXACT_LEN_WARN, .len = ETH_ALEN },
 356        [NL80211_ATTR_PREV_BSSID] = {
 357                .type = NLA_EXACT_LEN_WARN,
 358                .len = ETH_ALEN
 359        },
 360
 361        [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
 362        [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
 363                                    .len = WLAN_MAX_KEY_LEN },
 364        [NL80211_ATTR_KEY_IDX] = NLA_POLICY_MAX(NLA_U8, 5),
 365        [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
 366        [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
 367        [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
 368        [NL80211_ATTR_KEY_TYPE] =
 369                NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES),
 370
 371        [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
 372        [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
 373        [NL80211_ATTR_BEACON_HEAD] =
 374                NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_beacon_head,
 375                                       IEEE80211_MAX_DATA_LEN),
 376        [NL80211_ATTR_BEACON_TAIL] =
 377                NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
 378                                       IEEE80211_MAX_DATA_LEN),
 379        [NL80211_ATTR_STA_AID] =
 380                NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
 381        [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
 382        [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
 383        [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
 384                                               .len = NL80211_MAX_SUPP_RATES },
 385        [NL80211_ATTR_STA_PLINK_ACTION] =
 386                NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_ACTIONS - 1),
 387        [NL80211_ATTR_STA_TX_POWER_SETTING] =
 388                NLA_POLICY_RANGE(NLA_U8,
 389                                 NL80211_TX_POWER_AUTOMATIC,
 390                                 NL80211_TX_POWER_FIXED),
 391        [NL80211_ATTR_STA_TX_POWER] = { .type = NLA_S16 },
 392        [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
 393        [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
 394        [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
 395                                   .len = IEEE80211_MAX_MESH_ID_LEN },
 396        [NL80211_ATTR_MPATH_NEXT_HOP] = NLA_POLICY_ETH_ADDR_COMPAT,
 397
 398        [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
 399        [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
 400
 401        [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
 402        [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
 403        [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
 404        [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
 405                                           .len = NL80211_MAX_SUPP_RATES },
 406        [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
 407
 408        [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
 409        [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
 410
 411        [NL80211_ATTR_HT_CAPABILITY] = {
 412                .type = NLA_EXACT_LEN_WARN,
 413                .len = NL80211_HT_CAPABILITY_LEN
 414        },
 415
 416        [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
 417        [NL80211_ATTR_IE] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
 418                                                   validate_ie_attr,
 419                                                   IEEE80211_MAX_DATA_LEN),
 420        [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
 421        [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
 422
 423        [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
 424                                .len = IEEE80211_MAX_SSID_LEN },
 425        [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
 426        [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
 427        [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
 428        [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
 429        [NL80211_ATTR_USE_MFP] = NLA_POLICY_RANGE(NLA_U32,
 430                                                  NL80211_MFP_NO,
 431                                                  NL80211_MFP_OPTIONAL),
 432        [NL80211_ATTR_STA_FLAGS2] = {
 433                .len = sizeof(struct nl80211_sta_flag_update),
 434        },
 435        [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
 436        [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
 437        [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
 438        [NL80211_ATTR_CONTROL_PORT_OVER_NL80211] = { .type = NLA_FLAG },
 439        [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
 440        [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
 441        [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
 442        [NL80211_ATTR_PID] = { .type = NLA_U32 },
 443        [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
 444        [NL80211_ATTR_PMKID] = {
 445                .type = NLA_EXACT_LEN_WARN,
 446                .len = WLAN_PMKID_LEN
 447        },
 448        [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
 449        [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
 450        [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
 451        [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
 452                                 .len = IEEE80211_MAX_DATA_LEN },
 453        [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
 454        [NL80211_ATTR_PS_STATE] = NLA_POLICY_RANGE(NLA_U32,
 455                                                   NL80211_PS_DISABLED,
 456                                                   NL80211_PS_ENABLED),
 457        [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
 458        [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
 459        [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
 460        [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
 461        [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
 462        [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
 463        [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
 464        [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
 465        [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
 466        [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
 467        [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
 468        [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
 469        [NL80211_ATTR_STA_PLINK_STATE] =
 470                NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_STATES - 1),
 471        [NL80211_ATTR_MESH_PEER_AID] =
 472                NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
 473        [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
 474        [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
 475        [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
 476        [NL80211_ATTR_HIDDEN_SSID] =
 477                NLA_POLICY_RANGE(NLA_U32,
 478                                 NL80211_HIDDEN_SSID_NOT_IN_USE,
 479                                 NL80211_HIDDEN_SSID_ZERO_CONTENTS),
 480        [NL80211_ATTR_IE_PROBE_RESP] =
 481                NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
 482                                       IEEE80211_MAX_DATA_LEN),
 483        [NL80211_ATTR_IE_ASSOC_RESP] =
 484                NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
 485                                       IEEE80211_MAX_DATA_LEN),
 486        [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
 487        [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
 488        [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
 489        [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
 490        [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
 491        [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
 492        [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
 493        [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
 494        [NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
 495        [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
 496        [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
 497                                      .len = IEEE80211_MAX_DATA_LEN },
 498        [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
 499        [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
 500        [NL80211_ATTR_HT_CAPABILITY_MASK] = {
 501                .len = NL80211_HT_CAPABILITY_LEN
 502        },
 503        [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
 504        [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
 505        [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
 506        [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
 507        [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
 508        [NL80211_ATTR_AUTH_DATA] = { .type = NLA_BINARY, },
 509        [NL80211_ATTR_VHT_CAPABILITY] = {
 510                .type = NLA_EXACT_LEN_WARN,
 511                .len = NL80211_VHT_CAPABILITY_LEN
 512        },
 513        [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
 514        [NL80211_ATTR_P2P_CTWINDOW] = NLA_POLICY_MAX(NLA_U8, 127),
 515        [NL80211_ATTR_P2P_OPPPS] = NLA_POLICY_MAX(NLA_U8, 1),
 516        [NL80211_ATTR_LOCAL_MESH_POWER_MODE] =
 517                NLA_POLICY_RANGE(NLA_U32,
 518                                 NL80211_MESH_POWER_UNKNOWN + 1,
 519                                 NL80211_MESH_POWER_MAX),
 520        [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
 521        [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
 522        [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
 523        [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
 524        [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
 525        [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
 526        [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
 527                .len = NL80211_VHT_CAPABILITY_LEN,
 528        },
 529        [NL80211_ATTR_MDID] = { .type = NLA_U16 },
 530        [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
 531                                  .len = IEEE80211_MAX_DATA_LEN },
 532        [NL80211_ATTR_PEER_AID] =
 533                NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
 534        [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
 535        [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
 536        [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
 537        [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_BINARY },
 538        [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_BINARY },
 539        [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
 540        [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
 541        [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
 542        [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
 543        [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
 544        [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
 545        [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
 546        [NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
 547                                   .len = IEEE80211_QOS_MAP_LEN_MAX },
 548        [NL80211_ATTR_MAC_HINT] = {
 549                .type = NLA_EXACT_LEN_WARN,
 550                .len = ETH_ALEN
 551        },
 552        [NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
 553        [NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
 554        [NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
 555        [NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
 556        [NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
 557        [NL80211_ATTR_TSID] = NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_TIDS - 1),
 558        [NL80211_ATTR_USER_PRIO] =
 559                NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_UPS - 1),
 560        [NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
 561        [NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
 562        [NL80211_ATTR_MAC_MASK] = {
 563                .type = NLA_EXACT_LEN_WARN,
 564                .len = ETH_ALEN
 565        },
 566        [NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
 567        [NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
 568        [NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
 569        [NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
 570        [NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
 571        [NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
 572        [NL80211_ATTR_STA_SUPPORT_P2P_PS] =
 573                NLA_POLICY_MAX(NLA_U8, NUM_NL80211_P2P_PS_STATUS - 1),
 574        [NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
 575                .len = VHT_MUMIMO_GROUPS_DATA_LEN
 576        },
 577        [NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = {
 578                .type = NLA_EXACT_LEN_WARN,
 579                .len = ETH_ALEN
 580        },
 581        [NL80211_ATTR_NAN_MASTER_PREF] = NLA_POLICY_MIN(NLA_U8, 1),
 582        [NL80211_ATTR_BANDS] = { .type = NLA_U32 },
 583        [NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
 584        [NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
 585                                    .len = FILS_MAX_KEK_LEN },
 586        [NL80211_ATTR_FILS_NONCES] = {
 587                .type = NLA_EXACT_LEN_WARN,
 588                .len = 2 * FILS_NONCE_LEN
 589        },
 590        [NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
 591        [NL80211_ATTR_BSSID] = { .type = NLA_EXACT_LEN_WARN, .len = ETH_ALEN },
 592        [NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
 593        [NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
 594                .len = sizeof(struct nl80211_bss_select_rssi_adjust)
 595        },
 596        [NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
 597        [NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
 598                                             .len = FILS_ERP_MAX_USERNAME_LEN },
 599        [NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
 600                                          .len = FILS_ERP_MAX_REALM_LEN },
 601        [NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
 602        [NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
 603                                        .len = FILS_ERP_MAX_RRK_LEN },
 604        [NL80211_ATTR_FILS_CACHE_ID] = { .type = NLA_EXACT_LEN_WARN, .len = 2 },
 605        [NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
 606        [NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
 607        [NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
 608
 609        [NL80211_ATTR_TXQ_LIMIT] = { .type = NLA_U32 },
 610        [NL80211_ATTR_TXQ_MEMORY_LIMIT] = { .type = NLA_U32 },
 611        [NL80211_ATTR_TXQ_QUANTUM] = { .type = NLA_U32 },
 612        [NL80211_ATTR_HE_CAPABILITY] = { .type = NLA_BINARY,
 613                                         .len = NL80211_HE_MAX_CAPABILITY_LEN },
 614        [NL80211_ATTR_FTM_RESPONDER] = {
 615                .type = NLA_NESTED,
 616                .validation_data = nl80211_ftm_responder_policy,
 617        },
 618        [NL80211_ATTR_TIMEOUT] = NLA_POLICY_MIN(NLA_U32, 1),
 619        [NL80211_ATTR_PEER_MEASUREMENTS] =
 620                NLA_POLICY_NESTED(nl80211_pmsr_attr_policy),
 621        [NL80211_ATTR_AIRTIME_WEIGHT] = NLA_POLICY_MIN(NLA_U16, 1),
 622        [NL80211_ATTR_SAE_PASSWORD] = { .type = NLA_BINARY,
 623                                        .len = SAE_PASSWORD_MAX_LEN },
 624        [NL80211_ATTR_TWT_RESPONDER] = { .type = NLA_FLAG },
 625        [NL80211_ATTR_HE_OBSS_PD] = NLA_POLICY_NESTED(he_obss_pd_policy),
 626};
 627
 628/* policy for the key attributes */
 629static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
 630        [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
 631        [NL80211_KEY_IDX] = { .type = NLA_U8 },
 632        [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
 633        [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
 634        [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
 635        [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
 636        [NL80211_KEY_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES - 1),
 637        [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
 638        [NL80211_KEY_MODE] = NLA_POLICY_RANGE(NLA_U8, 0, NL80211_KEY_SET_TX),
 639};
 640
 641/* policy for the key default flags */
 642static const struct nla_policy
 643nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
 644        [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
 645        [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
 646};
 647
 648#ifdef CONFIG_PM
 649/* policy for WoWLAN attributes */
 650static const struct nla_policy
 651nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
 652        [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
 653        [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
 654        [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
 655        [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
 656        [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
 657        [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
 658        [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
 659        [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
 660        [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
 661        [NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
 662};
 663
 664static const struct nla_policy
 665nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
 666        [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
 667        [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
 668        [NL80211_WOWLAN_TCP_DST_MAC] = {
 669                .type = NLA_EXACT_LEN_WARN,
 670                .len = ETH_ALEN
 671        },
 672        [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
 673        [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
 674        [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .type = NLA_MIN_LEN, .len = 1 },
 675        [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
 676                .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
 677        },
 678        [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
 679                .len = sizeof(struct nl80211_wowlan_tcp_data_token)
 680        },
 681        [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
 682        [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .type = NLA_MIN_LEN, .len = 1 },
 683        [NL80211_WOWLAN_TCP_WAKE_MASK] = { .type = NLA_MIN_LEN, .len = 1 },
 684};
 685#endif /* CONFIG_PM */
 686
 687/* policy for coalesce rule attributes */
 688static const struct nla_policy
 689nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
 690        [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
 691        [NL80211_ATTR_COALESCE_RULE_CONDITION] =
 692                NLA_POLICY_RANGE(NLA_U32,
 693                                 NL80211_COALESCE_CONDITION_MATCH,
 694                                 NL80211_COALESCE_CONDITION_NO_MATCH),
 695        [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
 696};
 697
 698/* policy for GTK rekey offload attributes */
 699static const struct nla_policy
 700nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
 701        [NL80211_REKEY_DATA_KEK] = {
 702                .type = NLA_EXACT_LEN_WARN,
 703                .len = NL80211_KEK_LEN,
 704        },
 705        [NL80211_REKEY_DATA_KCK] = {
 706                .type = NLA_EXACT_LEN_WARN,
 707                .len = NL80211_KCK_LEN,
 708        },
 709        [NL80211_REKEY_DATA_REPLAY_CTR] = {
 710                .type = NLA_EXACT_LEN_WARN,
 711                .len = NL80211_REPLAY_CTR_LEN
 712        },
 713};
 714
 715static const struct nla_policy
 716nl80211_match_band_rssi_policy[NUM_NL80211_BANDS] = {
 717        [NL80211_BAND_2GHZ] = { .type = NLA_S32 },
 718        [NL80211_BAND_5GHZ] = { .type = NLA_S32 },
 719        [NL80211_BAND_6GHZ] = { .type = NLA_S32 },
 720        [NL80211_BAND_60GHZ] = { .type = NLA_S32 },
 721};
 722
 723static const struct nla_policy
 724nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
 725        [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
 726                                                 .len = IEEE80211_MAX_SSID_LEN },
 727        [NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = {
 728                .type = NLA_EXACT_LEN_WARN,
 729                .len = ETH_ALEN
 730        },
 731        [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
 732        [NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI] =
 733                NLA_POLICY_NESTED(nl80211_match_band_rssi_policy),
 734};
 735
 736static const struct nla_policy
 737nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
 738        [NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
 739        [NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
 740};
 741
 742static const struct nla_policy
 743nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
 744        [NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
 745        [NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
 746        [NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
 747                .len = sizeof(struct nl80211_bss_select_rssi_adjust)
 748        },
 749};
 750
 751/* policy for NAN function attributes */
 752static const struct nla_policy
 753nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
 754        [NL80211_NAN_FUNC_TYPE] = { .type = NLA_U8 },
 755        [NL80211_NAN_FUNC_SERVICE_ID] = {
 756                                    .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
 757        [NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
 758        [NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
 759        [NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
 760        [NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
 761        [NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
 762        [NL80211_NAN_FUNC_FOLLOW_UP_DEST] = {
 763                .type = NLA_EXACT_LEN_WARN,
 764                .len = ETH_ALEN
 765        },
 766        [NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
 767        [NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
 768        [NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
 769                        .len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
 770        [NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
 771        [NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
 772        [NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
 773        [NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
 774        [NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
 775};
 776
 777/* policy for Service Response Filter attributes */
 778static const struct nla_policy
 779nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
 780        [NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
 781        [NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
 782                                 .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
 783        [NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
 784        [NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
 785};
 786
 787/* policy for packet pattern attributes */
 788static const struct nla_policy
 789nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
 790        [NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
 791        [NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
 792        [NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
 793};
 794
 795int nl80211_prepare_wdev_dump(struct netlink_callback *cb,
 796                              struct cfg80211_registered_device **rdev,
 797                              struct wireless_dev **wdev)
 798{
 799        int err;
 800
 801        if (!cb->args[0]) {
 802                struct nlattr **attrbuf;
 803
 804                attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
 805                                  GFP_KERNEL);
 806                if (!attrbuf)
 807                        return -ENOMEM;
 808
 809                err = nlmsg_parse_deprecated(cb->nlh,
 810                                             GENL_HDRLEN + nl80211_fam.hdrsize,
 811                                             attrbuf, nl80211_fam.maxattr,
 812                                             nl80211_policy, NULL);
 813                if (err) {
 814                        kfree(attrbuf);
 815                        return err;
 816                }
 817
 818                *wdev = __cfg80211_wdev_from_attrs(sock_net(cb->skb->sk),
 819                                                   attrbuf);
 820                kfree(attrbuf);
 821                if (IS_ERR(*wdev))
 822                        return PTR_ERR(*wdev);
 823                *rdev = wiphy_to_rdev((*wdev)->wiphy);
 824                /* 0 is the first index - add 1 to parse only once */
 825                cb->args[0] = (*rdev)->wiphy_idx + 1;
 826                cb->args[1] = (*wdev)->identifier;
 827        } else {
 828                /* subtract the 1 again here */
 829                struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
 830                struct wireless_dev *tmp;
 831
 832                if (!wiphy)
 833                        return -ENODEV;
 834                *rdev = wiphy_to_rdev(wiphy);
 835                *wdev = NULL;
 836
 837                list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
 838                        if (tmp->identifier == cb->args[1]) {
 839                                *wdev = tmp;
 840                                break;
 841                        }
 842                }
 843
 844                if (!*wdev)
 845                        return -ENODEV;
 846        }
 847
 848        return 0;
 849}
 850
 851/* message building helper */
 852void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
 853                     int flags, u8 cmd)
 854{
 855        /* since there is no private header just add the generic one */
 856        return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
 857}
 858
 859static int nl80211_msg_put_wmm_rules(struct sk_buff *msg,
 860                                     const struct ieee80211_reg_rule *rule)
 861{
 862        int j;
 863        struct nlattr *nl_wmm_rules =
 864                nla_nest_start_noflag(msg, NL80211_FREQUENCY_ATTR_WMM);
 865
 866        if (!nl_wmm_rules)
 867                goto nla_put_failure;
 868
 869        for (j = 0; j < IEEE80211_NUM_ACS; j++) {
 870                struct nlattr *nl_wmm_rule = nla_nest_start_noflag(msg, j);
 871
 872                if (!nl_wmm_rule)
 873                        goto nla_put_failure;
 874
 875                if (nla_put_u16(msg, NL80211_WMMR_CW_MIN,
 876                                rule->wmm_rule.client[j].cw_min) ||
 877                    nla_put_u16(msg, NL80211_WMMR_CW_MAX,
 878                                rule->wmm_rule.client[j].cw_max) ||
 879                    nla_put_u8(msg, NL80211_WMMR_AIFSN,
 880                               rule->wmm_rule.client[j].aifsn) ||
 881                    nla_put_u16(msg, NL80211_WMMR_TXOP,
 882                                rule->wmm_rule.client[j].cot))
 883                        goto nla_put_failure;
 884
 885                nla_nest_end(msg, nl_wmm_rule);
 886        }
 887        nla_nest_end(msg, nl_wmm_rules);
 888
 889        return 0;
 890
 891nla_put_failure:
 892        return -ENOBUFS;
 893}
 894
 895static int nl80211_msg_put_channel(struct sk_buff *msg, struct wiphy *wiphy,
 896                                   struct ieee80211_channel *chan,
 897                                   bool large)
 898{
 899        /* Some channels must be completely excluded from the
 900         * list to protect old user-space tools from breaking
 901         */
 902        if (!large && chan->flags &
 903            (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
 904                return 0;
 905
 906        if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
 907                        chan->center_freq))
 908                goto nla_put_failure;
 909
 910        if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
 911            nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
 912                goto nla_put_failure;
 913        if (chan->flags & IEEE80211_CHAN_NO_IR) {
 914                if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
 915                        goto nla_put_failure;
 916                if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
 917                        goto nla_put_failure;
 918        }
 919        if (chan->flags & IEEE80211_CHAN_RADAR) {
 920                if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
 921                        goto nla_put_failure;
 922                if (large) {
 923                        u32 time;
 924
 925                        time = elapsed_jiffies_msecs(chan->dfs_state_entered);
 926
 927                        if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
 928                                        chan->dfs_state))
 929                                goto nla_put_failure;
 930                        if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
 931                                        time))
 932                                goto nla_put_failure;
 933                        if (nla_put_u32(msg,
 934                                        NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
 935                                        chan->dfs_cac_ms))
 936                                goto nla_put_failure;
 937                }
 938        }
 939
 940        if (large) {
 941                if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
 942                    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
 943                        goto nla_put_failure;
 944                if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
 945                    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
 946                        goto nla_put_failure;
 947                if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
 948                    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
 949                        goto nla_put_failure;
 950                if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
 951                    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
 952                        goto nla_put_failure;
 953                if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
 954                    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
 955                        goto nla_put_failure;
 956                if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
 957                    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
 958                        goto nla_put_failure;
 959                if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
 960                    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
 961                        goto nla_put_failure;
 962                if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
 963                    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
 964                        goto nla_put_failure;
 965        }
 966
 967        if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
 968                        DBM_TO_MBM(chan->max_power)))
 969                goto nla_put_failure;
 970
 971        if (large) {
 972                const struct ieee80211_reg_rule *rule =
 973                        freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
 974
 975                if (!IS_ERR_OR_NULL(rule) && rule->has_wmm) {
 976                        if (nl80211_msg_put_wmm_rules(msg, rule))
 977                                goto nla_put_failure;
 978                }
 979        }
 980
 981        return 0;
 982
 983 nla_put_failure:
 984        return -ENOBUFS;
 985}
 986
 987static bool nl80211_put_txq_stats(struct sk_buff *msg,
 988                                  struct cfg80211_txq_stats *txqstats,
 989                                  int attrtype)
 990{
 991        struct nlattr *txqattr;
 992
 993#define PUT_TXQVAL_U32(attr, memb) do {                                   \
 994        if (txqstats->filled & BIT(NL80211_TXQ_STATS_ ## attr) &&         \
 995            nla_put_u32(msg, NL80211_TXQ_STATS_ ## attr, txqstats->memb)) \
 996                return false;                                             \
 997        } while (0)
 998
 999        txqattr = nla_nest_start_noflag(msg, attrtype);
1000        if (!txqattr)
1001                return false;
1002
1003        PUT_TXQVAL_U32(BACKLOG_BYTES, backlog_bytes);
1004        PUT_TXQVAL_U32(BACKLOG_PACKETS, backlog_packets);
1005        PUT_TXQVAL_U32(FLOWS, flows);
1006        PUT_TXQVAL_U32(DROPS, drops);
1007        PUT_TXQVAL_U32(ECN_MARKS, ecn_marks);
1008        PUT_TXQVAL_U32(OVERLIMIT, overlimit);
1009        PUT_TXQVAL_U32(OVERMEMORY, overmemory);
1010        PUT_TXQVAL_U32(COLLISIONS, collisions);
1011        PUT_TXQVAL_U32(TX_BYTES, tx_bytes);
1012        PUT_TXQVAL_U32(TX_PACKETS, tx_packets);
1013        PUT_TXQVAL_U32(MAX_FLOWS, max_flows);
1014        nla_nest_end(msg, txqattr);
1015
1016#undef PUT_TXQVAL_U32
1017        return true;
1018}
1019
1020/* netlink command implementations */
1021
1022struct key_parse {
1023        struct key_params p;
1024        int idx;
1025        int type;
1026        bool def, defmgmt;
1027        bool def_uni, def_multi;
1028};
1029
1030static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
1031                                 struct key_parse *k)
1032{
1033        struct nlattr *tb[NL80211_KEY_MAX + 1];
1034        int err = nla_parse_nested_deprecated(tb, NL80211_KEY_MAX, key,
1035                                              nl80211_key_policy,
1036                                              info->extack);
1037        if (err)
1038                return err;
1039
1040        k->def = !!tb[NL80211_KEY_DEFAULT];
1041        k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
1042
1043        if (k->def) {
1044                k->def_uni = true;
1045                k->def_multi = true;
1046        }
1047        if (k->defmgmt)
1048                k->def_multi = true;
1049
1050        if (tb[NL80211_KEY_IDX])
1051                k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
1052
1053        if (tb[NL80211_KEY_DATA]) {
1054                k->p.key = nla_data(tb[NL80211_KEY_DATA]);
1055                k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
1056        }
1057
1058        if (tb[NL80211_KEY_SEQ]) {
1059                k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
1060                k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
1061        }
1062
1063        if (tb[NL80211_KEY_CIPHER])
1064                k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
1065
1066        if (tb[NL80211_KEY_TYPE])
1067                k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
1068
1069        if (tb[NL80211_KEY_DEFAULT_TYPES]) {
1070                struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1071
1072                err = nla_parse_nested_deprecated(kdt,
1073                                                  NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1074                                                  tb[NL80211_KEY_DEFAULT_TYPES],
1075                                                  nl80211_key_default_policy,
1076                                                  info->extack);
1077                if (err)
1078                        return err;
1079
1080                k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1081                k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1082        }
1083
1084        if (tb[NL80211_KEY_MODE])
1085                k->p.mode = nla_get_u8(tb[NL80211_KEY_MODE]);
1086
1087        return 0;
1088}
1089
1090static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
1091{
1092        if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1093                k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1094                k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1095        }
1096
1097        if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1098                k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1099                k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1100        }
1101
1102        if (info->attrs[NL80211_ATTR_KEY_IDX])
1103                k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1104
1105        if (info->attrs[NL80211_ATTR_KEY_CIPHER])
1106                k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1107
1108        k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
1109        k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
1110
1111        if (k->def) {
1112                k->def_uni = true;
1113                k->def_multi = true;
1114        }
1115        if (k->defmgmt)
1116                k->def_multi = true;
1117
1118        if (info->attrs[NL80211_ATTR_KEY_TYPE])
1119                k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1120
1121        if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
1122                struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1123                int err = nla_parse_nested_deprecated(kdt,
1124                                                      NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1125                                                      info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
1126                                                      nl80211_key_default_policy,
1127                                                      info->extack);
1128                if (err)
1129                        return err;
1130
1131                k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1132                k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1133        }
1134
1135        return 0;
1136}
1137
1138static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
1139{
1140        int err;
1141
1142        memset(k, 0, sizeof(*k));
1143        k->idx = -1;
1144        k->type = -1;
1145
1146        if (info->attrs[NL80211_ATTR_KEY])
1147                err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
1148        else
1149                err = nl80211_parse_key_old(info, k);
1150
1151        if (err)
1152                return err;
1153
1154        if (k->def && k->defmgmt) {
1155                GENL_SET_ERR_MSG(info, "key with def && defmgmt is invalid");
1156                return -EINVAL;
1157        }
1158
1159        if (k->defmgmt) {
1160                if (k->def_uni || !k->def_multi) {
1161                        GENL_SET_ERR_MSG(info, "defmgmt key must be mcast");
1162                        return -EINVAL;
1163                }
1164        }
1165
1166        if (k->idx != -1) {
1167                if (k->defmgmt) {
1168                        if (k->idx < 4 || k->idx > 5) {
1169                                GENL_SET_ERR_MSG(info,
1170                                                 "defmgmt key idx not 4 or 5");
1171                                return -EINVAL;
1172                        }
1173                } else if (k->def) {
1174                        if (k->idx < 0 || k->idx > 3) {
1175                                GENL_SET_ERR_MSG(info, "def key idx not 0-3");
1176                                return -EINVAL;
1177                        }
1178                } else {
1179                        if (k->idx < 0 || k->idx > 5) {
1180                                GENL_SET_ERR_MSG(info, "key idx not 0-5");
1181                                return -EINVAL;
1182                        }
1183                }
1184        }
1185
1186        return 0;
1187}
1188
1189static struct cfg80211_cached_keys *
1190nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
1191                       struct genl_info *info, bool *no_ht)
1192{
1193        struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
1194        struct key_parse parse;
1195        struct nlattr *key;
1196        struct cfg80211_cached_keys *result;
1197        int rem, err, def = 0;
1198        bool have_key = false;
1199
1200        nla_for_each_nested(key, keys, rem) {
1201                have_key = true;
1202                break;
1203        }
1204
1205        if (!have_key)
1206                return NULL;
1207
1208        result = kzalloc(sizeof(*result), GFP_KERNEL);
1209        if (!result)
1210                return ERR_PTR(-ENOMEM);
1211
1212        result->def = -1;
1213
1214        nla_for_each_nested(key, keys, rem) {
1215                memset(&parse, 0, sizeof(parse));
1216                parse.idx = -1;
1217
1218                err = nl80211_parse_key_new(info, key, &parse);
1219                if (err)
1220                        goto error;
1221                err = -EINVAL;
1222                if (!parse.p.key)
1223                        goto error;
1224                if (parse.idx < 0 || parse.idx > 3) {
1225                        GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
1226                        goto error;
1227                }
1228                if (parse.def) {
1229                        if (def) {
1230                                GENL_SET_ERR_MSG(info,
1231                                                 "only one key can be default");
1232                                goto error;
1233                        }
1234                        def = 1;
1235                        result->def = parse.idx;
1236                        if (!parse.def_uni || !parse.def_multi)
1237                                goto error;
1238                } else if (parse.defmgmt)
1239                        goto error;
1240                err = cfg80211_validate_key_settings(rdev, &parse.p,
1241                                                     parse.idx, false, NULL);
1242                if (err)
1243                        goto error;
1244                if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1245                    parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
1246                        GENL_SET_ERR_MSG(info, "connect key must be WEP");
1247                        err = -EINVAL;
1248                        goto error;
1249                }
1250                result->params[parse.idx].cipher = parse.p.cipher;
1251                result->params[parse.idx].key_len = parse.p.key_len;
1252                result->params[parse.idx].key = result->data[parse.idx];
1253                memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
1254
1255                /* must be WEP key if we got here */
1256                if (no_ht)
1257                        *no_ht = true;
1258        }
1259
1260        if (result->def < 0) {
1261                err = -EINVAL;
1262                GENL_SET_ERR_MSG(info, "need a default/TX key");
1263                goto error;
1264        }
1265
1266        return result;
1267 error:
1268        kfree(result);
1269        return ERR_PTR(err);
1270}
1271
1272static int nl80211_key_allowed(struct wireless_dev *wdev)
1273{
1274        ASSERT_WDEV_LOCK(wdev);
1275
1276        switch (wdev->iftype) {
1277        case NL80211_IFTYPE_AP:
1278        case NL80211_IFTYPE_AP_VLAN:
1279        case NL80211_IFTYPE_P2P_GO:
1280        case NL80211_IFTYPE_MESH_POINT:
1281                break;
1282        case NL80211_IFTYPE_ADHOC:
1283        case NL80211_IFTYPE_STATION:
1284        case NL80211_IFTYPE_P2P_CLIENT:
1285                if (!wdev->current_bss)
1286                        return -ENOLINK;
1287                break;
1288        case NL80211_IFTYPE_UNSPECIFIED:
1289        case NL80211_IFTYPE_OCB:
1290        case NL80211_IFTYPE_MONITOR:
1291        case NL80211_IFTYPE_NAN:
1292        case NL80211_IFTYPE_P2P_DEVICE:
1293        case NL80211_IFTYPE_WDS:
1294        case NUM_NL80211_IFTYPES:
1295                return -EINVAL;
1296        }
1297
1298        return 0;
1299}
1300
1301static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1302                                                        struct nlattr *tb)
1303{
1304        struct ieee80211_channel *chan;
1305
1306        if (tb == NULL)
1307                return NULL;
1308        chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
1309        if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1310                return NULL;
1311        return chan;
1312}
1313
1314static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1315{
1316        struct nlattr *nl_modes = nla_nest_start_noflag(msg, attr);
1317        int i;
1318
1319        if (!nl_modes)
1320                goto nla_put_failure;
1321
1322        i = 0;
1323        while (ifmodes) {
1324                if ((ifmodes & 1) && nla_put_flag(msg, i))
1325                        goto nla_put_failure;
1326                ifmodes >>= 1;
1327                i++;
1328        }
1329
1330        nla_nest_end(msg, nl_modes);
1331        return 0;
1332
1333nla_put_failure:
1334        return -ENOBUFS;
1335}
1336
1337static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1338                                          struct sk_buff *msg,
1339                                          bool large)
1340{
1341        struct nlattr *nl_combis;
1342        int i, j;
1343
1344        nl_combis = nla_nest_start_noflag(msg,
1345                                          NL80211_ATTR_INTERFACE_COMBINATIONS);
1346        if (!nl_combis)
1347                goto nla_put_failure;
1348
1349        for (i = 0; i < wiphy->n_iface_combinations; i++) {
1350                const struct ieee80211_iface_combination *c;
1351                struct nlattr *nl_combi, *nl_limits;
1352
1353                c = &wiphy->iface_combinations[i];
1354
1355                nl_combi = nla_nest_start_noflag(msg, i + 1);
1356                if (!nl_combi)
1357                        goto nla_put_failure;
1358
1359                nl_limits = nla_nest_start_noflag(msg,
1360                                                  NL80211_IFACE_COMB_LIMITS);
1361                if (!nl_limits)
1362                        goto nla_put_failure;
1363
1364                for (j = 0; j < c->n_limits; j++) {
1365                        struct nlattr *nl_limit;
1366
1367                        nl_limit = nla_nest_start_noflag(msg, j + 1);
1368                        if (!nl_limit)
1369                                goto nla_put_failure;
1370                        if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1371                                        c->limits[j].max))
1372                                goto nla_put_failure;
1373                        if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1374                                                c->limits[j].types))
1375                                goto nla_put_failure;
1376                        nla_nest_end(msg, nl_limit);
1377                }
1378
1379                nla_nest_end(msg, nl_limits);
1380
1381                if (c->beacon_int_infra_match &&
1382                    nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1383                        goto nla_put_failure;
1384                if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1385                                c->num_different_channels) ||
1386                    nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1387                                c->max_interfaces))
1388                        goto nla_put_failure;
1389                if (large &&
1390                    (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1391                                c->radar_detect_widths) ||
1392                     nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1393                                c->radar_detect_regions)))
1394                        goto nla_put_failure;
1395                if (c->beacon_int_min_gcd &&
1396                    nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1397                                c->beacon_int_min_gcd))
1398                        goto nla_put_failure;
1399
1400                nla_nest_end(msg, nl_combi);
1401        }
1402
1403        nla_nest_end(msg, nl_combis);
1404
1405        return 0;
1406nla_put_failure:
1407        return -ENOBUFS;
1408}
1409
1410#ifdef CONFIG_PM
1411static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1412                                        struct sk_buff *msg)
1413{
1414        const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1415        struct nlattr *nl_tcp;
1416
1417        if (!tcp)
1418                return 0;
1419
1420        nl_tcp = nla_nest_start_noflag(msg,
1421                                       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1422        if (!nl_tcp)
1423                return -ENOBUFS;
1424
1425        if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1426                        tcp->data_payload_max))
1427                return -ENOBUFS;
1428
1429        if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1430                        tcp->data_payload_max))
1431                return -ENOBUFS;
1432
1433        if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1434                return -ENOBUFS;
1435
1436        if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1437                                sizeof(*tcp->tok), tcp->tok))
1438                return -ENOBUFS;
1439
1440        if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1441                        tcp->data_interval_max))
1442                return -ENOBUFS;
1443
1444        if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1445                        tcp->wake_payload_max))
1446                return -ENOBUFS;
1447
1448        nla_nest_end(msg, nl_tcp);
1449        return 0;
1450}
1451
1452static int nl80211_send_wowlan(struct sk_buff *msg,
1453                               struct cfg80211_registered_device *rdev,
1454                               bool large)
1455{
1456        struct nlattr *nl_wowlan;
1457
1458        if (!rdev->wiphy.wowlan)
1459                return 0;
1460
1461        nl_wowlan = nla_nest_start_noflag(msg,
1462                                          NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1463        if (!nl_wowlan)
1464                return -ENOBUFS;
1465
1466        if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1467             nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1468            ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1469             nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1470            ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1471             nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1472            ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1473             nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1474            ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1475             nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1476            ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1477             nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1478            ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1479             nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1480            ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1481             nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1482                return -ENOBUFS;
1483
1484        if (rdev->wiphy.wowlan->n_patterns) {
1485                struct nl80211_pattern_support pat = {
1486                        .max_patterns = rdev->wiphy.wowlan->n_patterns,
1487                        .min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1488                        .max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1489                        .max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1490                };
1491
1492                if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1493                            sizeof(pat), &pat))
1494                        return -ENOBUFS;
1495        }
1496
1497        if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1498            nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1499                        rdev->wiphy.wowlan->max_nd_match_sets))
1500                return -ENOBUFS;
1501
1502        if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1503                return -ENOBUFS;
1504
1505        nla_nest_end(msg, nl_wowlan);
1506
1507        return 0;
1508}
1509#endif
1510
1511static int nl80211_send_coalesce(struct sk_buff *msg,
1512                                 struct cfg80211_registered_device *rdev)
1513{
1514        struct nl80211_coalesce_rule_support rule;
1515
1516        if (!rdev->wiphy.coalesce)
1517                return 0;
1518
1519        rule.max_rules = rdev->wiphy.coalesce->n_rules;
1520        rule.max_delay = rdev->wiphy.coalesce->max_delay;
1521        rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1522        rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1523        rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1524        rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1525
1526        if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1527                return -ENOBUFS;
1528
1529        return 0;
1530}
1531
1532static int
1533nl80211_send_iftype_data(struct sk_buff *msg,
1534                         const struct ieee80211_sband_iftype_data *iftdata)
1535{
1536        const struct ieee80211_sta_he_cap *he_cap = &iftdata->he_cap;
1537
1538        if (nl80211_put_iftypes(msg, NL80211_BAND_IFTYPE_ATTR_IFTYPES,
1539                                iftdata->types_mask))
1540                return -ENOBUFS;
1541
1542        if (he_cap->has_he) {
1543                if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC,
1544                            sizeof(he_cap->he_cap_elem.mac_cap_info),
1545                            he_cap->he_cap_elem.mac_cap_info) ||
1546                    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY,
1547                            sizeof(he_cap->he_cap_elem.phy_cap_info),
1548                            he_cap->he_cap_elem.phy_cap_info) ||
1549                    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET,
1550                            sizeof(he_cap->he_mcs_nss_supp),
1551                            &he_cap->he_mcs_nss_supp) ||
1552                    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE,
1553                            sizeof(he_cap->ppe_thres), he_cap->ppe_thres))
1554                        return -ENOBUFS;
1555        }
1556
1557        return 0;
1558}
1559
1560static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1561                                      struct ieee80211_supported_band *sband)
1562{
1563        struct nlattr *nl_rates, *nl_rate;
1564        struct ieee80211_rate *rate;
1565        int i;
1566
1567        /* add HT info */
1568        if (sband->ht_cap.ht_supported &&
1569            (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1570                     sizeof(sband->ht_cap.mcs),
1571                     &sband->ht_cap.mcs) ||
1572             nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1573                         sband->ht_cap.cap) ||
1574             nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1575                        sband->ht_cap.ampdu_factor) ||
1576             nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1577                        sband->ht_cap.ampdu_density)))
1578                return -ENOBUFS;
1579
1580        /* add VHT info */
1581        if (sband->vht_cap.vht_supported &&
1582            (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1583                     sizeof(sband->vht_cap.vht_mcs),
1584                     &sband->vht_cap.vht_mcs) ||
1585             nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1586                         sband->vht_cap.cap)))
1587                return -ENOBUFS;
1588
1589        if (sband->n_iftype_data) {
1590                struct nlattr *nl_iftype_data =
1591                        nla_nest_start_noflag(msg,
1592                                              NL80211_BAND_ATTR_IFTYPE_DATA);
1593                int err;
1594
1595                if (!nl_iftype_data)
1596                        return -ENOBUFS;
1597
1598                for (i = 0; i < sband->n_iftype_data; i++) {
1599                        struct nlattr *iftdata;
1600
1601                        iftdata = nla_nest_start_noflag(msg, i + 1);
1602                        if (!iftdata)
1603                                return -ENOBUFS;
1604
1605                        err = nl80211_send_iftype_data(msg,
1606                                                       &sband->iftype_data[i]);
1607                        if (err)
1608                                return err;
1609
1610                        nla_nest_end(msg, iftdata);
1611                }
1612
1613                nla_nest_end(msg, nl_iftype_data);
1614        }
1615
1616        /* add EDMG info */
1617        if (sband->edmg_cap.channels &&
1618            (nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_CHANNELS,
1619                       sband->edmg_cap.channels) ||
1620            nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_BW_CONFIG,
1621                       sband->edmg_cap.bw_config)))
1622
1623                return -ENOBUFS;
1624
1625        /* add bitrates */
1626        nl_rates = nla_nest_start_noflag(msg, NL80211_BAND_ATTR_RATES);
1627        if (!nl_rates)
1628                return -ENOBUFS;
1629
1630        for (i = 0; i < sband->n_bitrates; i++) {
1631                nl_rate = nla_nest_start_noflag(msg, i);
1632                if (!nl_rate)
1633                        return -ENOBUFS;
1634
1635                rate = &sband->bitrates[i];
1636                if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1637                                rate->bitrate))
1638                        return -ENOBUFS;
1639                if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1640                    nla_put_flag(msg,
1641                                 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1642                        return -ENOBUFS;
1643
1644                nla_nest_end(msg, nl_rate);
1645        }
1646
1647        nla_nest_end(msg, nl_rates);
1648
1649        return 0;
1650}
1651
1652static int
1653nl80211_send_mgmt_stypes(struct sk_buff *msg,
1654                         const struct ieee80211_txrx_stypes *mgmt_stypes)
1655{
1656        u16 stypes;
1657        struct nlattr *nl_ftypes, *nl_ifs;
1658        enum nl80211_iftype ift;
1659        int i;
1660
1661        if (!mgmt_stypes)
1662                return 0;
1663
1664        nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_TX_FRAME_TYPES);
1665        if (!nl_ifs)
1666                return -ENOBUFS;
1667
1668        for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1669                nl_ftypes = nla_nest_start_noflag(msg, ift);
1670                if (!nl_ftypes)
1671                        return -ENOBUFS;
1672                i = 0;
1673                stypes = mgmt_stypes[ift].tx;
1674                while (stypes) {
1675                        if ((stypes & 1) &&
1676                            nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1677                                        (i << 4) | IEEE80211_FTYPE_MGMT))
1678                                return -ENOBUFS;
1679                        stypes >>= 1;
1680                        i++;
1681                }
1682                nla_nest_end(msg, nl_ftypes);
1683        }
1684
1685        nla_nest_end(msg, nl_ifs);
1686
1687        nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_RX_FRAME_TYPES);
1688        if (!nl_ifs)
1689                return -ENOBUFS;
1690
1691        for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1692                nl_ftypes = nla_nest_start_noflag(msg, ift);
1693                if (!nl_ftypes)
1694                        return -ENOBUFS;
1695                i = 0;
1696                stypes = mgmt_stypes[ift].rx;
1697                while (stypes) {
1698                        if ((stypes & 1) &&
1699                            nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1700                                        (i << 4) | IEEE80211_FTYPE_MGMT))
1701                                return -ENOBUFS;
1702                        stypes >>= 1;
1703                        i++;
1704                }
1705                nla_nest_end(msg, nl_ftypes);
1706        }
1707        nla_nest_end(msg, nl_ifs);
1708
1709        return 0;
1710}
1711
1712#define CMD(op, n)                                                      \
1713         do {                                                           \
1714                if (rdev->ops->op) {                                    \
1715                        i++;                                            \
1716                        if (nla_put_u32(msg, i, NL80211_CMD_ ## n))     \
1717                                goto nla_put_failure;                   \
1718                }                                                       \
1719        } while (0)
1720
1721static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
1722                                        struct sk_buff *msg)
1723{
1724        int i = 0;
1725
1726        /*
1727         * do *NOT* add anything into this function, new things need to be
1728         * advertised only to new versions of userspace that can deal with
1729         * the split (and they can't possibly care about new features...
1730         */
1731        CMD(add_virtual_intf, NEW_INTERFACE);
1732        CMD(change_virtual_intf, SET_INTERFACE);
1733        CMD(add_key, NEW_KEY);
1734        CMD(start_ap, START_AP);
1735        CMD(add_station, NEW_STATION);
1736        CMD(add_mpath, NEW_MPATH);
1737        CMD(update_mesh_config, SET_MESH_CONFIG);
1738        CMD(change_bss, SET_BSS);
1739        CMD(auth, AUTHENTICATE);
1740        CMD(assoc, ASSOCIATE);
1741        CMD(deauth, DEAUTHENTICATE);
1742        CMD(disassoc, DISASSOCIATE);
1743        CMD(join_ibss, JOIN_IBSS);
1744        CMD(join_mesh, JOIN_MESH);
1745        CMD(set_pmksa, SET_PMKSA);
1746        CMD(del_pmksa, DEL_PMKSA);
1747        CMD(flush_pmksa, FLUSH_PMKSA);
1748        if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1749                CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1750        CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1751        CMD(mgmt_tx, FRAME);
1752        CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1753        if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1754                i++;
1755                if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1756                        goto nla_put_failure;
1757        }
1758        if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1759            rdev->ops->join_mesh) {
1760                i++;
1761                if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1762                        goto nla_put_failure;
1763        }
1764        CMD(set_wds_peer, SET_WDS_PEER);
1765        if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1766                CMD(tdls_mgmt, TDLS_MGMT);
1767                CMD(tdls_oper, TDLS_OPER);
1768        }
1769        if (rdev->wiphy.max_sched_scan_reqs)
1770                CMD(sched_scan_start, START_SCHED_SCAN);
1771        CMD(probe_client, PROBE_CLIENT);
1772        CMD(set_noack_map, SET_NOACK_MAP);
1773        if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1774                i++;
1775                if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1776                        goto nla_put_failure;
1777        }
1778        CMD(start_p2p_device, START_P2P_DEVICE);
1779        CMD(set_mcast_rate, SET_MCAST_RATE);
1780#ifdef CONFIG_NL80211_TESTMODE
1781        CMD(testmode_cmd, TESTMODE);
1782#endif
1783
1784        if (rdev->ops->connect || rdev->ops->auth) {
1785                i++;
1786                if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1787                        goto nla_put_failure;
1788        }
1789
1790        if (rdev->ops->disconnect || rdev->ops->deauth) {
1791                i++;
1792                if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1793                        goto nla_put_failure;
1794        }
1795
1796        return i;
1797 nla_put_failure:
1798        return -ENOBUFS;
1799}
1800
1801static int
1802nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities *cap,
1803                           struct sk_buff *msg)
1804{
1805        struct nlattr *ftm;
1806
1807        if (!cap->ftm.supported)
1808                return 0;
1809
1810        ftm = nla_nest_start_noflag(msg, NL80211_PMSR_TYPE_FTM);
1811        if (!ftm)
1812                return -ENOBUFS;
1813
1814        if (cap->ftm.asap && nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_ASAP))
1815                return -ENOBUFS;
1816        if (cap->ftm.non_asap &&
1817            nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP))
1818                return -ENOBUFS;
1819        if (cap->ftm.request_lci &&
1820            nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI))
1821                return -ENOBUFS;
1822        if (cap->ftm.request_civicloc &&
1823            nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC))
1824                return -ENOBUFS;
1825        if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES,
1826                        cap->ftm.preambles))
1827                return -ENOBUFS;
1828        if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS,
1829                        cap->ftm.bandwidths))
1830                return -ENOBUFS;
1831        if (cap->ftm.max_bursts_exponent >= 0 &&
1832            nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT,
1833                        cap->ftm.max_bursts_exponent))
1834                return -ENOBUFS;
1835        if (cap->ftm.max_ftms_per_burst &&
1836            nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST,
1837                        cap->ftm.max_ftms_per_burst))
1838                return -ENOBUFS;
1839
1840        nla_nest_end(msg, ftm);
1841        return 0;
1842}
1843
1844static int nl80211_send_pmsr_capa(struct cfg80211_registered_device *rdev,
1845                                  struct sk_buff *msg)
1846{
1847        const struct cfg80211_pmsr_capabilities *cap = rdev->wiphy.pmsr_capa;
1848        struct nlattr *pmsr, *caps;
1849
1850        if (!cap)
1851                return 0;
1852
1853        /*
1854         * we don't need to clean up anything here since the caller
1855         * will genlmsg_cancel() if we fail
1856         */
1857
1858        pmsr = nla_nest_start_noflag(msg, NL80211_ATTR_PEER_MEASUREMENTS);
1859        if (!pmsr)
1860                return -ENOBUFS;
1861
1862        if (nla_put_u32(msg, NL80211_PMSR_ATTR_MAX_PEERS, cap->max_peers))
1863                return -ENOBUFS;
1864
1865        if (cap->report_ap_tsf &&
1866            nla_put_flag(msg, NL80211_PMSR_ATTR_REPORT_AP_TSF))
1867                return -ENOBUFS;
1868
1869        if (cap->randomize_mac_addr &&
1870            nla_put_flag(msg, NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR))
1871                return -ENOBUFS;
1872
1873        caps = nla_nest_start_noflag(msg, NL80211_PMSR_ATTR_TYPE_CAPA);
1874        if (!caps)
1875                return -ENOBUFS;
1876
1877        if (nl80211_send_pmsr_ftm_capa(cap, msg))
1878                return -ENOBUFS;
1879
1880        nla_nest_end(msg, caps);
1881        nla_nest_end(msg, pmsr);
1882
1883        return 0;
1884}
1885
1886struct nl80211_dump_wiphy_state {
1887        s64 filter_wiphy;
1888        long start;
1889        long split_start, band_start, chan_start, capa_start;
1890        bool split;
1891};
1892
1893static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
1894                              enum nl80211_commands cmd,
1895                              struct sk_buff *msg, u32 portid, u32 seq,
1896                              int flags, struct nl80211_dump_wiphy_state *state)
1897{
1898        void *hdr;
1899        struct nlattr *nl_bands, *nl_band;
1900        struct nlattr *nl_freqs, *nl_freq;
1901        struct nlattr *nl_cmds;
1902        enum nl80211_band band;
1903        struct ieee80211_channel *chan;
1904        int i;
1905        const struct ieee80211_txrx_stypes *mgmt_stypes =
1906                                rdev->wiphy.mgmt_stypes;
1907        u32 features;
1908
1909        hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
1910        if (!hdr)
1911                return -ENOBUFS;
1912
1913        if (WARN_ON(!state))
1914                return -EINVAL;
1915
1916        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1917            nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1918                           wiphy_name(&rdev->wiphy)) ||
1919            nla_put_u32(msg, NL80211_ATTR_GENERATION,
1920                        cfg80211_rdev_list_generation))
1921                goto nla_put_failure;
1922
1923        if (cmd != NL80211_CMD_NEW_WIPHY)
1924                goto finish;
1925
1926        switch (state->split_start) {
1927        case 0:
1928                if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1929                               rdev->wiphy.retry_short) ||
1930                    nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1931                               rdev->wiphy.retry_long) ||
1932                    nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1933                                rdev->wiphy.frag_threshold) ||
1934                    nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1935                                rdev->wiphy.rts_threshold) ||
1936                    nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1937                               rdev->wiphy.coverage_class) ||
1938                    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1939                               rdev->wiphy.max_scan_ssids) ||
1940                    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1941                               rdev->wiphy.max_sched_scan_ssids) ||
1942                    nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1943                                rdev->wiphy.max_scan_ie_len) ||
1944                    nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1945                                rdev->wiphy.max_sched_scan_ie_len) ||
1946                    nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1947                               rdev->wiphy.max_match_sets) ||
1948                    nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
1949                                rdev->wiphy.max_sched_scan_plans) ||
1950                    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
1951                                rdev->wiphy.max_sched_scan_plan_interval) ||
1952                    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
1953                                rdev->wiphy.max_sched_scan_plan_iterations))
1954                        goto nla_put_failure;
1955
1956                if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1957                    nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1958                        goto nla_put_failure;
1959                if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1960                    nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1961                        goto nla_put_failure;
1962                if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1963                    nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1964                        goto nla_put_failure;
1965                if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1966                    nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1967                        goto nla_put_failure;
1968                if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1969                    nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1970                        goto nla_put_failure;
1971                if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1972                    nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
1973                        goto nla_put_failure;
1974                state->split_start++;
1975                if (state->split)
1976                        break;
1977                /* fall through */
1978        case 1:
1979                if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1980                            sizeof(u32) * rdev->wiphy.n_cipher_suites,
1981                            rdev->wiphy.cipher_suites))
1982                        goto nla_put_failure;
1983
1984                if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1985                               rdev->wiphy.max_num_pmkids))
1986                        goto nla_put_failure;
1987
1988                if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1989                    nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1990                        goto nla_put_failure;
1991
1992                if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1993                                rdev->wiphy.available_antennas_tx) ||
1994                    nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1995                                rdev->wiphy.available_antennas_rx))
1996                        goto nla_put_failure;
1997
1998                if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1999                    nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
2000                                rdev->wiphy.probe_resp_offload))
2001                        goto nla_put_failure;
2002
2003                if ((rdev->wiphy.available_antennas_tx ||
2004                     rdev->wiphy.available_antennas_rx) &&
2005                    rdev->ops->get_antenna) {
2006                        u32 tx_ant = 0, rx_ant = 0;
2007                        int res;
2008
2009                        res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
2010                        if (!res) {
2011                                if (nla_put_u32(msg,
2012                                                NL80211_ATTR_WIPHY_ANTENNA_TX,
2013                                                tx_ant) ||
2014                                    nla_put_u32(msg,
2015                                                NL80211_ATTR_WIPHY_ANTENNA_RX,
2016                                                rx_ant))
2017                                        goto nla_put_failure;
2018                        }
2019                }
2020
2021                state->split_start++;
2022                if (state->split)
2023                        break;
2024                /* fall through */
2025        case 2:
2026                if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
2027                                        rdev->wiphy.interface_modes))
2028                                goto nla_put_failure;
2029                state->split_start++;
2030                if (state->split)
2031                        break;
2032                /* fall through */
2033        case 3:
2034                nl_bands = nla_nest_start_noflag(msg,
2035                                                 NL80211_ATTR_WIPHY_BANDS);
2036                if (!nl_bands)
2037                        goto nla_put_failure;
2038
2039                for (band = state->band_start;
2040                     band < NUM_NL80211_BANDS; band++) {
2041                        struct ieee80211_supported_band *sband;
2042
2043                        sband = rdev->wiphy.bands[band];
2044
2045                        if (!sband)
2046                                continue;
2047
2048                        nl_band = nla_nest_start_noflag(msg, band);
2049                        if (!nl_band)
2050                                goto nla_put_failure;
2051
2052                        switch (state->chan_start) {
2053                        case 0:
2054                                if (nl80211_send_band_rateinfo(msg, sband))
2055                                        goto nla_put_failure;
2056                                state->chan_start++;
2057                                if (state->split)
2058                                        break;
2059                                /* fall through */
2060                        default:
2061                                /* add frequencies */
2062                                nl_freqs = nla_nest_start_noflag(msg,
2063                                                                 NL80211_BAND_ATTR_FREQS);
2064                                if (!nl_freqs)
2065                                        goto nla_put_failure;
2066
2067                                for (i = state->chan_start - 1;
2068                                     i < sband->n_channels;
2069                                     i++) {
2070                                        nl_freq = nla_nest_start_noflag(msg,
2071                                                                        i);
2072                                        if (!nl_freq)
2073                                                goto nla_put_failure;
2074
2075                                        chan = &sband->channels[i];
2076
2077                                        if (nl80211_msg_put_channel(
2078                                                        msg, &rdev->wiphy, chan,
2079                                                        state->split))
2080                                                goto nla_put_failure;
2081
2082                                        nla_nest_end(msg, nl_freq);
2083                                        if (state->split)
2084                                                break;
2085                                }
2086                                if (i < sband->n_channels)
2087                                        state->chan_start = i + 2;
2088                                else
2089                                        state->chan_start = 0;
2090                                nla_nest_end(msg, nl_freqs);
2091                        }
2092
2093                        nla_nest_end(msg, nl_band);
2094
2095                        if (state->split) {
2096                                /* start again here */
2097                                if (state->chan_start)
2098                                        band--;
2099                                break;
2100                        }
2101                }
2102                nla_nest_end(msg, nl_bands);
2103
2104                if (band < NUM_NL80211_BANDS)
2105                        state->band_start = band + 1;
2106                else
2107                        state->band_start = 0;
2108
2109                /* if bands & channels are done, continue outside */
2110                if (state->band_start == 0 && state->chan_start == 0)
2111                        state->split_start++;
2112                if (state->split)
2113                        break;
2114                /* fall through */
2115        case 4:
2116                nl_cmds = nla_nest_start_noflag(msg,
2117                                                NL80211_ATTR_SUPPORTED_COMMANDS);
2118                if (!nl_cmds)
2119                        goto nla_put_failure;
2120
2121                i = nl80211_add_commands_unsplit(rdev, msg);
2122                if (i < 0)
2123                        goto nla_put_failure;
2124                if (state->split) {
2125                        CMD(crit_proto_start, CRIT_PROTOCOL_START);
2126                        CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
2127                        if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
2128                                CMD(channel_switch, CHANNEL_SWITCH);
2129                        CMD(set_qos_map, SET_QOS_MAP);
2130                        if (rdev->wiphy.features &
2131                                        NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
2132                                CMD(add_tx_ts, ADD_TX_TS);
2133                        CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
2134                        CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
2135                        CMD(update_ft_ies, UPDATE_FT_IES);
2136                }
2137#undef CMD
2138
2139                nla_nest_end(msg, nl_cmds);
2140                state->split_start++;
2141                if (state->split)
2142                        break;
2143                /* fall through */
2144        case 5:
2145                if (rdev->ops->remain_on_channel &&
2146                    (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
2147                    nla_put_u32(msg,
2148                                NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
2149                                rdev->wiphy.max_remain_on_channel_duration))
2150                        goto nla_put_failure;
2151
2152                if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
2153                    nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
2154                        goto nla_put_failure;
2155
2156                if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
2157                        goto nla_put_failure;
2158                state->split_start++;
2159                if (state->split)
2160                        break;
2161                /* fall through */
2162        case 6:
2163#ifdef CONFIG_PM
2164                if (nl80211_send_wowlan(msg, rdev, state->split))
2165                        goto nla_put_failure;
2166                state->split_start++;
2167                if (state->split)
2168                        break;
2169#else
2170                state->split_start++;
2171#endif
2172                /* fall through */
2173        case 7:
2174                if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
2175                                        rdev->wiphy.software_iftypes))
2176                        goto nla_put_failure;
2177
2178                if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
2179                                                   state->split))
2180                        goto nla_put_failure;
2181
2182                state->split_start++;
2183                if (state->split)
2184                        break;
2185                /* fall through */
2186        case 8:
2187                if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
2188                    nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
2189                                rdev->wiphy.ap_sme_capa))
2190                        goto nla_put_failure;
2191
2192                features = rdev->wiphy.features;
2193                /*
2194                 * We can only add the per-channel limit information if the
2195                 * dump is split, otherwise it makes it too big. Therefore
2196                 * only advertise it in that case.
2197                 */
2198                if (state->split)
2199                        features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
2200                if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
2201                        goto nla_put_failure;
2202
2203                if (rdev->wiphy.ht_capa_mod_mask &&
2204                    nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
2205                            sizeof(*rdev->wiphy.ht_capa_mod_mask),
2206                            rdev->wiphy.ht_capa_mod_mask))
2207                        goto nla_put_failure;
2208
2209                if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
2210                    rdev->wiphy.max_acl_mac_addrs &&
2211                    nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
2212                                rdev->wiphy.max_acl_mac_addrs))
2213                        goto nla_put_failure;
2214
2215                /*
2216                 * Any information below this point is only available to
2217                 * applications that can deal with it being split. This
2218                 * helps ensure that newly added capabilities don't break
2219                 * older tools by overrunning their buffers.
2220                 *
2221                 * We still increment split_start so that in the split
2222                 * case we'll continue with more data in the next round,
2223                 * but break unconditionally so unsplit data stops here.
2224                 */
2225                state->split_start++;
2226                break;
2227        case 9:
2228                if (rdev->wiphy.extended_capabilities &&
2229                    (nla_put(msg, NL80211_ATTR_EXT_CAPA,
2230                             rdev->wiphy.extended_capabilities_len,
2231                             rdev->wiphy.extended_capabilities) ||
2232                     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2233                             rdev->wiphy.extended_capabilities_len,
2234                             rdev->wiphy.extended_capabilities_mask)))
2235                        goto nla_put_failure;
2236
2237                if (rdev->wiphy.vht_capa_mod_mask &&
2238                    nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
2239                            sizeof(*rdev->wiphy.vht_capa_mod_mask),
2240                            rdev->wiphy.vht_capa_mod_mask))
2241                        goto nla_put_failure;
2242
2243                if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
2244                            rdev->wiphy.perm_addr))
2245                        goto nla_put_failure;
2246
2247                if (!is_zero_ether_addr(rdev->wiphy.addr_mask) &&
2248                    nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
2249                            rdev->wiphy.addr_mask))
2250                        goto nla_put_failure;
2251
2252                if (rdev->wiphy.n_addresses > 1) {
2253                        void *attr;
2254
2255                        attr = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
2256                        if (!attr)
2257                                goto nla_put_failure;
2258
2259                        for (i = 0; i < rdev->wiphy.n_addresses; i++)
2260                                if (nla_put(msg, i + 1, ETH_ALEN,
2261                                            rdev->wiphy.addresses[i].addr))
2262                                        goto nla_put_failure;
2263
2264                        nla_nest_end(msg, attr);
2265                }
2266
2267                state->split_start++;
2268                break;
2269        case 10:
2270                if (nl80211_send_coalesce(msg, rdev))
2271                        goto nla_put_failure;
2272
2273                if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
2274                    (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
2275                     nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
2276                        goto nla_put_failure;
2277
2278                if (rdev->wiphy.max_ap_assoc_sta &&
2279                    nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
2280                                rdev->wiphy.max_ap_assoc_sta))
2281                        goto nla_put_failure;
2282
2283                state->split_start++;
2284                break;
2285        case 11:
2286                if (rdev->wiphy.n_vendor_commands) {
2287                        const struct nl80211_vendor_cmd_info *info;
2288                        struct nlattr *nested;
2289
2290                        nested = nla_nest_start_noflag(msg,
2291                                                       NL80211_ATTR_VENDOR_DATA);
2292                        if (!nested)
2293                                goto nla_put_failure;
2294
2295                        for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
2296                                info = &rdev->wiphy.vendor_commands[i].info;
2297                                if (nla_put(msg, i + 1, sizeof(*info), info))
2298                                        goto nla_put_failure;
2299                        }
2300                        nla_nest_end(msg, nested);
2301                }
2302
2303                if (rdev->wiphy.n_vendor_events) {
2304                        const struct nl80211_vendor_cmd_info *info;
2305                        struct nlattr *nested;
2306
2307                        nested = nla_nest_start_noflag(msg,
2308                                                       NL80211_ATTR_VENDOR_EVENTS);
2309                        if (!nested)
2310                                goto nla_put_failure;
2311
2312                        for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
2313                                info = &rdev->wiphy.vendor_events[i];
2314                                if (nla_put(msg, i + 1, sizeof(*info), info))
2315                                        goto nla_put_failure;
2316                        }
2317                        nla_nest_end(msg, nested);
2318                }
2319                state->split_start++;
2320                break;
2321        case 12:
2322                if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
2323                    nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
2324                               rdev->wiphy.max_num_csa_counters))
2325                        goto nla_put_failure;
2326
2327                if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
2328                    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
2329                        goto nla_put_failure;
2330
2331                if (rdev->wiphy.max_sched_scan_reqs &&
2332                    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
2333                                rdev->wiphy.max_sched_scan_reqs))
2334                        goto nla_put_failure;
2335
2336                if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
2337                            sizeof(rdev->wiphy.ext_features),
2338                            rdev->wiphy.ext_features))
2339                        goto nla_put_failure;
2340
2341                if (rdev->wiphy.bss_select_support) {
2342                        struct nlattr *nested;
2343                        u32 bss_select_support = rdev->wiphy.bss_select_support;
2344
2345                        nested = nla_nest_start_noflag(msg,
2346                                                       NL80211_ATTR_BSS_SELECT);
2347                        if (!nested)
2348                                goto nla_put_failure;
2349
2350                        i = 0;
2351                        while (bss_select_support) {
2352                                if ((bss_select_support & 1) &&
2353                                    nla_put_flag(msg, i))
2354                                        goto nla_put_failure;
2355                                i++;
2356                                bss_select_support >>= 1;
2357                        }
2358                        nla_nest_end(msg, nested);
2359                }
2360
2361                state->split_start++;
2362                break;
2363        case 13:
2364                if (rdev->wiphy.num_iftype_ext_capab &&
2365                    rdev->wiphy.iftype_ext_capab) {
2366                        struct nlattr *nested_ext_capab, *nested;
2367
2368                        nested = nla_nest_start_noflag(msg,
2369                                                       NL80211_ATTR_IFTYPE_EXT_CAPA);
2370                        if (!nested)
2371                                goto nla_put_failure;
2372
2373                        for (i = state->capa_start;
2374                             i < rdev->wiphy.num_iftype_ext_capab; i++) {
2375                                const struct wiphy_iftype_ext_capab *capab;
2376
2377                                capab = &rdev->wiphy.iftype_ext_capab[i];
2378
2379                                nested_ext_capab = nla_nest_start_noflag(msg,
2380                                                                         i);
2381                                if (!nested_ext_capab ||
2382                                    nla_put_u32(msg, NL80211_ATTR_IFTYPE,
2383                                                capab->iftype) ||
2384                                    nla_put(msg, NL80211_ATTR_EXT_CAPA,
2385                                            capab->extended_capabilities_len,
2386                                            capab->extended_capabilities) ||
2387                                    nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2388                                            capab->extended_capabilities_len,
2389                                            capab->extended_capabilities_mask))
2390                                        goto nla_put_failure;
2391
2392                                nla_nest_end(msg, nested_ext_capab);
2393                                if (state->split)
2394                                        break;
2395                        }
2396                        nla_nest_end(msg, nested);
2397                        if (i < rdev->wiphy.num_iftype_ext_capab) {
2398                                state->capa_start = i + 1;
2399                                break;
2400                        }
2401                }
2402
2403                if (nla_put_u32(msg, NL80211_ATTR_BANDS,
2404                                rdev->wiphy.nan_supported_bands))
2405                        goto nla_put_failure;
2406
2407                if (wiphy_ext_feature_isset(&rdev->wiphy,
2408                                            NL80211_EXT_FEATURE_TXQS)) {
2409                        struct cfg80211_txq_stats txqstats = {};
2410                        int res;
2411
2412                        res = rdev_get_txq_stats(rdev, NULL, &txqstats);
2413                        if (!res &&
2414                            !nl80211_put_txq_stats(msg, &txqstats,
2415                                                   NL80211_ATTR_TXQ_STATS))
2416                                goto nla_put_failure;
2417
2418                        if (nla_put_u32(msg, NL80211_ATTR_TXQ_LIMIT,
2419                                        rdev->wiphy.txq_limit))
2420                                goto nla_put_failure;
2421                        if (nla_put_u32(msg, NL80211_ATTR_TXQ_MEMORY_LIMIT,
2422                                        rdev->wiphy.txq_memory_limit))
2423                                goto nla_put_failure;
2424                        if (nla_put_u32(msg, NL80211_ATTR_TXQ_QUANTUM,
2425                                        rdev->wiphy.txq_quantum))
2426                                goto nla_put_failure;
2427                }
2428
2429                state->split_start++;
2430                break;
2431        case 14:
2432                if (nl80211_send_pmsr_capa(rdev, msg))
2433                        goto nla_put_failure;
2434
2435                state->split_start++;
2436                break;
2437        case 15:
2438                if (rdev->wiphy.akm_suites &&
2439                    nla_put(msg, NL80211_ATTR_AKM_SUITES,
2440                            sizeof(u32) * rdev->wiphy.n_akm_suites,
2441                            rdev->wiphy.akm_suites))
2442                        goto nla_put_failure;
2443
2444                /* done */
2445                state->split_start = 0;
2446                break;
2447        }
2448 finish:
2449        genlmsg_end(msg, hdr);
2450        return 0;
2451
2452 nla_put_failure:
2453        genlmsg_cancel(msg, hdr);
2454        return -EMSGSIZE;
2455}
2456
2457static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
2458                                    struct netlink_callback *cb,
2459                                    struct nl80211_dump_wiphy_state *state)
2460{
2461        struct nlattr **tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
2462        int ret;
2463
2464        if (!tb)
2465                return -ENOMEM;
2466
2467        ret = nlmsg_parse_deprecated(cb->nlh,
2468                                     GENL_HDRLEN + nl80211_fam.hdrsize,
2469                                     tb, nl80211_fam.maxattr,
2470                                     nl80211_policy, NULL);
2471        /* ignore parse errors for backward compatibility */
2472        if (ret) {
2473                ret = 0;
2474                goto out;
2475        }
2476
2477        state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
2478        if (tb[NL80211_ATTR_WIPHY])
2479                state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2480        if (tb[NL80211_ATTR_WDEV])
2481                state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
2482        if (tb[NL80211_ATTR_IFINDEX]) {
2483                struct net_device *netdev;
2484                struct cfg80211_registered_device *rdev;
2485                int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2486
2487                netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
2488                if (!netdev) {
2489                        ret = -ENODEV;
2490                        goto out;
2491                }
2492                if (netdev->ieee80211_ptr) {
2493                        rdev = wiphy_to_rdev(
2494                                netdev->ieee80211_ptr->wiphy);
2495                        state->filter_wiphy = rdev->wiphy_idx;
2496                }
2497        }
2498
2499        ret = 0;
2500out:
2501        kfree(tb);
2502        return ret;
2503}
2504
2505static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
2506{
2507        int idx = 0, ret;
2508        struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
2509        struct cfg80211_registered_device *rdev;
2510
2511        rtnl_lock();
2512        if (!state) {
2513                state = kzalloc(sizeof(*state), GFP_KERNEL);
2514                if (!state) {
2515                        rtnl_unlock();
2516                        return -ENOMEM;
2517                }
2518                state->filter_wiphy = -1;
2519                ret = nl80211_dump_wiphy_parse(skb, cb, state);
2520                if (ret) {
2521                        kfree(state);
2522                        rtnl_unlock();
2523                        return ret;
2524                }
2525                cb->args[0] = (long)state;
2526        }
2527
2528        list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2529                if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2530                        continue;
2531                if (++idx <= state->start)
2532                        continue;
2533                if (state->filter_wiphy != -1 &&
2534                    state->filter_wiphy != rdev->wiphy_idx)
2535                        continue;
2536                /* attempt to fit multiple wiphy data chunks into the skb */
2537                do {
2538                        ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
2539                                                 skb,
2540                                                 NETLINK_CB(cb->skb).portid,
2541                                                 cb->nlh->nlmsg_seq,
2542                                                 NLM_F_MULTI, state);
2543                        if (ret < 0) {
2544                                /*
2545                                 * If sending the wiphy data didn't fit (ENOBUFS
2546                                 * or EMSGSIZE returned), this SKB is still
2547                                 * empty (so it's not too big because another
2548                                 * wiphy dataset is already in the skb) and
2549                                 * we've not tried to adjust the dump allocation
2550                                 * yet ... then adjust the alloc size to be
2551                                 * bigger, and return 1 but with the empty skb.
2552                                 * This results in an empty message being RX'ed
2553                                 * in userspace, but that is ignored.
2554                                 *
2555                                 * We can then retry with the larger buffer.
2556                                 */
2557                                if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
2558                                    !skb->len && !state->split &&
2559                                    cb->min_dump_alloc < 4096) {
2560                                        cb->min_dump_alloc = 4096;
2561                                        state->split_start = 0;
2562                                        rtnl_unlock();
2563                                        return 1;
2564                                }
2565                                idx--;
2566                                break;
2567                        }
2568                } while (state->split_start > 0);
2569                break;
2570        }
2571        rtnl_unlock();
2572
2573        state->start = idx;
2574
2575        return skb->len;
2576}
2577
2578static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
2579{
2580        kfree((void *)cb->args[0]);
2581        return 0;
2582}
2583
2584static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
2585{
2586        struct sk_buff *msg;
2587        struct cfg80211_registered_device *rdev = info->user_ptr[0];
2588        struct nl80211_dump_wiphy_state state = {};
2589
2590        msg = nlmsg_new(4096, GFP_KERNEL);
2591        if (!msg)
2592                return -ENOMEM;
2593
2594        if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
2595                               info->snd_portid, info->snd_seq, 0,
2596                               &state) < 0) {
2597                nlmsg_free(msg);
2598                return -ENOBUFS;
2599        }
2600
2601        return genlmsg_reply(msg, info);
2602}
2603
2604static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
2605        [NL80211_TXQ_ATTR_QUEUE]                = { .type = NLA_U8 },
2606        [NL80211_TXQ_ATTR_TXOP]                 = { .type = NLA_U16 },
2607        [NL80211_TXQ_ATTR_CWMIN]                = { .type = NLA_U16 },
2608        [NL80211_TXQ_ATTR_CWMAX]                = { .type = NLA_U16 },
2609        [NL80211_TXQ_ATTR_AIFS]                 = { .type = NLA_U8 },
2610};
2611
2612static int parse_txq_params(struct nlattr *tb[],
2613                            struct ieee80211_txq_params *txq_params)
2614{
2615        u8 ac;
2616
2617        if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
2618            !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
2619            !tb[NL80211_TXQ_ATTR_AIFS])
2620                return -EINVAL;
2621
2622        ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
2623        txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
2624        txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
2625        txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
2626        txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
2627
2628        if (ac >= NL80211_NUM_ACS)
2629                return -EINVAL;
2630        txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
2631        return 0;
2632}
2633
2634static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
2635{
2636        /*
2637         * You can only set the channel explicitly for WDS interfaces,
2638         * all others have their channel managed via their respective
2639         * "establish a connection" command (connect, join, ...)
2640         *
2641         * For AP/GO and mesh mode, the channel can be set with the
2642         * channel userspace API, but is only stored and passed to the
2643         * low-level driver when the AP starts or the mesh is joined.
2644         * This is for backward compatibility, userspace can also give
2645         * the channel in the start-ap or join-mesh commands instead.
2646         *
2647         * Monitors are special as they are normally slaved to
2648         * whatever else is going on, so they have their own special
2649         * operation to set the monitor channel if possible.
2650         */
2651        return !wdev ||
2652                wdev->iftype == NL80211_IFTYPE_AP ||
2653                wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
2654                wdev->iftype == NL80211_IFTYPE_MONITOR ||
2655                wdev->iftype == NL80211_IFTYPE_P2P_GO;
2656}
2657
2658int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
2659                          struct genl_info *info,
2660                          struct cfg80211_chan_def *chandef)
2661{
2662        struct netlink_ext_ack *extack = info->extack;
2663        struct nlattr **attrs = info->attrs;
2664        u32 control_freq;
2665
2666        if (!attrs[NL80211_ATTR_WIPHY_FREQ])
2667                return -EINVAL;
2668
2669        control_freq = nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ]);
2670
2671        memset(chandef, 0, sizeof(*chandef));
2672
2673        chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
2674        chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
2675        chandef->center_freq1 = control_freq;
2676        chandef->center_freq2 = 0;
2677
2678        /* Primary channel not allowed */
2679        if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED) {
2680                NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
2681                                    "Channel is disabled");
2682                return -EINVAL;
2683        }
2684
2685        if (attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2686                enum nl80211_channel_type chantype;
2687
2688                chantype = nla_get_u32(attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2689
2690                switch (chantype) {
2691                case NL80211_CHAN_NO_HT:
2692                case NL80211_CHAN_HT20:
2693                case NL80211_CHAN_HT40PLUS:
2694                case NL80211_CHAN_HT40MINUS:
2695                        cfg80211_chandef_create(chandef, chandef->chan,
2696                                                chantype);
2697                        /* user input for center_freq is incorrect */
2698                        if (attrs[NL80211_ATTR_CENTER_FREQ1] &&
2699                            chandef->center_freq1 != nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1])) {
2700                                NL_SET_ERR_MSG_ATTR(extack,
2701                                                    attrs[NL80211_ATTR_CENTER_FREQ1],
2702                                                    "bad center frequency 1");
2703                                return -EINVAL;
2704                        }
2705                        /* center_freq2 must be zero */
2706                        if (attrs[NL80211_ATTR_CENTER_FREQ2] &&
2707                            nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2])) {
2708                                NL_SET_ERR_MSG_ATTR(extack,
2709                                                    attrs[NL80211_ATTR_CENTER_FREQ2],
2710                                                    "center frequency 2 can't be used");
2711                                return -EINVAL;
2712                        }
2713                        break;
2714                default:
2715                        NL_SET_ERR_MSG_ATTR(extack,
2716                                            attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
2717                                            "invalid channel type");
2718                        return -EINVAL;
2719                }
2720        } else if (attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
2721                chandef->width =
2722                        nla_get_u32(attrs[NL80211_ATTR_CHANNEL_WIDTH]);
2723                if (attrs[NL80211_ATTR_CENTER_FREQ1])
2724                        chandef->center_freq1 =
2725                                nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1]);
2726                if (attrs[NL80211_ATTR_CENTER_FREQ2])
2727                        chandef->center_freq2 =
2728                                nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2]);
2729        }
2730
2731        if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
2732                chandef->edmg.channels =
2733                      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
2734
2735                if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
2736                        chandef->edmg.bw_config =
2737                     nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
2738        } else {
2739                chandef->edmg.bw_config = 0;
2740                chandef->edmg.channels = 0;
2741        }
2742
2743        if (!cfg80211_chandef_valid(chandef)) {
2744                NL_SET_ERR_MSG(extack, "invalid channel definition");
2745                return -EINVAL;
2746        }
2747
2748        if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
2749                                     IEEE80211_CHAN_DISABLED)) {
2750                NL_SET_ERR_MSG(extack, "(extension) channel is disabled");
2751                return -EINVAL;
2752        }
2753
2754        if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
2755             chandef->width == NL80211_CHAN_WIDTH_10) &&
2756            !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ)) {
2757                NL_SET_ERR_MSG(extack, "5/10 MHz not supported");
2758                return -EINVAL;
2759        }
2760
2761        return 0;
2762}
2763
2764static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
2765                                 struct net_device *dev,
2766                                 struct genl_info *info)
2767{
2768        struct cfg80211_chan_def chandef;
2769        int result;
2770        enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
2771        struct wireless_dev *wdev = NULL;
2772
2773        if (dev)
2774                wdev = dev->ieee80211_ptr;
2775        if (!nl80211_can_set_dev_channel(wdev))
2776                return -EOPNOTSUPP;
2777        if (wdev)
2778                iftype = wdev->iftype;
2779
2780        result = nl80211_parse_chandef(rdev, info, &chandef);
2781        if (result)
2782                return result;
2783
2784        switch (iftype) {
2785        case NL80211_IFTYPE_AP:
2786        case NL80211_IFTYPE_P2P_GO:
2787                if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
2788                                                   iftype)) {
2789                        result = -EINVAL;
2790                        break;
2791                }
2792                if (wdev->beacon_interval) {
2793                        if (!dev || !rdev->ops->set_ap_chanwidth ||
2794                            !(rdev->wiphy.features &
2795                              NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
2796                                result = -EBUSY;
2797                                break;
2798                        }
2799
2800                        /* Only allow dynamic channel width changes */
2801                        if (chandef.chan != wdev->preset_chandef.chan) {
2802                                result = -EBUSY;
2803                                break;
2804                        }
2805                        result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
2806                        if (result)
2807                                break;
2808                }
2809                wdev->preset_chandef = chandef;
2810                result = 0;
2811                break;
2812        case NL80211_IFTYPE_MESH_POINT:
2813                result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
2814                break;
2815        case NL80211_IFTYPE_MONITOR:
2816                result = cfg80211_set_monitor_channel(rdev, &chandef);
2817                break;
2818        default:
2819                result = -EINVAL;
2820        }
2821
2822        return result;
2823}
2824
2825static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
2826{
2827        struct cfg80211_registered_device *rdev = info->user_ptr[0];
2828        struct net_device *netdev = info->user_ptr[1];
2829
2830        return __nl80211_set_channel(rdev, netdev, info);
2831}
2832
2833static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
2834{
2835        struct cfg80211_registered_device *rdev = info->user_ptr[0];
2836        struct net_device *dev = info->user_ptr[1];
2837        struct wireless_dev *wdev = dev->ieee80211_ptr;
2838        const u8 *bssid;
2839
2840        if (!info->attrs[NL80211_ATTR_MAC])
2841                return -EINVAL;
2842
2843        if (netif_running(dev))
2844                return -EBUSY;
2845
2846        if (!rdev->ops->set_wds_peer)
2847                return -EOPNOTSUPP;
2848
2849        if (wdev->iftype != NL80211_IFTYPE_WDS)
2850                return -EOPNOTSUPP;
2851
2852        bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
2853        return rdev_set_wds_peer(rdev, dev, bssid);
2854}
2855
2856static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
2857{
2858        struct cfg80211_registered_device *rdev;
2859        struct net_device *netdev = NULL;
2860        struct wireless_dev *wdev;
2861        int result = 0, rem_txq_params = 0;
2862        struct nlattr *nl_txq_params;
2863        u32 changed;
2864        u8 retry_short = 0, retry_long = 0;
2865        u32 frag_threshold = 0, rts_threshold = 0;
2866        u8 coverage_class = 0;
2867        u32 txq_limit = 0, txq_memory_limit = 0, txq_quantum = 0;
2868
2869        ASSERT_RTNL();
2870
2871        /*
2872         * Try to find the wiphy and netdev. Normally this
2873         * function shouldn't need the netdev, but this is
2874         * done for backward compatibility -- previously
2875         * setting the channel was done per wiphy, but now
2876         * it is per netdev. Previous userland like hostapd
2877         * also passed a netdev to set_wiphy, so that it is
2878         * possible to let that go to the right netdev!
2879         */
2880
2881        if (info->attrs[NL80211_ATTR_IFINDEX]) {
2882                int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2883
2884                netdev = __dev_get_by_index(genl_info_net(info), ifindex);
2885                if (netdev && netdev->ieee80211_ptr)
2886                        rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
2887                else
2888                        netdev = NULL;
2889        }
2890
2891        if (!netdev) {
2892                rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2893                                                  info->attrs);
2894                if (IS_ERR(rdev))
2895                        return PTR_ERR(rdev);
2896                wdev = NULL;
2897                netdev = NULL;
2898                result = 0;
2899        } else
2900                wdev = netdev->ieee80211_ptr;
2901
2902        /*
2903         * end workaround code, by now the rdev is available
2904         * and locked, and wdev may or may not be NULL.
2905         */
2906
2907        if (info->attrs[NL80211_ATTR_WIPHY_NAME])
2908                result = cfg80211_dev_rename(
2909                        rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
2910
2911        if (result)
2912                return result;
2913
2914        if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2915                struct ieee80211_txq_params txq_params;
2916                struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2917
2918                if (!rdev->ops->set_txq_params)
2919                        return -EOPNOTSUPP;
2920
2921                if (!netdev)
2922                        return -EINVAL;
2923
2924                if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2925                    netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2926                        return -EINVAL;
2927
2928                if (!netif_running(netdev))
2929                        return -ENETDOWN;
2930
2931                nla_for_each_nested(nl_txq_params,
2932                                    info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2933                                    rem_txq_params) {
2934                        result = nla_parse_nested_deprecated(tb,
2935                                                             NL80211_TXQ_ATTR_MAX,
2936                                                             nl_txq_params,
2937                                                             txq_params_policy,
2938                                                             info->extack);
2939                        if (result)
2940                                return result;
2941                        result = parse_txq_params(tb, &txq_params);
2942                        if (result)
2943                                return result;
2944
2945                        result = rdev_set_txq_params(rdev, netdev,
2946                                                     &txq_params);
2947                        if (result)
2948                                return result;
2949                }
2950        }
2951
2952        if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2953                result = __nl80211_set_channel(
2954                        rdev,
2955                        nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
2956                        info);
2957                if (result)
2958                        return result;
2959        }
2960
2961        if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
2962                struct wireless_dev *txp_wdev = wdev;
2963                enum nl80211_tx_power_setting type;
2964                int idx, mbm = 0;
2965
2966                if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2967                        txp_wdev = NULL;
2968
2969                if (!rdev->ops->set_tx_power)
2970                        return -EOPNOTSUPP;
2971
2972                idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2973                type = nla_get_u32(info->attrs[idx]);
2974
2975                if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
2976                    (type != NL80211_TX_POWER_AUTOMATIC))
2977                        return -EINVAL;
2978
2979                if (type != NL80211_TX_POWER_AUTOMATIC) {
2980                        idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2981                        mbm = nla_get_u32(info->attrs[idx]);
2982                }
2983
2984                result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
2985                if (result)
2986                        return result;
2987        }
2988
2989        if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2990            info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2991                u32 tx_ant, rx_ant;
2992
2993                if ((!rdev->wiphy.available_antennas_tx &&
2994                     !rdev->wiphy.available_antennas_rx) ||
2995                    !rdev->ops->set_antenna)
2996                        return -EOPNOTSUPP;
2997
2998                tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2999                rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
3000
3001                /* reject antenna configurations which don't match the
3002                 * available antenna masks, except for the "all" mask */
3003                if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
3004                    (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
3005                        return -EINVAL;
3006
3007                tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
3008                rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
3009
3010                result = rdev_set_antenna(rdev, tx_ant, rx_ant);
3011                if (result)
3012                        return result;
3013        }
3014
3015        changed = 0;
3016
3017        if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
3018                retry_short = nla_get_u8(
3019                        info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
3020
3021                changed |= WIPHY_PARAM_RETRY_SHORT;
3022        }
3023
3024        if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
3025                retry_long = nla_get_u8(
3026                        info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
3027
3028                changed |= WIPHY_PARAM_RETRY_LONG;
3029        }
3030
3031        if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
3032                frag_threshold = nla_get_u32(
3033                        info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
3034                if (frag_threshold < 256)
3035                        return -EINVAL;
3036
3037                if (frag_threshold != (u32) -1) {
3038                        /*
3039                         * Fragments (apart from the last one) are required to
3040                         * have even length. Make the fragmentation code
3041                         * simpler by stripping LSB should someone try to use
3042                         * odd threshold value.
3043                         */
3044                        frag_threshold &= ~0x1;
3045                }
3046                changed |= WIPHY_PARAM_FRAG_THRESHOLD;
3047        }
3048
3049        if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
3050                rts_threshold = nla_get_u32(
3051                        info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
3052                changed |= WIPHY_PARAM_RTS_THRESHOLD;
3053        }
3054
3055        if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
3056                if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK])
3057                        return -EINVAL;
3058
3059                coverage_class = nla_get_u8(
3060                        info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
3061                changed |= WIPHY_PARAM_COVERAGE_CLASS;
3062        }
3063
3064        if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3065                if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION))
3066                        return -EOPNOTSUPP;
3067
3068                changed |= WIPHY_PARAM_DYN_ACK;
3069        }
3070
3071        if (info->attrs[NL80211_ATTR_TXQ_LIMIT]) {
3072                if (!wiphy_ext_feature_isset(&rdev->wiphy,
3073                                             NL80211_EXT_FEATURE_TXQS))
3074                        return -EOPNOTSUPP;
3075                txq_limit = nla_get_u32(
3076                        info->attrs[NL80211_ATTR_TXQ_LIMIT]);
3077                changed |= WIPHY_PARAM_TXQ_LIMIT;
3078        }
3079
3080        if (info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]) {
3081                if (!wiphy_ext_feature_isset(&rdev->wiphy,
3082                                             NL80211_EXT_FEATURE_TXQS))
3083                        return -EOPNOTSUPP;
3084                txq_memory_limit = nla_get_u32(
3085                        info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]);
3086                changed |= WIPHY_PARAM_TXQ_MEMORY_LIMIT;
3087        }
3088
3089        if (info->attrs[NL80211_ATTR_TXQ_QUANTUM]) {
3090                if (!wiphy_ext_feature_isset(&rdev->wiphy,
3091                                             NL80211_EXT_FEATURE_TXQS))
3092                        return -EOPNOTSUPP;
3093                txq_quantum = nla_get_u32(
3094                        info->attrs[NL80211_ATTR_TXQ_QUANTUM]);
3095                changed |= WIPHY_PARAM_TXQ_QUANTUM;
3096        }
3097
3098        if (changed) {
3099                u8 old_retry_short, old_retry_long;
3100                u32 old_frag_threshold, old_rts_threshold;
3101                u8 old_coverage_class;
3102                u32 old_txq_limit, old_txq_memory_limit, old_txq_quantum;
3103
3104                if (!rdev->ops->set_wiphy_params)
3105                        return -EOPNOTSUPP;
3106
3107                old_retry_short = rdev->wiphy.retry_short;
3108                old_retry_long = rdev->wiphy.retry_long;
3109                old_frag_threshold = rdev->wiphy.frag_threshold;
3110                old_rts_threshold = rdev->wiphy.rts_threshold;
3111                old_coverage_class = rdev->wiphy.coverage_class;
3112                old_txq_limit = rdev->wiphy.txq_limit;
3113                old_txq_memory_limit = rdev->wiphy.txq_memory_limit;
3114                old_txq_quantum = rdev->wiphy.txq_quantum;
3115
3116                if (changed & WIPHY_PARAM_RETRY_SHORT)
3117                        rdev->wiphy.retry_short = retry_short;
3118                if (changed & WIPHY_PARAM_RETRY_LONG)
3119                        rdev->wiphy.retry_long = retry_long;
3120                if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
3121                        rdev->wiphy.frag_threshold = frag_threshold;
3122                if (changed & WIPHY_PARAM_RTS_THRESHOLD)
3123                        rdev->wiphy.rts_threshold = rts_threshold;
3124                if (changed & WIPHY_PARAM_COVERAGE_CLASS)
3125                        rdev->wiphy.coverage_class = coverage_class;
3126                if (changed & WIPHY_PARAM_TXQ_LIMIT)
3127                        rdev->wiphy.txq_limit = txq_limit;
3128                if (changed & WIPHY_PARAM_TXQ_MEMORY_LIMIT)
3129                        rdev->wiphy.txq_memory_limit = txq_memory_limit;
3130                if (changed & WIPHY_PARAM_TXQ_QUANTUM)
3131                        rdev->wiphy.txq_quantum = txq_quantum;
3132
3133                result = rdev_set_wiphy_params(rdev, changed);
3134                if (result) {
3135                        rdev->wiphy.retry_short = old_retry_short;
3136                        rdev->wiphy.retry_long = old_retry_long;
3137                        rdev->wiphy.frag_threshold = old_frag_threshold;
3138                        rdev->wiphy.rts_threshold = old_rts_threshold;
3139                        rdev->wiphy.coverage_class = old_coverage_class;
3140                        rdev->wiphy.txq_limit = old_txq_limit;
3141                        rdev->wiphy.txq_memory_limit = old_txq_memory_limit;
3142                        rdev->wiphy.txq_quantum = old_txq_quantum;
3143                        return result;
3144                }
3145        }
3146        return 0;
3147}
3148
3149static int nl80211_send_chandef(struct sk_buff *msg,
3150                                const struct cfg80211_chan_def *chandef)
3151{
3152        if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3153                return -EINVAL;
3154
3155        if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
3156                        chandef->chan->center_freq))
3157                return -ENOBUFS;
3158        switch (chandef->width) {
3159        case NL80211_CHAN_WIDTH_20_NOHT:
3160        case NL80211_CHAN_WIDTH_20:
3161        case NL80211_CHAN_WIDTH_40:
3162                if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3163                                cfg80211_get_chandef_type(chandef)))
3164                        return -ENOBUFS;
3165                break;
3166        default:
3167                break;
3168        }
3169        if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
3170                return -ENOBUFS;
3171        if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
3172                return -ENOBUFS;
3173        if (chandef->center_freq2 &&
3174            nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
3175                return -ENOBUFS;
3176        return 0;
3177}
3178
3179static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
3180                              struct cfg80211_registered_device *rdev,
3181                              struct wireless_dev *wdev,
3182                              enum nl80211_commands cmd)
3183{
3184        struct net_device *dev = wdev->netdev;
3185        void *hdr;
3186
3187        WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
3188                cmd != NL80211_CMD_DEL_INTERFACE &&
3189                cmd != NL80211_CMD_SET_INTERFACE);
3190
3191        hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3192        if (!hdr)
3193                return -1;
3194
3195        if (dev &&
3196            (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3197             nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
3198                goto nla_put_failure;
3199
3200        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
3201            nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
3202            nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
3203                              NL80211_ATTR_PAD) ||
3204            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
3205            nla_put_u32(msg, NL80211_ATTR_GENERATION,
3206                        rdev->devlist_generation ^
3207                        (cfg80211_rdev_list_generation << 2)) ||
3208            nla_put_u8(msg, NL80211_ATTR_4ADDR, wdev->use_4addr))
3209                goto nla_put_failure;
3210
3211        if (rdev->ops->get_channel) {
3212                int ret;
3213                struct cfg80211_chan_def chandef = {};
3214
3215                ret = rdev_get_channel(rdev, wdev, &chandef);
3216                if (ret == 0) {
3217                        if (nl80211_send_chandef(msg, &chandef))
3218                                goto nla_put_failure;
3219                }
3220        }
3221
3222        if (rdev->ops->get_tx_power) {
3223                int dbm, ret;
3224
3225                ret = rdev_get_tx_power(rdev, wdev, &dbm);
3226                if (ret == 0 &&
3227                    nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
3228                                DBM_TO_MBM(dbm)))
3229                        goto nla_put_failure;
3230        }
3231
3232        wdev_lock(wdev);
3233        switch (wdev->iftype) {
3234        case NL80211_IFTYPE_AP:
3235                if (wdev->ssid_len &&
3236                    nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
3237                        goto nla_put_failure_locked;
3238                break;
3239        case NL80211_IFTYPE_STATION:
3240        case NL80211_IFTYPE_P2P_CLIENT:
3241        case NL80211_IFTYPE_ADHOC: {
3242                const u8 *ssid_ie;
3243                if (!wdev->current_bss)
3244                        break;
3245                rcu_read_lock();
3246                ssid_ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
3247                                               WLAN_EID_SSID);
3248                if (ssid_ie &&
3249                    nla_put(msg, NL80211_ATTR_SSID, ssid_ie[1], ssid_ie + 2))
3250                        goto nla_put_failure_rcu_locked;
3251                rcu_read_unlock();
3252                break;
3253                }
3254        default:
3255                /* nothing */
3256                break;
3257        }
3258        wdev_unlock(wdev);
3259
3260        if (rdev->ops->get_txq_stats) {
3261                struct cfg80211_txq_stats txqstats = {};
3262                int ret = rdev_get_txq_stats(rdev, wdev, &txqstats);
3263
3264                if (ret == 0 &&
3265                    !nl80211_put_txq_stats(msg, &txqstats,
3266                                           NL80211_ATTR_TXQ_STATS))
3267                        goto nla_put_failure;
3268        }
3269
3270        genlmsg_end(msg, hdr);
3271        return 0;
3272
3273 nla_put_failure_rcu_locked:
3274        rcu_read_unlock();
3275 nla_put_failure_locked:
3276        wdev_unlock(wdev);
3277 nla_put_failure:
3278        genlmsg_cancel(msg, hdr);
3279        return -EMSGSIZE;
3280}
3281
3282static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
3283{
3284        int wp_idx = 0;
3285        int if_idx = 0;
3286        int wp_start = cb->args[0];
3287        int if_start = cb->args[1];
3288        int filter_wiphy = -1;
3289        struct cfg80211_registered_device *rdev;
3290        struct wireless_dev *wdev;
3291        int ret;
3292
3293        rtnl_lock();
3294        if (!cb->args[2]) {
3295                struct nl80211_dump_wiphy_state state = {
3296                        .filter_wiphy = -1,
3297                };
3298
3299                ret = nl80211_dump_wiphy_parse(skb, cb, &state);
3300                if (ret)
3301                        goto out_unlock;
3302
3303                filter_wiphy = state.filter_wiphy;
3304
3305                /*
3306                 * if filtering, set cb->args[2] to +1 since 0 is the default
3307                 * value needed to determine that parsing is necessary.
3308                 */
3309                if (filter_wiphy >= 0)
3310                        cb->args[2] = filter_wiphy + 1;
3311                else
3312                        cb->args[2] = -1;
3313        } else if (cb->args[2] > 0) {
3314                filter_wiphy = cb->args[2] - 1;
3315        }
3316
3317        list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3318                if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3319                        continue;
3320                if (wp_idx < wp_start) {
3321                        wp_idx++;
3322                        continue;
3323                }
3324
3325                if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
3326                        continue;
3327
3328                if_idx = 0;
3329
3330                list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
3331                        if (if_idx < if_start) {
3332                                if_idx++;
3333                                continue;
3334                        }
3335                        if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
3336                                               cb->nlh->nlmsg_seq, NLM_F_MULTI,
3337                                               rdev, wdev,
3338                                               NL80211_CMD_NEW_INTERFACE) < 0) {
3339                                goto out;
3340                        }
3341                        if_idx++;
3342                }
3343
3344                wp_idx++;
3345        }
3346 out:
3347        cb->args[0] = wp_idx;
3348        cb->args[1] = if_idx;
3349
3350        ret = skb->len;
3351 out_unlock:
3352        rtnl_unlock();
3353
3354        return ret;
3355}
3356
3357static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
3358{
3359        struct sk_buff *msg;
3360        struct cfg80211_registered_device *rdev = info->user_ptr[0];
3361        struct wireless_dev *wdev = info->user_ptr[1];
3362
3363        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3364        if (!msg)
3365                return -ENOMEM;
3366
3367        if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3368                               rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3369                nlmsg_free(msg);
3370                return -ENOBUFS;
3371        }
3372
3373        return genlmsg_reply(msg, info);
3374}
3375
3376static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
3377        [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
3378        [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
3379        [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
3380        [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
3381        [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
3382        [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
3383};
3384
3385static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
3386{
3387        struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
3388        int flag;
3389
3390        *mntrflags = 0;
3391
3392        if (!nla)
3393                return -EINVAL;
3394
3395        if (nla_parse_nested_deprecated(flags, NL80211_MNTR_FLAG_MAX, nla, mntr_flags_policy, NULL))
3396                return -EINVAL;
3397
3398        for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
3399                if (flags[flag])
3400                        *mntrflags |= (1<<flag);
3401
3402        *mntrflags |= MONITOR_FLAG_CHANGED;
3403
3404        return 0;
3405}
3406
3407static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
3408                                     enum nl80211_iftype type,
3409                                     struct genl_info *info,
3410                                     struct vif_params *params)
3411{
3412        bool change = false;
3413        int err;
3414
3415        if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
3416                if (type != NL80211_IFTYPE_MONITOR)
3417                        return -EINVAL;
3418
3419                err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
3420                                          &params->flags);
3421                if (err)
3422                        return err;
3423
3424                change = true;
3425        }
3426
3427        if (params->flags & MONITOR_FLAG_ACTIVE &&
3428            !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
3429                return -EOPNOTSUPP;
3430
3431        if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
3432                const u8 *mumimo_groups;
3433                u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3434
3435                if (type != NL80211_IFTYPE_MONITOR)
3436                        return -EINVAL;
3437
3438                if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3439                        return -EOPNOTSUPP;
3440
3441                mumimo_groups =
3442                        nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
3443
3444                /* bits 0 and 63 are reserved and must be zero */
3445                if ((mumimo_groups[0] & BIT(0)) ||
3446                    (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
3447                        return -EINVAL;
3448
3449                params->vht_mumimo_groups = mumimo_groups;
3450                change = true;
3451        }
3452
3453        if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
3454                u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3455
3456                if (type != NL80211_IFTYPE_MONITOR)
3457                        return -EINVAL;
3458
3459                if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3460                        return -EOPNOTSUPP;
3461
3462                params->vht_mumimo_follow_addr =
3463                        nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
3464                change = true;
3465        }
3466
3467        return change ? 1 : 0;
3468}
3469
3470static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
3471                               struct net_device *netdev, u8 use_4addr,
3472                               enum nl80211_iftype iftype)
3473{
3474        if (!use_4addr) {
3475                if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
3476                        return -EBUSY;
3477                return 0;
3478        }
3479
3480        switch (iftype) {
3481        case NL80211_IFTYPE_AP_VLAN:
3482                if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
3483                        return 0;
3484                break;
3485        case NL80211_IFTYPE_STATION:
3486                if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
3487                        return 0;
3488                break;
3489        default:
3490                break;
3491        }
3492
3493        return -EOPNOTSUPP;
3494}
3495
3496static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
3497{
3498        struct cfg80211_registered_device *rdev = info->user_ptr[0];
3499        struct vif_params params;
3500        int err;
3501        enum nl80211_iftype otype, ntype;
3502        struct net_device *dev = info->user_ptr[1];
3503        bool change = false;
3504
3505        memset(&params, 0, sizeof(params));
3506
3507        otype = ntype = dev->ieee80211_ptr->iftype;
3508
3509        if (info->attrs[NL80211_ATTR_IFTYPE]) {
3510                ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3511                if (otype != ntype)
3512                        change = true;
3513        }
3514
3515        if (info->attrs[NL80211_ATTR_MESH_ID]) {
3516                struct wireless_dev *wdev = dev->ieee80211_ptr;
3517
3518                if (ntype != NL80211_IFTYPE_MESH_POINT)
3519                        return -EINVAL;
3520                if (netif_running(dev))
3521                        return -EBUSY;
3522
3523                wdev_lock(wdev);
3524                BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3525                             IEEE80211_MAX_MESH_ID_LEN);
3526                wdev->mesh_id_up_len =
3527                        nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3528                memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3529                       wdev->mesh_id_up_len);
3530                wdev_unlock(wdev);
3531        }
3532
3533        if (info->attrs[NL80211_ATTR_4ADDR]) {
3534                params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3535                change = true;
3536                err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
3537                if (err)
3538                        return err;
3539        } else {
3540                params.use_4addr = -1;
3541        }
3542
3543        err = nl80211_parse_mon_options(rdev, ntype, info, &params);
3544        if (err < 0)
3545                return err;
3546        if (err > 0)
3547                change = true;
3548
3549        if (change)
3550                err = cfg80211_change_iface(rdev, dev, ntype, &params);
3551        else
3552                err = 0;
3553
3554        if (!err && params.use_4addr != -1)
3555                dev->ieee80211_ptr->use_4addr = params.use_4addr;
3556
3557        if (change && !err) {
3558                struct wireless_dev *wdev = dev->ieee80211_ptr;
3559
3560                nl80211_notify_iface(rdev, wdev, NL80211_CMD_SET_INTERFACE);
3561        }
3562
3563        return err;
3564}
3565
3566static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
3567{
3568        struct cfg80211_registered_device *rdev = info->user_ptr[0];
3569        struct vif_params params;
3570        struct wireless_dev *wdev;
3571        struct sk_buff *msg;
3572        int err;
3573        enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
3574
3575        /* to avoid failing a new interface creation due to pending removal */
3576        cfg80211_destroy_ifaces(rdev);
3577
3578        memset(&params, 0, sizeof(params));
3579
3580        if (!info->attrs[NL80211_ATTR_IFNAME])
3581                return -EINVAL;
3582
3583        if (info->attrs[NL80211_ATTR_IFTYPE])
3584                type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3585
3586        if (!rdev->ops->add_virtual_intf)
3587                return -EOPNOTSUPP;
3588
3589        if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
3590             rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
3591            info->attrs[NL80211_ATTR_MAC]) {
3592                nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
3593                           ETH_ALEN);
3594                if (!is_valid_ether_addr(params.macaddr))
3595                        return -EADDRNOTAVAIL;
3596        }
3597
3598        if (info->attrs[NL80211_ATTR_4ADDR]) {
3599                params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3600                err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
3601                if (err)
3602                        return err;
3603        }
3604
3605        if (!cfg80211_iftype_allowed(&rdev->wiphy, type, params.use_4addr, 0))
3606                return -EOPNOTSUPP;
3607
3608        err = nl80211_parse_mon_options(rdev, type, info, &params);
3609        if (err < 0)
3610                return err;
3611
3612        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3613        if (!msg)
3614                return -ENOMEM;
3615
3616        wdev = rdev_add_virtual_intf(rdev,
3617                                nla_data(info->attrs[NL80211_ATTR_IFNAME]),
3618                                NET_NAME_USER, type, &params);
3619        if (WARN_ON(!wdev)) {
3620                nlmsg_free(msg);
3621                return -EPROTO;
3622        } else if (IS_ERR(wdev)) {
3623                nlmsg_free(msg);
3624                return PTR_ERR(wdev);
3625        }
3626
3627        if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
3628                wdev->owner_nlportid = info->snd_portid;
3629
3630        switch (type) {
3631        case NL80211_IFTYPE_MESH_POINT:
3632                if (!info->attrs[NL80211_ATTR_MESH_ID])
3633                        break;
3634                wdev_lock(wdev);
3635                BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3636                             IEEE80211_MAX_MESH_ID_LEN);
3637                wdev->mesh_id_up_len =
3638                        nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3639                memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3640                       wdev->mesh_id_up_len);
3641                wdev_unlock(wdev);
3642                break;
3643        case NL80211_IFTYPE_NAN:
3644        case NL80211_IFTYPE_P2P_DEVICE:
3645                /*
3646                 * P2P Device and NAN do not have a netdev, so don't go
3647                 * through the netdev notifier and must be added here
3648                 */
3649                cfg80211_init_wdev(rdev, wdev);
3650                break;
3651        default:
3652                break;
3653        }
3654
3655        if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3656                               rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3657                nlmsg_free(msg);
3658                return -ENOBUFS;
3659        }
3660
3661        return genlmsg_reply(msg, info);
3662}
3663
3664static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
3665{
3666        struct cfg80211_registered_device *rdev = info->user_ptr[0];
3667        struct wireless_dev *wdev = info->user_ptr[1];
3668
3669        if (!rdev->ops->del_virtual_intf)
3670                return -EOPNOTSUPP;
3671
3672        /*
3673         * If we remove a wireless device without a netdev then clear
3674         * user_ptr[1] so that nl80211_post_doit won't dereference it
3675         * to check if it needs to do dev_put(). Otherwise it crashes
3676         * since the wdev has been freed, unlike with a netdev where
3677         * we need the dev_put() for the netdev to really be freed.
3678         */
3679        if (!wdev->netdev)
3680                info->user_ptr[1] = NULL;
3681
3682        return rdev_del_virtual_intf(rdev, wdev);
3683}
3684
3685static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
3686{
3687        struct cfg80211_registered_device *rdev = info->user_ptr[0];
3688        struct net_device *dev = info->user_ptr[1];
3689        u16 noack_map;
3690
3691        if (!info->attrs[NL80211_ATTR_NOACK_MAP])
3692                return -EINVAL;
3693
3694        if (!rdev->ops->set_noack_map)
3695                return -EOPNOTSUPP;
3696
3697        noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
3698
3699        return rdev_set_noack_map(rdev, dev, noack_map);
3700}
3701
3702struct get_key_cookie {
3703        struct sk_buff *msg;
3704        int error;
3705        int idx;
3706};
3707
3708static void get_key_callback(void *c, struct key_params *params)
3709{
3710        struct nlattr *key;
3711        struct get_key_cookie *cookie = c;
3712
3713        if ((params->key &&
3714             nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
3715                     params->key_len, params->key)) ||
3716            (params->seq &&
3717             nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
3718                     params->seq_len, params->seq)) ||
3719            (params->cipher &&
3720             nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
3721                         params->cipher)))
3722                goto nla_put_failure;
3723
3724        key = nla_nest_start_noflag(cookie->msg, NL80211_ATTR_KEY);
3725        if (!key)
3726                goto nla_put_failure;
3727
3728        if ((params->key &&
3729             nla_put(cookie->msg, NL80211_KEY_DATA,
3730                     params->key_len, params->key)) ||
3731            (params->seq &&
3732             nla_put(cookie->msg, NL80211_KEY_SEQ,
3733                     params->seq_len, params->seq)) ||
3734            (params->cipher &&
3735             nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
3736                         params->cipher)))
3737                goto nla_put_failure;
3738
3739        if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
3740                goto nla_put_failure;
3741
3742        nla_nest_end(cookie->msg, key);
3743
3744        return;
3745 nla_put_failure:
3746        cookie->error = 1;
3747}
3748
3749static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
3750{
3751        struct cfg80211_registered_device *rdev = info->user_ptr[0];
3752        int err;
3753        struct net_device *dev = info->user_ptr[1];
3754        u8 key_idx = 0;
3755        const u8 *mac_addr = NULL;
3756        bool pairwise;
3757        struct get_key_cookie cookie = {
3758                .error = 0,
3759        };
3760        void *hdr;
3761        struct sk_buff *msg;
3762
3763        if (info->attrs[NL80211_ATTR_KEY_IDX])
3764                key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
3765
3766        if (info->attrs[NL80211_ATTR_MAC])
3767                mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3768
3769        pairwise = !!mac_addr;
3770        if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
3771                u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
3772
3773                if (kt != NL80211_KEYTYPE_GROUP &&
3774                    kt != NL80211_KEYTYPE_PAIRWISE)
3775                        return -EINVAL;
3776                pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
3777        }
3778
3779        if (!rdev->ops->get_key)
3780                return -EOPNOTSUPP;
3781
3782        if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3783                return -ENOENT;
3784
3785        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3786        if (!msg)
3787                return -ENOMEM;
3788
3789        hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
3790                             NL80211_CMD_NEW_KEY);
3791        if (!hdr)
3792                goto nla_put_failure;
3793
3794        cookie.msg = msg;
3795        cookie.idx = key_idx;
3796
3797        if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3798            nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
3799                goto nla_put_failure;
3800        if (mac_addr &&
3801            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
3802                goto nla_put_failure;
3803
3804        err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
3805                           get_key_callback);
3806
3807        if (err)
3808                goto free_msg;
3809
3810        if (cookie.error)
3811                goto nla_put_failure;
3812
3813        genlmsg_end(msg, hdr);
3814        return genlmsg_reply(msg, info);
3815
3816 nla_put_failure:
3817        err = -ENOBUFS;
3818 free_msg:
3819        nlmsg_free(msg);
3820        return err;
3821}
3822
3823static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
3824{
3825        struct cfg80211_registered_device *rdev = info->user_ptr[0];
3826        struct key_parse key;
3827        int err;
3828        struct net_device *dev = info->user_ptr[1];
3829
3830        err = nl80211_parse_key(info, &key);
3831        if (err)
3832                return err;
3833
3834        if (key.idx < 0)
3835                return -EINVAL;
3836
3837        /* Only support setting default key and
3838         * Extended Key ID action NL80211_KEY_SET_TX.
3839         */
3840        if (!key.def && !key.defmgmt &&
3841            !(key.p.mode == NL80211_KEY_SET_TX))
3842                return -EINVAL;
3843
3844        wdev_lock(dev->ieee80211_ptr);
3845
3846        if (key.def) {
3847                if (!rdev->ops->set_default_key) {
3848                        err = -EOPNOTSUPP;
3849                        goto out;
3850                }
3851
3852                err = nl80211_key_allowed(dev->ieee80211_ptr);
3853                if (err)
3854                        goto out;
3855
3856                err = rdev_set_default_key(rdev, dev, key.idx,
3857                                                 key.def_uni, key.def_multi);
3858
3859                if (err)
3860                        goto out;
3861
3862#ifdef CONFIG_CFG80211_WEXT
3863                dev->ieee80211_ptr->wext.default_key = key.idx;
3864#endif
3865        } else if (key.defmgmt) {
3866                if (key.def_uni || !key.def_multi) {
3867                        err = -EINVAL;
3868                        goto out;
3869                }
3870
3871                if (!rdev->ops->set_default_mgmt_key) {
3872                        err = -EOPNOTSUPP;
3873                        goto out;
3874                }
3875
3876                err = nl80211_key_allowed(dev->ieee80211_ptr);
3877                if (err)
3878                        goto out;
3879
3880                err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
3881                if (err)
3882                        goto out;
3883
3884#ifdef CONFIG_CFG80211_WEXT
3885                dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
3886#endif
3887        } else if (key.p.mode == NL80211_KEY_SET_TX &&
3888                   wiphy_ext_feature_isset(&rdev->wiphy,
3889                                           NL80211_EXT_FEATURE_EXT_KEY_ID)) {
3890                u8 *mac_addr = NULL;
3891
3892                if (info->attrs[NL80211_ATTR_MAC])
3893                        mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3894
3895                if (!mac_addr || key.idx < 0 || key.idx > 1) {
3896                        err = -EINVAL;
3897                        goto out;
3898                }
3899
3900                err = rdev_add_key(rdev, dev, key.idx,
3901                                   NL80211_KEYTYPE_PAIRWISE,
3902                                   mac_addr, &key.p);
3903        } else {
3904                err = -EINVAL;
3905        }
3906 out:
3907        wdev_unlock(dev->ieee80211_ptr);
3908
3909        return err;
3910}
3911
3912static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
3913{
3914        struct cfg80211_registered_device *rdev = info->user_ptr[0];
3915        int err;
3916        struct net_device *dev = info->user_ptr[1];
3917        struct key_parse key;
3918        const u8 *mac_addr = NULL;
3919
3920        err = nl80211_parse_key(info, &key);
3921        if (err)
3922                return err;
3923
3924        if (!key.p.key)
3925                return -EINVAL;
3926
3927        if (info->attrs[NL80211_ATTR_MAC])
3928                mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3929
3930        if (key.type == -1) {
3931                if (mac_addr)
3932                        key.type = NL80211_KEYTYPE_PAIRWISE;
3933                else
3934                        key.type = NL80211_KEYTYPE_GROUP;
3935        }
3936
3937        /* for now */
3938        if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3939            key.type != NL80211_KEYTYPE_GROUP)
3940                return -EINVAL;
3941
3942        if (!rdev->ops->add_key)
3943                return -EOPNOTSUPP;
3944
3945        if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
3946                                           key.type == NL80211_KEYTYPE_PAIRWISE,
3947                                           mac_addr))
3948                return -EINVAL;
3949
3950        wdev_lock(dev->ieee80211_ptr);
3951        err = nl80211_key_allowed(dev->ieee80211_ptr);
3952        if (!err)
3953                err = rdev_add_key(rdev, dev, key.idx,
3954                                   key.type == NL80211_KEYTYPE_PAIRWISE,
3955                                    mac_addr, &key.p);
3956        wdev_unlock(dev->ieee80211_ptr);
3957
3958        return err;
3959}
3960
3961static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
3962{
3963        struct cfg80211_registered_device *rdev = info->user_ptr[0];
3964        int err;
3965        struct net_device *dev = info->user_ptr[1];
3966        u8 *mac_addr = NULL;
3967        struct key_parse key;
3968
3969        err = nl80211_parse_key(info, &key);
3970        if (err)
3971                return err;
3972
3973        if (info->attrs[NL80211_ATTR_MAC])
3974                mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3975
3976        if (key.type == -1) {
3977                if (mac_addr)
3978                        key.type = NL80211_KEYTYPE_PAIRWISE;
3979                else
3980                        key.type = NL80211_KEYTYPE_GROUP;
3981        }
3982
3983        /* for now */
3984        if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3985            key.type != NL80211_KEYTYPE_GROUP)
3986                return -EINVAL;
3987
3988        if (!rdev->ops->del_key)
3989                return -EOPNOTSUPP;
3990
3991        wdev_lock(dev->ieee80211_ptr);
3992        err = nl80211_key_allowed(dev->ieee80211_ptr);
3993
3994        if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
3995            !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3996                err = -ENOENT;
3997
3998        if (!err)
3999                err = rdev_del_key(rdev, dev, key.idx,
4000                                   key.type == NL80211_KEYTYPE_PAIRWISE,
4001                                   mac_addr);
4002
4003#ifdef CONFIG_CFG80211_WEXT
4004        if (!err) {
4005                if (key.idx == dev->ieee80211_ptr->wext.default_key)
4006                        dev->ieee80211_ptr->wext.default_key = -1;
4007                else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
4008                        dev->ieee80211_ptr->wext.default_mgmt_key = -1;
4009        }
4010#endif
4011        wdev_unlock(dev->ieee80211_ptr);
4012
4013        return err;
4014}
4015
4016/* This function returns an error or the number of nested attributes */
4017static int validate_acl_mac_addrs(struct nlattr *nl_attr)
4018{
4019        struct nlattr *attr;
4020        int n_entries = 0, tmp;
4021
4022        nla_for_each_nested(attr, nl_attr, tmp) {
4023                if (nla_len(attr) != ETH_ALEN)
4024                        return -EINVAL;
4025
4026                n_entries++;
4027        }
4028
4029        return n_entries;
4030}
4031
4032/*
4033 * This function parses ACL information and allocates memory for ACL data.
4034 * On successful return, the calling function is responsible to free the
4035 * ACL buffer returned by this function.
4036 */
4037static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
4038                                                struct genl_info *info)
4039{
4040        enum nl80211_acl_policy acl_policy;
4041        struct nlattr *attr;
4042        struct cfg80211_acl_data *acl;
4043        int i = 0, n_entries, tmp;
4044
4045        if (!wiphy->max_acl_mac_addrs)
4046                return ERR_PTR(-EOPNOTSUPP);
4047
4048        if (!info->attrs[NL80211_ATTR_ACL_POLICY])
4049                return ERR_PTR(-EINVAL);
4050
4051        acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
4052        if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
4053            acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
4054                return ERR_PTR(-EINVAL);
4055
4056        if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
4057                return ERR_PTR(-EINVAL);
4058
4059        n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
4060        if (n_entries < 0)
4061                return ERR_PTR(n_entries);
4062
4063        if (n_entries > wiphy->max_acl_mac_addrs)
4064                return ERR_PTR(-ENOTSUPP);
4065
4066        acl = kzalloc(struct_size(acl, mac_addrs, n_entries), GFP_KERNEL);
4067        if (!acl)
4068                return ERR_PTR(-ENOMEM);
4069
4070        nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
4071                memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
4072                i++;
4073        }
4074
4075        acl->n_acl_entries = n_entries;
4076        acl->acl_policy = acl_policy;
4077
4078        return acl;
4079}
4080
4081static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
4082{
4083        struct cfg80211_registered_device *rdev = info->user_ptr[0];
4084        struct net_device *dev = info->user_ptr[1];
4085        struct cfg80211_acl_data *acl;
4086        int err;
4087
4088        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4089            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4090                return -EOPNOTSUPP;
4091
4092        if (!dev->ieee80211_ptr->beacon_interval)
4093                return -EINVAL;
4094
4095        acl = parse_acl_data(&rdev->wiphy, info);
4096        if (IS_ERR(acl))
4097                return PTR_ERR(acl);
4098
4099        err = rdev_set_mac_acl(rdev, dev, acl);
4100
4101        kfree(acl);
4102
4103        return err;
4104}
4105
4106static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4107                           u8 *rates, u8 rates_len)
4108{
4109        u8 i;
4110        u32 mask = 0;
4111
4112        for (i = 0; i < rates_len; i++) {
4113                int rate = (rates[i] & 0x7f) * 5;
4114                int ridx;
4115
4116                for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4117                        struct ieee80211_rate *srate =
4118                                &sband->bitrates[ridx];
4119                        if (rate == srate->bitrate) {
4120                                mask |= 1 << ridx;
4121                                break;
4122                        }
4123                }
4124                if (ridx == sband->n_bitrates)
4125                        return 0; /* rate not found */
4126        }
4127
4128        return mask;
4129}
4130
4131static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
4132                               u8 *rates, u8 rates_len,
4133                               u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
4134{
4135        u8 i;
4136
4137        memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
4138
4139        for (i = 0; i < rates_len; i++) {
4140                int ridx, rbit;
4141
4142                ridx = rates[i] / 8;
4143                rbit = BIT(rates[i] % 8);
4144
4145                /* check validity */
4146                if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
4147                        return false;
4148
4149                /* check availability */
4150                ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
4151                if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
4152                        mcs[ridx] |= rbit;
4153                else
4154                        return false;
4155        }
4156
4157        return true;
4158}
4159
4160static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
4161{
4162        u16 mcs_mask = 0;
4163
4164        switch (vht_mcs_map) {
4165        case IEEE80211_VHT_MCS_NOT_SUPPORTED:
4166                break;
4167        case IEEE80211_VHT_MCS_SUPPORT_0_7:
4168                mcs_mask = 0x00FF;
4169                break;
4170        case IEEE80211_VHT_MCS_SUPPORT_0_8:
4171                mcs_mask = 0x01FF;
4172                break;
4173        case IEEE80211_VHT_MCS_SUPPORT_0_9:
4174                mcs_mask = 0x03FF;
4175                break;
4176        default:
4177                break;
4178        }
4179
4180        return mcs_mask;
4181}
4182
4183static void vht_build_mcs_mask(u16 vht_mcs_map,
4184                               u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
4185{
4186        u8 nss;
4187
4188        for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
4189                vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
4190                vht_mcs_map >>= 2;
4191        }
4192}
4193
4194static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
4195                             struct nl80211_txrate_vht *txrate,
4196                             u16 mcs[NL80211_VHT_NSS_MAX])
4197{
4198        u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4199        u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
4200        u8 i;
4201
4202        if (!sband->vht_cap.vht_supported)
4203                return false;
4204
4205        memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
4206
4207        /* Build vht_mcs_mask from VHT capabilities */
4208        vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
4209
4210        for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4211                if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
4212                        mcs[i] = txrate->mcs[i];
4213                else
4214                        return false;
4215        }
4216
4217        return true;
4218}
4219
4220static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
4221        [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4222                                    .len = NL80211_MAX_SUPP_RATES },
4223        [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
4224                                .len = NL80211_MAX_SUPP_HT_RATES },
4225        [NL80211_TXRATE_VHT] = {
4226                .type = NLA_EXACT_LEN_WARN,
4227                .len = sizeof(struct nl80211_txrate_vht),
4228        },
4229        [NL80211_TXRATE_GI] = { .type = NLA_U8 },
4230};
4231
4232static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
4233                                         struct cfg80211_bitrate_mask *mask)
4234{
4235        struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4236        struct cfg80211_registered_device *rdev = info->user_ptr[0];
4237        int rem, i;
4238        struct nlattr *tx_rates;
4239        struct ieee80211_supported_band *sband;
4240        u16 vht_tx_mcs_map;
4241
4242        memset(mask, 0, sizeof(*mask));
4243        /* Default to all rates enabled */
4244        for (i = 0; i < NUM_NL80211_BANDS; i++) {
4245                sband = rdev->wiphy.bands[i];
4246
4247                if (!sband)
4248                        continue;
4249
4250                mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
4251                memcpy(mask->control[i].ht_mcs,
4252                       sband->ht_cap.mcs.rx_mask,
4253                       sizeof(mask->control[i].ht_mcs));
4254
4255                if (!sband->vht_cap.vht_supported)
4256                        continue;
4257
4258                vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4259                vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
4260        }
4261
4262        /* if no rates are given set it back to the defaults */
4263        if (!info->attrs[NL80211_ATTR_TX_RATES])
4264                goto out;
4265
4266        /* The nested attribute uses enum nl80211_band as the index. This maps
4267         * directly to the enum nl80211_band values used in cfg80211.
4268         */
4269        BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
4270        nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
4271                enum nl80211_band band = nla_type(tx_rates);
4272                int err;
4273
4274                if (band < 0 || band >= NUM_NL80211_BANDS)
4275                        return -EINVAL;
4276                sband = rdev->wiphy.bands[band];
4277                if (sband == NULL)
4278                        return -EINVAL;
4279                err = nla_parse_nested_deprecated(tb, NL80211_TXRATE_MAX,
4280                                                  tx_rates,
4281                                                  nl80211_txattr_policy,
4282                                                  info->extack);
4283                if (err)
4284                        return err;
4285                if (tb[NL80211_TXRATE_LEGACY]) {
4286                        mask->control[band].legacy = rateset_to_mask(
4287                                sband,
4288                                nla_data(tb[NL80211_TXRATE_LEGACY]),
4289                                nla_len(tb[NL80211_TXRATE_LEGACY]));
4290                        if ((mask->control[band].legacy == 0) &&
4291                            nla_len(tb[NL80211_TXRATE_LEGACY]))
4292                                return -EINVAL;
4293                }
4294                if (tb[NL80211_TXRATE_HT]) {
4295                        if (!ht_rateset_to_mask(
4296                                        sband,
4297                                        nla_data(tb[NL80211_TXRATE_HT]),
4298                                        nla_len(tb[NL80211_TXRATE_HT]),
4299                                        mask->control[band].ht_mcs))
4300                                return -EINVAL;
4301                }
4302                if (tb[NL80211_TXRATE_VHT]) {
4303                        if (!vht_set_mcs_mask(
4304                                        sband,
4305                                        nla_data(tb[NL80211_TXRATE_VHT]),
4306                                        mask->control[band].vht_mcs))
4307                                return -EINVAL;
4308                }
4309                if (tb[NL80211_TXRATE_GI]) {
4310                        mask->control[band].gi =
4311                                nla_get_u8(tb[NL80211_TXRATE_GI]);
4312                        if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
4313                                return -EINVAL;
4314                }
4315
4316                if (mask->control[band].legacy == 0) {
4317                        /* don't allow empty legacy rates if HT or VHT
4318                         * are not even supported.
4319                         */
4320                        if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
4321                              rdev->wiphy.bands[band]->vht_cap.vht_supported))
4322                                return -EINVAL;
4323
4324                        for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4325                                if (mask->control[band].ht_mcs[i])
4326                                        goto out;
4327
4328                        for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4329                                if (mask->control[band].vht_mcs[i])
4330                                        goto out;
4331
4332                        /* legacy and mcs rates may not be both empty */
4333                        return -EINVAL;
4334                }
4335        }
4336
4337out:
4338        return 0;
4339}
4340
4341static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
4342                                   enum nl80211_band band,
4343                                   struct cfg80211_bitrate_mask *beacon_rate)
4344{
4345        u32 count_ht, count_vht, i;
4346        u32 rate = beacon_rate->control[band].legacy;
4347
4348        /* Allow only one rate */
4349        if (hweight32(rate) > 1)
4350                return -EINVAL;
4351
4352        count_ht = 0;
4353        for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4354                if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
4355                        return -EINVAL;
4356                } else if (beacon_rate->control[band].ht_mcs[i]) {
4357                        count_ht++;
4358                        if (count_ht > 1)
4359                                return -EINVAL;
4360                }
4361                if (count_ht && rate)
4362                        return -EINVAL;
4363        }
4364
4365        count_vht = 0;
4366        for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4367                if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
4368                        return -EINVAL;
4369                } else if (beacon_rate->control[band].vht_mcs[i]) {
4370                        count_vht++;
4371                        if (count_vht > 1)
4372                                return -EINVAL;
4373                }
4374                if (count_vht && rate)
4375                        return -EINVAL;
4376        }
4377
4378        if ((count_ht && count_vht) || (!rate && !count_ht && !count_vht))
4379                return -EINVAL;
4380
4381        if (rate &&
4382            !wiphy_ext_feature_isset(&rdev->wiphy,
4383                                     NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
4384                return -EINVAL;
4385        if (count_ht &&
4386            !wiphy_ext_feature_isset(&rdev->wiphy,
4387                                     NL80211_EXT_FEATURE_BEACON_RATE_HT))
4388                return -EINVAL;
4389        if (count_vht &&
4390            !wiphy_ext_feature_isset(&rdev->wiphy,
4391                                     NL80211_EXT_FEATURE_BEACON_RATE_VHT))
4392                return -EINVAL;
4393
4394        return 0;
4395}
4396
4397static int nl80211_parse_beacon(struct cfg80211_registered_device *rdev,
4398                                struct nlattr *attrs[],
4399                                struct cfg80211_beacon_data *bcn)
4400{
4401        bool haveinfo = false;
4402        int err;
4403
4404        memset(bcn, 0, sizeof(*bcn));
4405
4406        if (attrs[NL80211_ATTR_BEACON_HEAD]) {
4407                bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
4408                bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
4409                if (!bcn->head_len)
4410                        return -EINVAL;
4411                haveinfo = true;
4412        }
4413
4414        if (attrs[NL80211_ATTR_BEACON_TAIL]) {
4415                bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
4416                bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
4417                haveinfo = true;
4418        }
4419
4420        if (!haveinfo)
4421                return -EINVAL;
4422
4423        if (attrs[NL80211_ATTR_IE]) {
4424                bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
4425                bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
4426        }
4427
4428        if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
4429                bcn->proberesp_ies =
4430                        nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
4431                bcn->proberesp_ies_len =
4432                        nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
4433        }
4434
4435        if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
4436                bcn->assocresp_ies =
4437                        nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
4438                bcn->assocresp_ies_len =
4439                        nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
4440        }
4441
4442        if (attrs[NL80211_ATTR_PROBE_RESP]) {
4443                bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
4444                bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
4445        }
4446
4447        if (attrs[NL80211_ATTR_FTM_RESPONDER]) {
4448                struct nlattr *tb[NL80211_FTM_RESP_ATTR_MAX + 1];
4449
4450                err = nla_parse_nested_deprecated(tb,
4451                                                  NL80211_FTM_RESP_ATTR_MAX,
4452                                                  attrs[NL80211_ATTR_FTM_RESPONDER],
4453                                                  NULL, NULL);
4454                if (err)
4455                        return err;
4456
4457                if (tb[NL80211_FTM_RESP_ATTR_ENABLED] &&
4458                    wiphy_ext_feature_isset(&rdev->wiphy,
4459                                            NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
4460                        bcn->ftm_responder = 1;
4461                else
4462                        return -EOPNOTSUPP;
4463
4464                if (tb[NL80211_FTM_RESP_ATTR_LCI]) {
4465                        bcn->lci = nla_data(tb[NL80211_FTM_RESP_ATTR_LCI]);
4466                        bcn->lci_len = nla_len(tb[NL80211_FTM_RESP_ATTR_LCI]);
4467                }
4468
4469                if (tb[NL80211_FTM_RESP_ATTR_CIVICLOC]) {
4470                        bcn->civicloc = nla_data(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
4471                        bcn->civicloc_len = nla_len(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
4472                }
4473        } else {
4474                bcn->ftm_responder = -1;
4475        }
4476
4477        return 0;
4478}
4479
4480static int nl80211_parse_he_obss_pd(struct nlattr *attrs,
4481                                    struct ieee80211_he_obss_pd *he_obss_pd)
4482{
4483        struct nlattr *tb[NL80211_HE_OBSS_PD_ATTR_MAX + 1];
4484        int err;
4485
4486        err = nla_parse_nested(tb, NL80211_HE_OBSS_PD_ATTR_MAX, attrs,
4487                               he_obss_pd_policy, NULL);
4488        if (err)
4489                return err;
4490
4491        if (!tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] ||
4492            !tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET])
4493                return -EINVAL;
4494
4495        he_obss_pd->min_offset =
4496                nla_get_u32(tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET]);
4497        he_obss_pd->max_offset =
4498                nla_get_u32(tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET]);
4499
4500        if (he_obss_pd->min_offset >= he_obss_pd->max_offset)
4501                return -EINVAL;
4502
4503        he_obss_pd->enable = true;
4504
4505        return 0;
4506}
4507
4508static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
4509                                            const u8 *rates)
4510{
4511        int i;
4512
4513        if (!rates)
4514                return;
4515
4516        for (i = 0; i < rates[1]; i++) {
4517                if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
4518                        params->ht_required = true;
4519                if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
4520                        params->vht_required = true;
4521        }
4522}
4523
4524/*
4525 * Since the nl80211 API didn't include, from the beginning, attributes about
4526 * HT/VHT requirements/capabilities, we parse them out of the IEs for the
4527 * benefit of drivers that rebuild IEs in the firmware.
4528 */
4529static void nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
4530{
4531        const struct cfg80211_beacon_data *bcn = &params->beacon;
4532        size_t ies_len = bcn->tail_len;
4533        const u8 *ies = bcn->tail;
4534        const u8 *rates;
4535        const u8 *cap;
4536
4537        rates = cfg80211_find_ie(WLAN_EID_SUPP_RATES, ies, ies_len);
4538        nl80211_check_ap_rate_selectors(params, rates);
4539
4540        rates = cfg80211_find_ie(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
4541        nl80211_check_ap_rate_selectors(params, rates);
4542
4543        cap = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies, ies_len);
4544        if (cap && cap[1] >= sizeof(*params->ht_cap))
4545                params->ht_cap = (void *)(cap + 2);
4546        cap = cfg80211_find_ie(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
4547        if (cap && cap[1] >= sizeof(*params->vht_cap))
4548                params->vht_cap = (void *)(cap + 2);
4549        cap = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ies, ies_len);
4550        if (cap && cap[1] >= sizeof(*params->he_cap) + 1)
4551                params->he_cap = (void *)(cap + 3);
4552}
4553
4554static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
4555                                   struct cfg80211_ap_settings *params)
4556{
4557        struct wireless_dev *wdev;
4558        bool ret = false;
4559
4560        list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
4561                if (wdev->iftype != NL80211_IFTYPE_AP &&
4562                    wdev->iftype != NL80211_IFTYPE_P2P_GO)
4563                        continue;
4564
4565                if (!wdev->preset_chandef.chan)
4566                        continue;
4567
4568                params->chandef = wdev->preset_chandef;
4569                ret = true;
4570                break;
4571        }
4572
4573        return ret;
4574}
4575
4576static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
4577                                    enum nl80211_auth_type auth_type,
4578                                    enum nl80211_commands cmd)
4579{
4580        if (auth_type > NL80211_AUTHTYPE_MAX)
4581                return false;
4582
4583        switch (cmd) {
4584        case NL80211_CMD_AUTHENTICATE:
4585                if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
4586                    auth_type == NL80211_AUTHTYPE_SAE)
4587                        return false;
4588                if (!wiphy_ext_feature_isset(&rdev->wiphy,
4589                                             NL80211_EXT_FEATURE_FILS_STA) &&
4590                    (auth_type == NL80211_AUTHTYPE_FILS_SK ||
4591                     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4592                     auth_type == NL80211_AUTHTYPE_FILS_PK))
4593                        return false;
4594                return true;
4595        case NL80211_CMD_CONNECT:
4596                if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
4597                    !wiphy_ext_feature_isset(&rdev->wiphy,
4598                                             NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
4599                    auth_type == NL80211_AUTHTYPE_SAE)
4600                        return false;
4601
4602                /* FILS with SK PFS or PK not supported yet */
4603                if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4604                    auth_type == NL80211_AUTHTYPE_FILS_PK)
4605                        return false;
4606                if (!wiphy_ext_feature_isset(
4607                            &rdev->wiphy,
4608                            NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
4609                    auth_type == NL80211_AUTHTYPE_FILS_SK)
4610                        return false;
4611                return true;
4612        case NL80211_CMD_START_AP:
4613                /* SAE not supported yet */
4614                if (auth_type == NL80211_AUTHTYPE_SAE)
4615                        return false;
4616                /* FILS not supported yet */
4617                if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
4618                    auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4619                    auth_type == NL80211_AUTHTYPE_FILS_PK)
4620                        return false;
4621                return true;
4622        default:
4623                return false;
4624        }
4625}
4626
4627static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
4628{
4629        struct cfg80211_registered_device *rdev = info->user_ptr[0];
4630        struct net_device *dev = info->user_ptr[1];
4631        struct wireless_dev *wdev = dev->ieee80211_ptr;
4632        struct cfg80211_ap_settings params;
4633        int err;
4634
4635        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4636            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4637                return -EOPNOTSUPP;
4638
4639        if (!rdev->ops->start_ap)
4640                return -EOPNOTSUPP;
4641
4642        if (wdev->beacon_interval)
4643                return -EALREADY;
4644
4645        memset(&params, 0, sizeof(params));
4646
4647        /* these are required for START_AP */
4648        if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
4649            !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
4650            !info->attrs[NL80211_ATTR_BEACON_HEAD])
4651                return -EINVAL;
4652
4653        err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon);
4654        if (err)
4655                return err;
4656
4657        params.beacon_interval =
4658                nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4659        params.dtim_period =
4660                nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
4661
4662        err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
4663                                           params.beacon_interval);
4664        if (err)
4665                return err;
4666
4667        /*
4668         * In theory, some of these attributes should be required here
4669         * but since they were not used when the command was originally
4670         * added, keep them optional for old user space programs to let
4671         * them continue to work with drivers that do not need the
4672         * additional information -- drivers must check!
4673         */
4674        if (info->attrs[NL80211_ATTR_SSID]) {
4675                params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4676                params.ssid_len =
4677                        nla_len(info->attrs[NL80211_ATTR_SSID]);
4678                if (params.ssid_len == 0 ||
4679                    params.ssid_len > IEEE80211_MAX_SSID_LEN)
4680                        return -EINVAL;
4681        }
4682
4683        if (info->attrs[NL80211_ATTR_HIDDEN_SSID])
4684                params.hidden_ssid = nla_get_u32(
4685                        info->attrs[NL80211_ATTR_HIDDEN_SSID]);
4686
4687        params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
4688
4689        if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4690                params.auth_type = nla_get_u32(
4691                        info->attrs[NL80211_ATTR_AUTH_TYPE]);
4692                if (!nl80211_valid_auth_type(rdev, params.auth_type,
4693                                             NL80211_CMD_START_AP))
4694                        return -EINVAL;
4695        } else
4696                params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4697
4698        err = nl80211_crypto_settings(rdev, info, &params.crypto,
4699                                      NL80211_MAX_NR_CIPHER_SUITES);
4700        if (err)
4701                return err;
4702
4703        if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
4704                if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
4705                        return -EOPNOTSUPP;
4706                params.inactivity_timeout = nla_get_u16(
4707                        info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
4708        }
4709
4710        if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4711                if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4712                        return -EINVAL;
4713                params.p2p_ctwindow =
4714                        nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4715                if (params.p2p_ctwindow != 0 &&
4716                    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4717                        return -EINVAL;
4718        }
4719
4720        if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4721                u8 tmp;
4722
4723                if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4724                        return -EINVAL;
4725                tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4726                params.p2p_opp_ps = tmp;
4727                if (params.p2p_opp_ps != 0 &&
4728                    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4729                        return -EINVAL;
4730        }
4731
4732        if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4733                err = nl80211_parse_chandef(rdev, info, &params.chandef);
4734                if (err)
4735                        return err;
4736        } else if (wdev->preset_chandef.chan) {
4737                params.chandef = wdev->preset_chandef;
4738        } else if (!nl80211_get_ap_channel(rdev, &params))
4739                return -EINVAL;
4740
4741        if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
4742                                           wdev->iftype))
4743                return -EINVAL;
4744
4745        if (info->attrs[NL80211_ATTR_TX_RATES]) {
4746                err = nl80211_parse_tx_bitrate_mask(info, &params.beacon_rate);
4747                if (err)
4748                        return err;
4749
4750                err = validate_beacon_tx_rate(rdev, params.chandef.chan->band,
4751                                              &params.beacon_rate);
4752                if (err)
4753                        return err;
4754        }
4755
4756        if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
4757                params.smps_mode =
4758                        nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
4759                switch (params.smps_mode) {
4760                case NL80211_SMPS_OFF:
4761                        break;
4762                case NL80211_SMPS_STATIC:
4763                        if (!(rdev->wiphy.features &
4764                              NL80211_FEATURE_STATIC_SMPS))
4765                                return -EINVAL;
4766                        break;
4767                case NL80211_SMPS_DYNAMIC:
4768                        if (!(rdev->wiphy.features &
4769                              NL80211_FEATURE_DYNAMIC_SMPS))
4770                                return -EINVAL;
4771                        break;
4772                default:
4773                        return -EINVAL;
4774                }
4775        } else {
4776                params.smps_mode = NL80211_SMPS_OFF;
4777        }
4778
4779        params.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
4780        if (params.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ])
4781                return -EOPNOTSUPP;
4782
4783        if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
4784                params.acl = parse_acl_data(&rdev->wiphy, info);
4785                if (IS_ERR(params.acl))
4786                        return PTR_ERR(params.acl);
4787        }
4788
4789        params.twt_responder =
4790                    nla_get_flag(info->attrs[NL80211_ATTR_TWT_RESPONDER]);
4791
4792        if (info->attrs[NL80211_ATTR_HE_OBSS_PD]) {
4793                err = nl80211_parse_he_obss_pd(
4794                                        info->attrs[NL80211_ATTR_HE_OBSS_PD],
4795                                        &params.he_obss_pd);
4796                if (err)
4797                        return err;
4798        }
4799
4800        nl80211_calculate_ap_params(&params);
4801
4802        if (info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])
4803                params.flags |= AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
4804
4805        wdev_lock(wdev);
4806        err = rdev_start_ap(rdev, dev, &params);
4807        if (!err) {
4808                wdev->preset_chandef = params.chandef;
4809                wdev->beacon_interval = params.beacon_interval;
4810                wdev->chandef = params.chandef;
4811                wdev->ssid_len = params.ssid_len;
4812                memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
4813
4814                if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
4815                        wdev->conn_owner_nlportid = info->snd_portid;
4816        }
4817        wdev_unlock(wdev);
4818
4819        kfree(params.acl);
4820
4821        return err;
4822}
4823
4824static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
4825{
4826        struct cfg80211_registered_device *rdev = info->user_ptr[0];
4827        struct net_device *dev = info->user_ptr[1];
4828        struct wireless_dev *wdev = dev->ieee80211_ptr;
4829        struct cfg80211_beacon_data params;
4830        int err;
4831
4832        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4833            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4834                return -EOPNOTSUPP;
4835
4836        if (!rdev->ops->change_beacon)
4837                return -EOPNOTSUPP;
4838
4839        if (!wdev->beacon_interval)
4840                return -EINVAL;
4841
4842        err = nl80211_parse_beacon(rdev, info->attrs, &params);
4843        if (err)
4844                return err;
4845
4846        wdev_lock(wdev);
4847        err = rdev_change_beacon(rdev, dev, &params);
4848        wdev_unlock(wdev);
4849
4850        return err;
4851}
4852
4853static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
4854{
4855        struct cfg80211_registered_device *rdev = info->user_ptr[0];
4856        struct net_device *dev = info->user_ptr[1];
4857
4858        return cfg80211_stop_ap(rdev, dev, false);
4859}
4860
4861static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
4862        [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
4863        [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
4864        [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
4865        [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
4866        [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
4867        [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
4868};
4869
4870static int parse_station_flags(struct genl_info *info,
4871                               enum nl80211_iftype iftype,
4872                               struct station_parameters *params)
4873{
4874        struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
4875        struct nlattr *nla;
4876        int flag;
4877
4878        /*
4879         * Try parsing the new attribute first so userspace
4880         * can specify both for older kernels.
4881         */
4882        nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
4883        if (nla) {
4884                struct nl80211_sta_flag_update *sta_flags;
4885
4886                sta_flags = nla_data(nla);
4887                params->sta_flags_mask = sta_flags->mask;
4888                params->sta_flags_set = sta_flags->set;
4889                params->sta_flags_set &= params->sta_flags_mask;
4890                if ((params->sta_flags_mask |
4891                     params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
4892                        return -EINVAL;
4893                return 0;
4894        }
4895
4896        /* if present, parse the old attribute */
4897
4898        nla = info->attrs[NL80211_ATTR_STA_FLAGS];
4899        if (!nla)
4900                return 0;
4901
4902        if (nla_parse_nested_deprecated(flags, NL80211_STA_FLAG_MAX, nla, sta_flags_policy, info->extack))
4903                return -EINVAL;
4904
4905        /*
4906         * Only allow certain flags for interface types so that
4907         * other attributes are silently ignored. Remember that
4908         * this is backward compatibility code with old userspace
4909         * and shouldn't be hit in other cases anyway.
4910         */
4911        switch (iftype) {
4912        case NL80211_IFTYPE_AP:
4913        case NL80211_IFTYPE_AP_VLAN:
4914        case NL80211_IFTYPE_P2P_GO:
4915                params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4916                                         BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
4917                                         BIT(NL80211_STA_FLAG_WME) |
4918                                         BIT(NL80211_STA_FLAG_MFP);
4919                break;
4920        case NL80211_IFTYPE_P2P_CLIENT:
4921        case NL80211_IFTYPE_STATION:
4922                params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4923                                         BIT(NL80211_STA_FLAG_TDLS_PEER);
4924                break;
4925        case NL80211_IFTYPE_MESH_POINT:
4926                params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4927                                         BIT(NL80211_STA_FLAG_MFP) |
4928                                         BIT(NL80211_STA_FLAG_AUTHORIZED);
4929                break;
4930        default:
4931                return -EINVAL;
4932        }
4933
4934        for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
4935                if (flags[flag]) {
4936                        params->sta_flags_set |= (1<<flag);
4937
4938                        /* no longer support new API additions in old API */
4939                        if (flag > NL80211_STA_FLAG_MAX_OLD_API)
4940                                return -EINVAL;
4941                }
4942        }
4943
4944        return 0;
4945}
4946
4947bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info, int attr)
4948{
4949        struct nlattr *rate;
4950        u32 bitrate;
4951        u16 bitrate_compat;
4952        enum nl80211_rate_info rate_flg;
4953
4954        rate = nla_nest_start_noflag(msg, attr);
4955        if (!rate)
4956                return false;
4957
4958        /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
4959        bitrate = cfg80211_calculate_bitrate(info);
4960        /* report 16-bit bitrate only if we can */
4961        bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
4962        if (bitrate > 0 &&
4963            nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
4964                return false;
4965        if (bitrate_compat > 0 &&
4966            nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
4967                return false;
4968
4969        switch (info->bw) {
4970        case RATE_INFO_BW_5:
4971                rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
4972                break;
4973        case RATE_INFO_BW_10:
4974                rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
4975                break;
4976        default:
4977                WARN_ON(1);
4978                /* fall through */
4979        case RATE_INFO_BW_20:
4980                rate_flg = 0;
4981                break;
4982        case RATE_INFO_BW_40:
4983                rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
4984                break;
4985        case RATE_INFO_BW_80:
4986                rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
4987                break;
4988        case RATE_INFO_BW_160:
4989                rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
4990                break;
4991        case RATE_INFO_BW_HE_RU:
4992                rate_flg = 0;
4993                WARN_ON(!(info->flags & RATE_INFO_FLAGS_HE_MCS));
4994        }
4995
4996        if (rate_flg && nla_put_flag(msg, rate_flg))
4997                return false;
4998
4999        if (info->flags & RATE_INFO_FLAGS_MCS) {
5000                if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
5001                        return false;
5002                if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
5003                    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
5004                        return false;
5005        } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
5006                if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
5007                        return false;
5008                if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
5009                        return false;
5010                if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
5011                    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
5012                        return false;
5013        } else if (info->flags & RATE_INFO_FLAGS_HE_MCS) {
5014                if (nla_put_u8(msg, NL80211_RATE_INFO_HE_MCS, info->mcs))
5015                        return false;
5016                if (nla_put_u8(msg, NL80211_RATE_INFO_HE_NSS, info->nss))
5017                        return false;
5018                if (nla_put_u8(msg, NL80211_RATE_INFO_HE_GI, info->he_gi))
5019                        return false;
5020                if (nla_put_u8(msg, NL80211_RATE_INFO_HE_DCM, info->he_dcm))
5021                        return false;
5022                if (info->bw == RATE_INFO_BW_HE_RU &&
5023                    nla_put_u8(msg, NL80211_RATE_INFO_HE_RU_ALLOC,
5024                               info->he_ru_alloc))
5025                        return false;
5026        }
5027
5028        nla_nest_end(msg, rate);
5029        return true;
5030}
5031
5032static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
5033                               int id)
5034{
5035        void *attr;
5036        int i = 0;
5037
5038        if (!mask)
5039                return true;
5040
5041        attr = nla_nest_start_noflag(msg, id);
5042        if (!attr)
5043                return false;
5044
5045        for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
5046                if (!(mask & BIT(i)))
5047                        continue;
5048
5049                if (nla_put_u8(msg, i, signal[i]))
5050                        return false;
5051        }
5052
5053        nla_nest_end(msg, attr);
5054
5055        return true;
5056}
5057
5058static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
5059                                u32 seq, int flags,
5060                                struct cfg80211_registered_device *rdev,
5061                                struct net_device *dev,
5062                                const u8 *mac_addr, struct station_info *sinfo)
5063{
5064        void *hdr;
5065        struct nlattr *sinfoattr, *bss_param;
5066
5067        hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
5068        if (!hdr) {
5069                cfg80211_sinfo_release_content(sinfo);
5070                return -1;
5071        }
5072
5073        if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5074            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
5075            nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
5076                goto nla_put_failure;
5077
5078        sinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_STA_INFO);
5079        if (!sinfoattr)
5080                goto nla_put_failure;
5081
5082#define PUT_SINFO(attr, memb, type) do {                                \
5083        BUILD_BUG_ON(sizeof(type) == sizeof(u64));                      \
5084        if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&       \
5085            nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,            \
5086                             sinfo->memb))                              \
5087                goto nla_put_failure;                                   \
5088        } while (0)
5089#define PUT_SINFO_U64(attr, memb) do {                                  \
5090        if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&       \
5091            nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,           \
5092                              sinfo->memb, NL80211_STA_INFO_PAD))       \
5093                goto nla_put_failure;                                   \
5094        } while (0)
5095
5096        PUT_SINFO(CONNECTED_TIME, connected_time, u32);
5097        PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
5098        PUT_SINFO_U64(ASSOC_AT_BOOTTIME, assoc_at);
5099
5100        if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
5101                             BIT_ULL(NL80211_STA_INFO_RX_BYTES64)) &&
5102            nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
5103                        (u32)sinfo->rx_bytes))
5104                goto nla_put_failure;
5105
5106        if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
5107                             BIT_ULL(NL80211_STA_INFO_TX_BYTES64)) &&
5108            nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
5109                        (u32)sinfo->tx_bytes))
5110                goto nla_put_failure;
5111
5112        PUT_SINFO_U64(RX_BYTES64, rx_bytes);
5113        PUT_SINFO_U64(TX_BYTES64, tx_bytes);
5114        PUT_SINFO(LLID, llid, u16);
5115        PUT_SINFO(PLID, plid, u16);
5116        PUT_SINFO(PLINK_STATE, plink_state, u8);
5117        PUT_SINFO_U64(RX_DURATION, rx_duration);
5118        PUT_SINFO_U64(TX_DURATION, tx_duration);
5119
5120        if (wiphy_ext_feature_isset(&rdev->wiphy,
5121                                    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5122                PUT_SINFO(AIRTIME_WEIGHT, airtime_weight, u16);
5123
5124        switch (rdev->wiphy.signal_type) {
5125        case CFG80211_SIGNAL_TYPE_MBM:
5126                PUT_SINFO(SIGNAL, signal, u8);
5127                PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
5128                break;
5129        default:
5130                break;
5131        }
5132        if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) {
5133                if (!nl80211_put_signal(msg, sinfo->chains,
5134                                        sinfo->chain_signal,
5135                                        NL80211_STA_INFO_CHAIN_SIGNAL))
5136                        goto nla_put_failure;
5137        }
5138        if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
5139                if (!nl80211_put_signal(msg, sinfo->chains,
5140                                        sinfo->chain_signal_avg,
5141                                        NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
5142                        goto nla_put_failure;
5143        }
5144        if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) {
5145                if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
5146                                          NL80211_STA_INFO_TX_BITRATE))
5147                        goto nla_put_failure;
5148        }
5149        if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) {
5150                if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
5151                                          NL80211_STA_INFO_RX_BITRATE))
5152                        goto nla_put_failure;
5153        }
5154
5155        PUT_SINFO(RX_PACKETS, rx_packets, u32);
5156        PUT_SINFO(TX_PACKETS, tx_packets, u32);
5157        PUT_SINFO(TX_RETRIES, tx_retries, u32);
5158        PUT_SINFO(TX_FAILED, tx_failed, u32);
5159        PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
5160        PUT_SINFO(AIRTIME_LINK_METRIC, airtime_link_metric, u32);
5161        PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
5162        PUT_SINFO(LOCAL_PM, local_pm, u32);
5163        PUT_SINFO(PEER_PM, peer_pm, u32);
5164        PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
5165        PUT_SINFO(CONNECTED_TO_GATE, connected_to_gate, u8);
5166
5167        if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_BSS_PARAM)) {
5168                bss_param = nla_nest_start_noflag(msg,
5169                                                  NL80211_STA_INFO_BSS_PARAM);
5170                if (!bss_param)
5171                        goto nla_put_failure;
5172
5173                if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
5174                     nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
5175                    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
5176                     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
5177                    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
5178                     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
5179                    nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
5180                               sinfo->bss_param.dtim_period) ||
5181                    nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
5182                                sinfo->bss_param.beacon_interval))
5183                        goto nla_put_failure;
5184
5185                nla_nest_end(msg, bss_param);
5186        }
5187        if ((sinfo->filled & BIT_ULL(NL80211_STA_INFO_STA_FLAGS)) &&
5188            nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
5189                    sizeof(struct nl80211_sta_flag_update),
5190                    &sinfo->sta_flags))
5191                goto nla_put_failure;
5192
5193        PUT_SINFO_U64(T_OFFSET, t_offset);
5194        PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
5195        PUT_SINFO_U64(BEACON_RX, rx_beacon);
5196        PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
5197        PUT_SINFO(RX_MPDUS, rx_mpdu_count, u32);
5198        PUT_SINFO(FCS_ERROR_COUNT, fcs_err_count, u32);
5199        if (wiphy_ext_feature_isset(&rdev->wiphy,
5200                                    NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT)) {
5201                PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
5202                PUT_SINFO(ACK_SIGNAL_AVG, avg_ack_signal, s8);
5203        }
5204
5205#undef PUT_SINFO
5206#undef PUT_SINFO_U64
5207
5208        if (sinfo->pertid) {
5209                struct nlattr *tidsattr;
5210                int tid;
5211
5212                tidsattr = nla_nest_start_noflag(msg,
5213                                                 NL80211_STA_INFO_TID_STATS);
5214                if (!tidsattr)
5215                        goto nla_put_failure;
5216
5217                for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
5218                        struct cfg80211_tid_stats *tidstats;
5219                        struct nlattr *tidattr;
5220
5221                        tidstats = &sinfo->pertid[tid];
5222
5223                        if (!tidstats->filled)
5224                                continue;
5225
5226                        tidattr = nla_nest_start_noflag(msg, tid + 1);
5227                        if (!tidattr)
5228                                goto nla_put_failure;
5229
5230#define PUT_TIDVAL_U64(attr, memb) do {                                 \
5231        if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&       \
5232            nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,          \
5233                              tidstats->memb, NL80211_TID_STATS_PAD))   \
5234                goto nla_put_failure;                                   \
5235        } while (0)
5236
5237                        PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
5238                        PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
5239                        PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
5240                        PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
5241
5242#undef PUT_TIDVAL_U64
5243                        if ((tidstats->filled &
5244                             BIT(NL80211_TID_STATS_TXQ_STATS)) &&
5245                            !nl80211_put_txq_stats(msg, &tidstats->txq_stats,
5246                                                   NL80211_TID_STATS_TXQ_STATS))
5247                                goto nla_put_failure;
5248
5249                        nla_nest_end(msg, tidattr);
5250                }
5251
5252                nla_nest_end(msg, tidsattr);
5253        }
5254
5255        nla_nest_end(msg, sinfoattr);
5256
5257        if (sinfo->assoc_req_ies_len &&
5258            nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
5259                    sinfo->assoc_req_ies))
5260                goto nla_put_failure;
5261
5262        cfg80211_sinfo_release_content(sinfo);
5263        genlmsg_end(msg, hdr);
5264        return 0;
5265
5266 nla_put_failure:
5267        cfg80211_sinfo_release_content(sinfo);
5268        genlmsg_cancel(msg, hdr);
5269        return -EMSGSIZE;
5270}
5271
5272static int nl80211_dump_station(struct sk_buff *skb,
5273                                struct netlink_callback *cb)
5274{
5275        struct station_info sinfo;
5276        struct cfg80211_registered_device *rdev;
5277        struct wireless_dev *wdev;
5278        u8 mac_addr[ETH_ALEN];
5279        int sta_idx = cb->args[2];
5280        int err;
5281
5282        rtnl_lock();
5283        err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
5284        if (err)
5285                goto out_err;
5286
5287        if (!wdev->netdev) {
5288                err = -EINVAL;
5289                goto out_err;
5290        }
5291
5292        if (!rdev->ops->dump_station) {
5293                err = -EOPNOTSUPP;
5294                goto out_err;
5295        }
5296
5297        while (1) {
5298                memset(&sinfo, 0, sizeof(sinfo));
5299                err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
5300                                        mac_addr, &sinfo);
5301                if (err == -ENOENT)
5302                        break;
5303                if (err)
5304                        goto out_err;
5305
5306                if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
5307                                NETLINK_CB(cb->skb).portid,
5308                                cb->nlh->nlmsg_seq, NLM_F_MULTI,
5309                                rdev, wdev->netdev, mac_addr,
5310                                &sinfo) < 0)
5311                        goto out;
5312
5313                sta_idx++;
5314        }
5315
5316 out:
5317        cb->args[2] = sta_idx;
5318        err = skb->len;
5319 out_err:
5320        rtnl_unlock();
5321
5322        return err;
5323}
5324
5325static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
5326{
5327        struct cfg80211_registered_device *rdev = info->user_ptr[0];
5328        struct net_device *dev = info->user_ptr[1];
5329        struct station_info sinfo;
5330        struct sk_buff *msg;
5331        u8 *mac_addr = NULL;
5332        int err;
5333
5334        memset(&sinfo, 0, sizeof(sinfo));
5335
5336        if (!info->attrs[NL80211_ATTR_MAC])
5337                return -EINVAL;
5338
5339        mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5340
5341        if (!rdev->ops->get_station)
5342                return -EOPNOTSUPP;
5343
5344        err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
5345        if (err)
5346                return err;
5347
5348        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5349        if (!msg) {
5350                cfg80211_sinfo_release_content(&sinfo);
5351                return -ENOMEM;
5352        }
5353
5354        if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
5355                                 info->snd_portid, info->snd_seq, 0,
5356                                 rdev, dev, mac_addr, &sinfo) < 0) {
5357                nlmsg_free(msg);
5358                return -ENOBUFS;
5359        }
5360
5361        return genlmsg_reply(msg, info);
5362}
5363
5364int cfg80211_check_station_change(struct wiphy *wiphy,
5365                                  struct station_parameters *params,
5366                                  enum cfg80211_station_type statype)
5367{
5368        if (params->listen_interval != -1 &&
5369            statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5370                return -EINVAL;
5371
5372        if (params->support_p2p_ps != -1 &&
5373            statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5374                return -EINVAL;
5375
5376        if (params->aid &&
5377            !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
5378            statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5379                return -EINVAL;
5380
5381        /* When you run into this, adjust the code below for the new flag */
5382        BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
5383
5384        switch (statype) {
5385        case CFG80211_STA_MESH_PEER_KERNEL:
5386        case CFG80211_STA_MESH_PEER_USER:
5387                /*
5388                 * No ignoring the TDLS flag here -- the userspace mesh
5389                 * code doesn't have the bug of including TDLS in the
5390                 * mask everywhere.
5391                 */
5392                if (params->sta_flags_mask &
5393                                ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5394                                  BIT(NL80211_STA_FLAG_MFP) |
5395                                  BIT(NL80211_STA_FLAG_AUTHORIZED)))
5396                        return -EINVAL;
5397                break;
5398        case CFG80211_STA_TDLS_PEER_SETUP:
5399        case CFG80211_STA_TDLS_PEER_ACTIVE:
5400                if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
5401                        return -EINVAL;
5402                /* ignore since it can't change */
5403                params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5404                break;
5405        default:
5406                /* disallow mesh-specific things */
5407                if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
5408                        return -EINVAL;
5409                if (params->local_pm)
5410                        return -EINVAL;
5411                if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
5412                        return -EINVAL;
5413        }
5414
5415        if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
5416            statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
5417                /* TDLS can't be set, ... */
5418                if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
5419                        return -EINVAL;
5420                /*
5421                 * ... but don't bother the driver with it. This works around
5422                 * a hostapd/wpa_supplicant issue -- it always includes the
5423                 * TLDS_PEER flag in the mask even for AP mode.
5424                 */
5425                params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5426        }
5427
5428        if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
5429            statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
5430                /* reject other things that can't change */
5431                if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
5432                        return -EINVAL;
5433                if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
5434                        return -EINVAL;
5435                if (params->supported_rates)
5436                        return -EINVAL;
5437                if (params->ext_capab || params->ht_capa || params->vht_capa ||
5438                    params->he_capa)
5439                        return -EINVAL;
5440        }
5441
5442        if (statype != CFG80211_STA_AP_CLIENT &&
5443            statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
5444                if (params->vlan)
5445                        return -EINVAL;
5446        }
5447
5448        switch (statype) {
5449        case CFG80211_STA_AP_MLME_CLIENT:
5450                /* Use this only for authorizing/unauthorizing a station */
5451                if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
5452                        return -EOPNOTSUPP;
5453                break;
5454        case CFG80211_STA_AP_CLIENT:
5455        case CFG80211_STA_AP_CLIENT_UNASSOC:
5456                /* accept only the listed bits */
5457                if (params->sta_flags_mask &
5458                                ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
5459                                  BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5460                                  BIT(NL80211_STA_FLAG_ASSOCIATED) |
5461                                  BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
5462                                  BIT(NL80211_STA_FLAG_WME) |
5463                                  BIT(NL80211_STA_FLAG_MFP)))
5464                        return -EINVAL;
5465
5466                /* but authenticated/associated only if driver handles it */
5467                if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
5468                    params->sta_flags_mask &
5469                                (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5470                                 BIT(NL80211_STA_FLAG_ASSOCIATED)))
5471                        return -EINVAL;
5472                break;
5473        case CFG80211_STA_IBSS:
5474        case CFG80211_STA_AP_STA:
5475                /* reject any changes other than AUTHORIZED */
5476                if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
5477                        return -EINVAL;
5478                break;
5479        case CFG80211_STA_TDLS_PEER_SETUP:
5480                /* reject any changes other than AUTHORIZED or WME */
5481                if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
5482                                               BIT(NL80211_STA_FLAG_WME)))
5483                        return -EINVAL;
5484                /* force (at least) rates when authorizing */
5485                if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
5486                    !params->supported_rates)
5487                        return -EINVAL;
5488                break;
5489        case CFG80211_STA_TDLS_PEER_ACTIVE:
5490                /* reject any changes */
5491                return -EINVAL;
5492        case CFG80211_STA_MESH_PEER_KERNEL:
5493                if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
5494                        return -EINVAL;
5495                break;
5496        case CFG80211_STA_MESH_PEER_USER:
5497                if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
5498                    params->plink_action != NL80211_PLINK_ACTION_BLOCK)
5499                        return -EINVAL;
5500                break;
5501        }
5502
5503        /*
5504         * Older kernel versions ignored this attribute entirely, so don't
5505         * reject attempts to update it but mark it as unused instead so the
5506         * driver won't look at the data.
5507         */
5508        if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
5509            statype != CFG80211_STA_TDLS_PEER_SETUP)
5510                params->opmode_notif_used = false;
5511
5512        return 0;
5513}
5514EXPORT_SYMBOL(cfg80211_check_station_change);
5515
5516/*
5517 * Get vlan interface making sure it is running and on the right wiphy.
5518 */
5519static struct net_device *get_vlan(struct genl_info *info,
5520                                   struct cfg80211_registered_device *rdev)
5521{
5522        struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
5523        struct net_device *v;
5524        int ret;
5525
5526        if (!vlanattr)
5527                return NULL;
5528
5529        v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
5530        if (!v)
5531                return ERR_PTR(-ENODEV);
5532
5533        if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
5534                ret = -EINVAL;
5535                goto error;
5536        }
5537
5538        if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5539            v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5540            v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
5541                ret = -EINVAL;
5542                goto error;
5543        }
5544
5545        if (!netif_running(v)) {
5546                ret = -ENETDOWN;
5547                goto error;
5548        }
5549
5550        return v;
5551 error:
5552        dev_put(v);
5553        return ERR_PTR(ret);
5554}
5555
5556static const struct nla_policy
5557nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
5558        [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
5559        [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
5560};
5561
5562static int nl80211_parse_sta_wme(struct genl_info *info,
5563                                 struct station_parameters *params)
5564{
5565        struct nlattr *tb[NL80211_STA_WME_MAX + 1];
5566        struct nlattr *nla;
5567        int err;
5568
5569        /* parse WME attributes if present */
5570        if (!info->attrs[NL80211_ATTR_STA_WME])
5571                return 0;
5572
5573        nla = info->attrs[NL80211_ATTR_STA_WME];
5574        err = nla_parse_nested_deprecated(tb, NL80211_STA_WME_MAX, nla,
5575                                          nl80211_sta_wme_policy,
5576                                          info->extack);
5577        if (err)
5578                return err;
5579
5580        if (tb[NL80211_STA_WME_UAPSD_QUEUES])
5581                params->uapsd_queues = nla_get_u8(
5582                        tb[NL80211_STA_WME_UAPSD_QUEUES]);
5583        if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
5584                return -EINVAL;
5585
5586        if (tb[NL80211_STA_WME_MAX_SP])
5587                params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
5588
5589        if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
5590                return -EINVAL;
5591
5592        params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
5593
5594        return 0;
5595}
5596
5597static int nl80211_parse_sta_channel_info(struct genl_info *info,
5598                                      struct station_parameters *params)
5599{
5600        if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
5601                params->supported_channels =
5602                     nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
5603                params->supported_channels_len =
5604                     nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
5605                /*
5606                 * Need to include at least one (first channel, number of
5607                 * channels) tuple for each subband, and must have proper
5608                 * tuples for the rest of the data as well.
5609                 */
5610                if (params->supported_channels_len < 2)
5611                        return -EINVAL;
5612                if (params->supported_channels_len % 2)
5613                        return -EINVAL;
5614        }
5615
5616        if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
5617                params->supported_oper_classes =
5618                 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
5619                params->supported_oper_classes_len =
5620                  nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
5621                /*
5622                 * The value of the Length field of the Supported Operating
5623                 * Classes element is between 2 and 253.
5624                 */
5625                if (params->supported_oper_classes_len < 2 ||
5626                    params->supported_oper_classes_len > 253)
5627                        return -EINVAL;
5628        }
5629        return 0;
5630}
5631
5632static int nl80211_set_station_tdls(struct genl_info *info,
5633                                    struct station_parameters *params)
5634{
5635        int err;
5636        /* Dummy STA entry gets updated once the peer capabilities are known */
5637        if (info->attrs[NL80211_ATTR_PEER_AID])
5638                params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
5639        if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
5640                params->ht_capa =
5641                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5642        if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
5643                params->vht_capa =
5644                        nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
5645        if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
5646                params->he_capa =
5647                        nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5648                params->he_capa_len =
5649                        nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5650
5651                if (params->he_capa_len < NL80211_HE_MIN_CAPABILITY_LEN)
5652                        return -EINVAL;
5653        }
5654
5655        err = nl80211_parse_sta_channel_info(info, params);
5656        if (err)
5657                return err;
5658
5659        return nl80211_parse_sta_wme(info, params);
5660}
5661
5662static int nl80211_parse_sta_txpower_setting(struct genl_info *info,
5663                                             struct station_parameters *params)
5664{
5665        struct cfg80211_registered_device *rdev = info->user_ptr[0];
5666        int idx;
5667
5668        if (info->attrs[NL80211_ATTR_STA_TX_POWER_SETTING]) {
5669                if (!rdev->ops->set_tx_power ||
5670                    !wiphy_ext_feature_isset(&rdev->wiphy,
5671                                         NL80211_EXT_FEATURE_STA_TX_PWR))
5672                        return -EOPNOTSUPP;
5673
5674                idx = NL80211_ATTR_STA_TX_POWER_SETTING;
5675                params->txpwr.type = nla_get_u8(info->attrs[idx]);
5676
5677                if (params->txpwr.type == NL80211_TX_POWER_LIMITED) {
5678                        idx = NL80211_ATTR_STA_TX_POWER;
5679
5680                        if (info->attrs[idx])
5681                                params->txpwr.power =
5682                                        nla_get_s16(info->attrs[idx]);
5683                        else
5684                                return -EINVAL;
5685                }
5686                params->sta_modify_mask |= STATION_PARAM_APPLY_STA_TXPOWER;
5687        }
5688
5689        return 0;
5690}
5691
5692static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
5693{
5694        struct cfg80211_registered_device *rdev = info->user_ptr[0];
5695        struct net_device *dev = info->user_ptr[1];
5696        struct station_parameters params;
5697        u8 *mac_addr;
5698        int err;
5699
5700        memset(&params, 0, sizeof(params));
5701
5702        if (!rdev->ops->change_station)
5703                return -EOPNOTSUPP;
5704
5705        /*
5706         * AID and listen_interval properties can be set only for unassociated
5707         * station. Include these parameters here and will check them in
5708         * cfg80211_check_station_change().
5709         */
5710        if (info->attrs[NL80211_ATTR_STA_AID])
5711                params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
5712
5713        if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
5714                params.listen_interval =
5715                     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
5716        else
5717                params.listen_interval = -1;
5718
5719        if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS])
5720                params.support_p2p_ps =
5721                        nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
5722        else
5723                params.support_p2p_ps = -1;
5724
5725        if (!info->attrs[NL80211_ATTR_MAC])
5726                return -EINVAL;
5727
5728        mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5729
5730        if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
5731                params.supported_rates =
5732                        nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5733                params.supported_rates_len =
5734                        nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5735        }
5736
5737        if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
5738                params.capability =
5739                        nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
5740                params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
5741        }
5742
5743        if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
5744                params.ext_capab =
5745                        nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5746                params.ext_capab_len =
5747                        nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5748        }
5749
5750        if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5751                return -EINVAL;
5752
5753        if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
5754                params.plink_action =
5755                        nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5756
5757        if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
5758                params.plink_state =
5759                        nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
5760                if (info->attrs[NL80211_ATTR_MESH_PEER_AID])
5761                        params.peer_aid = nla_get_u16(
5762                                info->attrs[NL80211_ATTR_MESH_PEER_AID]);
5763                params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
5764        }
5765
5766        if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE])
5767                params.local_pm = nla_get_u32(
5768                        info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
5769
5770        if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5771                params.opmode_notif_used = true;
5772                params.opmode_notif =
5773                        nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5774        }
5775
5776        if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
5777                params.airtime_weight =
5778                        nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
5779
5780        if (params.airtime_weight &&
5781            !wiphy_ext_feature_isset(&rdev->wiphy,
5782                                     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5783                return -EOPNOTSUPP;
5784
5785        err = nl80211_parse_sta_txpower_setting(info, &params);
5786        if (err)
5787                return err;
5788
5789        /* Include parameters for TDLS peer (will check later) */
5790        err = nl80211_set_station_tdls(info, &params);
5791        if (err)
5792                return err;
5793
5794        params.vlan = get_vlan(info, rdev);
5795        if (IS_ERR(params.vlan))
5796                return PTR_ERR(params.vlan);
5797
5798        switch (dev->ieee80211_ptr->iftype) {
5799        case NL80211_IFTYPE_AP:
5800        case NL80211_IFTYPE_AP_VLAN:
5801        case NL80211_IFTYPE_P2P_GO:
5802        case NL80211_IFTYPE_P2P_CLIENT:
5803        case NL80211_IFTYPE_STATION:
5804        case NL80211_IFTYPE_ADHOC:
5805        case NL80211_IFTYPE_MESH_POINT:
5806                break;
5807        default:
5808                err = -EOPNOTSUPP;
5809                goto out_put_vlan;
5810        }
5811
5812        /* driver will call cfg80211_check_station_change() */
5813        err = rdev_change_station(rdev, dev, mac_addr, &params);
5814
5815 out_put_vlan:
5816        if (params.vlan)
5817                dev_put(params.vlan);
5818
5819        return err;
5820}
5821
5822static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
5823{
5824        struct cfg80211_registered_device *rdev = info->user_ptr[0];
5825        int err;
5826        struct net_device *dev = info->user_ptr[1];
5827        struct station_parameters params;
5828        u8 *mac_addr = NULL;
5829        u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5830                         BIT(NL80211_STA_FLAG_ASSOCIATED);
5831
5832        memset(&params, 0, sizeof(params));
5833
5834        if (!rdev->ops->add_station)
5835                return -EOPNOTSUPP;
5836
5837        if (!info->attrs[NL80211_ATTR_MAC])
5838                return -EINVAL;
5839
5840        if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
5841                return -EINVAL;
5842
5843        if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
5844                return -EINVAL;
5845
5846        if (!info->attrs[NL80211_ATTR_STA_AID] &&
5847            !info->attrs[NL80211_ATTR_PEER_AID])
5848                return -EINVAL;
5849
5850        mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5851        params.supported_rates =
5852                nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5853        params.supported_rates_len =
5854                nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5855        params.listen_interval =
5856                nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
5857
5858        if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
5859                params.support_p2p_ps =
5860                        nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
5861        } else {
5862                /*
5863                 * if not specified, assume it's supported for P2P GO interface,
5864                 * and is NOT supported for AP interface
5865                 */
5866                params.support_p2p_ps =
5867                        dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
5868        }
5869
5870        if (info->attrs[NL80211_ATTR_PEER_AID])
5871                params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
5872        else
5873                params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
5874
5875        if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
5876                params.capability =
5877                        nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
5878                params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
5879        }
5880
5881        if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
5882                params.ext_capab =
5883                        nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5884                params.ext_capab_len =
5885                        nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5886        }
5887
5888        if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
5889                params.ht_capa =
5890                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5891
5892        if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
5893                params.vht_capa =
5894                        nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
5895
5896        if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
5897                params.he_capa =
5898                        nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5899                params.he_capa_len =
5900                        nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5901
5902                /* max len is validated in nla policy */
5903                if (params.he_capa_len < NL80211_HE_MIN_CAPABILITY_LEN)
5904                        return -EINVAL;
5905        }
5906
5907        if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5908                params.opmode_notif_used = true;
5909                params.opmode_notif =
5910                        nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5911        }
5912
5913        if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
5914                params.plink_action =
5915                        nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5916
5917        if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
5918                params.airtime_weight =
5919                        nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
5920
5921        if (params.airtime_weight &&
5922            !wiphy_ext_feature_isset(&rdev->wiphy,
5923                                     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5924                return -EOPNOTSUPP;
5925
5926        err = nl80211_parse_sta_txpower_setting(info, &params);
5927        if (err)
5928                return err;
5929
5930        err = nl80211_parse_sta_channel_info(info, &params);
5931        if (err)
5932                return err;
5933
5934        err = nl80211_parse_sta_wme(info, &params);
5935        if (err)
5936                return err;
5937
5938        if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5939                return -EINVAL;
5940
5941        /* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
5942         * as userspace might just pass through the capabilities from the IEs
5943         * directly, rather than enforcing this restriction and returning an
5944         * error in this case.
5945         */
5946        if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
5947                params.ht_capa = NULL;
5948                params.vht_capa = NULL;
5949
5950                /* HE requires WME */
5951                if (params.he_capa_len)
5952                        return -EINVAL;
5953        }
5954
5955        /* When you run into this, adjust the code below for the new flag */
5956        BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
5957
5958        switch (dev->ieee80211_ptr->iftype) {
5959        case NL80211_IFTYPE_AP:
5960        case NL80211_IFTYPE_AP_VLAN:
5961        case NL80211_IFTYPE_P2P_GO:
5962                /* ignore WME attributes if iface/sta is not capable */
5963                if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
5964                    !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
5965                        params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5966
5967                /* TDLS peers cannot be added */
5968                if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
5969                    info->attrs[NL80211_ATTR_PEER_AID])
5970                        return -EINVAL;
5971                /* but don't bother the driver with it */
5972                params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5973
5974                /* allow authenticated/associated only if driver handles it */
5975                if (!(rdev->wiphy.features &
5976                                NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
5977                    params.sta_flags_mask & auth_assoc)
5978                        return -EINVAL;
5979
5980                /* Older userspace, or userspace wanting to be compatible with
5981                 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
5982                 * and assoc flags in the mask, but assumes the station will be
5983                 * added as associated anyway since this was the required driver
5984                 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
5985                 * introduced.
5986                 * In order to not bother drivers with this quirk in the API
5987                 * set the flags in both the mask and set for new stations in
5988                 * this case.
5989                 */
5990                if (!(params.sta_flags_mask & auth_assoc)) {
5991                        params.sta_flags_mask |= auth_assoc;
5992                        params.sta_flags_set |= auth_assoc;
5993                }
5994
5995                /* must be last in here for error handling */
5996                params.vlan = get_vlan(info, rdev);
5997                if (IS_ERR(params.vlan))
5998                        return PTR_ERR(params.vlan);
5999                break;
6000        case NL80211_IFTYPE_MESH_POINT:
6001                /* ignore uAPSD data */
6002                params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
6003
6004                /* associated is disallowed */
6005                if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
6006                        return -EINVAL;
6007                /* TDLS peers cannot be added */
6008                if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
6009                    info->attrs[NL80211_ATTR_PEER_AID])
6010                        return -EINVAL;
6011                break;
6012        case NL80211_IFTYPE_STATION:
6013        case NL80211_IFTYPE_P2P_CLIENT:
6014                /* ignore uAPSD data */
6015                params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
6016
6017                /* these are disallowed */
6018                if (params.sta_flags_mask &
6019                                (BIT(NL80211_STA_FLAG_ASSOCIATED) |
6020                                 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
6021                        return -EINVAL;
6022                /* Only TDLS peers can be added */
6023                if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
6024                        return -EINVAL;
6025                /* Can only add if TDLS ... */
6026                if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
6027                        return -EOPNOTSUPP;
6028                /* ... with external setup is supported */
6029                if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
6030                        return -EOPNOTSUPP;
6031                /*
6032                 * Older wpa_supplicant versions always mark the TDLS peer
6033                 * as authorized, but it shouldn't yet be.
6034                 */
6035                params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
6036                break;
6037        default:
6038                return -EOPNOTSUPP;
6039        }
6040
6041        /* be aware of params.vlan when changing code here */
6042
6043        err = rdev_add_station(rdev, dev, mac_addr, &params);
6044
6045        if (params.vlan)
6046                dev_put(params.vlan);
6047        return err;
6048}
6049
6050static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
6051{
6052        struct cfg80211_registered_device *rdev = info->user_ptr[0];
6053        struct net_device *dev = info->user_ptr[1];
6054        struct station_del_parameters params;
6055
6056        memset(&params, 0, sizeof(params));
6057
6058        if (info->attrs[NL80211_ATTR_MAC])
6059                params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
6060
6061        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6062            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
6063            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
6064            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6065                return -EINVAL;
6066
6067        if (!rdev->ops->del_station)
6068                return -EOPNOTSUPP;
6069
6070        if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
6071                params.subtype =
6072                        nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
6073                if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
6074                    params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
6075                        return -EINVAL;
6076        } else {
6077                /* Default to Deauthentication frame */
6078                params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
6079        }
6080
6081        if (info->attrs[NL80211_ATTR_REASON_CODE]) {
6082                params.reason_code =
6083                        nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6084                if (params.reason_code == 0)
6085                        return -EINVAL; /* 0 is reserved */
6086        } else {
6087                /* Default to reason code 2 */
6088                params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
6089        }
6090
6091        return rdev_del_station(rdev, dev, &params);
6092}
6093
6094static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
6095                                int flags, struct net_device *dev,
6096                                u8 *dst, u8 *next_hop,
6097                                struct mpath_info *pinfo)
6098{
6099        void *hdr;
6100        struct nlattr *pinfoattr;
6101
6102        hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
6103        if (!hdr)
6104                return -1;
6105
6106        if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6107            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
6108            nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
6109            nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
6110                goto nla_put_failure;
6111
6112        pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MPATH_INFO);
6113        if (!pinfoattr)
6114                goto nla_put_failure;
6115        if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
6116            nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
6117                        pinfo->frame_qlen))
6118                goto nla_put_failure;
6119        if (((pinfo->filled & MPATH_INFO_SN) &&
6120             nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
6121            ((pinfo->filled & MPATH_INFO_METRIC) &&
6122             nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
6123                         pinfo->metric)) ||
6124            ((pinfo->filled & MPATH_INFO_EXPTIME) &&
6125             nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
6126                         pinfo->exptime)) ||
6127            ((pinfo->filled & MPATH_INFO_FLAGS) &&
6128             nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
6129                        pinfo->flags)) ||
6130            ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
6131             nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
6132                         pinfo->discovery_timeout)) ||
6133            ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
6134             nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
6135                        pinfo->discovery_retries)) ||
6136            ((pinfo->filled & MPATH_INFO_HOP_COUNT) &&
6137             nla_put_u8(msg, NL80211_MPATH_INFO_HOP_COUNT,
6138                        pinfo->hop_count)) ||
6139            ((pinfo->filled & MPATH_INFO_PATH_CHANGE) &&
6140             nla_put_u32(msg, NL80211_MPATH_INFO_PATH_CHANGE,
6141                         pinfo->path_change_count)))
6142                goto nla_put_failure;
6143
6144        nla_nest_end(msg, pinfoattr);
6145
6146        genlmsg_end(msg, hdr);
6147        return 0;
6148
6149 nla_put_failure:
6150        genlmsg_cancel(msg, hdr);
6151        return -EMSGSIZE;
6152}
6153
6154static int nl80211_dump_mpath(struct sk_buff *skb,
6155                              struct netlink_callback *cb)
6156{
6157        struct mpath_info pinfo;
6158        struct cfg80211_registered_device *rdev;
6159        struct wireless_dev *wdev;
6160        u8 dst[ETH_ALEN];
6161        u8 next_hop[ETH_ALEN];
6162        int path_idx = cb->args[2];
6163        int err;
6164
6165        rtnl_lock();
6166        err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
6167        if (err)
6168                goto out_err;
6169
6170        if (!rdev->ops->dump_mpath) {
6171                err = -EOPNOTSUPP;
6172                goto out_err;
6173        }
6174
6175        if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
6176                err = -EOPNOTSUPP;
6177                goto out_err;
6178        }
6179
6180        while (1) {
6181                err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
6182                                      next_hop, &pinfo);
6183                if (err == -ENOENT)
6184                        break;
6185                if (err)
6186                        goto out_err;
6187
6188                if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
6189                                       cb->nlh->nlmsg_seq, NLM_F_MULTI,
6190                                       wdev->netdev, dst, next_hop,
6191                                       &pinfo) < 0)
6192                        goto out;
6193
6194                path_idx++;
6195        }
6196
6197 out:
6198        cb->args[2] = path_idx;
6199        err = skb->len;
6200 out_err:
6201        rtnl_unlock();
6202        return err;
6203}
6204
6205static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
6206{
6207        struct cfg80211_registered_device *rdev = info->user_ptr[0];
6208        int err;
6209        struct net_device *dev = info->user_ptr[1];
6210        struct mpath_info pinfo;
6211        struct sk_buff *msg;
6212        u8 *dst = NULL;
6213        u8 next_hop[ETH_ALEN];
6214
6215        memset(&pinfo, 0, sizeof(pinfo));
6216
6217        if (!info->attrs[NL80211_ATTR_MAC])
6218                return -EINVAL;
6219
6220        dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6221
6222        if (!rdev->ops->get_mpath)
6223                return -EOPNOTSUPP;
6224
6225        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6226                return -EOPNOTSUPP;
6227
6228        err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
6229        if (err)
6230                return err;
6231
6232        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6233        if (!msg)
6234                return -ENOMEM;
6235
6236        if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
6237                                 dev, dst, next_hop, &pinfo) < 0) {
6238                nlmsg_free(msg);
6239                return -ENOBUFS;
6240        }
6241
6242        return genlmsg_reply(msg, info);
6243}
6244
6245static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
6246{
6247        struct cfg80211_registered_device *rdev = info->user_ptr[0];
6248        struct net_device *dev = info->user_ptr[1];
6249        u8 *dst = NULL;
6250        u8 *next_hop = NULL;
6251
6252        if (!info->attrs[NL80211_ATTR_MAC])
6253                return -EINVAL;
6254
6255        if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
6256                return -EINVAL;
6257
6258        dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6259        next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
6260
6261        if (!rdev->ops->change_mpath)
6262                return -EOPNOTSUPP;
6263
6264        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6265                return -EOPNOTSUPP;
6266
6267        return rdev_change_mpath(rdev, dev, dst, next_hop);
6268}
6269
6270static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
6271{
6272        struct cfg80211_registered_device *rdev = info->user_ptr[0];
6273        struct net_device *dev = info->user_ptr[1];
6274        u8 *dst = NULL;
6275        u8 *next_hop = NULL;
6276
6277        if (!info->attrs[NL80211_ATTR_MAC])
6278                return -EINVAL;
6279
6280        if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
6281                return -EINVAL;
6282
6283        dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6284        next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
6285
6286        if (!rdev->ops->add_mpath)
6287                return -EOPNOTSUPP;
6288
6289        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6290                return -EOPNOTSUPP;
6291
6292        return rdev_add_mpath(rdev, dev, dst, next_hop);
6293}
6294
6295static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
6296{
6297        struct cfg80211_registered_device *rdev = info->user_ptr[0];
6298        struct net_device *dev = info->user_ptr[1];
6299        u8 *dst = NULL;
6300
6301        if (info->attrs[NL80211_ATTR_MAC])
6302                dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6303
6304        if (!rdev->ops->del_mpath)
6305                return -EOPNOTSUPP;
6306
6307        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6308                return -EOPNOTSUPP;
6309
6310        return rdev_del_mpath(rdev, dev, dst);
6311}
6312
6313static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
6314{
6315        struct cfg80211_registered_device *rdev = info->user_ptr[0];
6316        int err;
6317        struct net_device *dev = info->user_ptr[1];
6318        struct mpath_info pinfo;
6319        struct sk_buff *msg;
6320        u8 *dst = NULL;
6321        u8 mpp[ETH_ALEN];
6322
6323        memset(&pinfo, 0, sizeof(pinfo));
6324
6325        if (!info->attrs[NL80211_ATTR_MAC])
6326                return -EINVAL;
6327
6328        dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6329
6330        if (!rdev->ops->get_mpp)
6331                return -EOPNOTSUPP;
6332
6333        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6334                return -EOPNOTSUPP;
6335
6336        err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
6337        if (err)
6338                return err;
6339
6340        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6341        if (!msg)
6342                return -ENOMEM;
6343
6344        if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
6345                               dev, dst, mpp, &pinfo) < 0) {
6346                nlmsg_free(msg);
6347                return -ENOBUFS;
6348        }
6349
6350        return genlmsg_reply(msg, info);
6351}
6352
6353static int nl80211_dump_mpp(struct sk_buff *skb,
6354                            struct netlink_callback *cb)
6355{
6356        struct mpath_info pinfo;
6357        struct cfg80211_registered_device *rdev;
6358        struct wireless_dev *wdev;
6359        u8 dst[ETH_ALEN];
6360        u8 mpp[ETH_ALEN];
6361        int path_idx = cb->args[2];
6362        int err;
6363
6364        rtnl_lock();
6365        err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
6366        if (err)
6367                goto out_err;
6368
6369        if (!rdev->ops->dump_mpp) {
6370                err = -EOPNOTSUPP;
6371                goto out_err;
6372        }
6373
6374        if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
6375                err = -EOPNOTSUPP;
6376                goto out_err;
6377        }
6378
6379        while (1) {
6380                err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
6381                                    mpp, &pinfo);
6382                if (err == -ENOENT)
6383                        break;
6384                if (err)
6385                        goto out_err;
6386
6387                if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
6388                                       cb->nlh->nlmsg_seq, NLM_F_MULTI,
6389                                       wdev->netdev, dst, mpp,
6390                                       &pinfo) < 0)
6391                        goto out;
6392
6393                path_idx++;
6394        }
6395
6396 out:
6397        cb->args[2] = path_idx;
6398        err = skb->len;
6399 out_err:
6400        rtnl_unlock();
6401        return err;
6402}
6403
6404static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
6405{
6406        struct cfg80211_registered_device *rdev = info->user_ptr[0];
6407        struct net_device *dev = info->user_ptr[1];
6408        struct wireless_dev *wdev = dev->ieee80211_ptr;
6409        struct bss_parameters params;
6410        int err;
6411
6412        memset(&params, 0, sizeof(params));
6413        /* default to not changing parameters */
6414        params.use_cts_prot = -1;
6415        params.use_short_preamble = -1;
6416        params.use_short_slot_time = -1;
6417        params.ap_isolate = -1;
6418        params.ht_opmode = -1;
6419        params.p2p_ctwindow = -1;
6420        params.p2p_opp_ps = -1;
6421
6422        if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
6423                params.use_cts_prot =
6424                    nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
6425        if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
6426                params.use_short_preamble =
6427                    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
6428        if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
6429                params.use_short_slot_time =
6430                    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
6431        if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6432                params.basic_rates =
6433                        nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6434                params.basic_rates_len =
6435                        nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6436        }
6437        if (info->attrs[NL80211_ATTR_AP_ISOLATE])
6438                params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
6439        if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
6440                params.ht_opmode =
6441                        nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
6442
6443        if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
6444                if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6445                        return -EINVAL;
6446                params.p2p_ctwindow =
6447                        nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
6448                if (params.p2p_ctwindow != 0 &&
6449                    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
6450                        return -EINVAL;
6451        }
6452
6453        if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
6454                u8 tmp;
6455
6456                if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6457                        return -EINVAL;
6458                tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
6459                params.p2p_opp_ps = tmp;
6460                if (params.p2p_opp_ps &&
6461                    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
6462                        return -EINVAL;
6463        }
6464
6465        if (!rdev->ops->change_bss)
6466                return -EOPNOTSUPP;
6467
6468        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6469            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6470                return -EOPNOTSUPP;
6471
6472        wdev_lock(wdev);
6473        err = rdev_change_bss(rdev, dev, &params);
6474        wdev_unlock(wdev);
6475
6476        return err;
6477}
6478
6479static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
6480{
6481        char *data = NULL;
6482        bool is_indoor;
6483        enum nl80211_user_reg_hint_type user_reg_hint_type;
6484        u32 owner_nlportid;
6485
6486        /*
6487         * You should only get this when cfg80211 hasn't yet initialized
6488         * completely when built-in to the kernel right between the time
6489         * window between nl80211_init() and regulatory_init(), if that is
6490         * even possible.
6491         */
6492        if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
6493                return -EINPROGRESS;
6494
6495        if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
6496                user_reg_hint_type =
6497                  nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
6498        else
6499                user_reg_hint_type = NL80211_USER_REG_HINT_USER;
6500
6501        switch (user_reg_hint_type) {
6502        case NL80211_USER_REG_HINT_USER:
6503        case NL80211_USER_REG_HINT_CELL_BASE:
6504                if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
6505                        return -EINVAL;
6506
6507                data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
6508                return regulatory_hint_user(data, user_reg_hint_type);
6509        case NL80211_USER_REG_HINT_INDOOR:
6510                if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
6511                        owner_nlportid = info->snd_portid;
6512                        is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
6513                } else {
6514                        owner_nlportid = 0;
6515                        is_indoor = true;
6516                }
6517
6518                return regulatory_hint_indoor(is_indoor, owner_nlportid);
6519        default:
6520                return -EINVAL;
6521        }
6522}
6523
6524static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
6525{
6526        return reg_reload_regdb();
6527}
6528
6529static int nl80211_get_mesh_config(struct sk_buff *skb,
6530                                   struct genl_info *info)
6531{
6532        struct cfg80211_registered_device *rdev = info->user_ptr[0];
6533        struct net_device *dev = info->user_ptr[1];
6534        struct wireless_dev *wdev = dev->ieee80211_ptr;
6535        struct mesh_config cur_params;
6536        int err = 0;
6537        void *hdr;
6538        struct nlattr *pinfoattr;
6539        struct sk_buff *msg;
6540
6541        if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
6542                return -EOPNOTSUPP;
6543
6544        if (!rdev->ops->get_mesh_config)
6545                return -EOPNOTSUPP;
6546
6547        wdev_lock(wdev);
6548        /* If not connected, get default parameters */
6549        if (!wdev->mesh_id_len)
6550                memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
6551        else
6552                err = rdev_get_mesh_config(rdev, dev, &cur_params);
6553        wdev_unlock(wdev);
6554
6555        if (err)
6556                return err;
6557
6558        /* Draw up a netlink message to send back */
6559        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6560        if (!msg)
6561                return -ENOMEM;
6562        hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
6563                             NL80211_CMD_GET_MESH_CONFIG);
6564        if (!hdr)
6565                goto out;
6566        pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MESH_CONFIG);
6567        if (!pinfoattr)
6568                goto nla_put_failure;
6569        if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6570            nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
6571                        cur_params.dot11MeshRetryTimeout) ||
6572            nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
6573                        cur_params.dot11MeshConfirmTimeout) ||
6574            nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
6575                        cur_params.dot11MeshHoldingTimeout) ||
6576            nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
6577                        cur_params.dot11MeshMaxPeerLinks) ||
6578            nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
6579                       cur_params.dot11MeshMaxRetries) ||
6580            nla_put_u8(msg, NL80211_MESHCONF_TTL,
6581                       cur_params.dot11MeshTTL) ||
6582            nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
6583                       cur_params.element_ttl) ||
6584            nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
6585                       cur_params.auto_open_plinks) ||
6586            nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
6587                        cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
6588            nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
6589                       cur_params.dot11MeshHWMPmaxPREQretries) ||
6590            nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
6591                        cur_params.path_refresh_time) ||
6592            nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
6593                        cur_params.min_discovery_timeout) ||
6594            nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
6595                        cur_params.dot11MeshHWMPactivePathTimeout) ||
6596            nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
6597                        cur_params.dot11MeshHWMPpreqMinInterval) ||
6598            nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
6599                        cur_params.dot11MeshHWMPperrMinInterval) ||
6600            nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
6601                        cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
6602            nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
6603                       cur_params.dot11MeshHWMPRootMode) ||
6604            nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
6605                        cur_params.dot11MeshHWMPRannInterval) ||
6606            nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
6607                       cur_params.dot11MeshGateAnnouncementProtocol) ||
6608            nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
6609                       cur_params.dot11MeshForwarding) ||
6610            nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
6611                        cur_params.rssi_threshold) ||
6612            nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
6613                        cur_params.ht_opmode) ||
6614            nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
6615                        cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
6616            nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
6617                        cur_params.dot11MeshHWMProotInterval) ||
6618            nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
6619                        cur_params.dot11MeshHWMPconfirmationInterval) ||
6620            nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
6621                        cur_params.power_mode) ||
6622            nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
6623                        cur_params.dot11MeshAwakeWindowDuration) ||
6624            nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
6625                        cur_params.plink_timeout) ||
6626            nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_GATE,
6627                       cur_params.dot11MeshConnectedToMeshGate))
6628                goto nla_put_failure;
6629        nla_nest_end(msg, pinfoattr);
6630        genlmsg_end(msg, hdr);
6631        return genlmsg_reply(msg, info);
6632
6633 nla_put_failure:
6634 out:
6635        nlmsg_free(msg);
6636        return -ENOBUFS;
6637}
6638
6639static const struct nla_policy
6640nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
6641        [NL80211_MESHCONF_RETRY_TIMEOUT] =
6642                NLA_POLICY_RANGE(NLA_U16, 1, 255),
6643        [NL80211_MESHCONF_CONFIRM_TIMEOUT] =
6644                NLA_POLICY_RANGE(NLA_U16, 1, 255),
6645        [NL80211_MESHCONF_HOLDING_TIMEOUT] =
6646                NLA_POLICY_RANGE(NLA_U16, 1, 255),
6647        [NL80211_MESHCONF_MAX_PEER_LINKS] =
6648                NLA_POLICY_RANGE(NLA_U16, 0, 255),
6649        [NL80211_MESHCONF_MAX_RETRIES] = NLA_POLICY_MAX(NLA_U8, 16),
6650        [NL80211_MESHCONF_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
6651        [NL80211_MESHCONF_ELEMENT_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
6652        [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = NLA_POLICY_MAX(NLA_U8, 1),
6653        [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] =
6654                NLA_POLICY_RANGE(NLA_U32, 1, 255),
6655        [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
6656        [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
6657        [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = NLA_POLICY_MIN(NLA_U16, 1),
6658        [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
6659        [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] =
6660                NLA_POLICY_MIN(NLA_U16, 1),
6661        [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] =
6662                NLA_POLICY_MIN(NLA_U16, 1),
6663        [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] =
6664                NLA_POLICY_MIN(NLA_U16, 1),
6665        [NL80211_MESHCONF_HWMP_ROOTMODE] = NLA_POLICY_MAX(NLA_U8, 4),
6666        [NL80211_MESHCONF_HWMP_RANN_INTERVAL] =
6667                NLA_POLICY_MIN(NLA_U16, 1),
6668        [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = NLA_POLICY_MAX(NLA_U8, 1),
6669        [NL80211_MESHCONF_FORWARDING] = NLA_POLICY_MAX(NLA_U8, 1),
6670        [NL80211_MESHCONF_RSSI_THRESHOLD] =
6671                NLA_POLICY_RANGE(NLA_S32, -255, 0),
6672        [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
6673        [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
6674
6675        [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] =
6676                NLA_POLICY_MIN(NLA_U16, 1),
6677        [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] =
6678                NLA_POLICY_MIN(NLA_U16, 1),
6679        [NL80211_MESHCONF_POWER_MODE] =
6680                NLA_POLICY_RANGE(NLA_U32,
6681                                 NL80211_MESH_POWER_ACTIVE,
6682                                 NL80211_MESH_POWER_MAX),
6683        [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
6684        [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
6685        [NL80211_MESHCONF_CONNECTED_TO_GATE] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
6686};
6687
6688static const struct nla_policy
6689        nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
6690        [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
6691        [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
6692        [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
6693        [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
6694        [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
6695        [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
6696        [NL80211_MESH_SETUP_IE] =
6697                NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
6698                                       IEEE80211_MAX_DATA_LEN),
6699        [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
6700};
6701
6702static int nl80211_parse_mesh_config(struct genl_info *info,
6703                                     struct mesh_config *cfg,
6704                                     u32 *mask_out)
6705{
6706        struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
6707        u32 mask = 0;
6708        u16 ht_opmode;
6709
6710#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, mask, attr, fn)       \
6711do {                                                                    \
6712        if (tb[attr]) {                                                 \
6713                cfg->param = fn(tb[attr]);                              \
6714                mask |= BIT((attr) - 1);                                \
6715        }                                                               \
6716} while (0)
6717
6718        if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
6719                return -EINVAL;
6720        if (nla_parse_nested_deprecated(tb, NL80211_MESHCONF_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_CONFIG], nl80211_meshconf_params_policy, info->extack))
6721                return -EINVAL;
6722
6723        /* This makes sure that there aren't more than 32 mesh config
6724         * parameters (otherwise our bitfield scheme would not work.) */
6725        BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
6726
6727        /* Fill in the params struct */
6728        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, mask,
6729                                  NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
6730        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, mask,
6731                                  NL80211_MESHCONF_CONFIRM_TIMEOUT,
6732                                  nla_get_u16);
6733        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, mask,
6734                                  NL80211_MESHCONF_HOLDING_TIMEOUT,
6735                                  nla_get_u16);
6736        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, mask,
6737                                  NL80211_MESHCONF_MAX_PEER_LINKS,
6738                                  nla_get_u16);
6739        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, mask,
6740                                  NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
6741        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, mask,
6742                                  NL80211_MESHCONF_TTL, nla_get_u8);
6743        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, mask,
6744                                  NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
6745        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, mask,
6746                                  NL80211_MESHCONF_AUTO_OPEN_PLINKS,
6747                                  nla_get_u8);
6748        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
6749                                  mask,
6750                                  NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
6751                                  nla_get_u32);
6752        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, mask,
6753                                  NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
6754                                  nla_get_u8);
6755        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, mask,
6756                                  NL80211_MESHCONF_PATH_REFRESH_TIME,
6757                                  nla_get_u32);
6758        if (mask & BIT(NL80211_MESHCONF_PATH_REFRESH_TIME) &&
6759            (cfg->path_refresh_time < 1 || cfg->path_refresh_time > 65535))
6760                return -EINVAL;
6761        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, mask,
6762                                  NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
6763                                  nla_get_u16);
6764        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
6765                                  mask,
6766                                  NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
6767                                  nla_get_u32);
6768        if (mask & BIT(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT) &&
6769            (cfg->dot11MeshHWMPactivePathTimeout < 1 ||
6770             cfg->dot11MeshHWMPactivePathTimeout > 65535))
6771                return -EINVAL;
6772        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, mask,
6773                                  NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
6774                                  nla_get_u16);
6775        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval, mask,
6776                                  NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
6777                                  nla_get_u16);
6778        FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6779                                  dot11MeshHWMPnetDiameterTraversalTime, mask,
6780                                  NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
6781                                  nla_get_u16);
6782        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
6783                                  NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
6784        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
6785                                  NL80211_MESHCONF_HWMP_RANN_INTERVAL,
6786                                  nla_get_u16);
6787        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshGateAnnouncementProtocol,
6788                                  mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
6789                                  nla_get_u8);
6790        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, mask,
6791                                  NL80211_MESHCONF_FORWARDING, nla_get_u8);
6792        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, mask,
6793                                  NL80211_MESHCONF_RSSI_THRESHOLD,
6794                                  nla_get_s32);
6795        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToMeshGate, mask,
6796                                  NL80211_MESHCONF_CONNECTED_TO_GATE,
6797                                  nla_get_u8);
6798        /*
6799         * Check HT operation mode based on
6800         * IEEE 802.11-2016 9.4.2.57 HT Operation element.
6801         */
6802        if (tb[NL80211_MESHCONF_HT_OPMODE]) {
6803                ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
6804
6805                if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
6806                                  IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
6807                                  IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
6808                        return -EINVAL;
6809
6810                /* NON_HT_STA bit is reserved, but some programs set it */
6811                ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
6812
6813                cfg->ht_opmode = ht_opmode;
6814                mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
6815        }
6816        FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6817                                  dot11MeshHWMPactivePathToRootTimeout, mask,
6818                                  NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
6819                                  nla_get_u32);
6820        if (mask & BIT(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT) &&
6821            (cfg->dot11MeshHWMPactivePathToRootTimeout < 1 ||
6822             cfg->dot11MeshHWMPactivePathToRootTimeout > 65535))
6823                return -EINVAL;
6824        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, mask,
6825                                  NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
6826                                  nla_get_u16);
6827        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPconfirmationInterval,
6828                                  mask,
6829                                  NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
6830                                  nla_get_u16);
6831        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode, mask,
6832                                  NL80211_MESHCONF_POWER_MODE, nla_get_u32);
6833        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration, mask,
6834                                  NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
6835        FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, mask,
6836                                  NL80211_MESHCONF_PLINK_TIMEOUT, nla_get_u32);
6837        if (mask_out)
6838                *mask_out = mask;
6839
6840        return 0;
6841
6842#undef FILL_IN_MESH_PARAM_IF_SET
6843}
6844
6845static int nl80211_parse_mesh_setup(struct genl_info *info,
6846                                     struct mesh_setup *setup)
6847{
6848        struct cfg80211_registered_device *rdev = info->user_ptr[0];
6849        struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
6850
6851        if (!info->attrs[NL80211_ATTR_MESH_SETUP])
6852                return -EINVAL;
6853        if (nla_parse_nested_deprecated(tb, NL80211_MESH_SETUP_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_SETUP], nl80211_mesh_setup_params_policy, info->extack))
6854                return -EINVAL;
6855
6856        if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
6857                setup->sync_method =
6858                (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
6859                 IEEE80211_SYNC_METHOD_VENDOR :
6860                 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
6861
6862        if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
6863                setup->path_sel_proto =
6864                (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
6865                 IEEE80211_PATH_PROTOCOL_VENDOR :
6866                 IEEE80211_PATH_PROTOCOL_HWMP;
6867
6868        if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
6869                setup->path_metric =
6870                (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
6871                 IEEE80211_PATH_METRIC_VENDOR :
6872                 IEEE80211_PATH_METRIC_AIRTIME;
6873
6874        if (tb[NL80211_MESH_SETUP_IE]) {
6875                struct nlattr *ieattr =
6876                        tb[NL80211_MESH_SETUP_IE];
6877                setup->ie = nla_data(ieattr);
6878                setup->ie_len = nla_len(ieattr);
6879        }
6880        if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
6881            !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
6882                return -EINVAL;
6883        setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
6884        setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
6885        setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
6886        if (setup->is_secure)
6887                setup->user_mpm = true;
6888
6889        if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
6890                if (!setup->user_mpm)
6891                        return -EINVAL;
6892                setup->auth_id =
6893                        nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
6894        }
6895
6896        return 0;
6897}
6898
6899static int nl80211_update_mesh_config(struct sk_buff *skb,
6900                                      struct genl_info *info)
6901{
6902        struct cfg80211_registered_device *rdev = info->user_ptr[0];
6903        struct net_device *dev = info->user_ptr[1];
6904        struct wireless_dev *wdev = dev->ieee80211_ptr;
6905        struct mesh_config cfg;
6906        u32 mask;
6907        int err;
6908
6909        if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
6910                return -EOPNOTSUPP;
6911
6912        if (!rdev->ops->update_mesh_config)
6913                return -EOPNOTSUPP;
6914
6915        err = nl80211_parse_mesh_config(info, &cfg, &mask);
6916        if (err)
6917                return err;
6918
6919        wdev_lock(wdev);
6920        if (!wdev->mesh_id_len)
6921                err = -ENOLINK;
6922
6923        if (!err)
6924                err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
6925
6926        wdev_unlock(wdev);
6927
6928        return err;
6929}
6930
6931static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
6932                              struct sk_buff *msg)
6933{
6934        struct nlattr *nl_reg_rules;
6935        unsigned int i;
6936
6937        if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
6938            (regdom->dfs_region &&
6939             nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
6940                goto nla_put_failure;
6941
6942        nl_reg_rules = nla_nest_start_noflag(msg, NL80211_ATTR_REG_RULES);
6943        if (!nl_reg_rules)
6944                goto nla_put_failure;
6945
6946        for (i = 0; i < regdom->n_reg_rules; i++) {
6947                struct nlattr *nl_reg_rule;
6948                const struct ieee80211_reg_rule *reg_rule;
6949                const struct ieee80211_freq_range *freq_range;
6950                const struct ieee80211_power_rule *power_rule;
6951                unsigned int max_bandwidth_khz;
6952
6953                reg_rule = &regdom->reg_rules[i];
6954                freq_range = &reg_rule->freq_range;
6955                power_rule = &reg_rule->power_rule;
6956
6957                nl_reg_rule = nla_nest_start_noflag(msg, i);
6958                if (!nl_reg_rule)
6959                        goto nla_put_failure;
6960
6961                max_bandwidth_khz = freq_range->max_bandwidth_khz;
6962                if (!max_bandwidth_khz)
6963                        max_bandwidth_khz = reg_get_max_bandwidth(regdom,
6964                                                                  reg_rule);
6965
6966                if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
6967                                reg_rule->flags) ||
6968                    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
6969                                freq_range->start_freq_khz) ||
6970                    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
6971                                freq_range->end_freq_khz) ||
6972                    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
6973                                max_bandwidth_khz) ||
6974                    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
6975                                power_rule->max_antenna_gain) ||
6976                    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
6977                                power_rule->max_eirp) ||
6978                    nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
6979                                reg_rule->dfs_cac_ms))
6980                        goto nla_put_failure;
6981
6982                nla_nest_end(msg, nl_reg_rule);
6983        }
6984
6985        nla_nest_end(msg, nl_reg_rules);
6986        return 0;
6987
6988nla_put_failure:
6989        return -EMSGSIZE;
6990}
6991
6992static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
6993{
6994        const struct ieee80211_regdomain *regdom = NULL;
6995        struct cfg80211_registered_device *rdev;
6996        struct wiphy *wiphy = NULL;
6997        struct sk_buff *msg;
6998        void *hdr;
6999
7000        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7001        if (!msg)
7002                return -ENOBUFS;
7003
7004        hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7005                             NL80211_CMD_GET_REG);
7006        if (!hdr)
7007                goto put_failure;
7008
7009        if (info->attrs[NL80211_ATTR_WIPHY]) {
7010                bool self_managed;
7011
7012                rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
7013                if (IS_ERR(rdev)) {
7014                        nlmsg_free(msg);
7015                        return PTR_ERR(rdev);
7016                }
7017
7018                wiphy = &rdev->wiphy;
7019                self_managed = wiphy->regulatory_flags &
7020                               REGULATORY_WIPHY_SELF_MANAGED;
7021                regdom = get_wiphy_regdom(wiphy);
7022
7023                /* a self-managed-reg device must have a private regdom */
7024                if (WARN_ON(!regdom && self_managed)) {
7025                        nlmsg_free(msg);
7026                        return -EINVAL;
7027                }
7028
7029                if (regdom &&
7030                    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7031                        goto nla_put_failure;
7032        }
7033
7034        if (!wiphy && reg_last_request_cell_base() &&
7035            nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
7036                        NL80211_USER_REG_HINT_CELL_BASE))
7037                goto nla_put_failure;
7038
7039        rcu_read_lock();
7040
7041        if (!regdom)
7042                regdom = rcu_dereference(cfg80211_regdomain);
7043
7044        if (nl80211_put_regdom(regdom, msg))
7045                goto nla_put_failure_rcu;
7046
7047        rcu_read_unlock();
7048
7049        genlmsg_end(msg, hdr);
7050        return genlmsg_reply(msg, info);
7051
7052nla_put_failure_rcu:
7053        rcu_read_unlock();
7054nla_put_failure:
7055put_failure:
7056        nlmsg_free(msg);
7057        return -EMSGSIZE;
7058}
7059
7060static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
7061                               u32 seq, int flags, struct wiphy *wiphy,
7062                               const struct ieee80211_regdomain *regdom)
7063{
7064        void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
7065                                   NL80211_CMD_GET_REG);
7066
7067        if (!hdr)
7068                return -1;
7069
7070        genl_dump_check_consistent(cb, hdr);
7071
7072        if (nl80211_put_regdom(regdom, msg))
7073                goto nla_put_failure;
7074
7075        if (!wiphy && reg_last_request_cell_base() &&
7076            nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
7077                        NL80211_USER_REG_HINT_CELL_BASE))
7078                goto nla_put_failure;
7079
7080        if (wiphy &&
7081            nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7082                goto nla_put_failure;
7083
7084        if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
7085            nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
7086                goto nla_put_failure;
7087
7088        genlmsg_end(msg, hdr);
7089        return 0;
7090
7091nla_put_failure:
7092        genlmsg_cancel(msg, hdr);
7093        return -EMSGSIZE;
7094}
7095
7096static int nl80211_get_reg_dump(struct sk_buff *skb,
7097                                struct netlink_callback *cb)
7098{
7099        const struct ieee80211_regdomain *regdom = NULL;
7100        struct cfg80211_registered_device *rdev;
7101        int err, reg_idx, start = cb->args[2];
7102
7103        rtnl_lock();
7104
7105        if (cfg80211_regdomain && start == 0) {
7106                err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
7107                                          NLM_F_MULTI, NULL,
7108                                          rtnl_dereference(cfg80211_regdomain));
7109                if (err < 0)
7110                        goto out_err;
7111        }
7112
7113        /* the global regdom is idx 0 */
7114        reg_idx = 1;
7115        list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
7116                regdom = get_wiphy_regdom(&rdev->wiphy);
7117                if (!regdom)
7118                        continue;
7119
7120                if (++reg_idx <= start)
7121                        continue;
7122
7123                err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
7124                                          NLM_F_MULTI, &rdev->wiphy, regdom);
7125                if (err < 0) {
7126                        reg_idx--;
7127                        break;
7128                }
7129        }
7130
7131        cb->args[2] = reg_idx;
7132        err = skb->len;
7133out_err:
7134        rtnl_unlock();
7135        return err;
7136}
7137
7138#ifdef CONFIG_CFG80211_CRDA_SUPPORT
7139static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
7140        [NL80211_ATTR_REG_RULE_FLAGS]           = { .type = NLA_U32 },
7141        [NL80211_ATTR_FREQ_RANGE_START]         = { .type = NLA_U32 },
7142        [NL80211_ATTR_FREQ_RANGE_END]           = { .type = NLA_U32 },
7143        [NL80211_ATTR_FREQ_RANGE_MAX_BW]        = { .type = NLA_U32 },
7144        [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]  = { .type = NLA_U32 },
7145        [NL80211_ATTR_POWER_RULE_MAX_EIRP]      = { .type = NLA_U32 },
7146        [NL80211_ATTR_DFS_CAC_TIME]             = { .type = NLA_U32 },
7147};
7148
7149static int parse_reg_rule(struct nlattr *tb[],
7150        struct ieee80211_reg_rule *reg_rule)
7151{
7152        struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
7153        struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
7154
7155        if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
7156                return -EINVAL;
7157        if (!tb[NL80211_ATTR_FREQ_RANGE_START])
7158                return -EINVAL;
7159        if (!tb[NL80211_ATTR_FREQ_RANGE_END])
7160                return -EINVAL;
7161        if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
7162                return -EINVAL;
7163        if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
7164                return -EINVAL;
7165
7166        reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
7167
7168        freq_range->start_freq_khz =
7169                nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
7170        freq_range->end_freq_khz =
7171                nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
7172        freq_range->max_bandwidth_khz =
7173                nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
7174
7175        power_rule->max_eirp =
7176                nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
7177
7178        if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
7179                power_rule->max_antenna_gain =
7180                        nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
7181
7182        if (tb[NL80211_ATTR_DFS_CAC_TIME])
7183                reg_rule->dfs_cac_ms =
7184                        nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
7185
7186        return 0;
7187}
7188
7189static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
7190{
7191        struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
7192        struct nlattr *nl_reg_rule;
7193        char *alpha2;
7194        int rem_reg_rules, r;
7195        u32 num_rules = 0, rule_idx = 0;
7196        enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
7197        struct ieee80211_regdomain *rd;
7198
7199        if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
7200                return -EINVAL;
7201
7202        if (!info->attrs[NL80211_ATTR_REG_RULES])
7203                return -EINVAL;
7204
7205        alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
7206
7207        if (info->attrs[NL80211_ATTR_DFS_REGION])
7208                dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
7209
7210        nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
7211                            rem_reg_rules) {
7212                num_rules++;
7213                if (num_rules > NL80211_MAX_SUPP_REG_RULES)
7214                        return -EINVAL;
7215        }
7216
7217        if (!reg_is_valid_request(alpha2))
7218                return -EINVAL;
7219
7220        rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
7221        if (!rd)
7222                return -ENOMEM;
7223
7224        rd->n_reg_rules = num_rules;
7225        rd->alpha2[0] = alpha2[0];
7226        rd->alpha2[1] = alpha2[1];
7227
7228        /*
7229         * Disable DFS master mode if the DFS region was
7230         * not supported or known on this kernel.
7231         */
7232        if (reg_supported_dfs_region(dfs_region))
7233                rd->dfs_region = dfs_region;
7234
7235        nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
7236                            rem_reg_rules) {
7237                r = nla_parse_nested_deprecated(tb, NL80211_REG_RULE_ATTR_MAX,
7238                                                nl_reg_rule, reg_rule_policy,
7239                                                info->extack);
7240                if (r)
7241                        goto bad_reg;
7242                r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
7243                if (r)
7244                        goto bad_reg;
7245
7246                rule_idx++;
7247
7248                if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
7249                        r = -EINVAL;
7250                        goto bad_reg;
7251                }
7252        }
7253
7254        /* set_regdom takes ownership of rd */
7255        return set_regdom(rd, REGD_SOURCE_CRDA);
7256 bad_reg:
7257        kfree(rd);
7258        return r;
7259}
7260#endif /* CONFIG_CFG80211_CRDA_SUPPORT */
7261
7262static int validate_scan_freqs(struct nlattr *freqs)
7263{
7264        struct nlattr *attr1, *attr2;
7265        int n_channels = 0, tmp1, tmp2;
7266
7267        nla_for_each_nested(attr1, freqs, tmp1)
7268                if (nla_len(attr1) != sizeof(u32))
7269                        return 0;
7270
7271        nla_for_each_nested(attr1, freqs, tmp1) {
7272                n_channels++;
7273                /*
7274                 * Some hardware has a limited channel list for
7275                 * scanning, and it is pretty much nonsensical
7276                 * to scan for a channel twice, so disallow that
7277                 * and don't require drivers to check that the
7278                 * channel list they get isn't longer than what
7279                 * they can scan, as long as they can scan all
7280                 * the channels they registered at once.
7281                 */
7282                nla_for_each_nested(attr2, freqs, tmp2)
7283                        if (attr1 != attr2 &&
7284                            nla_get_u32(attr1) == nla_get_u32(attr2))
7285                                return 0;
7286        }
7287
7288        return n_channels;
7289}
7290
7291static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
7292{
7293        return b < NUM_NL80211_BANDS && wiphy->bands[b];
7294}
7295
7296static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
7297                            struct cfg80211_bss_selection *bss_select)
7298{
7299        struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
7300        struct nlattr *nest;
7301        int err;
7302        bool found = false;
7303        int i;
7304
7305        /* only process one nested attribute */
7306        nest = nla_data(nla);
7307        if (!nla_ok(nest, nla_len(nest)))
7308                return -EINVAL;
7309
7310        err = nla_parse_nested_deprecated(attr, NL80211_BSS_SELECT_ATTR_MAX,
7311                                          nest, nl80211_bss_select_policy,
7312                                          NULL);
7313        if (err)
7314                return err;
7315
7316        /* only one attribute may be given */
7317        for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
7318                if (attr[i]) {
7319                        if (found)
7320                                return -EINVAL;
7321                        found = true;
7322                }
7323        }
7324
7325        bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
7326
7327        if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
7328                bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
7329
7330        if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
7331                bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
7332                bss_select->param.band_pref =
7333                        nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
7334                if (!is_band_valid(wiphy, bss_select->param.band_pref))
7335                        return -EINVAL;
7336        }
7337
7338        if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
7339                struct nl80211_bss_select_rssi_adjust *adj_param;
7340
7341                adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
7342                bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
7343                bss_select->param.adjust.band = adj_param->band;
7344                bss_select->param.adjust.delta = adj_param->delta;
7345                if (!is_band_valid(wiphy, bss_select->param.adjust.band))
7346                        return -EINVAL;
7347        }
7348
7349        /* user-space did not provide behaviour attribute */
7350        if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
7351                return -EINVAL;
7352
7353        if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
7354                return -EINVAL;
7355
7356        return 0;
7357}
7358
7359int nl80211_parse_random_mac(struct nlattr **attrs,
7360                             u8 *mac_addr, u8 *mac_addr_mask)
7361{
7362        int i;
7363
7364        if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
7365                eth_zero_addr(mac_addr);
7366                eth_zero_addr(mac_addr_mask);
7367                mac_addr[0] = 0x2;
7368                mac_addr_mask[0] = 0x3;
7369
7370                return 0;
7371        }
7372
7373        /* need both or none */
7374        if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
7375                return -EINVAL;
7376
7377        memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
7378        memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
7379
7380        /* don't allow or configure an mcast address */
7381        if (!is_multicast_ether_addr(mac_addr_mask) ||
7382            is_multicast_ether_addr(mac_addr))
7383                return -EINVAL;
7384
7385        /*
7386         * allow users to pass a MAC address that has bits set outside
7387         * of the mask, but don't bother drivers with having to deal
7388         * with such bits
7389         */
7390        for (i = 0; i < ETH_ALEN; i++)
7391                mac_addr[i] &= mac_addr_mask[i];
7392
7393        return 0;
7394}
7395
7396static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev)
7397{
7398        ASSERT_WDEV_LOCK(wdev);
7399
7400        if (!cfg80211_beaconing_iface_active(wdev))
7401                return true;
7402
7403        if (!(wdev->chandef.chan->flags & IEEE80211_CHAN_RADAR))
7404                return true;
7405
7406        return regulatory_pre_cac_allowed(wdev->wiphy);
7407}
7408
7409static bool nl80211_check_scan_feat(struct wiphy *wiphy, u32 flags, u32 flag,
7410                                    enum nl80211_ext_feature_index feat)
7411{
7412        if (!(flags & flag))
7413                return true;
7414        if (wiphy_ext_feature_isset(wiphy, feat))
7415                return true;
7416        return false;
7417}
7418
7419static int
7420nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
7421                         void *request, struct nlattr **attrs,
7422                         bool is_sched_scan)
7423{
7424        u8 *mac_addr, *mac_addr_mask;
7425        u32 *flags;
7426        enum nl80211_feature_flags randomness_flag;
7427
7428        if (!attrs[NL80211_ATTR_SCAN_FLAGS])
7429                return 0;
7430
7431        if (is_sched_scan) {
7432                struct cfg80211_sched_scan_request *req = request;
7433
7434                randomness_flag = wdev ?
7435                                  NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
7436                                  NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
7437                flags = &req->flags;
7438                mac_addr = req->mac_addr;
7439                mac_addr_mask = req->mac_addr_mask;
7440        } else {
7441                struct cfg80211_scan_request *req = request;
7442
7443                randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
7444                flags = &req->flags;
7445                mac_addr = req->mac_addr;
7446                mac_addr_mask = req->mac_addr_mask;
7447        }
7448
7449        *flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
7450
7451        if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
7452             !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
7453            !nl80211_check_scan_feat(wiphy, *flags,
7454                                     NL80211_SCAN_FLAG_LOW_SPAN,
7455                                     NL80211_EXT_FEATURE_LOW_SPAN_SCAN) ||
7456            !nl80211_check_scan_feat(wiphy, *flags,
7457                                     NL80211_SCAN_FLAG_LOW_POWER,
7458                                     NL80211_EXT_FEATURE_LOW_POWER_SCAN) ||
7459            !nl80211_check_scan_feat(wiphy, *flags,
7460                                     NL80211_SCAN_FLAG_HIGH_ACCURACY,
7461                                     NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN) ||
7462            !nl80211_check_scan_feat(wiphy, *flags,
7463                                     NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
7464                                     NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) ||
7465            !nl80211_check_scan_feat(wiphy, *flags,
7466                                     NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
7467                                     NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) ||
7468            !nl80211_check_scan_feat(wiphy, *flags,
7469                                     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
7470                                     NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) ||
7471            !nl80211_check_scan_feat(wiphy, *flags,
7472                                     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
7473                                     NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) ||
7474            !nl80211_check_scan_feat(wiphy, *flags,
7475                                     NL80211_SCAN_FLAG_RANDOM_SN,
7476                                     NL80211_EXT_FEATURE_SCAN_RANDOM_SN) ||
7477            !nl80211_check_scan_feat(wiphy, *flags,
7478                                     NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,
7479                                     NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
7480                return -EOPNOTSUPP;
7481
7482        if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
7483                int err;
7484
7485                if (!(wiphy->features & randomness_flag) ||
7486                    (wdev && wdev->current_bss))
7487                        return -EOPNOTSUPP;
7488
7489                err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
7490                if (err)
7491                        return err;
7492        }
7493
7494        return 0;
7495}
7496
7497static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
7498{
7499        struct cfg80211_registered_device *rdev = info->user_ptr[0];
7500        struct wireless_dev *wdev = info->user_ptr[1];
7501        struct cfg80211_scan_request *request;
7502        struct nlattr *attr;
7503        struct wiphy *wiphy;
7504        int err, tmp, n_ssids = 0, n_channels, i;
7505        size_t ie_len;
7506
7507        wiphy = &rdev->wiphy;
7508
7509        if (wdev->iftype == NL80211_IFTYPE_NAN)
7510                return -EOPNOTSUPP;
7511
7512        if (!rdev->ops->scan)
7513                return -EOPNOTSUPP;
7514
7515        if (rdev->scan_req || rdev->scan_msg) {
7516                err = -EBUSY;
7517                goto unlock;
7518        }
7519
7520        if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7521                n_channels = validate_scan_freqs(
7522                                info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
7523                if (!n_channels) {
7524                        err = -EINVAL;
7525                        goto unlock;
7526                }
7527        } else {
7528                n_channels = ieee80211_get_num_supported_channels(wiphy);
7529        }
7530
7531        if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
7532                nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
7533                        n_ssids++;
7534
7535        if (n_ssids > wiphy->max_scan_ssids) {
7536                err = -EINVAL;
7537                goto unlock;
7538        }
7539
7540        if (info->attrs[NL80211_ATTR_IE])
7541                ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7542        else
7543                ie_len = 0;
7544
7545        if (ie_len > wiphy->max_scan_ie_len) {
7546                err = -EINVAL;
7547                goto unlock;
7548        }
7549
7550        request = kzalloc(sizeof(*request)
7551                        + sizeof(*request->ssids) * n_ssids
7552                        + sizeof(*request->channels) * n_channels
7553                        + ie_len, GFP_KERNEL);
7554        if (!request) {
7555                err = -ENOMEM;
7556                goto unlock;
7557        }
7558
7559        if (n_ssids)
7560                request->ssids = (void *)&request->channels[n_channels];
7561        request->n_ssids = n_ssids;
7562        if (ie_len) {
7563                if (n_ssids)
7564                        request->ie = (void *)(request->ssids + n_ssids);
7565                else
7566                        request->ie = (void *)(request->channels + n_channels);
7567        }
7568
7569        i = 0;
7570        if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7571                /* user specified, bail out if channel not found */
7572                nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
7573                        struct ieee80211_channel *chan;
7574
7575                        chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
7576
7577                        if (!chan) {
7578                                err = -EINVAL;
7579                                goto out_free;
7580                        }
7581
7582                        /* ignore disabled channels */
7583                        if (chan->flags & IEEE80211_CHAN_DISABLED)
7584                                continue;
7585
7586                        request->channels[i] = chan;
7587                        i++;
7588                }
7589        } else {
7590                enum nl80211_band band;
7591
7592                /* all channels */
7593                for (band = 0; band < NUM_NL80211_BANDS; band++) {
7594                        int j;
7595
7596                        if (!wiphy->bands[band])
7597                                continue;
7598                        for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
7599                                struct ieee80211_channel *chan;
7600
7601                                chan = &wiphy->bands[band]->channels[j];
7602
7603                                if (chan->flags & IEEE80211_CHAN_DISABLED)
7604                                        continue;
7605
7606                                request->channels[i] = chan;
7607                                i++;
7608                        }
7609                }
7610        }
7611
7612        if (!i) {
7613                err = -EINVAL;
7614                goto out_free;
7615        }
7616
7617        request->n_channels = i;
7618
7619        wdev_lock(wdev);
7620        if (!cfg80211_off_channel_oper_allowed(wdev)) {
7621                struct ieee80211_channel *chan;
7622
7623                if (request->n_channels != 1) {
7624                        wdev_unlock(wdev);
7625                        err = -EBUSY;
7626                        goto out_free;
7627                }
7628
7629                chan = request->channels[0];
7630                if (chan->center_freq != wdev->chandef.chan->center_freq) {
7631                        wdev_unlock(wdev);
7632                        err = -EBUSY;
7633                        goto out_free;
7634                }
7635        }
7636        wdev_unlock(wdev);
7637
7638        i = 0;
7639        if (n_ssids) {
7640                nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
7641                        if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
7642                                err = -EINVAL;
7643                                goto out_free;
7644                        }
7645                        request->ssids[i].ssid_len = nla_len(attr);
7646                        memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
7647                        i++;
7648                }
7649        }
7650
7651        if (info->attrs[NL80211_ATTR_IE]) {
7652                request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7653                memcpy((void *)request->ie,
7654                       nla_data(info->attrs[NL80211_ATTR_IE]),
7655                       request->ie_len);
7656        }
7657
7658        for (i = 0; i < NUM_NL80211_BANDS; i++)
7659                if (wiphy->bands[i])
7660                        request->rates[i] =
7661                                (1 << wiphy->bands[i]->n_bitrates) - 1;
7662
7663        if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
7664                nla_for_each_nested(attr,
7665                                    info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
7666                                    tmp) {
7667                        enum nl80211_band band = nla_type(attr);
7668
7669                        if (band < 0 || band >= NUM_NL80211_BANDS) {
7670                                err = -EINVAL;
7671                                goto out_free;
7672                        }
7673
7674                        if (!wiphy->bands[band])
7675                                continue;
7676
7677                        err = ieee80211_get_ratemask(wiphy->bands[band],
7678                                                     nla_data(attr),
7679                                                     nla_len(attr),
7680                                                     &request->rates[band]);
7681                        if (err)
7682                                goto out_free;
7683                }
7684        }
7685
7686        if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
7687                if (!wiphy_ext_feature_isset(wiphy,
7688                                        NL80211_EXT_FEATURE_SET_SCAN_DWELL)) {
7689                        err = -EOPNOTSUPP;
7690                        goto out_free;
7691                }
7692
7693                request->duration =
7694                        nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
7695                request->duration_mandatory =
7696                        nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
7697        }
7698
7699        err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
7700                                       false);
7701        if (err)
7702                goto out_free;
7703
7704        request->no_cck =
7705                nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7706
7707        /* Initial implementation used NL80211_ATTR_MAC to set the specific
7708         * BSSID to scan for. This was problematic because that same attribute
7709         * was already used for another purpose (local random MAC address). The
7710         * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
7711         * compatibility with older userspace components, also use the
7712         * NL80211_ATTR_MAC value here if it can be determined to be used for
7713         * the specific BSSID use case instead of the random MAC address
7714         * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
7715         */
7716        if (info->attrs[NL80211_ATTR_BSSID])
7717                memcpy(request->bssid,
7718                       nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
7719        else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
7720                 info->attrs[NL80211_ATTR_MAC])
7721                memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
7722                       ETH_ALEN);
7723        else
7724                eth_broadcast_addr(request->bssid);
7725
7726        request->wdev = wdev;
7727        request->wiphy = &rdev->wiphy;
7728        request->scan_start = jiffies;
7729
7730        rdev->scan_req = request;
7731        err = rdev_scan(rdev, request);
7732
7733        if (!err) {
7734                nl80211_send_scan_start(rdev, wdev);
7735                if (wdev->netdev)
7736                        dev_hold(wdev->netdev);
7737        } else {
7738 out_free:
7739                rdev->scan_req = NULL;
7740                kfree(request);
7741        }
7742
7743 unlock:
7744        return err;
7745}
7746
7747static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
7748{
7749        struct cfg80211_registered_device *rdev = info->user_ptr[0];
7750        struct wireless_dev *wdev = info->user_ptr[1];
7751
7752        if (!rdev->ops->abort_scan)
7753                return -EOPNOTSUPP;
7754
7755        if (rdev->scan_msg)
7756                return 0;
7757
7758        if (!rdev->scan_req)
7759                return -ENOENT;
7760
7761        rdev_abort_scan(rdev, wdev);
7762        return 0;
7763}
7764
7765static int
7766nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
7767                               struct cfg80211_sched_scan_request *request,
7768                               struct nlattr **attrs)
7769{
7770        int tmp, err, i = 0;
7771        struct nlattr *attr;
7772
7773        if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7774                u32 interval;
7775
7776                /*
7777                 * If scan plans are not specified,
7778                 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
7779                 * case one scan plan will be set with the specified scan
7780                 * interval and infinite number of iterations.
7781                 */
7782                interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
7783                if (!interval)
7784                        return -EINVAL;
7785
7786                request->scan_plans[0].interval =
7787                        DIV_ROUND_UP(interval, MSEC_PER_SEC);
7788                if (!request->scan_plans[0].interval)
7789                        return -EINVAL;
7790
7791                if (request->scan_plans[0].interval >
7792                    wiphy->max_sched_scan_plan_interval)
7793                        request->scan_plans[0].interval =
7794                                wiphy->max_sched_scan_plan_interval;
7795
7796                return 0;
7797        }
7798
7799        nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
7800                struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
7801
7802                if (WARN_ON(i >= n_plans))
7803                        return -EINVAL;
7804
7805                err = nla_parse_nested_deprecated(plan,
7806                                                  NL80211_SCHED_SCAN_PLAN_MAX,
7807                                                  attr, nl80211_plan_policy,
7808                                                  NULL);
7809                if (err)
7810                        return err;
7811
7812                if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
7813                        return -EINVAL;
7814
7815                request->scan_plans[i].interval =
7816                        nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
7817                if (!request->scan_plans[i].interval ||
7818                    request->scan_plans[i].interval >
7819                    wiphy->max_sched_scan_plan_interval)
7820                        return -EINVAL;
7821
7822                if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
7823                        request->scan_plans[i].iterations =
7824                                nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
7825                        if (!request->scan_plans[i].iterations ||
7826                            (request->scan_plans[i].iterations >
7827                             wiphy->max_sched_scan_plan_iterations))
7828                                return -EINVAL;
7829                } else if (i < n_plans - 1) {
7830                        /*
7831                         * All scan plans but the last one must specify
7832                         * a finite number of iterations
7833                         */
7834                        return -EINVAL;
7835                }
7836
7837                i++;
7838        }
7839
7840        /*
7841         * The last scan plan must not specify the number of
7842         * iterations, it is supposed to run infinitely
7843         */
7844        if (request->scan_plans[n_plans - 1].iterations)
7845                return  -EINVAL;
7846
7847        return 0;
7848}
7849
7850static int
7851nl80211_parse_sched_scan_per_band_rssi(struct wiphy *wiphy,
7852                                       struct cfg80211_match_set *match_sets,
7853                                       struct nlattr *tb_band_rssi,
7854                                       s32 rssi_thold)
7855{
7856        struct nlattr *attr;
7857        int i, tmp, ret = 0;
7858
7859        if (!wiphy_ext_feature_isset(wiphy,
7860                    NL80211_EXT_FEATURE_SCHED_SCAN_BAND_SPECIFIC_RSSI_THOLD)) {
7861                if (tb_band_rssi)
7862                        ret = -EOPNOTSUPP;
7863                else
7864                        for (i = 0; i < NUM_NL80211_BANDS; i++)
7865                                match_sets->per_band_rssi_thold[i] =
7866                                        NL80211_SCAN_RSSI_THOLD_OFF;
7867                return ret;
7868        }
7869
7870        for (i = 0; i < NUM_NL80211_BANDS; i++)
7871                match_sets->per_band_rssi_thold[i] = rssi_thold;
7872
7873        nla_for_each_nested(attr, tb_band_rssi, tmp) {
7874                enum nl80211_band band = nla_type(attr);
7875
7876                if (band < 0 || band >= NUM_NL80211_BANDS)
7877                        return -EINVAL;
7878
7879                match_sets->per_band_rssi_thold[band] = nla_get_s32(attr);
7880        }
7881
7882        return 0;
7883}
7884
7885static struct cfg80211_sched_scan_request *
7886nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
7887                         struct nlattr **attrs, int max_match_sets)
7888{
7889        struct cfg80211_sched_scan_request *request;
7890        struct nlattr *attr;
7891        int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
7892        enum nl80211_band band;
7893        size_t ie_len;
7894        struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
7895        s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
7896
7897        if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7898                n_channels = validate_scan_freqs(
7899                                attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
7900                if (!n_channels)
7901                        return ERR_PTR(-EINVAL);
7902        } else {
7903                n_channels = ieee80211_get_num_supported_channels(wiphy);
7904        }
7905
7906        if (attrs[NL80211_ATTR_SCAN_SSIDS])
7907                nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
7908                                    tmp)
7909                        n_ssids++;
7910
7911        if (n_ssids > wiphy->max_sched_scan_ssids)
7912                return ERR_PTR(-EINVAL);
7913
7914        /*
7915         * First, count the number of 'real' matchsets. Due to an issue with
7916         * the old implementation, matchsets containing only the RSSI attribute
7917         * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
7918         * RSSI for all matchsets, rather than their own matchset for reporting
7919         * all APs with a strong RSSI. This is needed to be compatible with
7920         * older userspace that treated a matchset with only the RSSI as the
7921         * global RSSI for all other matchsets - if there are other matchsets.
7922         */
7923        if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
7924                nla_for_each_nested(attr,
7925                                    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
7926                                    tmp) {
7927                        struct nlattr *rssi;
7928
7929                        err = nla_parse_nested_deprecated(tb,
7930                                                          NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
7931                                                          attr,
7932                                                          nl80211_match_policy,
7933                                                          NULL);
7934                        if (err)
7935                                return ERR_PTR(err);
7936
7937                        /* SSID and BSSID are mutually exclusive */
7938                        if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
7939                            tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
7940                                return ERR_PTR(-EINVAL);
7941
7942                        /* add other standalone attributes here */
7943                        if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
7944                            tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
7945                                n_match_sets++;
7946                                continue;
7947                        }
7948                        rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
7949                        if (rssi)
7950                                default_match_rssi = nla_get_s32(rssi);
7951                }
7952        }
7953
7954        /* However, if there's no other matchset, add the RSSI one */
7955        if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
7956                n_match_sets = 1;
7957
7958        if (n_match_sets > max_match_sets)
7959                return ERR_PTR(-EINVAL);
7960
7961        if (attrs[NL80211_ATTR_IE])
7962                ie_len = nla_len(attrs[NL80211_ATTR_IE]);
7963        else
7964                ie_len = 0;
7965
7966        if (ie_len > wiphy->max_sched_scan_ie_len)
7967                return ERR_PTR(-EINVAL);
7968
7969        if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7970                /*
7971                 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
7972                 * each scan plan already specifies its own interval
7973                 */
7974                if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7975                        return ERR_PTR(-EINVAL);
7976
7977                nla_for_each_nested(attr,
7978                                    attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
7979                        n_plans++;
7980        } else {
7981                /*
7982                 * The scan interval attribute is kept for backward
7983                 * compatibility. If no scan plans are specified and sched scan
7984                 * interval is specified, one scan plan will be set with this
7985                 * scan interval and infinite number of iterations.
7986                 */
7987                if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7988                        return ERR_PTR(-EINVAL);
7989
7990                n_plans = 1;
7991        }
7992
7993        if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
7994                return ERR_PTR(-EINVAL);
7995
7996        if (!wiphy_ext_feature_isset(
7997                    wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
7998            (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
7999             attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
8000                return ERR_PTR(-EINVAL);
8001
8002        request = kzalloc(sizeof(*request)
8003                        + sizeof(*request->ssids) * n_ssids
8004                        + sizeof(*request->match_sets) * n_match_sets
8005                        + sizeof(*request->scan_plans) * n_plans
8006                        + sizeof(*request->channels) * n_channels
8007                        + ie_len, GFP_KERNEL);
8008        if (!request)
8009                return ERR_PTR(-ENOMEM);
8010
8011        if (n_ssids)
8012                request->ssids = (void *)&request->channels[n_channels];
8013        request->n_ssids = n_ssids;
8014        if (ie_len) {
8015                if (n_ssids)
8016                        request->ie = (void *)(request->ssids + n_ssids);
8017                else
8018                        request->ie = (void *)(request->channels + n_channels);
8019        }
8020
8021        if (n_match_sets) {
8022                if (request->ie)
8023                        request->match_sets = (void *)(request->ie + ie_len);
8024                else if (n_ssids)
8025                        request->match_sets =
8026                                (void *)(request->ssids + n_ssids);
8027                else
8028                        request->match_sets =
8029                                (void *)(request->channels + n_channels);
8030        }
8031        request->n_match_sets = n_match_sets;
8032
8033        if (n_match_sets)
8034                request->scan_plans = (void *)(request->match_sets +
8035                                               n_match_sets);
8036        else if (request->ie)
8037                request->scan_plans = (void *)(request->ie + ie_len);
8038        else if (n_ssids)
8039                request->scan_plans = (void *)(request->ssids + n_ssids);
8040        else
8041                request->scan_plans = (void *)(request->channels + n_channels);
8042
8043        request->n_scan_plans = n_plans;
8044
8045        i = 0;
8046        if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
8047                /* user specified, bail out if channel not found */
8048                nla_for_each_nested(attr,
8049                                    attrs[NL80211_ATTR_SCAN_FREQUENCIES],
8050                                    tmp) {
8051                        struct ieee80211_channel *chan;
8052
8053                        chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
8054
8055                        if (!chan) {
8056                                err = -EINVAL;
8057                                goto out_free;
8058                        }
8059
8060                        /* ignore disabled channels */
8061                        if (chan->flags & IEEE80211_CHAN_DISABLED)
8062                                continue;
8063
8064                        request->channels[i] = chan;
8065                        i++;
8066                }
8067        } else {
8068                /* all channels */
8069                for (band = 0; band < NUM_NL80211_BANDS; band++) {
8070                        int j;
8071
8072                        if (!wiphy->bands[band])
8073                                continue;
8074                        for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
8075                                struct ieee80211_channel *chan;
8076
8077                                chan = &wiphy->bands[band]->channels[j];
8078
8079                                if (chan->flags & IEEE80211_CHAN_DISABLED)
8080                                        continue;
8081
8082                                request->channels[i] = chan;
8083                                i++;
8084                        }
8085                }
8086        }
8087
8088        if (!i) {
8089                err = -EINVAL;
8090                goto out_free;
8091        }
8092
8093        request->n_channels = i;
8094
8095        i = 0;
8096        if (n_ssids) {
8097                nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
8098                                    tmp) {
8099                        if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
8100                                err = -EINVAL;
8101                                goto out_free;
8102                        }
8103                        request->ssids[i].ssid_len = nla_len(attr);
8104                        memcpy(request->ssids[i].ssid, nla_data(attr),
8105                               nla_len(attr));
8106                        i++;
8107                }
8108        }
8109
8110        i = 0;
8111        if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
8112                nla_for_each_nested(attr,
8113                                    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
8114                                    tmp) {
8115                        struct nlattr *ssid, *bssid, *rssi;
8116
8117                        err = nla_parse_nested_deprecated(tb,
8118                                                          NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
8119                                                          attr,
8120                                                          nl80211_match_policy,
8121                                                          NULL);
8122                        if (err)
8123                                goto out_free;
8124                        ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
8125                        bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
8126
8127                        if (!ssid && !bssid) {
8128                                i++;
8129                                continue;
8130                        }
8131
8132                        if (WARN_ON(i >= n_match_sets)) {
8133                                /* this indicates a programming error,
8134                                 * the loop above should have verified
8135                                 * things properly
8136                                 */
8137                                err = -EINVAL;
8138                                goto out_free;
8139                        }
8140
8141                        if (ssid) {
8142                                if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
8143                                        err = -EINVAL;
8144                                        goto out_free;
8145                                }
8146                                memcpy(request->match_sets[i].ssid.ssid,
8147                                       nla_data(ssid), nla_len(ssid));
8148                                request->match_sets[i].ssid.ssid_len =
8149                                        nla_len(ssid);
8150                        }
8151                        if (bssid) {
8152                                if (nla_len(bssid) != ETH_ALEN) {
8153                                        err = -EINVAL;
8154                                        goto out_free;
8155                                }
8156                                memcpy(request->match_sets[i].bssid,
8157                                       nla_data(bssid), ETH_ALEN);
8158                        }
8159
8160                        /* special attribute - old implementation w/a */
8161                        request->match_sets[i].rssi_thold = default_match_rssi;
8162                        rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
8163                        if (rssi)
8164                                request->match_sets[i].rssi_thold =
8165                                        nla_get_s32(rssi);
8166
8167                        /* Parse per band RSSI attribute */
8168                        err = nl80211_parse_sched_scan_per_band_rssi(wiphy,
8169                                &request->match_sets[i],
8170                                tb[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI],
8171                                request->match_sets[i].rssi_thold);
8172                        if (err)
8173                                goto out_free;
8174
8175                        i++;
8176                }
8177
8178                /* there was no other matchset, so the RSSI one is alone */
8179                if (i == 0 && n_match_sets)
8180                        request->match_sets[0].rssi_thold = default_match_rssi;
8181
8182                request->min_rssi_thold = INT_MAX;
8183                for (i = 0; i < n_match_sets; i++)
8184                        request->min_rssi_thold =
8185                                min(request->match_sets[i].rssi_thold,
8186                                    request->min_rssi_thold);
8187        } else {
8188                request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
8189        }
8190
8191        if (ie_len) {
8192                request->ie_len = ie_len;
8193                memcpy((void *)request->ie,
8194                       nla_data(attrs[NL80211_ATTR_IE]),
8195                       request->ie_len);
8196        }
8197
8198        err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
8199        if (err)
8200                goto out_free;
8201
8202        if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
8203                request->delay =
8204                        nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
8205
8206        if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
8207                request->relative_rssi = nla_get_s8(
8208                        attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
8209                request->relative_rssi_set = true;
8210        }
8211
8212        if (request->relative_rssi_set &&
8213            attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
8214                struct nl80211_bss_select_rssi_adjust *rssi_adjust;
8215
8216                rssi_adjust = nla_data(
8217                        attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
8218                request->rssi_adjust.band = rssi_adjust->band;
8219                request->rssi_adjust.delta = rssi_adjust->delta;
8220                if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
8221                        err = -EINVAL;
8222                        goto out_free;
8223                }
8224        }
8225
8226        err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
8227        if (err)
8228                goto out_free;
8229
8230        request->scan_start = jiffies;
8231
8232        return request;
8233
8234out_free:
8235        kfree(request);
8236        return ERR_PTR(err);
8237}
8238
8239static int nl80211_start_sched_scan(struct sk_buff *skb,
8240                                    struct genl_info *info)
8241{
8242        struct cfg80211_registered_device *rdev = info->user_ptr[0];
8243        struct net_device *dev = info->user_ptr[1];
8244        struct wireless_dev *wdev = dev->ieee80211_ptr;
8245        struct cfg80211_sched_scan_request *sched_scan_req;
8246        bool want_multi;
8247        int err;
8248
8249        if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
8250                return -EOPNOTSUPP;
8251
8252        want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
8253        err = cfg80211_sched_scan_req_possible(rdev, want_multi);
8254        if (err)
8255                return err;
8256
8257        sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
8258                                                  info->attrs,
8259                                                  rdev->wiphy.max_match_sets);
8260
8261        err = PTR_ERR_OR_ZERO(sched_scan_req);
8262        if (err)
8263                goto out_err;
8264
8265        /* leave request id zero for legacy request
8266         * or if driver does not support multi-scheduled scan
8267         */
8268        if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1) {
8269                while (!sched_scan_req->reqid)
8270                        sched_scan_req->reqid = cfg80211_assign_cookie(rdev);
8271        }
8272
8273        err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
8274        if (err)
8275                goto out_free;
8276
8277        sched_scan_req->dev = dev;
8278        sched_scan_req->wiphy = &rdev->wiphy;
8279
8280        if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
8281                sched_scan_req->owner_nlportid = info->snd_portid;
8282
8283        cfg80211_add_sched_scan_req(rdev, sched_scan_req);
8284
8285        nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
8286        return 0;
8287
8288out_free:
8289        kfree(sched_scan_req);
8290out_err:
8291        return err;
8292}
8293
8294static int nl80211_stop_sched_scan(struct sk_buff *skb,
8295                                   struct genl_info *info)
8296{
8297        struct cfg80211_sched_scan_request *req;
8298        struct cfg80211_registered_device *rdev = info->user_ptr[0];
8299        u64 cookie;
8300
8301        if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
8302                return -EOPNOTSUPP;
8303
8304        if (info->attrs[NL80211_ATTR_COOKIE]) {
8305                cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
8306                return __cfg80211_stop_sched_scan(rdev, cookie, false);
8307        }
8308
8309        req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
8310                                     struct cfg80211_sched_scan_request,
8311                                     list);
8312        if (!req || req->reqid ||
8313            (req->owner_nlportid &&
8314             req->owner_nlportid != info->snd_portid))
8315                return -ENOENT;
8316
8317        return cfg80211_stop_sched_scan_req(rdev, req, false);
8318}
8319
8320static int nl80211_start_radar_detection(struct sk_buff *skb,
8321                                         struct genl_info *info)
8322{
8323        struct cfg80211_registered_device *rdev = info->user_ptr[0];
8324        struct net_device *dev = info->user_ptr[1];
8325        struct wireless_dev *wdev = dev->ieee80211_ptr;
8326        struct wiphy *wiphy = wdev->wiphy;
8327        struct cfg80211_chan_def chandef;
8328        enum nl80211_dfs_regions dfs_region;
8329        unsigned int cac_time_ms;
8330        int err;
8331
8332        dfs_region = reg_get_dfs_region(wiphy);
8333        if (dfs_region == NL80211_DFS_UNSET)
8334                return -EINVAL;
8335
8336        err = nl80211_parse_chandef(rdev, info, &chandef);
8337        if (err)
8338                return err;
8339
8340        if (netif_carrier_ok(dev))
8341                return -EBUSY;
8342
8343        if (wdev->cac_started)
8344                return -EBUSY;
8345
8346        err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
8347        if (err < 0)
8348                return err;
8349
8350        if (err == 0)
8351                return -EINVAL;
8352
8353        if (!cfg80211_chandef_dfs_usable(wiphy, &chandef))
8354                return -EINVAL;
8355
8356        /* CAC start is offloaded to HW and can't be started manually */
8357        if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD))
8358                return -EOPNOTSUPP;
8359
8360        if (!rdev->ops->start_radar_detection)
8361                return -EOPNOTSUPP;
8362
8363        cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
8364        if (WARN_ON(!cac_time_ms))
8365                cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
8366
8367        err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
8368        if (!err) {
8369                wdev->chandef = chandef;
8370                wdev->cac_started = true;
8371                wdev->cac_start_time = jiffies;
8372                wdev->cac_time_ms = cac_time_ms;
8373        }
8374        return err;
8375}
8376
8377static int nl80211_notify_radar_detection(struct sk_buff *skb,
8378                                          struct genl_info *info)
8379{
8380        struct cfg80211_registered_device *rdev = info->user_ptr[0];
8381        struct net_device *dev = info->user_ptr[1];
8382        struct wireless_dev *wdev = dev->ieee80211_ptr;
8383        struct wiphy *wiphy = wdev->wiphy;
8384        struct cfg80211_chan_def chandef;
8385        enum nl80211_dfs_regions dfs_region;
8386        int err;
8387
8388        dfs_region = reg_get_dfs_region(wiphy);
8389        if (dfs_region == NL80211_DFS_UNSET) {
8390                GENL_SET_ERR_MSG(info,
8391                                 "DFS Region is not set. Unexpected Radar indication");
8392                return -EINVAL;
8393        }
8394
8395        err = nl80211_parse_chandef(rdev, info, &chandef);
8396        if (err) {
8397                GENL_SET_ERR_MSG(info, "Unable to extract chandef info");
8398                return err;
8399        }
8400
8401        err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
8402        if (err < 0) {
8403                GENL_SET_ERR_MSG(info, "chandef is invalid");
8404                return err;
8405        }
8406
8407        if (err == 0) {
8408                GENL_SET_ERR_MSG(info,
8409                                 "Unexpected Radar indication for chandef/iftype");
8410                return -EINVAL;
8411        }
8412
8413        /* Do not process this notification if radar is already detected
8414         * by kernel on this channel, and return success.
8415         */
8416        if (chandef.chan->dfs_state == NL80211_DFS_UNAVAILABLE)
8417                return 0;
8418
8419        cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_UNAVAILABLE);
8420
8421        cfg80211_sched_dfs_chan_update(rdev);
8422
8423        rdev->radar_chandef = chandef;
8424
8425        /* Propagate this notification to other radios as well */
8426        queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
8427
8428        return 0;
8429}
8430
8431static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
8432{
8433        struct cfg80211_registered_device *rdev = info->user_ptr[0];
8434        struct net_device *dev = info->user_ptr[1];
8435        struct wireless_dev *wdev = dev->ieee80211_ptr;
8436        struct cfg80211_csa_settings params;
8437        /* csa_attrs is defined static to avoid waste of stack size - this
8438         * function is called under RTNL lock, so this should not be a problem.
8439         */
8440        static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
8441        int err;
8442        bool need_new_beacon = false;
8443        bool need_handle_dfs_flag = true;
8444        int len, i;
8445        u32 cs_count;
8446
8447        if (!rdev->ops->channel_switch ||
8448            !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
8449                return -EOPNOTSUPP;
8450
8451        switch (dev->ieee80211_ptr->iftype) {
8452        case NL80211_IFTYPE_AP:
8453        case NL80211_IFTYPE_P2P_GO:
8454                need_new_beacon = true;
8455                /* For all modes except AP the handle_dfs flag needs to be
8456                 * supplied to tell the kernel that userspace will handle radar
8457                 * events when they happen. Otherwise a switch to a channel
8458                 * requiring DFS will be rejected.
8459                 */
8460                need_handle_dfs_flag = false;
8461
8462                /* useless if AP is not running */
8463                if (!wdev->beacon_interval)
8464                        return -ENOTCONN;
8465                break;
8466        case NL80211_IFTYPE_ADHOC:
8467                if (!wdev->ssid_len)
8468                        return -ENOTCONN;
8469                break;
8470        case NL80211_IFTYPE_MESH_POINT:
8471                if (!wdev->mesh_id_len)
8472                        return -ENOTCONN;
8473                break;
8474        default:
8475                return -EOPNOTSUPP;
8476        }
8477
8478        memset(&params, 0, sizeof(params));
8479        params.beacon_csa.ftm_responder = -1;
8480
8481        if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
8482            !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
8483                return -EINVAL;
8484
8485        /* only important for AP, IBSS and mesh create IEs internally */
8486        if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
8487                return -EINVAL;
8488
8489        /* Even though the attribute is u32, the specification says
8490         * u8, so let's make sure we don't overflow.
8491         */
8492        cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
8493        if (cs_count > 255)
8494                return -EINVAL;
8495
8496        params.count = cs_count;
8497
8498        if (!need_new_beacon)
8499                goto skip_beacons;
8500
8501        err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_after);
8502        if (err)
8503                return err;
8504
8505        err = nla_parse_nested_deprecated(csa_attrs, NL80211_ATTR_MAX,
8506                                          info->attrs[NL80211_ATTR_CSA_IES],
8507                                          nl80211_policy, info->extack);
8508        if (err)
8509                return err;
8510
8511        err = nl80211_parse_beacon(rdev, csa_attrs, &params.beacon_csa);
8512        if (err)
8513                return err;
8514
8515        if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
8516                return -EINVAL;
8517
8518        len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
8519        if (!len || (len % sizeof(u16)))
8520                return -EINVAL;
8521
8522        params.n_counter_offsets_beacon = len / sizeof(u16);
8523        if (rdev->wiphy.max_num_csa_counters &&
8524            (params.n_counter_offsets_beacon >
8525             rdev->wiphy.max_num_csa_counters))
8526                return -EINVAL;
8527
8528        params.counter_offsets_beacon =
8529                nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
8530
8531        /* sanity checks - counters should fit and be the same */
8532        for (i = 0; i < params.n_counter_offsets_beacon; i++) {
8533                u16 offset = params.counter_offsets_beacon[i];
8534
8535                if (offset >= params.beacon_csa.tail_len)
8536                        return -EINVAL;
8537
8538                if (params.beacon_csa.tail[offset] != params.count)
8539                        return -EINVAL;
8540        }
8541
8542        if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
8543                len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
8544                if (!len || (len % sizeof(u16)))
8545                        return -EINVAL;
8546
8547                params.n_counter_offsets_presp = len / sizeof(u16);
8548                if (rdev->wiphy.max_num_csa_counters &&
8549                    (params.n_counter_offsets_presp >
8550                     rdev->wiphy.max_num_csa_counters))
8551                        return -EINVAL;
8552
8553                params.counter_offsets_presp =
8554                        nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
8555
8556                /* sanity checks - counters should fit and be the same */
8557                for (i = 0; i < params.n_counter_offsets_presp; i++) {
8558                        u16 offset = params.counter_offsets_presp[i];
8559
8560                        if (offset >= params.beacon_csa.probe_resp_len)
8561                                return -EINVAL;
8562
8563                        if (params.beacon_csa.probe_resp[offset] !=
8564                            params.count)
8565                                return -EINVAL;
8566                }
8567        }
8568
8569skip_beacons:
8570        err = nl80211_parse_chandef(rdev, info, &params.chandef);
8571        if (err)
8572                return err;
8573
8574        if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
8575                                           wdev->iftype))
8576                return -EINVAL;
8577
8578        err = cfg80211_chandef_dfs_required(wdev->wiphy,
8579                                            &params.chandef,
8580                                            wdev->iftype);
8581        if (err < 0)
8582                return err;
8583
8584        if (err > 0) {
8585                params.radar_required = true;
8586                if (need_handle_dfs_flag &&
8587                    !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
8588                        return -EINVAL;
8589                }
8590        }
8591
8592        if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
8593                params.block_tx = true;
8594
8595        wdev_lock(wdev);
8596        err = rdev_channel_switch(rdev, dev, &params);
8597        wdev_unlock(wdev);
8598
8599        return err;
8600}
8601
8602static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
8603                            u32 seq, int flags,
8604                            struct cfg80211_registered_device *rdev,
8605                            struct wireless_dev *wdev,
8606                            struct cfg80211_internal_bss *intbss)
8607{
8608        struct cfg80211_bss *res = &intbss->pub;
8609        const struct cfg80211_bss_ies *ies;
8610        void *hdr;
8611        struct nlattr *bss;
8612
8613        ASSERT_WDEV_LOCK(wdev);
8614
8615        hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
8616                             NL80211_CMD_NEW_SCAN_RESULTS);
8617        if (!hdr)
8618                return -1;
8619
8620        genl_dump_check_consistent(cb, hdr);
8621
8622        if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
8623                goto nla_put_failure;
8624        if (wdev->netdev &&
8625            nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
8626                goto nla_put_failure;
8627        if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
8628                              NL80211_ATTR_PAD))
8629                goto nla_put_failure;
8630
8631        bss = nla_nest_start_noflag(msg, NL80211_ATTR_BSS);
8632        if (!bss)
8633                goto nla_put_failure;
8634        if ((!is_zero_ether_addr(res->bssid) &&
8635             nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
8636                goto nla_put_failure;
8637
8638        rcu_read_lock();
8639        /* indicate whether we have probe response data or not */
8640        if (rcu_access_pointer(res->proberesp_ies) &&
8641            nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
8642                goto fail_unlock_rcu;
8643
8644        /* this pointer prefers to be pointed to probe response data
8645         * but is always valid
8646         */
8647        ies = rcu_dereference(res->ies);
8648        if (ies) {
8649                if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
8650                                      NL80211_BSS_PAD))
8651                        goto fail_unlock_rcu;
8652                if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
8653                                        ies->len, ies->data))
8654                        goto fail_unlock_rcu;
8655        }
8656
8657        /* and this pointer is always (unless driver didn't know) beacon data */
8658        ies = rcu_dereference(res->beacon_ies);
8659        if (ies && ies->from_beacon) {
8660                if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
8661                                      NL80211_BSS_PAD))
8662                        goto fail_unlock_rcu;
8663                if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
8664                                        ies->len, ies->data))
8665                        goto fail_unlock_rcu;
8666        }
8667        rcu_read_unlock();
8668
8669        if (res->beacon_interval &&
8670            nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
8671                goto nla_put_failure;
8672        if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
8673            nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
8674            nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
8675            nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
8676                        jiffies_to_msecs(jiffies - intbss->ts)))
8677                goto nla_put_failure;
8678
8679        if (intbss->parent_tsf &&
8680            (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
8681                               intbss->parent_tsf, NL80211_BSS_PAD) ||
8682             nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
8683                     intbss->parent_bssid)))
8684                goto nla_put_failure;
8685
8686        if (intbss->ts_boottime &&
8687            nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
8688                              intbss->ts_boottime, NL80211_BSS_PAD))
8689                goto nla_put_failure;
8690
8691        if (!nl80211_put_signal(msg, intbss->pub.chains,
8692                                intbss->pub.chain_signal,
8693                                NL80211_BSS_CHAIN_SIGNAL))
8694                goto nla_put_failure;
8695
8696        switch (rdev->wiphy.signal_type) {
8697        case CFG80211_SIGNAL_TYPE_MBM:
8698                if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
8699                        goto nla_put_failure;
8700                break;
8701        case CFG80211_SIGNAL_TYPE_UNSPEC:
8702                if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
8703                        goto nla_put_failure;
8704                break;
8705        default:
8706                break;
8707        }
8708
8709        switch (wdev->iftype) {
8710        case NL80211_IFTYPE_P2P_CLIENT:
8711        case NL80211_IFTYPE_STATION:
8712                if (intbss == wdev->current_bss &&
8713                    nla_put_u32(msg, NL80211_BSS_STATUS,
8714                                NL80211_BSS_STATUS_ASSOCIATED))
8715                        goto nla_put_failure;
8716                break;
8717        case NL80211_IFTYPE_ADHOC:
8718                if (intbss == wdev->current_bss &&
8719                    nla_put_u32(msg, NL80211_BSS_STATUS,
8720                                NL80211_BSS_STATUS_IBSS_JOINED))
8721                        goto nla_put_failure;
8722                break;
8723        default:
8724                break;
8725        }
8726
8727        nla_nest_end(msg, bss);
8728
8729        genlmsg_end(msg, hdr);
8730        return 0;
8731
8732 fail_unlock_rcu:
8733        rcu_read_unlock();
8734 nla_put_failure:
8735        genlmsg_cancel(msg, hdr);
8736        return -EMSGSIZE;
8737}
8738
8739static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
8740{
8741        struct cfg80211_registered_device *rdev;
8742        struct cfg80211_internal_bss *scan;
8743        struct wireless_dev *wdev;
8744        int start = cb->args[2], idx = 0;
8745        int err;
8746
8747        rtnl_lock();
8748        err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
8749        if (err) {
8750                rtnl_unlock();
8751                return err;
8752        }
8753
8754        wdev_lock(wdev);
8755        spin_lock_bh(&rdev->bss_lock);
8756
8757        /*
8758         * dump_scan will be called multiple times to break up the scan results
8759         * into multiple messages.  It is unlikely that any more bss-es will be
8760         * expired after the first call, so only call only call this on the
8761         * first dump_scan invocation.
8762         */
8763        if (start == 0)
8764                cfg80211_bss_expire(rdev);
8765
8766        cb->seq = rdev->bss_generation;
8767
8768        list_for_each_entry(scan, &rdev->bss_list, list) {
8769                if (++idx <= start)
8770                        continue;
8771                if (nl80211_send_bss(skb, cb,
8772                                cb->nlh->nlmsg_seq, NLM_F_MULTI,
8773                                rdev, wdev, scan) < 0) {
8774                        idx--;
8775                        break;
8776                }
8777        }
8778
8779        spin_unlock_bh(&rdev->bss_lock);
8780        wdev_unlock(wdev);
8781
8782        cb->args[2] = idx;
8783        rtnl_unlock();
8784
8785        return skb->len;
8786}
8787
8788static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
8789                               int flags, struct net_device *dev,
8790                               bool allow_radio_stats,
8791                               struct survey_info *survey)
8792{
8793        void *hdr;
8794        struct nlattr *infoattr;
8795
8796        /* skip radio stats if userspace didn't request them */
8797        if (!survey->channel && !allow_radio_stats)
8798                return 0;
8799
8800        hdr = nl80211hdr_put(msg, portid, seq, flags,
8801                             NL80211_CMD_NEW_SURVEY_RESULTS);
8802        if (!hdr)
8803                return -ENOMEM;
8804
8805        if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
8806                goto nla_put_failure;
8807
8808        infoattr = nla_nest_start_noflag(msg, NL80211_ATTR_SURVEY_INFO);
8809        if (!infoattr)
8810                goto nla_put_failure;
8811
8812        if (survey->channel &&
8813            nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
8814                        survey->channel->center_freq))
8815                goto nla_put_failure;
8816
8817        if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
8818            nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
8819                goto nla_put_failure;
8820        if ((survey->filled & SURVEY_INFO_IN_USE) &&
8821            nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
8822                goto nla_put_failure;
8823        if ((survey->filled & SURVEY_INFO_TIME) &&
8824            nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
8825                        survey->time, NL80211_SURVEY_INFO_PAD))
8826                goto nla_put_failure;
8827        if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
8828            nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
8829                              survey->time_busy, NL80211_SURVEY_INFO_PAD))
8830                goto nla_put_failure;
8831        if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
8832            nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
8833                              survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
8834                goto nla_put_failure;
8835        if ((survey->filled & SURVEY_INFO_TIME_RX) &&
8836            nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
8837                              survey->time_rx, NL80211_SURVEY_INFO_PAD))
8838                goto nla_put_failure;
8839        if ((survey->filled & SURVEY_INFO_TIME_TX) &&
8840            nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
8841                              survey->time_tx, NL80211_SURVEY_INFO_PAD))
8842                goto nla_put_failure;
8843        if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
8844            nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
8845                              survey->time_scan, NL80211_SURVEY_INFO_PAD))
8846                goto nla_put_failure;
8847        if ((survey->filled & SURVEY_INFO_TIME_BSS_RX) &&
8848            nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BSS_RX,
8849                              survey->time_bss_rx, NL80211_SURVEY_INFO_PAD))
8850                goto nla_put_failure;
8851
8852        nla_nest_end(msg, infoattr);
8853
8854        genlmsg_end(msg, hdr);
8855        return 0;
8856
8857 nla_put_failure:
8858        genlmsg_cancel(msg, hdr);
8859        return -EMSGSIZE;
8860}
8861
8862static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
8863{
8864        struct nlattr **attrbuf;
8865        struct survey_info survey;
8866        struct cfg80211_registered_device *rdev;
8867        struct wireless_dev *wdev;
8868        int survey_idx = cb->args[2];
8869        int res;
8870        bool radio_stats;
8871
8872        attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
8873        if (!attrbuf)
8874                return -ENOMEM;
8875
8876        rtnl_lock();
8877        res = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
8878        if (res)
8879                goto out_err;
8880
8881        /* prepare_wdev_dump parsed the attributes */
8882        radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
8883
8884        if (!wdev->netdev) {
8885                res = -EINVAL;
8886                goto out_err;
8887        }
8888
8889        if (!rdev->ops->dump_survey) {
8890                res = -EOPNOTSUPP;
8891                goto out_err;
8892        }
8893
8894        while (1) {
8895                res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
8896                if (res == -ENOENT)
8897                        break;
8898                if (res)
8899                        goto out_err;
8900
8901                /* don't send disabled channels, but do send non-channel data */
8902                if (survey.channel &&
8903                    survey.channel->flags & IEEE80211_CHAN_DISABLED) {
8904                        survey_idx++;
8905                        continue;
8906                }
8907
8908                if (nl80211_send_survey(skb,
8909                                NETLINK_CB(cb->skb).portid,
8910                                cb->nlh->nlmsg_seq, NLM_F_MULTI,
8911                                wdev->netdev, radio_stats, &survey) < 0)
8912                        goto out;
8913                survey_idx++;
8914        }
8915
8916 out:
8917        cb->args[2] = survey_idx;
8918        res = skb->len;
8919 out_err:
8920        kfree(attrbuf);
8921        rtnl_unlock();
8922        return res;
8923}
8924
8925static bool nl80211_valid_wpa_versions(u32 wpa_versions)
8926{
8927        return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
8928                                  NL80211_WPA_VERSION_2 |
8929                                  NL80211_WPA_VERSION_3));
8930}
8931
8932static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
8933{
8934        struct cfg80211_registered_device *rdev = info->user_ptr[0];
8935        struct net_device *dev = info->user_ptr[1];
8936        struct ieee80211_channel *chan;
8937        const u8 *bssid, *ssid, *ie = NULL, *auth_data = NULL;
8938        int err, ssid_len, ie_len = 0, auth_data_len = 0;
8939        enum nl80211_auth_type auth_type;
8940        struct key_parse key;
8941        bool local_state_change;
8942
8943        if (!info->attrs[NL80211_ATTR_MAC])
8944                return -EINVAL;
8945
8946        if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
8947                return -EINVAL;
8948
8949        if (!info->attrs[NL80211_ATTR_SSID])
8950                return -EINVAL;
8951
8952        if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
8953                return -EINVAL;
8954
8955        err = nl80211_parse_key(info, &key);
8956        if (err)
8957                return err;
8958
8959        if (key.idx >= 0) {
8960                if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
8961                        return -EINVAL;
8962                if (!key.p.key || !key.p.key_len)
8963                        return -EINVAL;
8964                if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
8965                     key.p.key_len != WLAN_KEY_LEN_WEP40) &&
8966                    (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
8967                     key.p.key_len != WLAN_KEY_LEN_WEP104))
8968                        return -EINVAL;
8969                if (key.idx > 3)
8970                        return -EINVAL;
8971        } else {
8972                key.p.key_len = 0;
8973                key.p.key = NULL;
8974        }
8975
8976        if (key.idx >= 0) {
8977                int i;
8978                bool ok = false;
8979
8980                for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
8981                        if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
8982                                ok = true;
8983                                break;
8984                        }
8985                }
8986                if (!ok)
8987                        return -EINVAL;
8988        }
8989
8990        if (!rdev->ops->auth)
8991                return -EOPNOTSUPP;
8992
8993        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8994            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8995                return -EOPNOTSUPP;
8996
8997        bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8998        chan = nl80211_get_valid_chan(&rdev->wiphy,
8999                                      info->attrs[NL80211_ATTR_WIPHY_FREQ]);
9000        if (!chan)
9001                return -EINVAL;
9002
9003        ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9004        ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9005
9006        if (info->attrs[NL80211_ATTR_IE]) {
9007                ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9008                ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9009        }
9010
9011        auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
9012        if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
9013                return -EINVAL;
9014
9015        if ((auth_type == NL80211_AUTHTYPE_SAE ||
9016             auth_type == NL80211_AUTHTYPE_FILS_SK ||
9017             auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
9018             auth_type == NL80211_AUTHTYPE_FILS_PK) &&
9019            !info->attrs[NL80211_ATTR_AUTH_DATA])
9020                return -EINVAL;
9021
9022        if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
9023                if (auth_type != NL80211_AUTHTYPE_SAE &&
9024                    auth_type != NL80211_AUTHTYPE_FILS_SK &&
9025                    auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
9026                    auth_type != NL80211_AUTHTYPE_FILS_PK)
9027                        return -EINVAL;
9028                auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
9029                auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
9030                /* need to include at least Auth Transaction and Status Code */
9031                if (auth_data_len < 4)
9032                        return -EINVAL;
9033        }
9034
9035        local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9036
9037        /*
9038         * Since we no longer track auth state, ignore
9039         * requests to only change local state.
9040         */
9041        if (local_state_change)
9042                return 0;
9043
9044        wdev_lock(dev->ieee80211_ptr);
9045        err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
9046                                 ssid, ssid_len, ie, ie_len,
9047                                 key.p.key, key.p.key_len, key.idx,
9048                                 auth_data, auth_data_len);
9049        wdev_unlock(dev->ieee80211_ptr);
9050        return err;
9051}
9052
9053static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
9054                                     struct genl_info *info)
9055{
9056        if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9057                GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
9058                return -EINVAL;
9059        }
9060
9061        if (!rdev->ops->tx_control_port ||
9062            !wiphy_ext_feature_isset(&rdev->wiphy,
9063                                     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
9064                return -EOPNOTSUPP;
9065
9066        return 0;
9067}
9068
9069static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
9070                                   struct genl_info *info,
9071                                   struct cfg80211_crypto_settings *settings,
9072                                   int cipher_limit)
9073{
9074        memset(settings, 0, sizeof(*settings));
9075
9076        settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
9077
9078        if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
9079                u16 proto;
9080
9081                proto = nla_get_u16(
9082                        info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
9083                settings->control_port_ethertype = cpu_to_be16(proto);
9084                if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
9085                    proto != ETH_P_PAE)
9086                        return -EINVAL;
9087                if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
9088                        settings->control_port_no_encrypt = true;
9089        } else
9090                settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
9091
9092        if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
9093                int r = validate_pae_over_nl80211(rdev, info);
9094
9095                if (r < 0)
9096                        return r;
9097
9098                settings->control_port_over_nl80211 = true;
9099        }
9100
9101        if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
9102                void *data;
9103                int len, i;
9104
9105                data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
9106                len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
9107                settings->n_ciphers_pairwise = len / sizeof(u32);
9108
9109                if (len % sizeof(u32))
9110                        return -EINVAL;
9111
9112                if (settings->n_ciphers_pairwise > cipher_limit)
9113                        return -EINVAL;
9114
9115                memcpy(settings->ciphers_pairwise, data, len);
9116
9117                for (i = 0; i < settings->n_ciphers_pairwise; i++)
9118                        if (!cfg80211_supported_cipher_suite(
9119                                        &rdev->wiphy,
9120                                        settings->ciphers_pairwise[i]))
9121                                return -EINVAL;
9122        }
9123
9124        if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
9125                settings->cipher_group =
9126                        nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
9127                if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
9128                                                     settings->cipher_group))
9129                        return -EINVAL;
9130        }
9131
9132        if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
9133                settings->wpa_versions =
9134                        nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
9135                if (!nl80211_valid_wpa_versions(settings->wpa_versions))
9136                        return -EINVAL;
9137        }
9138
9139        if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
9140                void *data;
9141                int len;
9142
9143                data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
9144                len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
9145                settings->n_akm_suites = len / sizeof(u32);
9146
9147                if (len % sizeof(u32))
9148                        return -EINVAL;
9149
9150                if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
9151                        return -EINVAL;
9152
9153                memcpy(settings->akm_suites, data, len);
9154        }
9155
9156        if (info->attrs[NL80211_ATTR_PMK]) {
9157                if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
9158                        return -EINVAL;
9159                if (!wiphy_ext_feature_isset(&rdev->wiphy,
9160                                             NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK))
9161                        return -EINVAL;
9162                settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
9163        }
9164
9165        if (info->attrs[NL80211_ATTR_SAE_PASSWORD]) {
9166                if (!wiphy_ext_feature_isset(&rdev->wiphy,
9167                                             NL80211_EXT_FEATURE_SAE_OFFLOAD))
9168                        return -EINVAL;
9169                settings->sae_pwd =
9170                        nla_data(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
9171                settings->sae_pwd_len =
9172                        nla_len(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
9173        }
9174
9175        return 0;
9176}
9177
9178static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
9179{
9180        struct cfg80211_registered_device *rdev = info->user_ptr[0];
9181        struct net_device *dev = info->user_ptr[1];
9182        struct ieee80211_channel *chan;
9183        struct cfg80211_assoc_request req = {};
9184        const u8 *bssid, *ssid;
9185        int err, ssid_len = 0;
9186
9187        if (dev->ieee80211_ptr->conn_owner_nlportid &&
9188            dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9189                return -EPERM;
9190
9191        if (!info->attrs[NL80211_ATTR_MAC] ||
9192            !info->attrs[NL80211_ATTR_SSID] ||
9193            !info->attrs[NL80211_ATTR_WIPHY_FREQ])
9194                return -EINVAL;
9195
9196        if (!rdev->ops->assoc)
9197                return -EOPNOTSUPP;
9198
9199        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9200            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9201                return -EOPNOTSUPP;
9202
9203        bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9204
9205        chan = nl80211_get_valid_chan(&rdev->wiphy,
9206                                      info->attrs[NL80211_ATTR_WIPHY_FREQ]);
9207        if (!chan)
9208                return -EINVAL;
9209
9210        ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9211        ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9212
9213        if (info->attrs[NL80211_ATTR_IE]) {
9214                req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9215                req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9216        }
9217
9218        if (info->attrs[NL80211_ATTR_USE_MFP]) {
9219                enum nl80211_mfp mfp =
9220                        nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
9221                if (mfp == NL80211_MFP_REQUIRED)
9222                        req.use_mfp = true;
9223                else if (mfp != NL80211_MFP_NO)
9224                        return -EINVAL;
9225        }
9226
9227        if (info->attrs[NL80211_ATTR_PREV_BSSID])
9228                req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
9229
9230        if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
9231                req.flags |= ASSOC_REQ_DISABLE_HT;
9232
9233        if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9234                memcpy(&req.ht_capa_mask,
9235                       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9236                       sizeof(req.ht_capa_mask));
9237
9238        if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9239                if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9240                        return -EINVAL;
9241                memcpy(&req.ht_capa,
9242                       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9243                       sizeof(req.ht_capa));
9244        }
9245
9246        if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
9247                req.flags |= ASSOC_REQ_DISABLE_VHT;
9248
9249        if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9250                memcpy(&req.vht_capa_mask,
9251                       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
9252                       sizeof(req.vht_capa_mask));
9253
9254        if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
9255                if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9256                        return -EINVAL;
9257                memcpy(&req.vht_capa,
9258                       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
9259                       sizeof(req.vht_capa));
9260        }
9261
9262        if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
9263                if (!((rdev->wiphy.features &
9264                        NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
9265                       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
9266                    !wiphy_ext_feature_isset(&rdev->wiphy,
9267                                             NL80211_EXT_FEATURE_RRM))
9268                        return -EINVAL;
9269                req.flags |= ASSOC_REQ_USE_RRM;
9270        }
9271
9272        if (info->attrs[NL80211_ATTR_FILS_KEK]) {
9273                req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
9274                req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
9275                if (!info->attrs[NL80211_ATTR_FILS_NONCES])
9276                        return -EINVAL;
9277                req.fils_nonces =
9278                        nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
9279        }
9280
9281        err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
9282        if (!err) {
9283                wdev_lock(dev->ieee80211_ptr);
9284
9285                err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
9286                                          ssid, ssid_len, &req);
9287
9288                if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9289                        dev->ieee80211_ptr->conn_owner_nlportid =
9290                                info->snd_portid;
9291                        memcpy(dev->ieee80211_ptr->disconnect_bssid,
9292                               bssid, ETH_ALEN);
9293                }
9294
9295                wdev_unlock(dev->ieee80211_ptr);
9296        }
9297
9298        return err;
9299}
9300
9301static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
9302{
9303        struct cfg80211_registered_device *rdev = info->user_ptr[0];
9304        struct net_device *dev = info->user_ptr[1];
9305        const u8 *ie = NULL, *bssid;
9306        int ie_len = 0, err;
9307        u16 reason_code;
9308        bool local_state_change;
9309
9310        if (dev->ieee80211_ptr->conn_owner_nlportid &&
9311            dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9312                return -EPERM;
9313
9314        if (!info->attrs[NL80211_ATTR_MAC])
9315                return -EINVAL;
9316
9317        if (!info->attrs[NL80211_ATTR_REASON_CODE])
9318                return -EINVAL;
9319
9320        if (!rdev->ops->deauth)
9321                return -EOPNOTSUPP;
9322
9323        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9324            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9325                return -EOPNOTSUPP;
9326
9327        bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9328
9329        reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9330        if (reason_code == 0) {
9331                /* Reason Code 0 is reserved */
9332                return -EINVAL;
9333        }
9334
9335        if (info->attrs[NL80211_ATTR_IE]) {
9336                ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9337                ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9338        }
9339
9340        local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9341
9342        wdev_lock(dev->ieee80211_ptr);
9343        err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
9344                                   local_state_change);
9345        wdev_unlock(dev->ieee80211_ptr);
9346        return err;
9347}
9348
9349static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
9350{
9351        struct cfg80211_registered_device *rdev = info->user_ptr[0];
9352        struct net_device *dev = info->user_ptr[1];
9353        const u8 *ie = NULL, *bssid;
9354        int ie_len = 0, err;
9355        u16 reason_code;
9356        bool local_state_change;
9357
9358        if (dev->ieee80211_ptr->conn_owner_nlportid &&
9359            dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9360                return -EPERM;
9361
9362        if (!info->attrs[NL80211_ATTR_MAC])
9363                return -EINVAL;
9364
9365        if (!info->attrs[NL80211_ATTR_REASON_CODE])
9366                return -EINVAL;
9367
9368        if (!rdev->ops->disassoc)
9369                return -EOPNOTSUPP;
9370
9371        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9372            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9373                return -EOPNOTSUPP;
9374
9375        bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9376
9377        reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9378        if (reason_code == 0) {
9379                /* Reason Code 0 is reserved */
9380                return -EINVAL;
9381        }
9382
9383        if (info->attrs[NL80211_ATTR_IE]) {
9384                ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9385                ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9386        }
9387
9388        local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9389
9390        wdev_lock(dev->ieee80211_ptr);
9391        err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
9392                                     local_state_change);
9393        wdev_unlock(dev->ieee80211_ptr);
9394        return err;
9395}
9396
9397static bool
9398nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
9399                         int mcast_rate[NUM_NL80211_BANDS],
9400                         int rateval)
9401{
9402        struct wiphy *wiphy = &rdev->wiphy;
9403        bool found = false;
9404        int band, i;
9405
9406        for (band = 0; band < NUM_NL80211_BANDS; band++) {
9407                struct ieee80211_supported_band *sband;
9408
9409                sband = wiphy->bands[band];
9410                if (!sband)
9411                        continue;
9412
9413                for (i = 0; i < sband->n_bitrates; i++) {
9414                        if (sband->bitrates[i].bitrate == rateval) {
9415                                mcast_rate[band] = i + 1;
9416                                found = true;
9417                                break;
9418                        }
9419                }
9420        }
9421
9422        return found;
9423}
9424
9425static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
9426{
9427        struct cfg80211_registered_device *rdev = info->user_ptr[0];
9428        struct net_device *dev = info->user_ptr[1];
9429        struct cfg80211_ibss_params ibss;
9430        struct wiphy *wiphy;
9431        struct cfg80211_cached_keys *connkeys = NULL;
9432        int err;
9433
9434        memset(&ibss, 0, sizeof(ibss));
9435
9436        if (!info->attrs[NL80211_ATTR_SSID] ||
9437            !nla_len(info->attrs[NL80211_ATTR_SSID]))
9438                return -EINVAL;
9439
9440        ibss.beacon_interval = 100;
9441
9442        if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
9443                ibss.beacon_interval =
9444                        nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
9445
9446        err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
9447                                           ibss.beacon_interval);
9448        if (err)
9449                return err;
9450
9451        if (!rdev->ops->join_ibss)
9452                return -EOPNOTSUPP;
9453
9454        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
9455                return -EOPNOTSUPP;
9456
9457        wiphy = &rdev->wiphy;
9458
9459        if (info->attrs[NL80211_ATTR_MAC]) {
9460                ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9461
9462                if (!is_valid_ether_addr(ibss.bssid))
9463                        return -EINVAL;
9464        }
9465        ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9466        ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9467
9468        if (info->attrs[NL80211_ATTR_IE]) {
9469                ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9470                ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9471        }
9472
9473        err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
9474        if (err)
9475                return err;
9476
9477        if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
9478                                     NL80211_IFTYPE_ADHOC))
9479                return -EINVAL;
9480
9481        switch (ibss.chandef.width) {
9482        case NL80211_CHAN_WIDTH_5:
9483        case NL80211_CHAN_WIDTH_10:
9484        case NL80211_CHAN_WIDTH_20_NOHT:
9485                break;
9486        case NL80211_CHAN_WIDTH_20:
9487        case NL80211_CHAN_WIDTH_40:
9488                if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
9489                        return -EINVAL;
9490                break;
9491        case NL80211_CHAN_WIDTH_80:
9492        case NL80211_CHAN_WIDTH_80P80:
9493        case NL80211_CHAN_WIDTH_160:
9494                if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
9495                        return -EINVAL;
9496                if (!wiphy_ext_feature_isset(&rdev->wiphy,
9497                                             NL80211_EXT_FEATURE_VHT_IBSS))
9498                        return -EINVAL;
9499                break;
9500        default:
9501                return -EINVAL;
9502        }
9503
9504        ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
9505        ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
9506
9507        if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
9508                u8 *rates =
9509                        nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
9510                int n_rates =
9511                        nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
9512                struct ieee80211_supported_band *sband =
9513                        wiphy->bands[ibss.chandef.chan->band];
9514
9515                err = ieee80211_get_ratemask(sband, rates, n_rates,
9516                                             &ibss.basic_rates);
9517                if (err)
9518                        return err;
9519        }
9520
9521        if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9522                memcpy(&ibss.ht_capa_mask,
9523                       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9524                       sizeof(ibss.ht_capa_mask));
9525
9526        if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9527                if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9528                        return -EINVAL;
9529                memcpy(&ibss.ht_capa,
9530                       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9531                       sizeof(ibss.ht_capa));
9532        }
9533
9534        if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
9535            !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
9536                        nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
9537                return -EINVAL;
9538
9539        if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
9540                bool no_ht = false;
9541
9542                connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
9543                if (IS_ERR(connkeys))
9544                        return PTR_ERR(connkeys);
9545
9546                if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
9547                    no_ht) {
9548                        kzfree(connkeys);
9549                        return -EINVAL;
9550                }
9551        }
9552
9553        ibss.control_port =
9554                nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
9555
9556        if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
9557                int r = validate_pae_over_nl80211(rdev, info);
9558
9559                if (r < 0) {
9560                        kzfree(connkeys);
9561                        return r;
9562                }
9563
9564                ibss.control_port_over_nl80211 = true;
9565        }
9566
9567        ibss.userspace_handles_dfs =
9568                nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
9569
9570        wdev_lock(dev->ieee80211_ptr);
9571        err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
9572        if (err)
9573                kzfree(connkeys);
9574        else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
9575                dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
9576        wdev_unlock(dev->ieee80211_ptr);
9577
9578        return err;
9579}
9580
9581static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
9582{
9583        struct cfg80211_registered_device *rdev = info->user_ptr[0];
9584        struct net_device *dev = info->user_ptr[1];
9585
9586        if (!rdev->ops->leave_ibss)
9587                return -EOPNOTSUPP;
9588
9589        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
9590                return -EOPNOTSUPP;
9591
9592        return cfg80211_leave_ibss(rdev, dev, false);
9593}
9594
9595static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
9596{
9597        struct cfg80211_registered_device *rdev = info->user_ptr[0];
9598        struct net_device *dev = info->user_ptr[1];
9599        int mcast_rate[NUM_NL80211_BANDS];
9600        u32 nla_rate;
9601        int err;
9602
9603        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
9604            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
9605            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
9606                return -EOPNOTSUPP;
9607
9608        if (!rdev->ops->set_mcast_rate)
9609                return -EOPNOTSUPP;
9610
9611        memset(mcast_rate, 0, sizeof(mcast_rate));
9612
9613        if (!info->attrs[NL80211_ATTR_MCAST_RATE])
9614                return -EINVAL;
9615
9616        nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
9617        if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
9618                return -EINVAL;
9619
9620        err = rdev_set_mcast_rate(rdev, dev, mcast_rate);
9621
9622        return err;
9623}
9624
9625static struct sk_buff *
9626__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
9627                            struct wireless_dev *wdev, int approxlen,
9628                            u32 portid, u32 seq, enum nl80211_commands cmd,
9629                            enum nl80211_attrs attr,
9630                            const struct nl80211_vendor_cmd_info *info,
9631                            gfp_t gfp)
9632{
9633        struct sk_buff *skb;
9634        void *hdr;
9635        struct nlattr *data;
9636
9637        skb = nlmsg_new(approxlen + 100, gfp);
9638        if (!skb)
9639                return NULL;
9640
9641        hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
9642        if (!hdr) {
9643                kfree_skb(skb);
9644                return NULL;
9645        }
9646
9647        if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
9648                goto nla_put_failure;
9649
9650        if (info) {
9651                if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
9652                                info->vendor_id))
9653                        goto nla_put_failure;
9654                if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
9655                                info->subcmd))
9656                        goto nla_put_failure;
9657        }
9658
9659        if (wdev) {
9660                if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
9661                                      wdev_id(wdev), NL80211_ATTR_PAD))
9662                        goto nla_put_failure;
9663                if (wdev->netdev &&
9664                    nla_put_u32(skb, NL80211_ATTR_IFINDEX,
9665                                wdev->netdev->ifindex))
9666                        goto nla_put_failure;
9667        }
9668
9669        data = nla_nest_start_noflag(skb, attr);
9670        if (!data)
9671                goto nla_put_failure;
9672
9673        ((void **)skb->cb)[0] = rdev;
9674        ((void **)skb->cb)[1] = hdr;
9675        ((void **)skb->cb)[2] = data;
9676
9677        return skb;
9678
9679 nla_put_failure:
9680        kfree_skb(skb);
9681        return NULL;
9682}
9683
9684struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
9685                                           struct wireless_dev *wdev,
9686                                           enum nl80211_commands cmd,
9687                                           enum nl80211_attrs attr,
9688                                           unsigned int portid,
9689                                           int vendor_event_idx,
9690                                           int approxlen, gfp_t gfp)
9691{
9692        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
9693        const struct nl80211_vendor_cmd_info *info;
9694
9695        switch (cmd) {
9696        case NL80211_CMD_TESTMODE:
9697                if (WARN_ON(vendor_event_idx != -1))
9698                        return NULL;
9699                info = NULL;
9700                break;
9701        case NL80211_CMD_VENDOR:
9702                if (WARN_ON(vendor_event_idx < 0 ||
9703                            vendor_event_idx >= wiphy->n_vendor_events))
9704                        return NULL;
9705                info = &wiphy->vendor_events[vendor_event_idx];
9706                break;
9707        default:
9708                WARN_ON(1);
9709                return NULL;
9710        }
9711
9712        return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, portid, 0,
9713                                           cmd, attr, info, gfp);
9714}
9715EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
9716
9717void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
9718{
9719        struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9720        void *hdr = ((void **)skb->cb)[1];
9721        struct nlmsghdr *nlhdr = nlmsg_hdr(skb);
9722        struct nlattr *data = ((void **)skb->cb)[2];
9723        enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
9724
9725        /* clear CB data for netlink core to own from now on */
9726        memset(skb->cb, 0, sizeof(skb->cb));
9727
9728        nla_nest_end(skb, data);
9729        genlmsg_end(skb, hdr);
9730
9731        if (nlhdr->nlmsg_pid) {
9732                genlmsg_unicast(wiphy_net(&rdev->wiphy), skb,
9733                                nlhdr->nlmsg_pid);
9734        } else {
9735                if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
9736                        mcgrp = NL80211_MCGRP_VENDOR;
9737
9738                genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
9739                                        skb, 0, mcgrp, gfp);
9740        }
9741}
9742EXPORT_SYMBOL(__cfg80211_send_event_skb);
9743
9744#ifdef CONFIG_NL80211_TESTMODE
9745static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
9746{
9747        struct cfg80211_registered_device *rdev = info->user_ptr[0];
9748        struct wireless_dev *wdev =
9749                __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
9750        int err;
9751
9752        if (!rdev->ops->testmode_cmd)
9753                return -EOPNOTSUPP;
9754
9755        if (IS_ERR(wdev)) {
9756                err = PTR_ERR(wdev);
9757                if (err != -EINVAL)
9758                        return err;
9759                wdev = NULL;
9760        } else if (wdev->wiphy != &rdev->wiphy) {
9761                return -EINVAL;
9762        }
9763
9764        if (!info->attrs[NL80211_ATTR_TESTDATA])
9765                return -EINVAL;
9766
9767        rdev->cur_cmd_info = info;
9768        err = rdev_testmode_cmd(rdev, wdev,
9769                                nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
9770                                nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
9771        rdev->cur_cmd_info = NULL;
9772
9773        return err;
9774}
9775
9776static int nl80211_testmode_dump(struct sk_buff *skb,
9777                                 struct netlink_callback *cb)
9778{
9779        struct cfg80211_registered_device *rdev;
9780        struct nlattr **attrbuf = NULL;
9781        int err;
9782        long phy_idx;
9783        void *data = NULL;
9784        int data_len = 0;
9785
9786        rtnl_lock();
9787
9788        if (cb->args[0]) {
9789                /*
9790                 * 0 is a valid index, but not valid for args[0],
9791                 * so we need to offset by 1.
9792                 */
9793                phy_idx = cb->args[0] - 1;
9794
9795                rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
9796                if (!rdev) {
9797                        err = -ENOENT;
9798                        goto out_err;
9799                }
9800        } else {
9801                attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
9802                                  GFP_KERNEL);
9803                if (!attrbuf) {
9804                        err = -ENOMEM;
9805                        goto out_err;
9806                }
9807
9808                err = nlmsg_parse_deprecated(cb->nlh,
9809                                             GENL_HDRLEN + nl80211_fam.hdrsize,
9810                                             attrbuf, nl80211_fam.maxattr,
9811                                             nl80211_policy, NULL);
9812                if (err)
9813                        goto out_err;
9814
9815                rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
9816                if (IS_ERR(rdev)) {
9817                        err = PTR_ERR(rdev);
9818                        goto out_err;
9819                }
9820                phy_idx = rdev->wiphy_idx;
9821
9822                if (attrbuf[NL80211_ATTR_TESTDATA])
9823                        cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
9824        }
9825
9826        if (cb->args[1]) {
9827                data = nla_data((void *)cb->args[1]);
9828                data_len = nla_len((void *)cb->args[1]);
9829        }
9830
9831        if (!rdev->ops->testmode_dump) {
9832                err = -EOPNOTSUPP;
9833                goto out_err;
9834        }
9835
9836        while (1) {
9837                void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
9838                                           cb->nlh->nlmsg_seq, NLM_F_MULTI,
9839                                           NL80211_CMD_TESTMODE);
9840                struct nlattr *tmdata;
9841
9842                if (!hdr)
9843                        break;
9844
9845                if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
9846                        genlmsg_cancel(skb, hdr);
9847                        break;
9848                }
9849
9850                tmdata = nla_nest_start_noflag(skb, NL80211_ATTR_TESTDATA);
9851                if (!tmdata) {
9852                        genlmsg_cancel(skb, hdr);
9853                        break;
9854                }
9855                err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
9856                nla_nest_end(skb, tmdata);
9857
9858                if (err == -ENOBUFS || err == -ENOENT) {
9859                        genlmsg_cancel(skb, hdr);
9860                        break;
9861                } else if (err) {
9862                        genlmsg_cancel(skb, hdr);
9863                        goto out_err;
9864                }
9865
9866                genlmsg_end(skb, hdr);
9867        }
9868
9869        err = skb->len;
9870        /* see above */
9871        cb->args[0] = phy_idx + 1;
9872 out_err:
9873        kfree(attrbuf);
9874        rtnl_unlock();
9875        return err;
9876}
9877#endif
9878
9879static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
9880{
9881        struct cfg80211_registered_device *rdev = info->user_ptr[0];
9882        struct net_device *dev = info->user_ptr[1];
9883        struct cfg80211_connect_params connect;
9884        struct wiphy *wiphy;
9885        struct cfg80211_cached_keys *connkeys = NULL;
9886        int err;
9887
9888        memset(&connect, 0, sizeof(connect));
9889
9890        if (!info->attrs[NL80211_ATTR_SSID] ||
9891            !nla_len(info->attrs[NL80211_ATTR_SSID]))
9892                return -EINVAL;
9893
9894        if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
9895                connect.auth_type =
9896                        nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
9897                if (!nl80211_valid_auth_type(rdev, connect.auth_type,
9898                                             NL80211_CMD_CONNECT))
9899                        return -EINVAL;
9900        } else
9901                connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
9902
9903        connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
9904
9905        if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
9906            !wiphy_ext_feature_isset(&rdev->wiphy,
9907                                     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
9908                return -EINVAL;
9909        connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
9910
9911        err = nl80211_crypto_settings(rdev, info, &connect.crypto,
9912                                      NL80211_MAX_NR_CIPHER_SUITES);
9913        if (err)
9914                return err;
9915
9916        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9917            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9918                return -EOPNOTSUPP;
9919
9920        wiphy = &rdev->wiphy;
9921
9922        connect.bg_scan_period = -1;
9923        if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
9924                (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
9925                connect.bg_scan_period =
9926                        nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
9927        }
9928
9929        if (info->attrs[NL80211_ATTR_MAC])
9930                connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9931        else if (info->attrs[NL80211_ATTR_MAC_HINT])
9932                connect.bssid_hint =
9933                        nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
9934        connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9935        connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9936
9937        if (info->attrs[NL80211_ATTR_IE]) {
9938                connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9939                connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9940        }
9941
9942        if (info->attrs[NL80211_ATTR_USE_MFP]) {
9943                connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
9944                if (connect.mfp == NL80211_MFP_OPTIONAL &&
9945                    !wiphy_ext_feature_isset(&rdev->wiphy,
9946                                             NL80211_EXT_FEATURE_MFP_OPTIONAL))
9947                        return -EOPNOTSUPP;
9948        } else {
9949                connect.mfp = NL80211_MFP_NO;
9950        }
9951
9952        if (info->attrs[NL80211_ATTR_PREV_BSSID])
9953                connect.prev_bssid =
9954                        nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
9955
9956        if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
9957                connect.channel = nl80211_get_valid_chan(
9958                        wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
9959                if (!connect.channel)
9960                        return -EINVAL;
9961        } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
9962                connect.channel_hint = nl80211_get_valid_chan(
9963                        wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
9964                if (!connect.channel_hint)
9965                        return -EINVAL;
9966        }
9967
9968        if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
9969                connect.edmg.channels =
9970                      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
9971
9972                if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
9973                        connect.edmg.bw_config =
9974                                nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
9975        }
9976
9977        if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
9978                connkeys = nl80211_parse_connkeys(rdev, info, NULL);
9979                if (IS_ERR(connkeys))
9980                        return PTR_ERR(connkeys);
9981        }
9982
9983        if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
9984                connect.flags |= ASSOC_REQ_DISABLE_HT;
9985
9986        if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9987                memcpy(&connect.ht_capa_mask,
9988                       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9989                       sizeof(connect.ht_capa_mask));
9990
9991        if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9992                if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
9993                        kzfree(connkeys);
9994                        return -EINVAL;
9995                }
9996                memcpy(&connect.ht_capa,
9997                       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9998                       sizeof(connect.ht_capa));
9999        }
10000
10001        if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
10002                connect.flags |= ASSOC_REQ_DISABLE_VHT;
10003
10004        if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
10005                memcpy(&connect.vht_capa_mask,
10006                       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
10007                       sizeof(connect.vht_capa_mask));
10008
10009        if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
10010                if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
10011                        kzfree(connkeys);
10012                        return -EINVAL;
10013                }
10014                memcpy(&connect.vht_capa,
10015                       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
10016                       sizeof(connect.vht_capa));
10017        }
10018
10019        if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
10020                if (!((rdev->wiphy.features &
10021                        NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
10022                       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
10023                    !wiphy_ext_feature_isset(&rdev->wiphy,
10024                                             NL80211_EXT_FEATURE_RRM)) {
10025                        kzfree(connkeys);
10026                        return -EINVAL;
10027                }
10028                connect.flags |= ASSOC_REQ_USE_RRM;
10029        }
10030
10031        connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
10032        if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
10033                kzfree(connkeys);
10034                return -EOPNOTSUPP;
10035        }
10036
10037        if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
10038                /* bss selection makes no sense if bssid is set */
10039                if (connect.bssid) {
10040                        kzfree(connkeys);
10041                        return -EINVAL;
10042                }
10043
10044                err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
10045                                       wiphy, &connect.bss_select);
10046                if (err) {
10047                        kzfree(connkeys);
10048                        return err;
10049                }
10050        }
10051
10052        if (wiphy_ext_feature_isset(&rdev->wiphy,
10053                                    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
10054            info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
10055            info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
10056            info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
10057            info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10058                connect.fils_erp_username =
10059                        nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10060                connect.fils_erp_username_len =
10061                        nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10062                connect.fils_erp_realm =
10063                        nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10064                connect.fils_erp_realm_len =
10065                        nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10066                connect.fils_erp_next_seq_num =
10067                        nla_get_u16(
10068                           info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
10069                connect.fils_erp_rrk =
10070                        nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10071                connect.fils_erp_rrk_len =
10072                        nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10073        } else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
10074                   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
10075                   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
10076                   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10077                kzfree(connkeys);
10078                return -EINVAL;
10079        }
10080
10081        if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
10082                if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10083                        kzfree(connkeys);
10084                        GENL_SET_ERR_MSG(info,
10085                                         "external auth requires connection ownership");
10086                        return -EINVAL;
10087                }
10088                connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
10089        }
10090
10091        wdev_lock(dev->ieee80211_ptr);
10092
10093        err = cfg80211_connect(rdev, dev, &connect, connkeys,
10094                               connect.prev_bssid);
10095        if (err)
10096                kzfree(connkeys);
10097
10098        if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10099                dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
10100                if (connect.bssid)
10101                        memcpy(dev->ieee80211_ptr->disconnect_bssid,
10102                               connect.bssid, ETH_ALEN);
10103                else
10104                        memset(dev->ieee80211_ptr->disconnect_bssid,
10105                               0, ETH_ALEN);
10106        }
10107
10108        wdev_unlock(dev->ieee80211_ptr);
10109
10110        return err;
10111}
10112
10113static int nl80211_update_connect_params(struct sk_buff *skb,
10114                                         struct genl_info *info)
10115{
10116        struct cfg80211_connect_params connect = {};
10117        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10118        struct net_device *dev = info->user_ptr[1];
10119        struct wireless_dev *wdev = dev->ieee80211_ptr;
10120        bool fils_sk_offload;
10121        u32 auth_type;
10122        u32 changed = 0;
10123        int ret;
10124
10125        if (!rdev->ops->update_connect_params)
10126                return -EOPNOTSUPP;
10127
10128        if (info->attrs[NL80211_ATTR_IE]) {
10129                connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10130                connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10131                changed |= UPDATE_ASSOC_IES;
10132        }
10133
10134        fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
10135                                                  NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
10136
10137        /*
10138         * when driver supports fils-sk offload all attributes must be
10139         * provided. So the else covers "fils-sk-not-all" and
10140         * "no-fils-sk-any".
10141         */
10142        if (fils_sk_offload &&
10143            info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
10144            info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
10145            info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
10146            info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10147                connect.fils_erp_username =
10148                        nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10149                connect.fils_erp_username_len =
10150                        nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10151                connect.fils_erp_realm =
10152                        nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10153                connect.fils_erp_realm_len =
10154                        nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10155                connect.fils_erp_next_seq_num =
10156                        nla_get_u16(
10157                           info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
10158                connect.fils_erp_rrk =
10159                        nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10160                connect.fils_erp_rrk_len =
10161                        nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10162                changed |= UPDATE_FILS_ERP_INFO;
10163        } else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
10164                   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
10165                   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
10166                   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10167                return -EINVAL;
10168        }
10169
10170        if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
10171                auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10172                if (!nl80211_valid_auth_type(rdev, auth_type,
10173                                             NL80211_CMD_CONNECT))
10174                        return -EINVAL;
10175
10176                if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
10177                    fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
10178                        return -EINVAL;
10179
10180                connect.auth_type = auth_type;
10181                changed |= UPDATE_AUTH_TYPE;
10182        }
10183
10184        wdev_lock(dev->ieee80211_ptr);
10185        if (!wdev->current_bss)
10186                ret = -ENOLINK;
10187        else
10188                ret = rdev_update_connect_params(rdev, dev, &connect, changed);
10189        wdev_unlock(dev->ieee80211_ptr);
10190
10191        return ret;
10192}
10193
10194static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
10195{
10196        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10197        struct net_device *dev = info->user_ptr[1];
10198        u16 reason;
10199        int ret;
10200
10201        if (dev->ieee80211_ptr->conn_owner_nlportid &&
10202            dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10203                return -EPERM;
10204
10205        if (!info->attrs[NL80211_ATTR_REASON_CODE])
10206                reason = WLAN_REASON_DEAUTH_LEAVING;
10207        else
10208                reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
10209
10210        if (reason == 0)
10211                return -EINVAL;
10212
10213        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10214            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10215                return -EOPNOTSUPP;
10216
10217        wdev_lock(dev->ieee80211_ptr);
10218        ret = cfg80211_disconnect(rdev, dev, reason, true);
10219        wdev_unlock(dev->ieee80211_ptr);
10220        return ret;
10221}
10222
10223static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
10224{
10225        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10226        struct net *net;
10227        int err;
10228
10229        if (info->attrs[NL80211_ATTR_PID]) {
10230                u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
10231
10232                net = get_net_ns_by_pid(pid);
10233        } else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
10234                u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
10235
10236                net = get_net_ns_by_fd(fd);
10237        } else {
10238                return -EINVAL;
10239        }
10240
10241        if (IS_ERR(net))
10242                return PTR_ERR(net);
10243
10244        err = 0;
10245
10246        /* check if anything to do */
10247        if (!net_eq(wiphy_net(&rdev->wiphy), net))
10248                err = cfg80211_switch_netns(rdev, net);
10249
10250        put_net(net);
10251        return err;
10252}
10253
10254static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
10255{
10256        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10257        int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
10258                        struct cfg80211_pmksa *pmksa) = NULL;
10259        struct net_device *dev = info->user_ptr[1];
10260        struct cfg80211_pmksa pmksa;
10261
10262        memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
10263
10264        if (!info->attrs[NL80211_ATTR_PMKID])
10265                return -EINVAL;
10266
10267        pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
10268
10269        if (info->attrs[NL80211_ATTR_MAC]) {
10270                pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10271        } else if (info->attrs[NL80211_ATTR_SSID] &&
10272                   info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
10273                   (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
10274                    info->attrs[NL80211_ATTR_PMK])) {
10275                pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10276                pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10277                pmksa.cache_id =
10278                        nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
10279        } else {
10280                return -EINVAL;
10281        }
10282        if (info->attrs[NL80211_ATTR_PMK]) {
10283                pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
10284                pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
10285        }
10286
10287        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10288            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
10289            !(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP &&
10290              wiphy_ext_feature_isset(&rdev->wiphy,
10291                                      NL80211_EXT_FEATURE_AP_PMKSA_CACHING)))
10292                return -EOPNOTSUPP;
10293
10294        switch (info->genlhdr->cmd) {
10295        case NL80211_CMD_SET_PMKSA:
10296                rdev_ops = rdev->ops->set_pmksa;
10297                break;
10298        case NL80211_CMD_DEL_PMKSA:
10299                rdev_ops = rdev->ops->del_pmksa;
10300                break;
10301        default:
10302                WARN_ON(1);
10303                break;
10304        }
10305
10306        if (!rdev_ops)
10307                return -EOPNOTSUPP;
10308
10309        return rdev_ops(&rdev->wiphy, dev, &pmksa);
10310}
10311
10312static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
10313{
10314        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10315        struct net_device *dev = info->user_ptr[1];
10316
10317        if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10318            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10319                return -EOPNOTSUPP;
10320
10321        if (!rdev->ops->flush_pmksa)
10322                return -EOPNOTSUPP;
10323
10324        return rdev_flush_pmksa(rdev, dev);
10325}
10326
10327static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
10328{
10329        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10330        struct net_device *dev = info->user_ptr[1];
10331        u8 action_code, dialog_token;
10332        u32 peer_capability = 0;
10333        u16 status_code;
10334        u8 *peer;
10335        bool initiator;
10336
10337        if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
10338            !rdev->ops->tdls_mgmt)
10339                return -EOPNOTSUPP;
10340
10341        if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
10342            !info->attrs[NL80211_ATTR_STATUS_CODE] ||
10343            !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
10344            !info->attrs[NL80211_ATTR_IE] ||
10345            !info->attrs[NL80211_ATTR_MAC])
10346                return -EINVAL;
10347
10348        peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
10349        action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
10350        status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
10351        dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
10352        initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
10353        if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
10354                peer_capability =
10355                        nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
10356
10357        return rdev_tdls_mgmt(rdev, dev, peer, action_code,
10358                              dialog_token, status_code, peer_capability,
10359                              initiator,
10360                              nla_data(info->attrs[NL80211_ATTR_IE]),
10361                              nla_len(info->attrs[NL80211_ATTR_IE]));
10362}
10363
10364static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
10365{
10366        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10367        struct net_device *dev = info->user_ptr[1];
10368        enum nl80211_tdls_operation operation;
10369        u8 *peer;
10370
10371        if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
10372            !rdev->ops->tdls_oper)
10373                return -EOPNOTSUPP;
10374
10375        if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
10376            !info->attrs[NL80211_ATTR_MAC])
10377                return -EINVAL;
10378
10379        operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
10380        peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
10381
10382        return rdev_tdls_oper(rdev, dev, peer, operation);
10383}
10384
10385static int nl80211_remain_on_channel(struct sk_buff *skb,
10386                                     struct genl_info *info)
10387{
10388        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10389        struct wireless_dev *wdev = info->user_ptr[1];
10390        struct cfg80211_chan_def chandef;
10391        const struct cfg80211_chan_def *compat_chandef;
10392        struct sk_buff *msg;
10393        void *hdr;
10394        u64 cookie;
10395        u32 duration;
10396        int err;
10397
10398        if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
10399            !info->attrs[NL80211_ATTR_DURATION])
10400                return -EINVAL;
10401
10402        duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
10403
10404        if (!rdev->ops->remain_on_channel ||
10405            !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
10406                return -EOPNOTSUPP;
10407
10408        /*
10409         * We should be on that channel for at least a minimum amount of
10410         * time (10ms) but no longer than the driver supports.
10411         */
10412        if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
10413            duration > rdev->wiphy.max_remain_on_channel_duration)
10414                return -EINVAL;
10415
10416        err = nl80211_parse_chandef(rdev, info, &chandef);
10417        if (err)
10418                return err;
10419
10420        wdev_lock(wdev);
10421        if (!cfg80211_off_channel_oper_allowed(wdev) &&
10422            !cfg80211_chandef_identical(&wdev->chandef, &chandef)) {
10423                compat_chandef = cfg80211_chandef_compatible(&wdev->chandef,
10424                                                             &chandef);
10425                if (compat_chandef != &chandef) {
10426                        wdev_unlock(wdev);
10427                        return -EBUSY;
10428                }
10429        }
10430        wdev_unlock(wdev);
10431
10432        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10433        if (!msg)
10434                return -ENOMEM;
10435
10436        hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10437                             NL80211_CMD_REMAIN_ON_CHANNEL);
10438        if (!hdr) {
10439                err = -ENOBUFS;
10440                goto free_msg;
10441        }
10442
10443        err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
10444                                     duration, &cookie);
10445
10446        if (err)
10447                goto free_msg;
10448
10449        if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
10450                              NL80211_ATTR_PAD))
10451                goto nla_put_failure;
10452
10453        genlmsg_end(msg, hdr);
10454
10455        return genlmsg_reply(msg, info);
10456
10457 nla_put_failure:
10458        err = -ENOBUFS;
10459 free_msg:
10460        nlmsg_free(msg);
10461        return err;
10462}
10463
10464static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
10465                                            struct genl_info *info)
10466{
10467        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10468        struct wireless_dev *wdev = info->user_ptr[1];
10469        u64 cookie;
10470
10471        if (!info->attrs[NL80211_ATTR_COOKIE])
10472                return -EINVAL;
10473
10474        if (!rdev->ops->cancel_remain_on_channel)
10475                return -EOPNOTSUPP;
10476
10477        cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
10478
10479        return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
10480}
10481
10482static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
10483                                       struct genl_info *info)
10484{
10485        struct cfg80211_bitrate_mask mask;
10486        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10487        struct net_device *dev = info->user_ptr[1];
10488        int err;
10489
10490        if (!rdev->ops->set_bitrate_mask)
10491                return -EOPNOTSUPP;
10492
10493        err = nl80211_parse_tx_bitrate_mask(info, &mask);
10494        if (err)
10495                return err;
10496
10497        return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
10498}
10499
10500static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
10501{
10502        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10503        struct wireless_dev *wdev = info->user_ptr[1];
10504        u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
10505
10506        if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
10507                return -EINVAL;
10508
10509        if (info->attrs[NL80211_ATTR_FRAME_TYPE])
10510                frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
10511
10512        switch (wdev->iftype) {
10513        case NL80211_IFTYPE_STATION:
10514        case NL80211_IFTYPE_ADHOC:
10515        case NL80211_IFTYPE_P2P_CLIENT:
10516        case NL80211_IFTYPE_AP:
10517        case NL80211_IFTYPE_AP_VLAN:
10518        case NL80211_IFTYPE_MESH_POINT:
10519        case NL80211_IFTYPE_P2P_GO:
10520        case NL80211_IFTYPE_P2P_DEVICE:
10521                break;
10522        case NL80211_IFTYPE_NAN:
10523        default:
10524                return -EOPNOTSUPP;
10525        }
10526
10527        /* not much point in registering if we can't reply */
10528        if (!rdev->ops->mgmt_tx)
10529                return -EOPNOTSUPP;
10530
10531        return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
10532                        nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
10533                        nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
10534}
10535
10536static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
10537{
10538        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10539        struct wireless_dev *wdev = info->user_ptr[1];
10540        struct cfg80211_chan_def chandef;
10541        int err;
10542        void *hdr = NULL;
10543        u64 cookie;
10544        struct sk_buff *msg = NULL;
10545        struct cfg80211_mgmt_tx_params params = {
10546                .dont_wait_for_ack =
10547                        info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
10548        };
10549
10550        if (!info->attrs[NL80211_ATTR_FRAME])
10551                return -EINVAL;
10552
10553        if (!rdev->ops->mgmt_tx)
10554                return -EOPNOTSUPP;
10555
10556        switch (wdev->iftype) {
10557        case NL80211_IFTYPE_P2P_DEVICE:
10558                if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
10559                        return -EINVAL;
10560        case NL80211_IFTYPE_STATION:
10561        case NL80211_IFTYPE_ADHOC:
10562        case NL80211_IFTYPE_P2P_CLIENT:
10563        case NL80211_IFTYPE_AP:
10564        case NL80211_IFTYPE_AP_VLAN:
10565        case NL80211_IFTYPE_MESH_POINT:
10566        case NL80211_IFTYPE_P2P_GO:
10567                break;
10568        case NL80211_IFTYPE_NAN:
10569        default:
10570                return -EOPNOTSUPP;
10571        }
10572
10573        if (info->attrs[NL80211_ATTR_DURATION]) {
10574                if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
10575                        return -EINVAL;
10576                params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
10577
10578                /*
10579                 * We should wait on the channel for at least a minimum amount
10580                 * of time (10ms) but no longer than the driver supports.
10581                 */
10582                if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
10583                    params.wait > rdev->wiphy.max_remain_on_channel_duration)
10584                        return -EINVAL;
10585        }
10586
10587        params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
10588
10589        if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
10590                return -EINVAL;
10591
10592        params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
10593
10594        /* get the channel if any has been specified, otherwise pass NULL to
10595         * the driver. The latter will use the current one
10596         */
10597        chandef.chan = NULL;
10598        if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
10599                err = nl80211_parse_chandef(rdev, info, &chandef);
10600                if (err)
10601                        return err;
10602        }
10603
10604        if (!chandef.chan && params.offchan)
10605                return -EINVAL;
10606
10607        wdev_lock(wdev);
10608        if (params.offchan && !cfg80211_off_channel_oper_allowed(wdev)) {
10609                wdev_unlock(wdev);
10610                return -EBUSY;
10611        }
10612        wdev_unlock(wdev);
10613
10614        params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
10615        params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
10616
10617        if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
10618                int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
10619                int i;
10620
10621                if (len % sizeof(u16))
10622                        return -EINVAL;
10623
10624                params.n_csa_offsets = len / sizeof(u16);
10625                params.csa_offsets =
10626                        nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
10627
10628                /* check that all the offsets fit the frame */
10629                for (i = 0; i < params.n_csa_offsets; i++) {
10630                        if (params.csa_offsets[i] >= params.len)
10631                                return -EINVAL;
10632                }
10633        }
10634
10635        if (!params.dont_wait_for_ack) {
10636                msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10637                if (!msg)
10638                        return -ENOMEM;
10639
10640                hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10641                                     NL80211_CMD_FRAME);
10642                if (!hdr) {
10643                        err = -ENOBUFS;
10644                        goto free_msg;
10645                }
10646        }
10647
10648        params.chan = chandef.chan;
10649        err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
10650        if (err)
10651                goto free_msg;
10652
10653        if (msg) {
10654                if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
10655                                      NL80211_ATTR_PAD))
10656                        goto nla_put_failure;
10657
10658                genlmsg_end(msg, hdr);
10659                return genlmsg_reply(msg, info);
10660        }
10661
10662        return 0;
10663
10664 nla_put_failure:
10665        err = -ENOBUFS;
10666 free_msg:
10667        nlmsg_free(msg);
10668        return err;
10669}
10670
10671static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
10672{
10673        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10674        struct wireless_dev *wdev = info->user_ptr[1];
10675        u64 cookie;
10676
10677        if (!info->attrs[NL80211_ATTR_COOKIE])
10678                return -EINVAL;
10679
10680        if (!rdev->ops->mgmt_tx_cancel_wait)
10681                return -EOPNOTSUPP;
10682
10683        switch (wdev->iftype) {
10684        case NL80211_IFTYPE_STATION:
10685        case NL80211_IFTYPE_ADHOC:
10686        case NL80211_IFTYPE_P2P_CLIENT:
10687        case NL80211_IFTYPE_AP:
10688        case NL80211_IFTYPE_AP_VLAN:
10689        case NL80211_IFTYPE_P2P_GO:
10690        case NL80211_IFTYPE_P2P_DEVICE:
10691                break;
10692        case NL80211_IFTYPE_NAN:
10693        default:
10694                return -EOPNOTSUPP;
10695        }
10696
10697        cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
10698
10699        return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
10700}
10701
10702static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
10703{
10704        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10705        struct wireless_dev *wdev;
10706        struct net_device *dev = info->user_ptr[1];
10707        u8 ps_state;
10708        bool state;
10709        int err;
10710
10711        if (!info->attrs[NL80211_ATTR_PS_STATE])
10712                return -EINVAL;
10713
10714        ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
10715
10716        wdev = dev->ieee80211_ptr;
10717
10718        if (!rdev->ops->set_power_mgmt)
10719                return -EOPNOTSUPP;
10720
10721        state = (ps_state == NL80211_PS_ENABLED) ? true : false;
10722
10723        if (state == wdev->ps)
10724                return 0;
10725
10726        err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
10727        if (!err)
10728                wdev->ps = state;
10729        return err;
10730}
10731
10732static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
10733{
10734        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10735        enum nl80211_ps_state ps_state;
10736        struct wireless_dev *wdev;
10737        struct net_device *dev = info->user_ptr[1];
10738        struct sk_buff *msg;
10739        void *hdr;
10740        int err;
10741
10742        wdev = dev->ieee80211_ptr;
10743
10744        if (!rdev->ops->set_power_mgmt)
10745                return -EOPNOTSUPP;
10746
10747        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10748        if (!msg)
10749                return -ENOMEM;
10750
10751        hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10752                             NL80211_CMD_GET_POWER_SAVE);
10753        if (!hdr) {
10754                err = -ENOBUFS;
10755                goto free_msg;
10756        }
10757
10758        if (wdev->ps)
10759                ps_state = NL80211_PS_ENABLED;
10760        else
10761                ps_state = NL80211_PS_DISABLED;
10762
10763        if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
10764                goto nla_put_failure;
10765
10766        genlmsg_end(msg, hdr);
10767        return genlmsg_reply(msg, info);
10768
10769 nla_put_failure:
10770        err = -ENOBUFS;
10771 free_msg:
10772        nlmsg_free(msg);
10773        return err;
10774}
10775
10776static const struct nla_policy
10777nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
10778        [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
10779        [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
10780        [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
10781        [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
10782        [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
10783        [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
10784        [NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
10785};
10786
10787static int nl80211_set_cqm_txe(struct genl_info *info,
10788                               u32 rate, u32 pkts, u32 intvl)
10789{
10790        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10791        struct net_device *dev = info->user_ptr[1];
10792        struct wireless_dev *wdev = dev->ieee80211_ptr;
10793
10794        if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
10795                return -EINVAL;
10796
10797        if (!rdev->ops->set_cqm_txe_config)
10798                return -EOPNOTSUPP;
10799
10800        if (wdev->iftype != NL80211_IFTYPE_STATION &&
10801            wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
10802                return -EOPNOTSUPP;
10803
10804        return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
10805}
10806
10807static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
10808                                    struct net_device *dev)
10809{
10810        struct wireless_dev *wdev = dev->ieee80211_ptr;
10811        s32 last, low, high;
10812        u32 hyst;
10813        int i, n, low_index;
10814        int err;
10815
10816        /* RSSI reporting disabled? */
10817        if (!wdev->cqm_config)
10818                return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
10819
10820        /*
10821         * Obtain current RSSI value if possible, if not and no RSSI threshold
10822         * event has been received yet, we should receive an event after a
10823         * connection is established and enough beacons received to calculate
10824         * the average.
10825         */
10826        if (!wdev->cqm_config->last_rssi_event_value && wdev->current_bss &&
10827            rdev->ops->get_station) {
10828                struct station_info sinfo = {};
10829                u8 *mac_addr;
10830
10831                mac_addr = wdev->current_bss->pub.bssid;
10832
10833                err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
10834                if (err)
10835                        return err;
10836
10837                if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
10838                        wdev->cqm_config->last_rssi_event_value =
10839                                (s8) sinfo.rx_beacon_signal_avg;
10840        }
10841
10842        last = wdev->cqm_config->last_rssi_event_value;
10843        hyst = wdev->cqm_config->rssi_hyst;
10844        n = wdev->cqm_config->n_rssi_thresholds;
10845
10846        for (i = 0; i < n; i++) {
10847                i = array_index_nospec(i, n);
10848                if (last < wdev->cqm_config->rssi_thresholds[i])
10849                        break;
10850        }
10851
10852        low_index = i - 1;
10853        if (low_index >= 0) {
10854                low_index = array_index_nospec(low_index, n);
10855                low = wdev->cqm_config->rssi_thresholds[low_index] - hyst;
10856        } else {
10857                low = S32_MIN;
10858        }
10859        if (i < n) {
10860                i = array_index_nospec(i, n);
10861                high = wdev->cqm_config->rssi_thresholds[i] + hyst - 1;
10862        } else {
10863                high = S32_MAX;
10864        }
10865
10866        return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
10867}
10868
10869static int nl80211_set_cqm_rssi(struct genl_info *info,
10870                                const s32 *thresholds, int n_thresholds,
10871                                u32 hysteresis)
10872{
10873        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10874        struct net_device *dev = info->user_ptr[1];
10875        struct wireless_dev *wdev = dev->ieee80211_ptr;
10876        int i, err;
10877        s32 prev = S32_MIN;
10878
10879        /* Check all values negative and sorted */
10880        for (i = 0; i < n_thresholds; i++) {
10881                if (thresholds[i] > 0 || thresholds[i] <= prev)
10882                        return -EINVAL;
10883
10884                prev = thresholds[i];
10885        }
10886
10887        if (wdev->iftype != NL80211_IFTYPE_STATION &&
10888            wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
10889                return -EOPNOTSUPP;
10890
10891        wdev_lock(wdev);
10892        cfg80211_cqm_config_free(wdev);
10893        wdev_unlock(wdev);
10894
10895        if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
10896                if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
10897                        return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
10898
10899                return rdev_set_cqm_rssi_config(rdev, dev,
10900                                                thresholds[0], hysteresis);
10901        }
10902
10903        if (!wiphy_ext_feature_isset(&rdev->wiphy,
10904                                     NL80211_EXT_FEATURE_CQM_RSSI_LIST))
10905                return -EOPNOTSUPP;
10906
10907        if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
10908                n_thresholds = 0;
10909
10910        wdev_lock(wdev);
10911        if (n_thresholds) {
10912                struct cfg80211_cqm_config *cqm_config;
10913
10914                cqm_config = kzalloc(sizeof(struct cfg80211_cqm_config) +
10915                                     n_thresholds * sizeof(s32), GFP_KERNEL);
10916                if (!cqm_config) {
10917                        err = -ENOMEM;
10918                        goto unlock;
10919                }
10920
10921                cqm_config->rssi_hyst = hysteresis;
10922                cqm_config->n_rssi_thresholds = n_thresholds;
10923                memcpy(cqm_config->rssi_thresholds, thresholds,
10924                       n_thresholds * sizeof(s32));
10925
10926                wdev->cqm_config = cqm_config;
10927        }
10928
10929        err = cfg80211_cqm_rssi_update(rdev, dev);
10930
10931unlock:
10932        wdev_unlock(wdev);
10933
10934        return err;
10935}
10936
10937static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
10938{
10939        struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
10940        struct nlattr *cqm;
10941        int err;
10942
10943        cqm = info->attrs[NL80211_ATTR_CQM];
10944        if (!cqm)
10945                return -EINVAL;
10946
10947        err = nla_parse_nested_deprecated(attrs, NL80211_ATTR_CQM_MAX, cqm,
10948                                          nl80211_attr_cqm_policy,
10949                                          info->extack);
10950        if (err)
10951                return err;
10952
10953        if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
10954            attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
10955                const s32 *thresholds =
10956                        nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
10957                int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
10958                u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
10959
10960                if (len % 4)
10961                        return -EINVAL;
10962
10963                return nl80211_set_cqm_rssi(info, thresholds, len / 4,
10964                                            hysteresis);
10965        }
10966
10967        if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
10968            attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
10969            attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
10970                u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
10971                u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
10972                u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
10973
10974                return nl80211_set_cqm_txe(info, rate, pkts, intvl);
10975        }
10976
10977        return -EINVAL;
10978}
10979
10980static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
10981{
10982        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10983        struct net_device *dev = info->user_ptr[1];
10984        struct ocb_setup setup = {};
10985        int err;
10986
10987        err = nl80211_parse_chandef(rdev, info, &setup.chandef);
10988        if (err)
10989                return err;
10990
10991        return cfg80211_join_ocb(rdev, dev, &setup);
10992}
10993
10994static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
10995{
10996        struct cfg80211_registered_device *rdev = info->user_ptr[0];
10997        struct net_device *dev = info->user_ptr[1];
10998
10999        return cfg80211_leave_ocb(rdev, dev);
11000}
11001
11002static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
11003{
11004        struct cfg80211_registered_device *rdev = info->user_ptr[0];
11005        struct net_device *dev = info->user_ptr[1];
11006        struct mesh_config cfg;
11007        struct mesh_setup setup;
11008        int err;
11009
11010        /* start with default */
11011        memcpy(&cfg, &default_mesh_config, sizeof(cfg));
11012        memcpy(&setup, &default_mesh_setup, sizeof(setup));
11013
11014        if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
11015                /* and parse parameters if given */
11016                err = nl80211_parse_mesh_config(info, &cfg, NULL);
11017                if (err)
11018                        return err;
11019        }
11020
11021        if (!info->attrs[NL80211_ATTR_MESH_ID] ||
11022            !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
11023                return -EINVAL;
11024
11025        setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
11026        setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
11027
11028        if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
11029            !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
11030                            nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
11031                        return -EINVAL;
11032
11033        if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
11034                setup.beacon_interval =
11035                        nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
11036
11037                err = cfg80211_validate_beacon_int(rdev,
11038                                                   NL80211_IFTYPE_MESH_POINT,
11039                                                   setup.beacon_interval);
11040                if (err)
11041                        return err;
11042        }
11043
11044        if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
11045                setup.dtim_period =
11046                        nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
11047                if (setup.dtim_period < 1 || setup.dtim_period > 100)
11048                        return -EINVAL;
11049        }
11050
11051        if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
11052                /* parse additional setup parameters if given */
11053                err = nl80211_parse_mesh_setup(info, &setup);
11054                if (err)
11055                        return err;
11056        }
11057
11058        if (setup.user_mpm)
11059                cfg.auto_open_plinks = false;
11060
11061        if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
11062                err = nl80211_parse_chandef(rdev, info, &setup.chandef);
11063                if (err)
11064                        return err;
11065        } else {
11066                /* __cfg80211_join_mesh() will sort it out */
11067                setup.chandef.chan = NULL;
11068        }
11069
11070        if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
11071                u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11072                int n_rates =
11073                        nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11074                struct ieee80211_supported_band *sband;
11075
11076                if (!setup.chandef.chan)
11077                        return -EINVAL;
11078
11079                sband = rdev->wiphy.bands[setup.chandef.chan->band];
11080
11081                err = ieee80211_get_ratemask(sband, rates, n_rates,
11082                                             &setup.basic_rates);
11083                if (err)
11084                        return err;
11085        }
11086
11087        if (info->attrs[NL80211_ATTR_TX_RATES]) {
11088                err = nl80211_parse_tx_bitrate_mask(info, &setup.beacon_rate);
11089                if (err)
11090                        return err;
11091
11092                if (!setup.chandef.chan)
11093                        return -EINVAL;
11094
11095                err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
11096                                              &setup.beacon_rate);
11097                if (err)
11098                        return err;
11099        }
11100
11101        setup.userspace_handles_dfs =
11102                nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
11103
11104        if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
11105                int r = validate_pae_over_nl80211(rdev, info);
11106
11107                if (r < 0)
11108                        return r;
11109
11110                setup.control_port_over_nl80211 = true;
11111        }
11112
11113        wdev_lock(dev->ieee80211_ptr);
11114        err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
11115        if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
11116                dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11117        wdev_unlock(dev->ieee80211_ptr);
11118
11119        return err;
11120}
11121
11122static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
11123{
11124        struct cfg80211_registered_device *rdev = info->user_ptr[0];
11125        struct net_device *dev = info->user_ptr[1];
11126
11127        return cfg80211_leave_mesh(rdev, dev);
11128}
11129
11130#ifdef CONFIG_PM
11131static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
11132                                        struct cfg80211_registered_device *rdev)
11133{
11134        struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
11135        struct nlattr *nl_pats, *nl_pat;
11136        int i, pat_len;
11137
11138        if (!wowlan->n_patterns)
11139                return 0;
11140
11141        nl_pats = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
11142        if (!nl_pats)
11143                return -ENOBUFS;
11144
11145        for (i = 0; i < wowlan->n_patterns; i++) {
11146                nl_pat = nla_nest_start_noflag(msg, i + 1);
11147                if (!nl_pat)
11148                        return -ENOBUFS;
11149                pat_len = wowlan->patterns[i].pattern_len;
11150                if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
11151                            wowlan->patterns[i].mask) ||
11152                    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
11153                            wowlan->patterns[i].pattern) ||
11154                    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
11155                                wowlan->patterns[i].pkt_offset))
11156                        return -ENOBUFS;
11157                nla_nest_end(msg, nl_pat);
11158        }
11159        nla_nest_end(msg, nl_pats);
11160
11161        return 0;
11162}
11163
11164static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
11165                                   struct cfg80211_wowlan_tcp *tcp)
11166{
11167        struct nlattr *nl_tcp;
11168
11169        if (!tcp)
11170                return 0;
11171
11172        nl_tcp = nla_nest_start_noflag(msg,
11173                                       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
11174        if (!nl_tcp)
11175                return -ENOBUFS;
11176
11177        if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
11178            nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
11179            nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
11180            nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
11181            nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
11182            nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
11183                    tcp->payload_len, tcp->payload) ||
11184            nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
11185                        tcp->data_interval) ||
11186            nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
11187                    tcp->wake_len, tcp->wake_data) ||
11188            nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
11189                    DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
11190                return -ENOBUFS;
11191
11192        if (tcp->payload_seq.len &&
11193            nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
11194                    sizeof(tcp->payload_seq), &tcp->payload_seq))
11195                return -ENOBUFS;
11196
11197        if (tcp->payload_tok.len &&
11198            nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
11199                    sizeof(tcp->payload_tok) + tcp->tokens_size,
11200                    &tcp->payload_tok))
11201                return -ENOBUFS;
11202
11203        nla_nest_end(msg, nl_tcp);
11204
11205        return 0;
11206}
11207
11208static int nl80211_send_wowlan_nd(struct sk_buff *msg,
11209                                  struct cfg80211_sched_scan_request *req)
11210{
11211        struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
11212        int i;
11213
11214        if (!req)
11215                return 0;
11216
11217        nd = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
11218        if (!nd)
11219                return -ENOBUFS;
11220
11221        if (req->n_scan_plans == 1 &&
11222            nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
11223                        req->scan_plans[0].interval * 1000))
11224                return -ENOBUFS;
11225
11226        if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
11227                return -ENOBUFS;
11228
11229        if (req->relative_rssi_set) {
11230                struct nl80211_bss_select_rssi_adjust rssi_adjust;
11231
11232                if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
11233                               req->relative_rssi))
11234                        return -ENOBUFS;
11235
11236                rssi_adjust.band = req->rssi_adjust.band;
11237                rssi_adjust.delta = req->rssi_adjust.delta;
11238                if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
11239                            sizeof(rssi_adjust), &rssi_adjust))
11240                        return -ENOBUFS;
11241        }
11242
11243        freqs = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
11244        if (!freqs)
11245                return -ENOBUFS;
11246
11247        for (i = 0; i < req->n_channels; i++) {
11248                if (nla_put_u32(msg, i, req->channels[i]->center_freq))
11249                        return -ENOBUFS;
11250        }
11251
11252        nla_nest_end(msg, freqs);
11253
11254        if (req->n_match_sets) {
11255                matches = nla_nest_start_noflag(msg,
11256                                                NL80211_ATTR_SCHED_SCAN_MATCH);
11257                if (!matches)
11258                        return -ENOBUFS;
11259
11260                for (i = 0; i < req->n_match_sets; i++) {
11261                        match = nla_nest_start_noflag(msg, i);
11262                        if (!match)
11263                                return -ENOBUFS;
11264
11265                        if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
11266                                    req->match_sets[i].ssid.ssid_len,
11267                                    req->match_sets[i].ssid.ssid))
11268                                return -ENOBUFS;
11269                        nla_nest_end(msg, match);
11270                }
11271                nla_nest_end(msg, matches);
11272        }
11273
11274        scan_plans = nla_nest_start_noflag(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
11275        if (!scan_plans)
11276                return -ENOBUFS;
11277
11278        for (i = 0; i < req->n_scan_plans; i++) {
11279                scan_plan = nla_nest_start_noflag(msg, i + 1);
11280                if (!scan_plan)
11281                        return -ENOBUFS;
11282
11283                if (nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
11284                                req->scan_plans[i].interval) ||
11285                    (req->scan_plans[i].iterations &&
11286                     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
11287                                 req->scan_plans[i].iterations)))
11288                        return -ENOBUFS;
11289                nla_nest_end(msg, scan_plan);
11290        }
11291        nla_nest_end(msg, scan_plans);
11292
11293        nla_nest_end(msg, nd);
11294
11295        return 0;
11296}
11297
11298static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
11299{
11300        struct cfg80211_registered_device *rdev = info->user_ptr[0];
11301        struct sk_buff *msg;
11302        void *hdr;
11303        u32 size = NLMSG_DEFAULT_SIZE;
11304
11305        if (!rdev->wiphy.wowlan)
11306                return -EOPNOTSUPP;
11307
11308        if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
11309                /* adjust size to have room for all the data */
11310                size += rdev->wiphy.wowlan_config->tcp->tokens_size +
11311                        rdev->wiphy.wowlan_config->tcp->payload_len +
11312                        rdev->wiphy.wowlan_config->tcp->wake_len +
11313                        rdev->wiphy.wowlan_config->tcp->wake_len / 8;
11314        }
11315
11316        msg = nlmsg_new(size, GFP_KERNEL);
11317        if (!msg)
11318                return -ENOMEM;
11319
11320        hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11321                             NL80211_CMD_GET_WOWLAN);
11322        if (!hdr)
11323                goto nla_put_failure;
11324
11325        if (rdev->wiphy.wowlan_config) {
11326                struct nlattr *nl_wowlan;
11327
11328                nl_wowlan = nla_nest_start_noflag(msg,
11329                                                  NL80211_ATTR_WOWLAN_TRIGGERS);
11330                if (!nl_wowlan)
11331                        goto nla_put_failure;
11332
11333                if ((rdev->wiphy.wowlan_config->any &&
11334                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
11335                    (rdev->wiphy.wowlan_config->disconnect &&
11336                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
11337                    (rdev->wiphy.wowlan_config->magic_pkt &&
11338                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
11339                    (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
11340                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
11341                    (rdev->wiphy.wowlan_config->eap_identity_req &&
11342                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
11343                    (rdev->wiphy.wowlan_config->four_way_handshake &&
11344                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
11345                    (rdev->wiphy.wowlan_config->rfkill_release &&
11346                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
11347                        goto nla_put_failure;
11348
11349                if (nl80211_send_wowlan_patterns(msg, rdev))
11350                        goto nla_put_failure;
11351
11352                if (nl80211_send_wowlan_tcp(msg,
11353                                            rdev->wiphy.wowlan_config->tcp))
11354                        goto nla_put_failure;
11355
11356                if (nl80211_send_wowlan_nd(
11357                            msg,
11358                            rdev->wiphy.wowlan_config->nd_config))
11359                        goto nla_put_failure;
11360
11361                nla_nest_end(msg, nl_wowlan);
11362        }
11363
11364        genlmsg_end(msg, hdr);
11365        return genlmsg_reply(msg, info);
11366
11367nla_put_failure:
11368        nlmsg_free(msg);
11369        return -ENOBUFS;
11370}
11371
11372static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
11373                                    struct nlattr *attr,
11374                                    struct cfg80211_wowlan *trig)
11375{
11376        struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
11377        struct cfg80211_wowlan_tcp *cfg;
11378        struct nl80211_wowlan_tcp_data_token *tok = NULL;
11379        struct nl80211_wowlan_tcp_data_seq *seq = NULL;
11380        u32 size;
11381        u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
11382        int err, port;
11383
11384        if (!rdev->wiphy.wowlan->tcp)
11385                return -EINVAL;
11386
11387        err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TCP, attr,
11388                                          nl80211_wowlan_tcp_policy, NULL);
11389        if (err)
11390                return err;
11391
11392        if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
11393            !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
11394            !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
11395            !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
11396            !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
11397            !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
11398            !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
11399            !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
11400                return -EINVAL;
11401
11402        data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
11403        if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
11404                return -EINVAL;
11405
11406        if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
11407                        rdev->wiphy.wowlan->tcp->data_interval_max ||
11408            nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
11409                return -EINVAL;
11410
11411        wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
11412        if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
11413                return -EINVAL;
11414
11415        wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
11416        if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
11417                return -EINVAL;
11418
11419        if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
11420                u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
11421
11422                tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
11423                tokens_size = tokln - sizeof(*tok);
11424
11425                if (!tok->len || tokens_size % tok->len)
11426                        return -EINVAL;
11427                if (!rdev->wiphy.wowlan->tcp->tok)
11428                        return -EINVAL;
11429                if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
11430                        return -EINVAL;
11431                if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
11432                        return -EINVAL;
11433                if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
11434                        return -EINVAL;
11435                if (tok->offset + tok->len > data_size)
11436                        return -EINVAL;
11437        }
11438
11439        if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
11440                seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
11441                if (!rdev->wiphy.wowlan->tcp->seq)
11442                        return -EINVAL;
11443                if (seq->len == 0 || seq->len > 4)
11444                        return -EINVAL;
11445                if (seq->len + seq->offset > data_size)
11446                        return -EINVAL;
11447        }
11448
11449        size = sizeof(*cfg);
11450        size += data_size;
11451        size += wake_size + wake_mask_size;
11452        size += tokens_size;
11453
11454        cfg = kzalloc(size, GFP_KERNEL);
11455        if (!cfg)
11456                return -ENOMEM;
11457        cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
11458        cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
11459        memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
11460               ETH_ALEN);
11461        if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
11462                port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
11463        else
11464                port = 0;
11465#ifdef CONFIG_INET
11466        /* allocate a socket and port for it and use it */
11467        err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
11468                            IPPROTO_TCP, &cfg->sock, 1);
11469        if (err) {
11470                kfree(cfg);
11471                return err;
11472        }
11473        if (inet_csk_get_port(cfg->sock->sk, port)) {
11474                sock_release(cfg->sock);
11475                kfree(cfg);
11476                return -EADDRINUSE;
11477        }
11478        cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
11479#else
11480        if (!port) {
11481                kfree(cfg);
11482                return -EINVAL;
11483        }
11484        cfg->src_port = port;
11485#endif
11486
11487        cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
11488        cfg->payload_len = data_size;
11489        cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
11490        memcpy((void *)cfg->payload,
11491               nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
11492               data_size);
11493        if (seq)
11494                cfg->payload_seq = *seq;
11495        cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
11496        cfg->wake_len = wake_size;
11497        cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
11498        memcpy((void *)cfg->wake_data,
11499               nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
11500               wake_size);
11501        cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
11502                         data_size + wake_size;
11503        memcpy((void *)cfg->wake_mask,
11504               nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
11505               wake_mask_size);
11506        if (tok) {
11507                cfg->tokens_size = tokens_size;
11508                memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
11509        }
11510
11511        trig->tcp = cfg;
11512
11513        return 0;
11514}
11515
11516static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
11517                                   const struct wiphy_wowlan_support *wowlan,
11518                                   struct nlattr *attr,
11519                                   struct cfg80211_wowlan *trig)
11520{
11521        struct nlattr **tb;
11522        int err;
11523
11524        tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
11525        if (!tb)
11526                return -ENOMEM;
11527
11528        if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
11529                err = -EOPNOTSUPP;
11530                goto out;
11531        }
11532
11533        err = nla_parse_nested_deprecated(tb, NL80211_ATTR_MAX, attr,
11534                                          nl80211_policy, NULL);
11535        if (err)
11536                goto out;
11537
11538        trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
11539                                                   wowlan->max_nd_match_sets);
11540        err = PTR_ERR_OR_ZERO(trig->nd_config);
11541        if (err)
11542                trig->nd_config = NULL;
11543
11544out:
11545        kfree(tb);
11546        return err;
11547}
11548
11549static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
11550{
11551        struct cfg80211_registered_device *rdev = info->user_ptr[0];
11552        struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
11553        struct cfg80211_wowlan new_triggers = {};
11554        struct cfg80211_wowlan *ntrig;
11555        const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
11556        int err, i;
11557        bool prev_enabled = rdev->wiphy.wowlan_config;
11558        bool regular = false;
11559
11560        if (!wowlan)
11561                return -EOPNOTSUPP;
11562
11563        if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
11564                cfg80211_rdev_free_wowlan(rdev);
11565                rdev->wiphy.wowlan_config = NULL;
11566                goto set_wakeup;
11567        }
11568
11569        err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TRIG,
11570                                          info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
11571                                          nl80211_wowlan_policy, info->extack);
11572        if (err)
11573                return err;
11574
11575        if (tb[NL80211_WOWLAN_TRIG_ANY]) {
11576                if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
11577                        return -EINVAL;
11578                new_triggers.any = true;
11579        }
11580
11581        if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
11582                if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
11583                        return -EINVAL;
11584                new_triggers.disconnect = true;
11585                regular = true;
11586        }
11587
11588        if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
11589                if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
11590                        return -EINVAL;
11591                new_triggers.magic_pkt = true;
11592                regular = true;
11593        }
11594
11595        if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
11596                return -EINVAL;
11597
11598        if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
11599                if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
11600                        return -EINVAL;
11601                new_triggers.gtk_rekey_failure = true;
11602                regular = true;
11603        }
11604
11605        if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
11606                if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
11607                        return -EINVAL;
11608                new_triggers.eap_identity_req = true;
11609                regular = true;
11610        }
11611
11612        if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
11613                if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
11614                        return -EINVAL;
11615                new_triggers.four_way_handshake = true;
11616                regular = true;
11617        }
11618
11619        if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
11620                if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
11621                        return -EINVAL;
11622                new_triggers.rfkill_release = true;
11623                regular = true;
11624        }
11625
11626        if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
11627                struct nlattr *pat;
11628                int n_patterns = 0;
11629                int rem, pat_len, mask_len, pkt_offset;
11630                struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
11631
11632                regular = true;
11633
11634                nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
11635                                    rem)
11636                        n_patterns++;
11637                if (n_patterns > wowlan->n_patterns)
11638                        return -EINVAL;
11639
11640                new_triggers.patterns = kcalloc(n_patterns,
11641                                                sizeof(new_triggers.patterns[0]),
11642                                                GFP_KERNEL);
11643                if (!new_triggers.patterns)
11644                        return -ENOMEM;
11645
11646                new_triggers.n_patterns = n_patterns;
11647                i = 0;
11648
11649                nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
11650                                    rem) {
11651                        u8 *mask_pat;
11652
11653                        err = nla_parse_nested_deprecated(pat_tb,
11654                                                          MAX_NL80211_PKTPAT,
11655                                                          pat,
11656                                                          nl80211_packet_pattern_policy,
11657                                                          info->extack);
11658                        if (err)
11659                                goto error;
11660
11661                        err = -EINVAL;
11662                        if (!pat_tb[NL80211_PKTPAT_MASK] ||
11663                            !pat_tb[NL80211_PKTPAT_PATTERN])
11664                                goto error;
11665                        pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
11666                        mask_len = DIV_ROUND_UP(pat_len, 8);
11667                        if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
11668                                goto error;
11669                        if (pat_len > wowlan->pattern_max_len ||
11670                            pat_len < wowlan->pattern_min_len)
11671                                goto error;
11672
11673                        if (!pat_tb[NL80211_PKTPAT_OFFSET])
11674                                pkt_offset = 0;
11675                        else
11676                                pkt_offset = nla_get_u32(
11677                                        pat_tb[NL80211_PKTPAT_OFFSET]);
11678                        if (pkt_offset > wowlan->max_pkt_offset)
11679                                goto error;
11680                        new_triggers.patterns[i].pkt_offset = pkt_offset;
11681
11682                        mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
11683                        if (!mask_pat) {
11684                                err = -ENOMEM;
11685                                goto error;
11686                        }
11687                        new_triggers.patterns[i].mask = mask_pat;
11688                        memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
11689                               mask_len);
11690                        mask_pat += mask_len;
11691                        new_triggers.patterns[i].pattern = mask_pat;
11692                        new_triggers.patterns[i].pattern_len = pat_len;
11693                        memcpy(mask_pat,
11694                               nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
11695                               pat_len);
11696                        i++;
11697                }
11698        }
11699
11700        if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
11701                regular = true;
11702                err = nl80211_parse_wowlan_tcp(
11703                        rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
11704                        &new_triggers);
11705                if (err)
11706                        goto error;
11707        }
11708
11709        if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
11710                regular = true;
11711                err = nl80211_parse_wowlan_nd(
11712                        rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
11713                        &new_triggers);
11714                if (err)
11715                        goto error;
11716        }
11717
11718        /* The 'any' trigger means the device continues operating more or less
11719         * as in its normal operation mode and wakes up the host on most of the
11720         * normal interrupts (like packet RX, ...)
11721         * It therefore makes little sense to combine with the more constrained
11722         * wakeup trigger modes.
11723         */
11724        if (new_triggers.any && regular) {
11725                err = -EINVAL;
11726                goto error;
11727        }
11728
11729        ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
11730        if (!ntrig) {
11731                err = -ENOMEM;
11732                goto error;
11733        }
11734        cfg80211_rdev_free_wowlan(rdev);
11735        rdev->wiphy.wowlan_config = ntrig;
11736
11737 set_wakeup:
11738        if (rdev->ops->set_wakeup &&
11739            prev_enabled != !!rdev->wiphy.wowlan_config)
11740                rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
11741
11742        return 0;
11743 error:
11744        for (i = 0; i < new_triggers.n_patterns; i++)
11745                kfree(new_triggers.patterns[i].mask);
11746        kfree(new_triggers.patterns);
11747        if (new_triggers.tcp && new_triggers.tcp->sock)
11748                sock_release(new_triggers.tcp->sock);
11749        kfree(new_triggers.tcp);
11750        kfree(new_triggers.nd_config);
11751        return err;
11752}
11753#endif
11754
11755static int nl80211_send_coalesce_rules(struct sk_buff *msg,
11756                                       struct cfg80211_registered_device *rdev)
11757{
11758        struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
11759        int i, j, pat_len;
11760        struct cfg80211_coalesce_rules *rule;
11761
11762        if (!rdev->coalesce->n_rules)
11763                return 0;
11764
11765        nl_rules = nla_nest_start_noflag(msg, NL80211_ATTR_COALESCE_RULE);
11766        if (!nl_rules)
11767                return -ENOBUFS;
11768
11769        for (i = 0; i < rdev->coalesce->n_rules; i++) {
11770                nl_rule = nla_nest_start_noflag(msg, i + 1);
11771                if (!nl_rule)
11772                        return -ENOBUFS;
11773
11774                rule = &rdev->coalesce->rules[i];
11775                if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
11776                                rule->delay))
11777                        return -ENOBUFS;
11778
11779                if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
11780                                rule->condition))
11781                        return -ENOBUFS;
11782
11783                nl_pats = nla_nest_start_noflag(msg,
11784                                                NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
11785                if (!nl_pats)
11786                        return -ENOBUFS;
11787
11788                for (j = 0; j < rule->n_patterns; j++) {
11789                        nl_pat = nla_nest_start_noflag(msg, j + 1);
11790                        if (!nl_pat)
11791                                return -ENOBUFS;
11792                        pat_len = rule->patterns[j].pattern_len;
11793                        if (nla_put(msg, NL80211_PKTPAT_MASK,
11794                                    DIV_ROUND_UP(pat_len, 8),
11795                                    rule->patterns[j].mask) ||
11796                            nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
11797                                    rule->patterns[j].pattern) ||
11798                            nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
11799                                        rule->patterns[j].pkt_offset))
11800                                return -ENOBUFS;
11801                        nla_nest_end(msg, nl_pat);
11802                }
11803                nla_nest_end(msg, nl_pats);
11804                nla_nest_end(msg, nl_rule);
11805        }
11806        nla_nest_end(msg, nl_rules);
11807
11808        return 0;
11809}
11810
11811static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
11812{
11813        struct cfg80211_registered_device *rdev = info->user_ptr[0];
11814        struct sk_buff *msg;
11815        void *hdr;
11816
11817        if (!rdev->wiphy.coalesce)
11818                return -EOPNOTSUPP;
11819
11820        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11821        if (!msg)
11822                return -ENOMEM;
11823
11824        hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11825                             NL80211_CMD_GET_COALESCE);
11826        if (!hdr)
11827                goto nla_put_failure;
11828
11829        if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
11830                goto nla_put_failure;
11831
11832        genlmsg_end(msg, hdr);
11833        return genlmsg_reply(msg, info);
11834
11835nla_put_failure:
11836        nlmsg_free(msg);
11837        return -ENOBUFS;
11838}
11839
11840void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
11841{
11842        struct cfg80211_coalesce *coalesce = rdev->coalesce;
11843        int i, j;
11844        struct cfg80211_coalesce_rules *rule;
11845
11846        if (!coalesce)
11847                return;
11848
11849        for (i = 0; i < coalesce->n_rules; i++) {
11850                rule = &coalesce->rules[i];
11851                for (j = 0; j < rule->n_patterns; j++)
11852                        kfree(rule->patterns[j].mask);
11853                kfree(rule->patterns);
11854        }
11855        kfree(coalesce->rules);
11856        kfree(coalesce);
11857        rdev->coalesce = NULL;
11858}
11859
11860static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
11861                                       struct nlattr *rule,
11862                                       struct cfg80211_coalesce_rules *new_rule)
11863{
11864        int err, i;
11865        const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
11866        struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
11867        int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
11868        struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
11869
11870        err = nla_parse_nested_deprecated(tb, NL80211_ATTR_COALESCE_RULE_MAX,
11871                                          rule, nl80211_coalesce_policy, NULL);
11872        if (err)
11873                return err;
11874
11875        if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
11876                new_rule->delay =
11877                        nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
11878        if (new_rule->delay > coalesce->max_delay)
11879                return -EINVAL;
11880
11881        if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
11882                new_rule->condition =
11883                        nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
11884
11885        if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
11886                return -EINVAL;
11887
11888        nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
11889                            rem)
11890                n_patterns++;
11891        if (n_patterns > coalesce->n_patterns)
11892                return -EINVAL;
11893
11894        new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
11895                                     GFP_KERNEL);
11896        if (!new_rule->patterns)
11897                return -ENOMEM;
11898
11899        new_rule->n_patterns = n_patterns;
11900        i = 0;
11901
11902        nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
11903                            rem) {
11904                u8 *mask_pat;
11905
11906                err = nla_parse_nested_deprecated(pat_tb, MAX_NL80211_PKTPAT,
11907                                                  pat,
11908                                                  nl80211_packet_pattern_policy,
11909                                                  NULL);
11910                if (err)
11911                        return err;
11912
11913                if (!pat_tb[NL80211_PKTPAT_MASK] ||
11914                    !pat_tb[NL80211_PKTPAT_PATTERN])
11915                        return -EINVAL;
11916                pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
11917                mask_len = DIV_ROUND_UP(pat_len, 8);
11918                if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
11919                        return -EINVAL;
11920                if (pat_len > coalesce->pattern_max_len ||
11921                    pat_len < coalesce->pattern_min_len)
11922                        return -EINVAL;
11923
11924                if (!pat_tb[NL80211_PKTPAT_OFFSET])
11925                        pkt_offset = 0;
11926                else
11927                        pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
11928                if (pkt_offset > coalesce->max_pkt_offset)
11929                        return -EINVAL;
11930                new_rule->patterns[i].pkt_offset = pkt_offset;
11931
11932                mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
11933                if (!mask_pat)
11934                        return -ENOMEM;
11935
11936                new_rule->patterns[i].mask = mask_pat;
11937                memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
11938                       mask_len);
11939
11940                mask_pat += mask_len;
11941                new_rule->patterns[i].pattern = mask_pat;
11942                new_rule->patterns[i].pattern_len = pat_len;
11943                memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
11944                       pat_len);
11945                i++;
11946        }
11947
11948        return 0;
11949}
11950
11951static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
11952{
11953        struct cfg80211_registered_device *rdev = info->user_ptr[0];
11954        const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
11955        struct cfg80211_coalesce new_coalesce = {};
11956        struct cfg80211_coalesce *n_coalesce;
11957        int err, rem_rule, n_rules = 0, i, j;
11958        struct nlattr *rule;
11959        struct cfg80211_coalesce_rules *tmp_rule;
11960
11961        if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
11962                return -EOPNOTSUPP;
11963
11964        if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
11965                cfg80211_rdev_free_coalesce(rdev);
11966                rdev_set_coalesce(rdev, NULL);
11967                return 0;
11968        }
11969
11970        nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
11971                            rem_rule)
11972                n_rules++;
11973        if (n_rules > coalesce->n_rules)
11974                return -EINVAL;
11975
11976        new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
11977                                     GFP_KERNEL);
11978        if (!new_coalesce.rules)
11979                return -ENOMEM;
11980
11981        new_coalesce.n_rules = n_rules;
11982        i = 0;
11983
11984        nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
11985                            rem_rule) {
11986                err = nl80211_parse_coalesce_rule(rdev, rule,
11987                                                  &new_coalesce.rules[i]);
11988                if (err)
11989                        goto error;
11990
11991                i++;
11992        }
11993
11994        err = rdev_set_coalesce(rdev, &new_coalesce);
11995        if (err)
11996                goto error;
11997
11998        n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
11999        if (!n_coalesce) {
12000                err = -ENOMEM;
12001                goto error;
12002        }
12003        cfg80211_rdev_free_coalesce(rdev);
12004        rdev->coalesce = n_coalesce;
12005
12006        return 0;
12007error:
12008        for (i = 0; i < new_coalesce.n_rules; i++) {
12009                tmp_rule = &new_coalesce.rules[i];
12010                for (j = 0; j < tmp_rule->n_patterns; j++)
12011                        kfree(tmp_rule->patterns[j].mask);
12012                kfree(tmp_rule->patterns);
12013        }
12014        kfree(new_coalesce.rules);
12015
12016        return err;
12017}
12018
12019static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
12020{
12021        struct cfg80211_registered_device *rdev = info->user_ptr[0];
12022        struct net_device *dev = info->user_ptr[1];
12023        struct wireless_dev *wdev = dev->ieee80211_ptr;
12024        struct nlattr *tb[NUM_NL80211_REKEY_DATA];
12025        struct cfg80211_gtk_rekey_data rekey_data;
12026        int err;
12027
12028        if (!info->attrs[NL80211_ATTR_REKEY_DATA])
12029                return -EINVAL;
12030
12031        err = nla_parse_nested_deprecated(tb, MAX_NL80211_REKEY_DATA,
12032                                          info->attrs[NL80211_ATTR_REKEY_DATA],
12033                                          nl80211_rekey_policy, info->extack);
12034        if (err)
12035                return err;
12036
12037        if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
12038            !tb[NL80211_REKEY_DATA_KCK])
12039                return -EINVAL;
12040        if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
12041                return -ERANGE;
12042        if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
12043                return -ERANGE;
12044        if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
12045                return -ERANGE;
12046
12047        rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
12048        rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
12049        rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
12050
12051        wdev_lock(wdev);
12052        if (!wdev->current_bss) {
12053                err = -ENOTCONN;
12054                goto out;
12055        }
12056
12057        if (!rdev->ops->set_rekey_data) {
12058                err = -EOPNOTSUPP;
12059                goto out;
12060        }
12061
12062        err = rdev_set_rekey_data(rdev, dev, &rekey_data);
12063 out:
12064        wdev_unlock(wdev);
12065        return err;
12066}
12067
12068static int nl80211_register_unexpected_frame(struct sk_buff *skb,
12069                                             struct genl_info *info)
12070{
12071        struct net_device *dev = info->user_ptr[1];
12072        struct wireless_dev *wdev = dev->ieee80211_ptr;
12073
12074        if (wdev->iftype != NL80211_IFTYPE_AP &&
12075            wdev->iftype != NL80211_IFTYPE_P2P_GO)
12076                return -EINVAL;
12077
12078        if (wdev->ap_unexpected_nlportid)
12079                return -EBUSY;
12080
12081        wdev->ap_unexpected_nlportid = info->snd_portid;
12082        return 0;
12083}
12084
12085static int nl80211_probe_client(struct sk_buff *skb,
12086                                struct genl_info *info)
12087{
12088        struct cfg80211_registered_device *rdev = info->user_ptr[0];
12089        struct net_device *dev = info->user_ptr[1];
12090        struct wireless_dev *wdev = dev->ieee80211_ptr;
12091        struct sk_buff *msg;
12092        void *hdr;
12093        const u8 *addr;
12094        u64 cookie;
12095        int err;
12096
12097        if (wdev->iftype != NL80211_IFTYPE_AP &&
12098            wdev->iftype != NL80211_IFTYPE_P2P_GO)
12099                return -EOPNOTSUPP;
12100
12101        if (!info->attrs[NL80211_ATTR_MAC])
12102                return -EINVAL;
12103
12104        if (!rdev->ops->probe_client)
12105                return -EOPNOTSUPP;
12106
12107        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12108        if (!msg)
12109                return -ENOMEM;
12110
12111        hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12112                             NL80211_CMD_PROBE_CLIENT);
12113        if (!hdr) {
12114                err = -ENOBUFS;
12115                goto free_msg;
12116        }
12117
12118        addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12119
12120        err = rdev_probe_client(rdev, dev, addr, &cookie);
12121        if (err)
12122                goto free_msg;
12123
12124        if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12125                              NL80211_ATTR_PAD))
12126                goto nla_put_failure;
12127
12128        genlmsg_end(msg, hdr);
12129
12130        return genlmsg_reply(msg, info);
12131
12132 nla_put_failure:
12133        err = -ENOBUFS;
12134 free_msg:
12135        nlmsg_free(msg);
12136        return err;
12137}
12138
12139static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
12140{
12141        struct cfg80211_registered_device *rdev = info->user_ptr[0];
12142        struct cfg80211_beacon_registration *reg, *nreg;
12143        int rv;
12144
12145        if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
12146                return -EOPNOTSUPP;
12147
12148        nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
12149        if (!nreg)
12150                return -ENOMEM;
12151
12152        /* First, check if already registered. */
12153        spin_lock_bh(&rdev->beacon_registrations_lock);
12154        list_for_each_entry(reg, &rdev->beacon_registrations, list) {
12155                if (reg->nlportid == info->snd_portid) {
12156                        rv = -EALREADY;
12157                        goto out_err;
12158                }
12159        }
12160        /* Add it to the list */
12161        nreg->nlportid = info->snd_portid;
12162        list_add(&nreg->list, &rdev->beacon_registrations);
12163
12164        spin_unlock_bh(&rdev->beacon_registrations_lock);
12165
12166        return 0;
12167out_err:
12168        spin_unlock_bh(&rdev->beacon_registrations_lock);
12169        kfree(nreg);
12170        return rv;
12171}
12172
12173static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
12174{
12175        struct cfg80211_registered_device *rdev = info->user_ptr[0];
12176        struct wireless_dev *wdev = info->user_ptr[1];
12177        int err;
12178
12179        if (!rdev->ops->start_p2p_device)
12180                return -EOPNOTSUPP;
12181
12182        if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
12183                return -EOPNOTSUPP;
12184
12185        if (wdev_running(wdev))
12186                return 0;
12187
12188        if (rfkill_blocked(rdev->rfkill))
12189                return -ERFKILL;
12190
12191        err = rdev_start_p2p_device(rdev, wdev);
12192        if (err)
12193                return err;
12194
12195        wdev->is_running = true;
12196        rdev->opencount++;
12197
12198        return 0;
12199}
12200
12201static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
12202{
12203        struct cfg80211_registered_device *rdev = info->user_ptr[0];
12204        struct wireless_dev *wdev = info->user_ptr[1];
12205
12206        if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
12207                return -EOPNOTSUPP;
12208
12209        if (!rdev->ops->stop_p2p_device)
12210                return -EOPNOTSUPP;
12211
12212        cfg80211_stop_p2p_device(rdev, wdev);
12213
12214        return 0;
12215}
12216
12217static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
12218{
12219        struct cfg80211_registered_device *rdev = info->user_ptr[0];
12220        struct wireless_dev *wdev = info->user_ptr[1];
12221        struct cfg80211_nan_conf conf = {};
12222        int err;
12223
12224        if (wdev->iftype != NL80211_IFTYPE_NAN)
12225                return -EOPNOTSUPP;
12226
12227        if (wdev_running(wdev))
12228                return -EEXIST;
12229
12230        if (rfkill_blocked(rdev->rfkill))
12231                return -ERFKILL;
12232
12233        if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
12234                return -EINVAL;
12235
12236        conf.master_pref =
12237                nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
12238
12239        if (info->attrs[NL80211_ATTR_BANDS]) {
12240                u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
12241
12242                if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
12243                        return -EOPNOTSUPP;
12244
12245                if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
12246                        return -EINVAL;
12247
12248                conf.bands = bands;
12249        }
12250
12251        err = rdev_start_nan(rdev, wdev, &conf);
12252        if (err)
12253                return err;
12254
12255        wdev->is_running = true;
12256        rdev->opencount++;
12257
12258        return 0;
12259}
12260
12261static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
12262{
12263        struct cfg80211_registered_device *rdev = info->user_ptr[0];
12264        struct wireless_dev *wdev = info->user_ptr[1];
12265
12266        if (wdev->iftype != NL80211_IFTYPE_NAN)
12267                return -EOPNOTSUPP;
12268
12269        cfg80211_stop_nan(rdev, wdev);
12270
12271        return 0;
12272}
12273
12274static int validate_nan_filter(struct nlattr *filter_attr)
12275{
12276        struct nlattr *attr;
12277        int len = 0, n_entries = 0, rem;
12278
12279        nla_for_each_nested(attr, filter_attr, rem) {
12280                len += nla_len(attr);
12281                n_entries++;
12282        }
12283
12284        if (len >= U8_MAX)
12285                return -EINVAL;
12286
12287        return n_entries;
12288}
12289
12290static int handle_nan_filter(struct nlattr *attr_filter,
12291                             struct cfg80211_nan_func *func,
12292                             bool tx)
12293{
12294        struct nlattr *attr;
12295        int n_entries, rem, i;
12296        struct cfg80211_nan_func_filter *filter;
12297
12298        n_entries = validate_nan_filter(attr_filter);
12299        if (n_entries < 0)
12300                return n_entries;
12301
12302        BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
12303
12304        filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
12305        if (!filter)
12306                return -ENOMEM;
12307
12308        i = 0;
12309        nla_for_each_nested(attr, attr_filter, rem) {
12310                filter[i].filter = nla_memdup(attr, GFP_KERNEL);
12311                filter[i].len = nla_len(attr);
12312                i++;
12313        }
12314        if (tx) {
12315                func->num_tx_filters = n_entries;
12316                func->tx_filters = filter;
12317        } else {
12318                func->num_rx_filters = n_entries;
12319                func->rx_filters = filter;
12320        }
12321
12322        return 0;
12323}
12324
12325static int nl80211_nan_add_func(struct sk_buff *skb,
12326                                struct genl_info *info)
12327{
12328        struct cfg80211_registered_device *rdev = info->user_ptr[0];
12329        struct wireless_dev *wdev = info->user_ptr[1];
12330        struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
12331        struct cfg80211_nan_func *func;
12332        struct sk_buff *msg = NULL;
12333        void *hdr = NULL;
12334        int err = 0;
12335
12336        if (wdev->iftype != NL80211_IFTYPE_NAN)
12337                return -EOPNOTSUPP;
12338
12339        if (!wdev_running(wdev))
12340                return -ENOTCONN;
12341
12342        if (!info->attrs[NL80211_ATTR_NAN_FUNC])
12343                return -EINVAL;
12344
12345        err = nla_parse_nested_deprecated(tb, NL80211_NAN_FUNC_ATTR_MAX,
12346                                          info->attrs[NL80211_ATTR_NAN_FUNC],
12347                                          nl80211_nan_func_policy,
12348                                          info->extack);
12349        if (err)
12350                return err;
12351
12352        func = kzalloc(sizeof(*func), GFP_KERNEL);
12353        if (!func)
12354                return -ENOMEM;
12355
12356        func->cookie = cfg80211_assign_cookie(rdev);
12357
12358        if (!tb[NL80211_NAN_FUNC_TYPE] ||
12359            nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]) > NL80211_NAN_FUNC_MAX_TYPE) {
12360                err = -EINVAL;
12361                goto out;
12362        }
12363
12364
12365        func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
12366
12367        if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
12368                err = -EINVAL;
12369                goto out;
12370        }
12371
12372        memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
12373               sizeof(func->service_id));
12374
12375        func->close_range =
12376                nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
12377
12378        if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
12379                func->serv_spec_info_len =
12380                        nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
12381                func->serv_spec_info =
12382                        kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
12383                                func->serv_spec_info_len,
12384                                GFP_KERNEL);
12385                if (!func->serv_spec_info) {
12386                        err = -ENOMEM;
12387                        goto out;
12388                }
12389        }
12390
12391        if (tb[NL80211_NAN_FUNC_TTL])
12392                func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
12393
12394        switch (func->type) {
12395        case NL80211_NAN_FUNC_PUBLISH:
12396                if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
12397                        err = -EINVAL;
12398                        goto out;
12399                }
12400
12401                func->publish_type =
12402                        nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
12403                func->publish_bcast =
12404                        nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
12405
12406                if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
12407                        func->publish_bcast) {
12408                        err = -EINVAL;
12409                        goto out;
12410                }
12411                break;
12412        case NL80211_NAN_FUNC_SUBSCRIBE:
12413                func->subscribe_active =
12414                        nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
12415                break;
12416        case NL80211_NAN_FUNC_FOLLOW_UP:
12417                if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
12418                    !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
12419                    !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
12420                        err = -EINVAL;
12421                        goto out;
12422                }
12423
12424                func->followup_id =
12425                        nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
12426                func->followup_reqid =
12427                        nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
12428                memcpy(func->followup_dest.addr,
12429                       nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
12430                       sizeof(func->followup_dest.addr));
12431                if (func->ttl) {
12432                        err = -EINVAL;
12433                        goto out;
12434                }
12435                break;
12436        default:
12437                err = -EINVAL;
12438                goto out;
12439        }
12440
12441        if (tb[NL80211_NAN_FUNC_SRF]) {
12442                struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
12443
12444                err = nla_parse_nested_deprecated(srf_tb,
12445                                                  NL80211_NAN_SRF_ATTR_MAX,
12446                                                  tb[NL80211_NAN_FUNC_SRF],
12447                                                  nl80211_nan_srf_policy,
12448                                                  info->extack);
12449                if (err)
12450                        goto out;
12451
12452                func->srf_include =
12453                        nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
12454
12455                if (srf_tb[NL80211_NAN_SRF_BF]) {
12456                        if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
12457                            !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
12458                                err = -EINVAL;
12459                                goto out;
12460                        }
12461
12462                        func->srf_bf_len =
12463                                nla_len(srf_tb[NL80211_NAN_SRF_BF]);
12464                        func->srf_bf =
12465                                kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
12466                                        func->srf_bf_len, GFP_KERNEL);
12467                        if (!func->srf_bf) {
12468                                err = -ENOMEM;
12469                                goto out;
12470                        }
12471
12472                        func->srf_bf_idx =
12473                                nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
12474                } else {
12475                        struct nlattr *attr, *mac_attr =
12476                                srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
12477                        int n_entries, rem, i = 0;
12478
12479                        if (!mac_attr) {
12480                                err = -EINVAL;
12481                                goto out;
12482                        }
12483
12484                        n_entries = validate_acl_mac_addrs(mac_attr);
12485                        if (n_entries <= 0) {
12486                                err = -EINVAL;
12487                                goto out;
12488                        }
12489
12490                        func->srf_num_macs = n_entries;
12491                        func->srf_macs =
12492                                kcalloc(n_entries, sizeof(*func->srf_macs),
12493                                        GFP_KERNEL);
12494                        if (!func->srf_macs) {
12495                                err = -ENOMEM;
12496                                goto out;
12497                        }
12498
12499                        nla_for_each_nested(attr, mac_attr, rem)
12500                                memcpy(func->srf_macs[i++].addr, nla_data(attr),
12501                                       sizeof(*func->srf_macs));
12502                }
12503        }
12504
12505        if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
12506                err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
12507                                        func, true);
12508                if (err)
12509                        goto out;
12510        }
12511
12512        if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
12513                err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
12514                                        func, false);
12515                if (err)
12516                        goto out;
12517        }
12518
12519        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12520        if (!msg) {
12521                err = -ENOMEM;
12522                goto out;
12523        }
12524
12525        hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12526                             NL80211_CMD_ADD_NAN_FUNCTION);
12527        /* This can't really happen - we just allocated 4KB */
12528        if (WARN_ON(!hdr)) {
12529                err = -ENOMEM;
12530                goto out;
12531        }
12532
12533        err = rdev_add_nan_func(rdev, wdev, func);
12534out:
12535        if (err < 0) {
12536                cfg80211_free_nan_func(func);
12537                nlmsg_free(msg);
12538                return err;
12539        }
12540
12541        /* propagate the instance id and cookie to userspace  */
12542        if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
12543                              NL80211_ATTR_PAD))
12544                goto nla_put_failure;
12545
12546        func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
12547        if (!func_attr)
12548                goto nla_put_failure;
12549
12550        if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
12551                       func->instance_id))
12552                goto nla_put_failure;
12553
12554        nla_nest_end(msg, func_attr);
12555
12556        genlmsg_end(msg, hdr);
12557        return genlmsg_reply(msg, info);
12558
12559nla_put_failure:
12560        nlmsg_free(msg);
12561        return -ENOBUFS;
12562}
12563
12564static int nl80211_nan_del_func(struct sk_buff *skb,
12565                               struct genl_info *info)
12566{
12567        struct cfg80211_registered_device *rdev = info->user_ptr[0];
12568        struct wireless_dev *wdev = info->user_ptr[1];
12569        u64 cookie;
12570
12571        if (wdev->iftype != NL80211_IFTYPE_NAN)
12572                return -EOPNOTSUPP;
12573
12574        if (!wdev_running(wdev))
12575                return -ENOTCONN;
12576
12577        if (!info->attrs[NL80211_ATTR_COOKIE])
12578                return -EINVAL;
12579
12580        cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12581
12582        rdev_del_nan_func(rdev, wdev, cookie);
12583
12584        return 0;
12585}
12586
12587static int nl80211_nan_change_config(struct sk_buff *skb,
12588                                     struct genl_info *info)
12589{
12590        struct cfg80211_registered_device *rdev = info->user_ptr[0];
12591        struct wireless_dev *wdev = info->user_ptr[1];
12592        struct cfg80211_nan_conf conf = {};
12593        u32 changed = 0;
12594
12595        if (wdev->iftype != NL80211_IFTYPE_NAN)
12596                return -EOPNOTSUPP;
12597
12598        if (!wdev_running(wdev))
12599                return -ENOTCONN;
12600
12601        if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
12602                conf.master_pref =
12603                        nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
12604                if (conf.master_pref <= 1 || conf.master_pref == 255)
12605                        return -EINVAL;
12606
12607                changed |= CFG80211_NAN_CONF_CHANGED_PREF;
12608        }
12609
12610        if (info->attrs[NL80211_ATTR_BANDS]) {
12611                u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
12612
12613                if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
12614                        return -EOPNOTSUPP;
12615
12616                if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
12617                        return -EINVAL;
12618
12619                conf.bands = bands;
12620                changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
12621        }
12622
12623        if (!changed)
12624                return -EINVAL;
12625
12626        return rdev_nan_change_conf(rdev, wdev, &conf, changed);
12627}
12628
12629void cfg80211_nan_match(struct wireless_dev *wdev,
12630                        struct cfg80211_nan_match_params *match, gfp_t gfp)
12631{
12632        struct wiphy *wiphy = wdev->wiphy;
12633        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12634        struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
12635        struct sk_buff *msg;
12636        void *hdr;
12637
12638        if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
12639                return;
12640
12641        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12642        if (!msg)
12643                return;
12644
12645        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
12646        if (!hdr) {
12647                nlmsg_free(msg);
12648                return;
12649        }
12650
12651        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12652            (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12653                                         wdev->netdev->ifindex)) ||
12654            nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
12655                              NL80211_ATTR_PAD))
12656                goto nla_put_failure;
12657
12658        if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
12659                              NL80211_ATTR_PAD) ||
12660            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
12661                goto nla_put_failure;
12662
12663        match_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_MATCH);
12664        if (!match_attr)
12665                goto nla_put_failure;
12666
12667        local_func_attr = nla_nest_start_noflag(msg,
12668                                                NL80211_NAN_MATCH_FUNC_LOCAL);
12669        if (!local_func_attr)
12670                goto nla_put_failure;
12671
12672        if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
12673                goto nla_put_failure;
12674
12675        nla_nest_end(msg, local_func_attr);
12676
12677        peer_func_attr = nla_nest_start_noflag(msg,
12678                                               NL80211_NAN_MATCH_FUNC_PEER);
12679        if (!peer_func_attr)
12680                goto nla_put_failure;
12681
12682        if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
12683            nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
12684                goto nla_put_failure;
12685
12686        if (match->info && match->info_len &&
12687            nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
12688                    match->info))
12689                goto nla_put_failure;
12690
12691        nla_nest_end(msg, peer_func_attr);
12692        nla_nest_end(msg, match_attr);
12693        genlmsg_end(msg, hdr);
12694
12695        if (!wdev->owner_nlportid)
12696                genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
12697                                        msg, 0, NL80211_MCGRP_NAN, gfp);
12698        else
12699                genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
12700                                wdev->owner_nlportid);
12701
12702        return;
12703
12704nla_put_failure:
12705        nlmsg_free(msg);
12706}
12707EXPORT_SYMBOL(cfg80211_nan_match);
12708
12709void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
12710                                  u8 inst_id,
12711                                  enum nl80211_nan_func_term_reason reason,
12712                                  u64 cookie, gfp_t gfp)
12713{
12714        struct wiphy *wiphy = wdev->wiphy;
12715        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12716        struct sk_buff *msg;
12717        struct nlattr *func_attr;
12718        void *hdr;
12719
12720        if (WARN_ON(!inst_id))
12721                return;
12722
12723        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12724        if (!msg)
12725                return;
12726
12727        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
12728        if (!hdr) {
12729                nlmsg_free(msg);
12730                return;
12731        }
12732
12733        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12734            (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12735                                         wdev->netdev->ifindex)) ||
12736            nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
12737                              NL80211_ATTR_PAD))
12738                goto nla_put_failure;
12739
12740        if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12741                              NL80211_ATTR_PAD))
12742                goto nla_put_failure;
12743
12744        func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
12745        if (!func_attr)
12746                goto nla_put_failure;
12747
12748        if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
12749            nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
12750                goto nla_put_failure;
12751
12752        nla_nest_end(msg, func_attr);
12753        genlmsg_end(msg, hdr);
12754
12755        if (!wdev->owner_nlportid)
12756                genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
12757                                        msg, 0, NL80211_MCGRP_NAN, gfp);
12758        else
12759                genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
12760                                wdev->owner_nlportid);
12761
12762        return;
12763
12764nla_put_failure:
12765        nlmsg_free(msg);
12766}
12767EXPORT_SYMBOL(cfg80211_nan_func_terminated);
12768
12769static int nl80211_get_protocol_features(struct sk_buff *skb,
12770                                         struct genl_info *info)
12771{
12772        void *hdr;
12773        struct sk_buff *msg;
12774
12775        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12776        if (!msg)
12777                return -ENOMEM;
12778
12779        hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12780                             NL80211_CMD_GET_PROTOCOL_FEATURES);
12781        if (!hdr)
12782                goto nla_put_failure;
12783
12784        if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
12785                        NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
12786                goto nla_put_failure;
12787
12788        genlmsg_end(msg, hdr);
12789        return genlmsg_reply(msg, info);
12790
12791 nla_put_failure:
12792        kfree_skb(msg);
12793        return -ENOBUFS;
12794}
12795
12796static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
12797{
12798        struct cfg80211_registered_device *rdev = info->user_ptr[0];
12799        struct cfg80211_update_ft_ies_params ft_params;
12800        struct net_device *dev = info->user_ptr[1];
12801
12802        if (!rdev->ops->update_ft_ies)
12803                return -EOPNOTSUPP;
12804
12805        if (!info->attrs[NL80211_ATTR_MDID] ||
12806            !info->attrs[NL80211_ATTR_IE])
12807                return -EINVAL;
12808
12809        memset(&ft_params, 0, sizeof(ft_params));
12810        ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
12811        ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
12812        ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
12813
12814        return rdev_update_ft_ies(rdev, dev, &ft_params);
12815}
12816
12817static int nl80211_crit_protocol_start(struct sk_buff *skb,
12818                                       struct genl_info *info)
12819{
12820        struct cfg80211_registered_device *rdev = info->user_ptr[0];
12821        struct wireless_dev *wdev = info->user_ptr[1];
12822        enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
12823        u16 duration;
12824        int ret;
12825
12826        if (!rdev->ops->crit_proto_start)
12827                return -EOPNOTSUPP;
12828
12829        if (WARN_ON(!rdev->ops->crit_proto_stop))
12830                return -EINVAL;
12831
12832        if (rdev->crit_proto_nlportid)
12833                return -EBUSY;
12834
12835        /* determine protocol if provided */
12836        if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
12837                proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
12838
12839        if (proto >= NUM_NL80211_CRIT_PROTO)
12840                return -EINVAL;
12841
12842        /* timeout must be provided */
12843        if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
12844                return -EINVAL;
12845
12846        duration =
12847                nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
12848
12849        if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
12850                return -ERANGE;
12851
12852        ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
12853        if (!ret)
12854                rdev->crit_proto_nlportid = info->snd_portid;
12855
12856        return ret;
12857}
12858
12859static int nl80211_crit_protocol_stop(struct sk_buff *skb,
12860                                      struct genl_info *info)
12861{
12862        struct cfg80211_registered_device *rdev = info->user_ptr[0];
12863        struct wireless_dev *wdev = info->user_ptr[1];
12864
12865        if (!rdev->ops->crit_proto_stop)
12866                return -EOPNOTSUPP;
12867
12868        if (rdev->crit_proto_nlportid) {
12869                rdev->crit_proto_nlportid = 0;
12870                rdev_crit_proto_stop(rdev, wdev);
12871        }
12872        return 0;
12873}
12874
12875static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,
12876                                       struct nlattr *attr,
12877                                       struct netlink_ext_ack *extack)
12878{
12879        if (vcmd->policy == VENDOR_CMD_RAW_DATA) {
12880                if (attr->nla_type & NLA_F_NESTED) {
12881                        NL_SET_ERR_MSG_ATTR(extack, attr,
12882                                            "unexpected nested data");
12883                        return -EINVAL;
12884                }
12885
12886                return 0;
12887        }
12888
12889        if (!(attr->nla_type & NLA_F_NESTED)) {
12890                NL_SET_ERR_MSG_ATTR(extack, attr, "expected nested data");
12891                return -EINVAL;
12892        }
12893
12894        return nl80211_validate_nested(attr, vcmd->maxattr, vcmd->policy,
12895                                       extack);
12896}
12897
12898static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
12899{
12900        struct cfg80211_registered_device *rdev = info->user_ptr[0];
12901        struct wireless_dev *wdev =
12902                __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
12903        int i, err;
12904        u32 vid, subcmd;
12905
12906        if (!rdev->wiphy.vendor_commands)
12907                return -EOPNOTSUPP;
12908
12909        if (IS_ERR(wdev)) {
12910                err = PTR_ERR(wdev);
12911                if (err != -EINVAL)
12912                        return err;
12913                wdev = NULL;
12914        } else if (wdev->wiphy != &rdev->wiphy) {
12915                return -EINVAL;
12916        }
12917
12918        if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
12919            !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
12920                return -EINVAL;
12921
12922        vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
12923        subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
12924        for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
12925                const struct wiphy_vendor_command *vcmd;
12926                void *data = NULL;
12927                int len = 0;
12928
12929                vcmd = &rdev->wiphy.vendor_commands[i];
12930
12931                if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
12932                        continue;
12933
12934                if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
12935                                   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
12936                        if (!wdev)
12937                                return -EINVAL;
12938                        if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
12939                            !wdev->netdev)
12940                                return -EINVAL;
12941
12942                        if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
12943                                if (!wdev_running(wdev))
12944                                        return -ENETDOWN;
12945                        }
12946
12947                        if (!vcmd->doit)
12948                                return -EOPNOTSUPP;
12949                } else {
12950                        wdev = NULL;
12951                }
12952
12953                if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
12954                        data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
12955                        len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
12956
12957                        err = nl80211_vendor_check_policy(vcmd,
12958                                        info->attrs[NL80211_ATTR_VENDOR_DATA],
12959                                        info->extack);
12960                        if (err)
12961                                return err;
12962                }
12963
12964                rdev->cur_cmd_info = info;
12965                err = vcmd->doit(&rdev->wiphy, wdev, data, len);
12966                rdev->cur_cmd_info = NULL;
12967                return err;
12968        }
12969
12970        return -EOPNOTSUPP;
12971}
12972
12973static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
12974                                       struct netlink_callback *cb,
12975                                       struct cfg80211_registered_device **rdev,
12976                                       struct wireless_dev **wdev)
12977{
12978        struct nlattr **attrbuf;
12979        u32 vid, subcmd;
12980        unsigned int i;
12981        int vcmd_idx = -1;
12982        int err;
12983        void *data = NULL;
12984        unsigned int data_len = 0;
12985
12986        if (cb->args[0]) {
12987                /* subtract the 1 again here */
12988                struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
12989                struct wireless_dev *tmp;
12990
12991                if (!wiphy)
12992                        return -ENODEV;
12993                *rdev = wiphy_to_rdev(wiphy);
12994                *wdev = NULL;
12995
12996                if (cb->args[1]) {
12997                        list_for_each_entry(tmp, &wiphy->wdev_list, list) {
12998                                if (tmp->identifier == cb->args[1] - 1) {
12999                                        *wdev = tmp;
13000                                        break;
13001                                }
13002                        }
13003                }
13004
13005                /* keep rtnl locked in successful case */
13006                return 0;
13007        }
13008
13009        attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
13010        if (!attrbuf)
13011                return -ENOMEM;
13012
13013        err = nlmsg_parse_deprecated(cb->nlh,
13014                                     GENL_HDRLEN + nl80211_fam.hdrsize,
13015                                     attrbuf, nl80211_fam.maxattr,
13016                                     nl80211_policy, NULL);
13017        if (err)
13018                goto out;
13019
13020        if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
13021            !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
13022                err = -EINVAL;
13023                goto out;
13024        }
13025
13026        *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk), attrbuf);
13027        if (IS_ERR(*wdev))
13028                *wdev = NULL;
13029
13030        *rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
13031        if (IS_ERR(*rdev)) {
13032                err = PTR_ERR(*rdev);
13033                goto out;
13034        }
13035
13036        vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
13037        subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
13038
13039        for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
13040                const struct wiphy_vendor_command *vcmd;
13041
13042                vcmd = &(*rdev)->wiphy.vendor_commands[i];
13043
13044                if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
13045                        continue;
13046
13047                if (!vcmd->dumpit) {
13048                        err = -EOPNOTSUPP;
13049                        goto out;
13050                }
13051
13052                vcmd_idx = i;
13053                break;
13054        }
13055
13056        if (vcmd_idx < 0) {
13057                err = -EOPNOTSUPP;
13058                goto out;
13059        }
13060
13061        if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
13062                data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
13063                data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
13064
13065                err = nl80211_vendor_check_policy(
13066                                &(*rdev)->wiphy.vendor_commands[vcmd_idx],
13067                                attrbuf[NL80211_ATTR_VENDOR_DATA],
13068                                cb->extack);
13069                if (err)
13070                        return err;
13071        }
13072
13073        /* 0 is the first index - add 1 to parse only once */
13074        cb->args[0] = (*rdev)->wiphy_idx + 1;
13075        /* add 1 to know if it was NULL */
13076        cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
13077        cb->args[2] = vcmd_idx;
13078        cb->args[3] = (unsigned long)data;
13079        cb->args[4] = data_len;
13080
13081        /* keep rtnl locked in successful case */
13082        err = 0;
13083out:
13084        kfree(attrbuf);
13085        return err;
13086}
13087
13088static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
13089                                   struct netlink_callback *cb)
13090{
13091        struct cfg80211_registered_device *rdev;
13092        struct wireless_dev *wdev;
13093        unsigned int vcmd_idx;
13094        const struct wiphy_vendor_command *vcmd;
13095        void *data;
13096        int data_len;
13097        int err;
13098        struct nlattr *vendor_data;
13099
13100        rtnl_lock();
13101        err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
13102        if (err)
13103                goto out;
13104
13105        vcmd_idx = cb->args[2];
13106        data = (void *)cb->args[3];
13107        data_len = cb->args[4];
13108        vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
13109
13110        if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
13111                           WIPHY_VENDOR_CMD_NEED_NETDEV)) {
13112                if (!wdev) {
13113                        err = -EINVAL;
13114                        goto out;
13115                }
13116                if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
13117                    !wdev->netdev) {
13118                        err = -EINVAL;
13119                        goto out;
13120                }
13121
13122                if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
13123                        if (!wdev_running(wdev)) {
13124                                err = -ENETDOWN;
13125                                goto out;
13126                        }
13127                }
13128        }
13129
13130        while (1) {
13131                void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
13132                                           cb->nlh->nlmsg_seq, NLM_F_MULTI,
13133                                           NL80211_CMD_VENDOR);
13134                if (!hdr)
13135                        break;
13136
13137                if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13138                    (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
13139                                               wdev_id(wdev),
13140                                               NL80211_ATTR_PAD))) {
13141                        genlmsg_cancel(skb, hdr);
13142                        break;
13143                }
13144
13145                vendor_data = nla_nest_start_noflag(skb,
13146                                                    NL80211_ATTR_VENDOR_DATA);
13147                if (!vendor_data) {
13148                        genlmsg_cancel(skb, hdr);
13149                        break;
13150                }
13151
13152                err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
13153                                   (unsigned long *)&cb->args[5]);
13154                nla_nest_end(skb, vendor_data);
13155
13156                if (err == -ENOBUFS || err == -ENOENT) {
13157                        genlmsg_cancel(skb, hdr);
13158                        break;
13159                } else if (err) {
13160                        genlmsg_cancel(skb, hdr);
13161                        goto out;
13162                }
13163
13164                genlmsg_end(skb, hdr);
13165        }
13166
13167        err = skb->len;
13168 out:
13169        rtnl_unlock();
13170        return err;
13171}
13172
13173struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
13174                                           enum nl80211_commands cmd,
13175                                           enum nl80211_attrs attr,
13176                                           int approxlen)
13177{
13178        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13179
13180        if (WARN_ON(!rdev->cur_cmd_info))
13181                return NULL;
13182
13183        return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
13184                                           rdev->cur_cmd_info->snd_portid,
13185                                           rdev->cur_cmd_info->snd_seq,
13186                                           cmd, attr, NULL, GFP_KERNEL);
13187}
13188EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
13189
13190int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
13191{
13192        struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
13193        void *hdr = ((void **)skb->cb)[1];
13194        struct nlattr *data = ((void **)skb->cb)[2];
13195
13196        /* clear CB data for netlink core to own from now on */
13197        memset(skb->cb, 0, sizeof(skb->cb));
13198
13199        if (WARN_ON(!rdev->cur_cmd_info)) {
13200                kfree_skb(skb);
13201                return -EINVAL;
13202        }
13203
13204        nla_nest_end(skb, data);
13205        genlmsg_end(skb, hdr);
13206        return genlmsg_reply(skb, rdev->cur_cmd_info);
13207}
13208EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
13209
13210unsigned int cfg80211_vendor_cmd_get_sender(struct wiphy *wiphy)
13211{
13212        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13213
13214        if (WARN_ON(!rdev->cur_cmd_info))
13215                return 0;
13216
13217        return rdev->cur_cmd_info->snd_portid;
13218}
13219EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_get_sender);
13220
13221static int nl80211_set_qos_map(struct sk_buff *skb,
13222                               struct genl_info *info)
13223{
13224        struct cfg80211_registered_device *rdev = info->user_ptr[0];
13225        struct cfg80211_qos_map *qos_map = NULL;
13226        struct net_device *dev = info->user_ptr[1];
13227        u8 *pos, len, num_des, des_len, des;
13228        int ret;
13229
13230        if (!rdev->ops->set_qos_map)
13231                return -EOPNOTSUPP;
13232
13233        if (info->attrs[NL80211_ATTR_QOS_MAP]) {
13234                pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
13235                len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
13236
13237                if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
13238                    len > IEEE80211_QOS_MAP_LEN_MAX)
13239                        return -EINVAL;
13240
13241                qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
13242                if (!qos_map)
13243                        return -ENOMEM;
13244
13245                num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
13246                if (num_des) {
13247                        des_len = num_des *
13248                                sizeof(struct cfg80211_dscp_exception);
13249                        memcpy(qos_map->dscp_exception, pos, des_len);
13250                        qos_map->num_des = num_des;
13251                        for (des = 0; des < num_des; des++) {
13252                                if (qos_map->dscp_exception[des].up > 7) {
13253                                        kfree(qos_map);
13254                                        return -EINVAL;
13255                                }
13256                        }
13257                        pos += des_len;
13258                }
13259                memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
13260        }
13261
13262        wdev_lock(dev->ieee80211_ptr);
13263        ret = nl80211_key_allowed(dev->ieee80211_ptr);
13264        if (!ret)
13265                ret = rdev_set_qos_map(rdev, dev, qos_map);
13266        wdev_unlock(dev->ieee80211_ptr);
13267
13268        kfree(qos_map);
13269        return ret;
13270}
13271
13272static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
13273{
13274        struct cfg80211_registered_device *rdev = info->user_ptr[0];
13275        struct net_device *dev = info->user_ptr[1];
13276        struct wireless_dev *wdev = dev->ieee80211_ptr;
13277        const u8 *peer;
13278        u8 tsid, up;
13279        u16 admitted_time = 0;
13280        int err;
13281
13282        if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
13283                return -EOPNOTSUPP;
13284
13285        if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
13286            !info->attrs[NL80211_ATTR_USER_PRIO])
13287                return -EINVAL;
13288
13289        tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
13290        up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
13291
13292        /* WMM uses TIDs 0-7 even for TSPEC */
13293        if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
13294                /* TODO: handle 802.11 TSPEC/admission control
13295                 * need more attributes for that (e.g. BA session requirement);
13296                 * change the WMM adminssion test above to allow both then
13297                 */
13298                return -EINVAL;
13299        }
13300
13301        peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
13302
13303        if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
13304                admitted_time =
13305                        nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
13306                if (!admitted_time)
13307                        return -EINVAL;
13308        }
13309
13310        wdev_lock(wdev);
13311        switch (wdev->iftype) {
13312        case NL80211_IFTYPE_STATION:
13313        case NL80211_IFTYPE_P2P_CLIENT:
13314                if (wdev->current_bss)
13315                        break;
13316                err = -ENOTCONN;
13317                goto out;
13318        default:
13319                err = -EOPNOTSUPP;
13320                goto out;
13321        }
13322
13323        err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
13324
13325 out:
13326        wdev_unlock(wdev);
13327        return err;
13328}
13329
13330static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
13331{
13332        struct cfg80211_registered_device *rdev = info->user_ptr[0];
13333        struct net_device *dev = info->user_ptr[1];
13334        struct wireless_dev *wdev = dev->ieee80211_ptr;
13335        const u8 *peer;
13336        u8 tsid;
13337        int err;
13338
13339        if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
13340                return -EINVAL;
13341
13342        tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
13343        peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
13344
13345        wdev_lock(wdev);
13346        err = rdev_del_tx_ts(rdev, dev, tsid, peer);
13347        wdev_unlock(wdev);
13348
13349        return err;
13350}
13351
13352static int nl80211_tdls_channel_switch(struct sk_buff *skb,
13353                                       struct genl_info *info)
13354{
13355        struct cfg80211_registered_device *rdev = info->user_ptr[0];
13356        struct net_device *dev = info->user_ptr[1];
13357        struct wireless_dev *wdev = dev->ieee80211_ptr;
13358        struct cfg80211_chan_def chandef = {};
13359        const u8 *addr;
13360        u8 oper_class;
13361        int err;
13362
13363        if (!rdev->ops->tdls_channel_switch ||
13364            !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
13365                return -EOPNOTSUPP;
13366
13367        switch (dev->ieee80211_ptr->iftype) {
13368        case NL80211_IFTYPE_STATION:
13369        case NL80211_IFTYPE_P2P_CLIENT:
13370                break;
13371        default:
13372                return -EOPNOTSUPP;
13373        }
13374
13375        if (!info->attrs[NL80211_ATTR_MAC] ||
13376            !info->attrs[NL80211_ATTR_OPER_CLASS])
13377                return -EINVAL;
13378
13379        err = nl80211_parse_chandef(rdev, info, &chandef);
13380        if (err)
13381                return err;
13382
13383        /*
13384         * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
13385         * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
13386         * specification is not defined for them.
13387         */
13388        if (chandef.chan->band == NL80211_BAND_2GHZ &&
13389            chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
13390            chandef.width != NL80211_CHAN_WIDTH_20)
13391                return -EINVAL;
13392
13393        /* we will be active on the TDLS link */
13394        if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
13395                                           wdev->iftype))
13396                return -EINVAL;
13397
13398        /* don't allow switching to DFS channels */
13399        if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
13400                return -EINVAL;
13401
13402        addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
13403        oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
13404
13405        wdev_lock(wdev);
13406        err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
13407        wdev_unlock(wdev);
13408
13409        return err;
13410}
13411
13412static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
13413                                              struct genl_info *info)
13414{
13415        struct cfg80211_registered_device *rdev = info->user_ptr[0];
13416        struct net_device *dev = info->user_ptr[1];
13417        struct wireless_dev *wdev = dev->ieee80211_ptr;
13418        const u8 *addr;
13419
13420        if (!rdev->ops->tdls_channel_switch ||
13421            !rdev->ops->tdls_cancel_channel_switch ||
13422            !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
13423                return -EOPNOTSUPP;
13424
13425        switch (dev->ieee80211_ptr->iftype) {
13426        case NL80211_IFTYPE_STATION:
13427        case NL80211_IFTYPE_P2P_CLIENT:
13428                break;
13429        default:
13430                return -EOPNOTSUPP;
13431        }
13432
13433        if (!info->attrs[NL80211_ATTR_MAC])
13434                return -EINVAL;
13435
13436        addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
13437
13438        wdev_lock(wdev);
13439        rdev_tdls_cancel_channel_switch(rdev, dev, addr);
13440        wdev_unlock(wdev);
13441
13442        return 0;
13443}
13444
13445static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
13446                                            struct genl_info *info)
13447{
13448        struct cfg80211_registered_device *rdev = info->user_ptr[0];
13449        struct net_device *dev = info->user_ptr[1];
13450        struct wireless_dev *wdev = dev->ieee80211_ptr;
13451        const struct nlattr *nla;
13452        bool enabled;
13453
13454        if (!rdev->ops->set_multicast_to_unicast)
13455                return -EOPNOTSUPP;
13456
13457        if (wdev->iftype != NL80211_IFTYPE_AP &&
13458            wdev->iftype != NL80211_IFTYPE_P2P_GO)
13459                return -EOPNOTSUPP;
13460
13461        nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
13462        enabled = nla_get_flag(nla);
13463
13464        return rdev_set_multicast_to_unicast(rdev, dev, enabled);
13465}
13466
13467static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
13468{
13469        struct cfg80211_registered_device *rdev = info->user_ptr[0];
13470        struct net_device *dev = info->user_ptr[1];
13471        struct wireless_dev *wdev = dev->ieee80211_ptr;
13472        struct cfg80211_pmk_conf pmk_conf = {};
13473        int ret;
13474
13475        if (wdev->iftype != NL80211_IFTYPE_STATION &&
13476            wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
13477                return -EOPNOTSUPP;
13478
13479        if (!wiphy_ext_feature_isset(&rdev->wiphy,
13480                                     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
13481                return -EOPNOTSUPP;
13482
13483        if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
13484                return -EINVAL;
13485
13486        wdev_lock(wdev);
13487        if (!wdev->current_bss) {
13488                ret = -ENOTCONN;
13489                goto out;
13490        }
13491
13492        pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
13493        if (memcmp(pmk_conf.aa, wdev->current_bss->pub.bssid, ETH_ALEN)) {
13494                ret = -EINVAL;
13495                goto out;
13496        }
13497
13498        pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
13499        pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
13500        if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
13501            pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
13502                ret = -EINVAL;
13503                goto out;
13504        }
13505
13506        if (info->attrs[NL80211_ATTR_PMKR0_NAME]) {
13507                int r0_name_len = nla_len(info->attrs[NL80211_ATTR_PMKR0_NAME]);
13508
13509                if (r0_name_len != WLAN_PMK_NAME_LEN) {
13510                        ret = -EINVAL;
13511                        goto out;
13512                }
13513
13514                pmk_conf.pmk_r0_name =
13515                        nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
13516        }
13517
13518        ret = rdev_set_pmk(rdev, dev, &pmk_conf);
13519out:
13520        wdev_unlock(wdev);
13521        return ret;
13522}
13523
13524static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
13525{
13526        struct cfg80211_registered_device *rdev = info->user_ptr[0];
13527        struct net_device *dev = info->user_ptr[1];
13528        struct wireless_dev *wdev = dev->ieee80211_ptr;
13529        const u8 *aa;
13530        int ret;
13531
13532        if (wdev->iftype != NL80211_IFTYPE_STATION &&
13533            wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
13534                return -EOPNOTSUPP;
13535
13536        if (!wiphy_ext_feature_isset(&rdev->wiphy,
13537                                     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
13538                return -EOPNOTSUPP;
13539
13540        if (!info->attrs[NL80211_ATTR_MAC])
13541                return -EINVAL;
13542
13543        wdev_lock(wdev);
13544        aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
13545        ret = rdev_del_pmk(rdev, dev, aa);
13546        wdev_unlock(wdev);
13547
13548        return ret;
13549}
13550
13551static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
13552{
13553        struct cfg80211_registered_device *rdev = info->user_ptr[0];
13554        struct net_device *dev = info->user_ptr[1];
13555        struct cfg80211_external_auth_params params;
13556
13557        if (!rdev->ops->external_auth)
13558                return -EOPNOTSUPP;
13559
13560        if (!info->attrs[NL80211_ATTR_SSID] &&
13561            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
13562            dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
13563                return -EINVAL;
13564
13565        if (!info->attrs[NL80211_ATTR_BSSID])
13566                return -EINVAL;
13567
13568        if (!info->attrs[NL80211_ATTR_STATUS_CODE])
13569                return -EINVAL;
13570
13571        memset(&params, 0, sizeof(params));
13572
13573        if (info->attrs[NL80211_ATTR_SSID]) {
13574                params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
13575                if (params.ssid.ssid_len == 0 ||
13576                    params.ssid.ssid_len > IEEE80211_MAX_SSID_LEN)
13577                        return -EINVAL;
13578                memcpy(params.ssid.ssid,
13579                       nla_data(info->attrs[NL80211_ATTR_SSID]),
13580                       params.ssid.ssid_len);
13581        }
13582
13583        memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
13584               ETH_ALEN);
13585
13586        params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
13587
13588        if (info->attrs[NL80211_ATTR_PMKID])
13589                params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
13590
13591        return rdev_external_auth(rdev, dev, &params);
13592}
13593
13594static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
13595{
13596        struct cfg80211_registered_device *rdev = info->user_ptr[0];
13597        struct net_device *dev = info->user_ptr[1];
13598        struct wireless_dev *wdev = dev->ieee80211_ptr;
13599        const u8 *buf;
13600        size_t len;
13601        u8 *dest;
13602        u16 proto;
13603        bool noencrypt;
13604        int err;
13605
13606        if (!wiphy_ext_feature_isset(&rdev->wiphy,
13607                                     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
13608                return -EOPNOTSUPP;
13609
13610        if (!rdev->ops->tx_control_port)
13611                return -EOPNOTSUPP;
13612
13613        if (!info->attrs[NL80211_ATTR_FRAME] ||
13614            !info->attrs[NL80211_ATTR_MAC] ||
13615            !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
13616                GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
13617                return -EINVAL;
13618        }
13619
13620        wdev_lock(wdev);
13621
13622        switch (wdev->iftype) {
13623        case NL80211_IFTYPE_AP:
13624        case NL80211_IFTYPE_P2P_GO:
13625        case NL80211_IFTYPE_MESH_POINT:
13626                break;
13627        case NL80211_IFTYPE_ADHOC:
13628        case NL80211_IFTYPE_STATION:
13629        case NL80211_IFTYPE_P2P_CLIENT:
13630                if (wdev->current_bss)
13631                        break;
13632                err = -ENOTCONN;
13633                goto out;
13634        default:
13635                err = -EOPNOTSUPP;
13636                goto out;
13637        }
13638
13639        wdev_unlock(wdev);
13640
13641        buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
13642        len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
13643        dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
13644        proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
13645        noencrypt =
13646                nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
13647
13648        return rdev_tx_control_port(rdev, dev, buf, len,
13649                                    dest, cpu_to_be16(proto), noencrypt);
13650
13651 out:
13652        wdev_unlock(wdev);
13653        return err;
13654}
13655
13656static int nl80211_get_ftm_responder_stats(struct sk_buff *skb,
13657                                           struct genl_info *info)
13658{
13659        struct cfg80211_registered_device *rdev = info->user_ptr[0];
13660        struct net_device *dev = info->user_ptr[1];
13661        struct wireless_dev *wdev = dev->ieee80211_ptr;
13662        struct cfg80211_ftm_responder_stats ftm_stats = {};
13663        struct sk_buff *msg;
13664        void *hdr;
13665        struct nlattr *ftm_stats_attr;
13666        int err;
13667
13668        if (wdev->iftype != NL80211_IFTYPE_AP || !wdev->beacon_interval)
13669                return -EOPNOTSUPP;
13670
13671        err = rdev_get_ftm_responder_stats(rdev, dev, &ftm_stats);
13672        if (err)
13673                return err;
13674
13675        if (!ftm_stats.filled)
13676                return -ENODATA;
13677
13678        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13679        if (!msg)
13680                return -ENOMEM;
13681
13682        hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13683                             NL80211_CMD_GET_FTM_RESPONDER_STATS);
13684        if (!hdr)
13685                goto nla_put_failure;
13686
13687        if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
13688                goto nla_put_failure;
13689
13690        ftm_stats_attr = nla_nest_start_noflag(msg,
13691                                               NL80211_ATTR_FTM_RESPONDER_STATS);
13692        if (!ftm_stats_attr)
13693                goto nla_put_failure;
13694
13695#define SET_FTM(field, name, type)                                       \
13696        do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
13697            nla_put_ ## type(msg, NL80211_FTM_STATS_ ## name,            \
13698                             ftm_stats.field))                           \
13699                goto nla_put_failure; } while (0)
13700#define SET_FTM_U64(field, name)                                         \
13701        do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
13702            nla_put_u64_64bit(msg, NL80211_FTM_STATS_ ## name,           \
13703                              ftm_stats.field, NL80211_FTM_STATS_PAD))   \
13704                goto nla_put_failure; } while (0)
13705
13706        SET_FTM(success_num, SUCCESS_NUM, u32);
13707        SET_FTM(partial_num, PARTIAL_NUM, u32);
13708        SET_FTM(failed_num, FAILED_NUM, u32);
13709        SET_FTM(asap_num, ASAP_NUM, u32);
13710        SET_FTM(non_asap_num, NON_ASAP_NUM, u32);
13711        SET_FTM_U64(total_duration_ms, TOTAL_DURATION_MSEC);
13712        SET_FTM(unknown_triggers_num, UNKNOWN_TRIGGERS_NUM, u32);
13713        SET_FTM(reschedule_requests_num, RESCHEDULE_REQUESTS_NUM, u32);
13714        SET_FTM(out_of_window_triggers_num, OUT_OF_WINDOW_TRIGGERS_NUM, u32);
13715#undef SET_FTM
13716
13717        nla_nest_end(msg, ftm_stats_attr);
13718
13719        genlmsg_end(msg, hdr);
13720        return genlmsg_reply(msg, info);
13721
13722nla_put_failure:
13723        nlmsg_free(msg);
13724        return -ENOBUFS;
13725}
13726
13727static int nl80211_update_owe_info(struct sk_buff *skb, struct genl_info *info)
13728{
13729        struct cfg80211_registered_device *rdev = info->user_ptr[0];
13730        struct cfg80211_update_owe_info owe_info;
13731        struct net_device *dev = info->user_ptr[1];
13732
13733        if (!rdev->ops->update_owe_info)
13734                return -EOPNOTSUPP;
13735
13736        if (!info->attrs[NL80211_ATTR_STATUS_CODE] ||
13737            !info->attrs[NL80211_ATTR_MAC])
13738                return -EINVAL;
13739
13740        memset(&owe_info, 0, sizeof(owe_info));
13741        owe_info.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
13742        nla_memcpy(owe_info.peer, info->attrs[NL80211_ATTR_MAC], ETH_ALEN);
13743
13744        if (info->attrs[NL80211_ATTR_IE]) {
13745                owe_info.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
13746                owe_info.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
13747        }
13748
13749        return rdev_update_owe_info(rdev, dev, &owe_info);
13750}
13751
13752static int nl80211_probe_mesh_link(struct sk_buff *skb, struct genl_info *info)
13753{
13754        struct cfg80211_registered_device *rdev = info->user_ptr[0];
13755        struct net_device *dev = info->user_ptr[1];
13756        struct wireless_dev *wdev = dev->ieee80211_ptr;
13757        struct station_info sinfo = {};
13758        const u8 *buf;
13759        size_t len;
13760        u8 *dest;
13761        int err;
13762
13763        if (!rdev->ops->probe_mesh_link || !rdev->ops->get_station)
13764                return -EOPNOTSUPP;
13765
13766        if (!info->attrs[NL80211_ATTR_MAC] ||
13767            !info->attrs[NL80211_ATTR_FRAME]) {
13768                GENL_SET_ERR_MSG(info, "Frame or MAC missing");
13769                return -EINVAL;
13770        }
13771
13772        if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
13773                return -EOPNOTSUPP;
13774
13775        dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
13776        buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
13777        len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
13778
13779        if (len < sizeof(struct ethhdr))
13780                return -EINVAL;
13781
13782        if (!ether_addr_equal(buf, dest) || is_multicast_ether_addr(buf) ||
13783            !ether_addr_equal(buf + ETH_ALEN, dev->dev_addr))
13784                return -EINVAL;
13785
13786        err = rdev_get_station(rdev, dev, dest, &sinfo);
13787        if (err)
13788                return err;
13789
13790        return rdev_probe_mesh_link(rdev, dev, dest, buf, len);
13791}
13792
13793#define NL80211_FLAG_NEED_WIPHY         0x01
13794#define NL80211_FLAG_NEED_NETDEV        0x02
13795#define NL80211_FLAG_NEED_RTNL          0x04
13796#define NL80211_FLAG_CHECK_NETDEV_UP    0x08
13797#define NL80211_FLAG_NEED_NETDEV_UP     (NL80211_FLAG_NEED_NETDEV |\
13798                                         NL80211_FLAG_CHECK_NETDEV_UP)
13799#define NL80211_FLAG_NEED_WDEV          0x10
13800/* If a netdev is associated, it must be UP, P2P must be started */
13801#define NL80211_FLAG_NEED_WDEV_UP       (NL80211_FLAG_NEED_WDEV |\
13802                                         NL80211_FLAG_CHECK_NETDEV_UP)
13803#define NL80211_FLAG_CLEAR_SKB          0x20
13804
13805static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
13806                            struct genl_info *info)
13807{
13808        struct cfg80211_registered_device *rdev;
13809        struct wireless_dev *wdev;
13810        struct net_device *dev;
13811        bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
13812
13813        if (rtnl)
13814                rtnl_lock();
13815
13816        if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
13817                rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
13818                if (IS_ERR(rdev)) {
13819                        if (rtnl)
13820                                rtnl_unlock();
13821                        return PTR_ERR(rdev);
13822                }
13823                info->user_ptr[0] = rdev;
13824        } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
13825                   ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
13826                ASSERT_RTNL();
13827
13828                wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
13829                                                  info->attrs);
13830                if (IS_ERR(wdev)) {
13831                        if (rtnl)
13832                                rtnl_unlock();
13833                        return PTR_ERR(wdev);
13834                }
13835
13836                dev = wdev->netdev;
13837                rdev = wiphy_to_rdev(wdev->wiphy);
13838
13839                if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
13840                        if (!dev) {
13841                                if (rtnl)
13842                                        rtnl_unlock();
13843                                return -EINVAL;
13844                        }
13845
13846                        info->user_ptr[1] = dev;
13847                } else {
13848                        info->user_ptr[1] = wdev;
13849                }
13850
13851                if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
13852                    !wdev_running(wdev)) {
13853                        if (rtnl)
13854                                rtnl_unlock();
13855                        return -ENETDOWN;
13856                }
13857
13858                if (dev)
13859                        dev_hold(dev);
13860
13861                info->user_ptr[0] = rdev;
13862        }
13863
13864        return 0;
13865}
13866
13867static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
13868                              struct genl_info *info)
13869{
13870        if (info->user_ptr[1]) {
13871                if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
13872                        struct wireless_dev *wdev = info->user_ptr[1];
13873
13874                        if (wdev->netdev)
13875                                dev_put(wdev->netdev);
13876                } else {
13877                        dev_put(info->user_ptr[1]);
13878                }
13879        }
13880
13881        if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
13882                rtnl_unlock();
13883
13884        /* If needed, clear the netlink message payload from the SKB
13885         * as it might contain key data that shouldn't stick around on
13886         * the heap after the SKB is freed. The netlink message header
13887         * is still needed for further processing, so leave it intact.
13888         */
13889        if (ops->internal_flags & NL80211_FLAG_CLEAR_SKB) {
13890                struct nlmsghdr *nlh = nlmsg_hdr(skb);
13891
13892                memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
13893        }
13894}
13895
13896static const struct genl_ops nl80211_ops[] = {
13897        {
13898                .cmd = NL80211_CMD_GET_WIPHY,
13899                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13900                .doit = nl80211_get_wiphy,
13901                .dumpit = nl80211_dump_wiphy,
13902                .done = nl80211_dump_wiphy_done,
13903                /* can be retrieved by unprivileged users */
13904                .internal_flags = NL80211_FLAG_NEED_WIPHY |
13905                                  NL80211_FLAG_NEED_RTNL,
13906        },
13907        {
13908                .cmd = NL80211_CMD_SET_WIPHY,
13909                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13910                .doit = nl80211_set_wiphy,
13911                .flags = GENL_UNS_ADMIN_PERM,
13912                .internal_flags = NL80211_FLAG_NEED_RTNL,
13913        },
13914        {
13915                .cmd = NL80211_CMD_GET_INTERFACE,
13916                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13917                .doit = nl80211_get_interface,
13918                .dumpit = nl80211_dump_interface,
13919                /* can be retrieved by unprivileged users */
13920                .internal_flags = NL80211_FLAG_NEED_WDEV |
13921                                  NL80211_FLAG_NEED_RTNL,
13922        },
13923        {
13924                .cmd = NL80211_CMD_SET_INTERFACE,
13925                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13926                .doit = nl80211_set_interface,
13927                .flags = GENL_UNS_ADMIN_PERM,
13928                .internal_flags = NL80211_FLAG_NEED_NETDEV |
13929                                  NL80211_FLAG_NEED_RTNL,
13930        },
13931        {
13932                .cmd = NL80211_CMD_NEW_INTERFACE,
13933                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13934                .doit = nl80211_new_interface,
13935                .flags = GENL_UNS_ADMIN_PERM,
13936                .internal_flags = NL80211_FLAG_NEED_WIPHY |
13937                                  NL80211_FLAG_NEED_RTNL,
13938        },
13939        {
13940                .cmd = NL80211_CMD_DEL_INTERFACE,
13941                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13942                .doit = nl80211_del_interface,
13943                .flags = GENL_UNS_ADMIN_PERM,
13944                .internal_flags = NL80211_FLAG_NEED_WDEV |
13945                                  NL80211_FLAG_NEED_RTNL,
13946        },
13947        {
13948                .cmd = NL80211_CMD_GET_KEY,
13949                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13950                .doit = nl80211_get_key,
13951                .flags = GENL_UNS_ADMIN_PERM,
13952                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13953                                  NL80211_FLAG_NEED_RTNL,
13954        },
13955        {
13956                .cmd = NL80211_CMD_SET_KEY,
13957                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13958                .doit = nl80211_set_key,
13959                .flags = GENL_UNS_ADMIN_PERM,
13960                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13961                                  NL80211_FLAG_NEED_RTNL |
13962                                  NL80211_FLAG_CLEAR_SKB,
13963        },
13964        {
13965                .cmd = NL80211_CMD_NEW_KEY,
13966                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13967                .doit = nl80211_new_key,
13968                .flags = GENL_UNS_ADMIN_PERM,
13969                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13970                                  NL80211_FLAG_NEED_RTNL |
13971                                  NL80211_FLAG_CLEAR_SKB,
13972        },
13973        {
13974                .cmd = NL80211_CMD_DEL_KEY,
13975                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13976                .doit = nl80211_del_key,
13977                .flags = GENL_UNS_ADMIN_PERM,
13978                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13979                                  NL80211_FLAG_NEED_RTNL,
13980        },
13981        {
13982                .cmd = NL80211_CMD_SET_BEACON,
13983                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13984                .flags = GENL_UNS_ADMIN_PERM,
13985                .doit = nl80211_set_beacon,
13986                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13987                                  NL80211_FLAG_NEED_RTNL,
13988        },
13989        {
13990                .cmd = NL80211_CMD_START_AP,
13991                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13992                .flags = GENL_UNS_ADMIN_PERM,
13993                .doit = nl80211_start_ap,
13994                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13995                                  NL80211_FLAG_NEED_RTNL,
13996        },
13997        {
13998                .cmd = NL80211_CMD_STOP_AP,
13999                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14000                .flags = GENL_UNS_ADMIN_PERM,
14001                .doit = nl80211_stop_ap,
14002                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14003                                  NL80211_FLAG_NEED_RTNL,
14004        },
14005        {
14006                .cmd = NL80211_CMD_GET_STATION,
14007                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14008                .doit = nl80211_get_station,
14009                .dumpit = nl80211_dump_station,
14010                .internal_flags = NL80211_FLAG_NEED_NETDEV |
14011                                  NL80211_FLAG_NEED_RTNL,
14012        },
14013        {
14014                .cmd = NL80211_CMD_SET_STATION,
14015                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14016                .doit = nl80211_set_station,
14017                .flags = GENL_UNS_ADMIN_PERM,
14018                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14019                                  NL80211_FLAG_NEED_RTNL,
14020        },
14021        {
14022                .cmd = NL80211_CMD_NEW_STATION,
14023                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14024                .doit = nl80211_new_station,
14025                .flags = GENL_UNS_ADMIN_PERM,
14026                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14027                                  NL80211_FLAG_NEED_RTNL,
14028        },
14029        {
14030                .cmd = NL80211_CMD_DEL_STATION,
14031                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14032                .doit = nl80211_del_station,
14033                .flags = GENL_UNS_ADMIN_PERM,
14034                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14035                                  NL80211_FLAG_NEED_RTNL,
14036        },
14037        {
14038                .cmd = NL80211_CMD_GET_MPATH,
14039                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14040                .doit = nl80211_get_mpath,
14041                .dumpit = nl80211_dump_mpath,
14042                .flags = GENL_UNS_ADMIN_PERM,
14043                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14044                                  NL80211_FLAG_NEED_RTNL,
14045        },
14046        {
14047                .cmd = NL80211_CMD_GET_MPP,
14048                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14049                .doit = nl80211_get_mpp,
14050                .dumpit = nl80211_dump_mpp,
14051                .flags = GENL_UNS_ADMIN_PERM,
14052                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14053                                  NL80211_FLAG_NEED_RTNL,
14054        },
14055        {
14056                .cmd = NL80211_CMD_SET_MPATH,
14057                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14058                .doit = nl80211_set_mpath,
14059                .flags = GENL_UNS_ADMIN_PERM,
14060                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14061                                  NL80211_FLAG_NEED_RTNL,
14062        },
14063        {
14064                .cmd = NL80211_CMD_NEW_MPATH,
14065                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14066                .doit = nl80211_new_mpath,
14067                .flags = GENL_UNS_ADMIN_PERM,
14068                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14069                                  NL80211_FLAG_NEED_RTNL,
14070        },
14071        {
14072                .cmd = NL80211_CMD_DEL_MPATH,
14073                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14074                .doit = nl80211_del_mpath,
14075                .flags = GENL_UNS_ADMIN_PERM,
14076                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14077                                  NL80211_FLAG_NEED_RTNL,
14078        },
14079        {
14080                .cmd = NL80211_CMD_SET_BSS,
14081                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14082                .doit = nl80211_set_bss,
14083                .flags = GENL_UNS_ADMIN_PERM,
14084                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14085                                  NL80211_FLAG_NEED_RTNL,
14086        },
14087        {
14088                .cmd = NL80211_CMD_GET_REG,
14089                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14090                .doit = nl80211_get_reg_do,
14091                .dumpit = nl80211_get_reg_dump,
14092                .internal_flags = NL80211_FLAG_NEED_RTNL,
14093                /* can be retrieved by unprivileged users */
14094        },
14095#ifdef CONFIG_CFG80211_CRDA_SUPPORT
14096        {
14097                .cmd = NL80211_CMD_SET_REG,
14098                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14099                .doit = nl80211_set_reg,
14100                .flags = GENL_ADMIN_PERM,
14101                .internal_flags = NL80211_FLAG_NEED_RTNL,
14102        },
14103#endif
14104        {
14105                .cmd = NL80211_CMD_REQ_SET_REG,
14106                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14107                .doit = nl80211_req_set_reg,
14108                .flags = GENL_ADMIN_PERM,
14109        },
14110        {
14111                .cmd = NL80211_CMD_RELOAD_REGDB,
14112                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14113                .doit = nl80211_reload_regdb,
14114                .flags = GENL_ADMIN_PERM,
14115        },
14116        {
14117                .cmd = NL80211_CMD_GET_MESH_CONFIG,
14118                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14119                .doit = nl80211_get_mesh_config,
14120                /* can be retrieved by unprivileged users */
14121                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14122                                  NL80211_FLAG_NEED_RTNL,
14123        },
14124        {
14125                .cmd = NL80211_CMD_SET_MESH_CONFIG,
14126                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14127                .doit = nl80211_update_mesh_config,
14128                .flags = GENL_UNS_ADMIN_PERM,
14129                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14130                                  NL80211_FLAG_NEED_RTNL,
14131        },
14132        {
14133                .cmd = NL80211_CMD_TRIGGER_SCAN,
14134                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14135                .doit = nl80211_trigger_scan,
14136                .flags = GENL_UNS_ADMIN_PERM,
14137                .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14138                                  NL80211_FLAG_NEED_RTNL,
14139        },
14140        {
14141                .cmd = NL80211_CMD_ABORT_SCAN,
14142                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14143                .doit = nl80211_abort_scan,
14144                .flags = GENL_UNS_ADMIN_PERM,
14145                .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14146                                  NL80211_FLAG_NEED_RTNL,
14147        },
14148        {
14149                .cmd = NL80211_CMD_GET_SCAN,
14150                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14151                .dumpit = nl80211_dump_scan,
14152        },
14153        {
14154                .cmd = NL80211_CMD_START_SCHED_SCAN,
14155                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14156                .doit = nl80211_start_sched_scan,
14157                .flags = GENL_UNS_ADMIN_PERM,
14158                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14159                                  NL80211_FLAG_NEED_RTNL,
14160        },
14161        {
14162                .cmd = NL80211_CMD_STOP_SCHED_SCAN,
14163                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14164                .doit = nl80211_stop_sched_scan,
14165                .flags = GENL_UNS_ADMIN_PERM,
14166                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14167                                  NL80211_FLAG_NEED_RTNL,
14168        },
14169        {
14170                .cmd = NL80211_CMD_AUTHENTICATE,
14171                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14172                .doit = nl80211_authenticate,
14173                .flags = GENL_UNS_ADMIN_PERM,
14174                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14175                                  NL80211_FLAG_NEED_RTNL |
14176                                  NL80211_FLAG_CLEAR_SKB,
14177        },
14178        {
14179                .cmd = NL80211_CMD_ASSOCIATE,
14180                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14181                .doit = nl80211_associate,
14182                .flags = GENL_UNS_ADMIN_PERM,
14183                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14184                                  NL80211_FLAG_NEED_RTNL |
14185                                  NL80211_FLAG_CLEAR_SKB,
14186        },
14187        {
14188                .cmd = NL80211_CMD_DEAUTHENTICATE,
14189                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14190                .doit = nl80211_deauthenticate,
14191                .flags = GENL_UNS_ADMIN_PERM,
14192                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14193                                  NL80211_FLAG_NEED_RTNL,
14194        },
14195        {
14196                .cmd = NL80211_CMD_DISASSOCIATE,
14197                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14198                .doit = nl80211_disassociate,
14199                .flags = GENL_UNS_ADMIN_PERM,
14200                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14201                                  NL80211_FLAG_NEED_RTNL,
14202        },
14203        {
14204                .cmd = NL80211_CMD_JOIN_IBSS,
14205                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14206                .doit = nl80211_join_ibss,
14207                .flags = GENL_UNS_ADMIN_PERM,
14208                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14209                                  NL80211_FLAG_NEED_RTNL,
14210        },
14211        {
14212                .cmd = NL80211_CMD_LEAVE_IBSS,
14213                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14214                .doit = nl80211_leave_ibss,
14215                .flags = GENL_UNS_ADMIN_PERM,
14216                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14217                                  NL80211_FLAG_NEED_RTNL,
14218        },
14219#ifdef CONFIG_NL80211_TESTMODE
14220        {
14221                .cmd = NL80211_CMD_TESTMODE,
14222                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14223                .doit = nl80211_testmode_do,
14224                .dumpit = nl80211_testmode_dump,
14225                .flags = GENL_UNS_ADMIN_PERM,
14226                .internal_flags = NL80211_FLAG_NEED_WIPHY |
14227                                  NL80211_FLAG_NEED_RTNL,
14228        },
14229#endif
14230        {
14231                .cmd = NL80211_CMD_CONNECT,
14232                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14233                .doit = nl80211_connect,
14234                .flags = GENL_UNS_ADMIN_PERM,
14235                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14236                                  NL80211_FLAG_NEED_RTNL |
14237                                  NL80211_FLAG_CLEAR_SKB,
14238        },
14239        {
14240                .cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
14241                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14242                .doit = nl80211_update_connect_params,
14243                .flags = GENL_ADMIN_PERM,
14244                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14245                                  NL80211_FLAG_NEED_RTNL |
14246                                  NL80211_FLAG_CLEAR_SKB,
14247        },
14248        {
14249                .cmd = NL80211_CMD_DISCONNECT,
14250                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14251                .doit = nl80211_disconnect,
14252                .flags = GENL_UNS_ADMIN_PERM,
14253                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14254                                  NL80211_FLAG_NEED_RTNL,
14255        },
14256        {
14257                .cmd = NL80211_CMD_SET_WIPHY_NETNS,
14258                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14259                .doit = nl80211_wiphy_netns,
14260                .flags = GENL_UNS_ADMIN_PERM,
14261                .internal_flags = NL80211_FLAG_NEED_WIPHY |
14262                                  NL80211_FLAG_NEED_RTNL,
14263        },
14264        {
14265                .cmd = NL80211_CMD_GET_SURVEY,
14266                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14267                .dumpit = nl80211_dump_survey,
14268        },
14269        {
14270                .cmd = NL80211_CMD_SET_PMKSA,
14271                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14272                .doit = nl80211_setdel_pmksa,
14273                .flags = GENL_UNS_ADMIN_PERM,
14274                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14275                                  NL80211_FLAG_NEED_RTNL |
14276                                  NL80211_FLAG_CLEAR_SKB,
14277        },
14278        {
14279                .cmd = NL80211_CMD_DEL_PMKSA,
14280                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14281                .doit = nl80211_setdel_pmksa,
14282                .flags = GENL_UNS_ADMIN_PERM,
14283                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14284                                  NL80211_FLAG_NEED_RTNL,
14285        },
14286        {
14287                .cmd = NL80211_CMD_FLUSH_PMKSA,
14288                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14289                .doit = nl80211_flush_pmksa,
14290                .flags = GENL_UNS_ADMIN_PERM,
14291                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14292                                  NL80211_FLAG_NEED_RTNL,
14293        },
14294        {
14295                .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
14296                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14297                .doit = nl80211_remain_on_channel,
14298                .flags = GENL_UNS_ADMIN_PERM,
14299                .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14300                                  NL80211_FLAG_NEED_RTNL,
14301        },
14302        {
14303                .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
14304                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14305                .doit = nl80211_cancel_remain_on_channel,
14306                .flags = GENL_UNS_ADMIN_PERM,
14307                .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14308                                  NL80211_FLAG_NEED_RTNL,
14309        },
14310        {
14311                .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
14312                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14313                .doit = nl80211_set_tx_bitrate_mask,
14314                .flags = GENL_UNS_ADMIN_PERM,
14315                .internal_flags = NL80211_FLAG_NEED_NETDEV |
14316                                  NL80211_FLAG_NEED_RTNL,
14317        },
14318        {
14319                .cmd = NL80211_CMD_REGISTER_FRAME,
14320                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14321                .doit = nl80211_register_mgmt,
14322                .flags = GENL_UNS_ADMIN_PERM,
14323                .internal_flags = NL80211_FLAG_NEED_WDEV |
14324                                  NL80211_FLAG_NEED_RTNL,
14325        },
14326        {
14327                .cmd = NL80211_CMD_FRAME,
14328                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14329                .doit = nl80211_tx_mgmt,
14330                .flags = GENL_UNS_ADMIN_PERM,
14331                .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14332                                  NL80211_FLAG_NEED_RTNL,
14333        },
14334        {
14335                .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
14336                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14337                .doit = nl80211_tx_mgmt_cancel_wait,
14338                .flags = GENL_UNS_ADMIN_PERM,
14339                .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14340                                  NL80211_FLAG_NEED_RTNL,
14341        },
14342        {
14343                .cmd = NL80211_CMD_SET_POWER_SAVE,
14344                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14345                .doit = nl80211_set_power_save,
14346                .flags = GENL_UNS_ADMIN_PERM,
14347                .internal_flags = NL80211_FLAG_NEED_NETDEV |
14348                                  NL80211_FLAG_NEED_RTNL,
14349        },
14350        {
14351                .cmd = NL80211_CMD_GET_POWER_SAVE,
14352                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14353                .doit = nl80211_get_power_save,
14354                /* can be retrieved by unprivileged users */
14355                .internal_flags = NL80211_FLAG_NEED_NETDEV |
14356                                  NL80211_FLAG_NEED_RTNL,
14357        },
14358        {
14359                .cmd = NL80211_CMD_SET_CQM,
14360                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14361                .doit = nl80211_set_cqm,
14362                .flags = GENL_UNS_ADMIN_PERM,
14363                .internal_flags = NL80211_FLAG_NEED_NETDEV |
14364                                  NL80211_FLAG_NEED_RTNL,
14365        },
14366        {
14367                .cmd = NL80211_CMD_SET_CHANNEL,
14368                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14369                .doit = nl80211_set_channel,
14370                .flags = GENL_UNS_ADMIN_PERM,
14371                .internal_flags = NL80211_FLAG_NEED_NETDEV |
14372                                  NL80211_FLAG_NEED_RTNL,
14373        },
14374        {
14375                .cmd = NL80211_CMD_SET_WDS_PEER,
14376                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14377                .doit = nl80211_set_wds_peer,
14378                .flags = GENL_UNS_ADMIN_PERM,
14379                .internal_flags = NL80211_FLAG_NEED_NETDEV |
14380                                  NL80211_FLAG_NEED_RTNL,
14381        },
14382        {
14383                .cmd = NL80211_CMD_JOIN_MESH,
14384                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14385                .doit = nl80211_join_mesh,
14386                .flags = GENL_UNS_ADMIN_PERM,
14387                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14388                                  NL80211_FLAG_NEED_RTNL,
14389        },
14390        {
14391                .cmd = NL80211_CMD_LEAVE_MESH,
14392                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14393                .doit = nl80211_leave_mesh,
14394                .flags = GENL_UNS_ADMIN_PERM,
14395                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14396                                  NL80211_FLAG_NEED_RTNL,
14397        },
14398        {
14399                .cmd = NL80211_CMD_JOIN_OCB,
14400                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14401                .doit = nl80211_join_ocb,
14402                .flags = GENL_UNS_ADMIN_PERM,
14403                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14404                                  NL80211_FLAG_NEED_RTNL,
14405        },
14406        {
14407                .cmd = NL80211_CMD_LEAVE_OCB,
14408                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14409                .doit = nl80211_leave_ocb,
14410                .flags = GENL_UNS_ADMIN_PERM,
14411                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14412                                  NL80211_FLAG_NEED_RTNL,
14413        },
14414#ifdef CONFIG_PM
14415        {
14416                .cmd = NL80211_CMD_GET_WOWLAN,
14417                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14418                .doit = nl80211_get_wowlan,
14419                /* can be retrieved by unprivileged users */
14420                .internal_flags = NL80211_FLAG_NEED_WIPHY |
14421                                  NL80211_FLAG_NEED_RTNL,
14422        },
14423        {
14424                .cmd = NL80211_CMD_SET_WOWLAN,
14425                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14426                .doit = nl80211_set_wowlan,
14427                .flags = GENL_UNS_ADMIN_PERM,
14428                .internal_flags = NL80211_FLAG_NEED_WIPHY |
14429                                  NL80211_FLAG_NEED_RTNL,
14430        },
14431#endif
14432        {
14433                .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
14434                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14435                .doit = nl80211_set_rekey_data,
14436                .flags = GENL_UNS_ADMIN_PERM,
14437                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14438                                  NL80211_FLAG_NEED_RTNL |
14439                                  NL80211_FLAG_CLEAR_SKB,
14440        },
14441        {
14442                .cmd = NL80211_CMD_TDLS_MGMT,
14443                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14444                .doit = nl80211_tdls_mgmt,
14445                .flags = GENL_UNS_ADMIN_PERM,
14446                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14447                                  NL80211_FLAG_NEED_RTNL,
14448        },
14449        {
14450                .cmd = NL80211_CMD_TDLS_OPER,
14451                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14452                .doit = nl80211_tdls_oper,
14453                .flags = GENL_UNS_ADMIN_PERM,
14454                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14455                                  NL80211_FLAG_NEED_RTNL,
14456        },
14457        {
14458                .cmd = NL80211_CMD_UNEXPECTED_FRAME,
14459                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14460                .doit = nl80211_register_unexpected_frame,
14461                .flags = GENL_UNS_ADMIN_PERM,
14462                .internal_flags = NL80211_FLAG_NEED_NETDEV |
14463                                  NL80211_FLAG_NEED_RTNL,
14464        },
14465        {
14466                .cmd = NL80211_CMD_PROBE_CLIENT,
14467                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14468                .doit = nl80211_probe_client,
14469                .flags = GENL_UNS_ADMIN_PERM,
14470                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14471                                  NL80211_FLAG_NEED_RTNL,
14472        },
14473        {
14474                .cmd = NL80211_CMD_REGISTER_BEACONS,
14475                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14476                .doit = nl80211_register_beacons,
14477                .flags = GENL_UNS_ADMIN_PERM,
14478                .internal_flags = NL80211_FLAG_NEED_WIPHY |
14479                                  NL80211_FLAG_NEED_RTNL,
14480        },
14481        {
14482                .cmd = NL80211_CMD_SET_NOACK_MAP,
14483                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14484                .doit = nl80211_set_noack_map,
14485                .flags = GENL_UNS_ADMIN_PERM,
14486                .internal_flags = NL80211_FLAG_NEED_NETDEV |
14487                                  NL80211_FLAG_NEED_RTNL,
14488        },
14489        {
14490                .cmd = NL80211_CMD_START_P2P_DEVICE,
14491                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14492                .doit = nl80211_start_p2p_device,
14493                .flags = GENL_UNS_ADMIN_PERM,
14494                .internal_flags = NL80211_FLAG_NEED_WDEV |
14495                                  NL80211_FLAG_NEED_RTNL,
14496        },
14497        {
14498                .cmd = NL80211_CMD_STOP_P2P_DEVICE,
14499                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14500                .doit = nl80211_stop_p2p_device,
14501                .flags = GENL_UNS_ADMIN_PERM,
14502                .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14503                                  NL80211_FLAG_NEED_RTNL,
14504        },
14505        {
14506                .cmd = NL80211_CMD_START_NAN,
14507                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14508                .doit = nl80211_start_nan,
14509                .flags = GENL_ADMIN_PERM,
14510                .internal_flags = NL80211_FLAG_NEED_WDEV |
14511                                  NL80211_FLAG_NEED_RTNL,
14512        },
14513        {
14514                .cmd = NL80211_CMD_STOP_NAN,
14515                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14516                .doit = nl80211_stop_nan,
14517                .flags = GENL_ADMIN_PERM,
14518                .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14519                                  NL80211_FLAG_NEED_RTNL,
14520        },
14521        {
14522                .cmd = NL80211_CMD_ADD_NAN_FUNCTION,
14523                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14524                .doit = nl80211_nan_add_func,
14525                .flags = GENL_ADMIN_PERM,
14526                .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14527                                  NL80211_FLAG_NEED_RTNL,
14528        },
14529        {
14530                .cmd = NL80211_CMD_DEL_NAN_FUNCTION,
14531                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14532                .doit = nl80211_nan_del_func,
14533                .flags = GENL_ADMIN_PERM,
14534                .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14535                                  NL80211_FLAG_NEED_RTNL,
14536        },
14537        {
14538                .cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
14539                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14540                .doit = nl80211_nan_change_config,
14541                .flags = GENL_ADMIN_PERM,
14542                .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14543                                  NL80211_FLAG_NEED_RTNL,
14544        },
14545        {
14546                .cmd = NL80211_CMD_SET_MCAST_RATE,
14547                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14548                .doit = nl80211_set_mcast_rate,
14549                .flags = GENL_UNS_ADMIN_PERM,
14550                .internal_flags = NL80211_FLAG_NEED_NETDEV |
14551                                  NL80211_FLAG_NEED_RTNL,
14552        },
14553        {
14554                .cmd = NL80211_CMD_SET_MAC_ACL,
14555                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14556                .doit = nl80211_set_mac_acl,
14557                .flags = GENL_UNS_ADMIN_PERM,
14558                .internal_flags = NL80211_FLAG_NEED_NETDEV |
14559                                  NL80211_FLAG_NEED_RTNL,
14560        },
14561        {
14562                .cmd = NL80211_CMD_RADAR_DETECT,
14563                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14564                .doit = nl80211_start_radar_detection,
14565                .flags = GENL_UNS_ADMIN_PERM,
14566                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14567                                  NL80211_FLAG_NEED_RTNL,
14568        },
14569        {
14570                .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
14571                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14572                .doit = nl80211_get_protocol_features,
14573        },
14574        {
14575                .cmd = NL80211_CMD_UPDATE_FT_IES,
14576                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14577                .doit = nl80211_update_ft_ies,
14578                .flags = GENL_UNS_ADMIN_PERM,
14579                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14580                                  NL80211_FLAG_NEED_RTNL,
14581        },
14582        {
14583                .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
14584                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14585                .doit = nl80211_crit_protocol_start,
14586                .flags = GENL_UNS_ADMIN_PERM,
14587                .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14588                                  NL80211_FLAG_NEED_RTNL,
14589        },
14590        {
14591                .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
14592                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14593                .doit = nl80211_crit_protocol_stop,
14594                .flags = GENL_UNS_ADMIN_PERM,
14595                .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14596                                  NL80211_FLAG_NEED_RTNL,
14597        },
14598        {
14599                .cmd = NL80211_CMD_GET_COALESCE,
14600                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14601                .doit = nl80211_get_coalesce,
14602                .internal_flags = NL80211_FLAG_NEED_WIPHY |
14603                                  NL80211_FLAG_NEED_RTNL,
14604        },
14605        {
14606                .cmd = NL80211_CMD_SET_COALESCE,
14607                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14608                .doit = nl80211_set_coalesce,
14609                .flags = GENL_UNS_ADMIN_PERM,
14610                .internal_flags = NL80211_FLAG_NEED_WIPHY |
14611                                  NL80211_FLAG_NEED_RTNL,
14612        },
14613        {
14614                .cmd = NL80211_CMD_CHANNEL_SWITCH,
14615                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14616                .doit = nl80211_channel_switch,
14617                .flags = GENL_UNS_ADMIN_PERM,
14618                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14619                                  NL80211_FLAG_NEED_RTNL,
14620        },
14621        {
14622                .cmd = NL80211_CMD_VENDOR,
14623                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14624                .doit = nl80211_vendor_cmd,
14625                .dumpit = nl80211_vendor_cmd_dump,
14626                .flags = GENL_UNS_ADMIN_PERM,
14627                .internal_flags = NL80211_FLAG_NEED_WIPHY |
14628                                  NL80211_FLAG_NEED_RTNL |
14629                                  NL80211_FLAG_CLEAR_SKB,
14630        },
14631        {
14632                .cmd = NL80211_CMD_SET_QOS_MAP,
14633                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14634                .doit = nl80211_set_qos_map,
14635                .flags = GENL_UNS_ADMIN_PERM,
14636                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14637                                  NL80211_FLAG_NEED_RTNL,
14638        },
14639        {
14640                .cmd = NL80211_CMD_ADD_TX_TS,
14641                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14642                .doit = nl80211_add_tx_ts,
14643                .flags = GENL_UNS_ADMIN_PERM,
14644                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14645                                  NL80211_FLAG_NEED_RTNL,
14646        },
14647        {
14648                .cmd = NL80211_CMD_DEL_TX_TS,
14649                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14650                .doit = nl80211_del_tx_ts,
14651                .flags = GENL_UNS_ADMIN_PERM,
14652                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14653                                  NL80211_FLAG_NEED_RTNL,
14654        },
14655        {
14656                .cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
14657                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14658                .doit = nl80211_tdls_channel_switch,
14659                .flags = GENL_UNS_ADMIN_PERM,
14660                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14661                                  NL80211_FLAG_NEED_RTNL,
14662        },
14663        {
14664                .cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
14665                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14666                .doit = nl80211_tdls_cancel_channel_switch,
14667                .flags = GENL_UNS_ADMIN_PERM,
14668                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14669                                  NL80211_FLAG_NEED_RTNL,
14670        },
14671        {
14672                .cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
14673                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14674                .doit = nl80211_set_multicast_to_unicast,
14675                .flags = GENL_UNS_ADMIN_PERM,
14676                .internal_flags = NL80211_FLAG_NEED_NETDEV |
14677                                  NL80211_FLAG_NEED_RTNL,
14678        },
14679        {
14680                .cmd = NL80211_CMD_SET_PMK,
14681                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14682                .doit = nl80211_set_pmk,
14683                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14684                                  NL80211_FLAG_NEED_RTNL |
14685                                  NL80211_FLAG_CLEAR_SKB,
14686        },
14687        {
14688                .cmd = NL80211_CMD_DEL_PMK,
14689                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14690                .doit = nl80211_del_pmk,
14691                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14692                                  NL80211_FLAG_NEED_RTNL,
14693        },
14694        {
14695                .cmd = NL80211_CMD_EXTERNAL_AUTH,
14696                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14697                .doit = nl80211_external_auth,
14698                .flags = GENL_ADMIN_PERM,
14699                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14700                                  NL80211_FLAG_NEED_RTNL,
14701        },
14702        {
14703                .cmd = NL80211_CMD_CONTROL_PORT_FRAME,
14704                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14705                .doit = nl80211_tx_control_port,
14706                .flags = GENL_UNS_ADMIN_PERM,
14707                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14708                                  NL80211_FLAG_NEED_RTNL,
14709        },
14710        {
14711                .cmd = NL80211_CMD_GET_FTM_RESPONDER_STATS,
14712                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14713                .doit = nl80211_get_ftm_responder_stats,
14714                .internal_flags = NL80211_FLAG_NEED_NETDEV |
14715                                  NL80211_FLAG_NEED_RTNL,
14716        },
14717        {
14718                .cmd = NL80211_CMD_PEER_MEASUREMENT_START,
14719                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14720                .doit = nl80211_pmsr_start,
14721                .flags = GENL_UNS_ADMIN_PERM,
14722                .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14723                                  NL80211_FLAG_NEED_RTNL,
14724        },
14725        {
14726                .cmd = NL80211_CMD_NOTIFY_RADAR,
14727                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14728                .doit = nl80211_notify_radar_detection,
14729                .flags = GENL_UNS_ADMIN_PERM,
14730                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14731                                  NL80211_FLAG_NEED_RTNL,
14732        },
14733        {
14734                .cmd = NL80211_CMD_UPDATE_OWE_INFO,
14735                .doit = nl80211_update_owe_info,
14736                .flags = GENL_ADMIN_PERM,
14737                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14738                                  NL80211_FLAG_NEED_RTNL,
14739        },
14740        {
14741                .cmd = NL80211_CMD_PROBE_MESH_LINK,
14742                .doit = nl80211_probe_mesh_link,
14743                .flags = GENL_UNS_ADMIN_PERM,
14744                .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14745                                  NL80211_FLAG_NEED_RTNL,
14746        },
14747};
14748
14749static struct genl_family nl80211_fam __ro_after_init = {
14750        .name = NL80211_GENL_NAME,      /* have users key off the name instead */
14751        .hdrsize = 0,                   /* no private header */
14752        .version = 1,                   /* no particular meaning now */
14753        .maxattr = NL80211_ATTR_MAX,
14754        .policy = nl80211_policy,
14755        .netnsok = true,
14756        .pre_doit = nl80211_pre_doit,
14757        .post_doit = nl80211_post_doit,
14758        .module = THIS_MODULE,
14759        .ops = nl80211_ops,
14760        .n_ops = ARRAY_SIZE(nl80211_ops),
14761        .mcgrps = nl80211_mcgrps,
14762        .n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
14763        .parallel_ops = true,
14764};
14765
14766/* notification functions */
14767
14768void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
14769                          enum nl80211_commands cmd)
14770{
14771        struct sk_buff *msg;
14772        struct nl80211_dump_wiphy_state state = {};
14773
14774        WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
14775                cmd != NL80211_CMD_DEL_WIPHY);
14776
14777        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14778        if (!msg)
14779                return;
14780
14781        if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
14782                nlmsg_free(msg);
14783                return;
14784        }
14785
14786        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14787                                NL80211_MCGRP_CONFIG, GFP_KERNEL);
14788}
14789
14790void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
14791                                struct wireless_dev *wdev,
14792                                enum nl80211_commands cmd)
14793{
14794        struct sk_buff *msg;
14795
14796        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14797        if (!msg)
14798                return;
14799
14800        if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, cmd) < 0) {
14801                nlmsg_free(msg);
14802                return;
14803        }
14804
14805        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14806                                NL80211_MCGRP_CONFIG, GFP_KERNEL);
14807}
14808
14809static int nl80211_add_scan_req(struct sk_buff *msg,
14810                                struct cfg80211_registered_device *rdev)
14811{
14812        struct cfg80211_scan_request *req = rdev->scan_req;
14813        struct nlattr *nest;
14814        int i;
14815
14816        if (WARN_ON(!req))
14817                return 0;
14818
14819        nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_SSIDS);
14820        if (!nest)
14821                goto nla_put_failure;
14822        for (i = 0; i < req->n_ssids; i++) {
14823                if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
14824                        goto nla_put_failure;
14825        }
14826        nla_nest_end(msg, nest);
14827
14828        nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
14829        if (!nest)
14830                goto nla_put_failure;
14831        for (i = 0; i < req->n_channels; i++) {
14832                if (nla_put_u32(msg, i, req->channels[i]->center_freq))
14833                        goto nla_put_failure;
14834        }
14835        nla_nest_end(msg, nest);
14836
14837        if (req->ie &&
14838            nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
14839                goto nla_put_failure;
14840
14841        if (req->flags &&
14842            nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
14843                goto nla_put_failure;
14844
14845        if (req->info.scan_start_tsf &&
14846            (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
14847                               req->info.scan_start_tsf, NL80211_BSS_PAD) ||
14848             nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
14849                     req->info.tsf_bssid)))
14850                goto nla_put_failure;
14851
14852        return 0;
14853 nla_put_failure:
14854        return -ENOBUFS;
14855}
14856
14857static int nl80211_prep_scan_msg(struct sk_buff *msg,
14858                                 struct cfg80211_registered_device *rdev,
14859                                 struct wireless_dev *wdev,
14860                                 u32 portid, u32 seq, int flags,
14861                                 u32 cmd)
14862{
14863        void *hdr;
14864
14865        hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
14866        if (!hdr)
14867                return -1;
14868
14869        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14870            (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14871                                         wdev->netdev->ifindex)) ||
14872            nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14873                              NL80211_ATTR_PAD))
14874                goto nla_put_failure;
14875
14876        /* ignore errors and send incomplete event anyway */
14877        nl80211_add_scan_req(msg, rdev);
14878
14879        genlmsg_end(msg, hdr);
14880        return 0;
14881
14882 nla_put_failure:
14883        genlmsg_cancel(msg, hdr);
14884        return -EMSGSIZE;
14885}
14886
14887static int
14888nl80211_prep_sched_scan_msg(struct sk_buff *msg,
14889                            struct cfg80211_sched_scan_request *req, u32 cmd)
14890{
14891        void *hdr;
14892
14893        hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
14894        if (!hdr)
14895                return -1;
14896
14897        if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
14898                        wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
14899            nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
14900            nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
14901                              NL80211_ATTR_PAD))
14902                goto nla_put_failure;
14903
14904        genlmsg_end(msg, hdr);
14905        return 0;
14906
14907 nla_put_failure:
14908        genlmsg_cancel(msg, hdr);
14909        return -EMSGSIZE;
14910}
14911
14912void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
14913                             struct wireless_dev *wdev)
14914{
14915        struct sk_buff *msg;
14916
14917        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14918        if (!msg)
14919                return;
14920
14921        if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
14922                                  NL80211_CMD_TRIGGER_SCAN) < 0) {
14923                nlmsg_free(msg);
14924                return;
14925        }
14926
14927        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14928                                NL80211_MCGRP_SCAN, GFP_KERNEL);
14929}
14930
14931struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
14932                                       struct wireless_dev *wdev, bool aborted)
14933{
14934        struct sk_buff *msg;
14935
14936        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14937        if (!msg)
14938                return NULL;
14939
14940        if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
14941                                  aborted ? NL80211_CMD_SCAN_ABORTED :
14942                                            NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
14943                nlmsg_free(msg);
14944                return NULL;
14945        }
14946
14947        return msg;
14948}
14949
14950/* send message created by nl80211_build_scan_msg() */
14951void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
14952                           struct sk_buff *msg)
14953{
14954        if (!msg)
14955                return;
14956
14957        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14958                                NL80211_MCGRP_SCAN, GFP_KERNEL);
14959}
14960
14961void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
14962{
14963        struct sk_buff *msg;
14964
14965        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14966        if (!msg)
14967                return;
14968
14969        if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
14970                nlmsg_free(msg);
14971                return;
14972        }
14973
14974        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
14975                                NL80211_MCGRP_SCAN, GFP_KERNEL);
14976}
14977
14978static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
14979                                          struct regulatory_request *request)
14980{
14981        /* Userspace can always count this one always being set */
14982        if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
14983                goto nla_put_failure;
14984
14985        if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
14986                if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
14987                               NL80211_REGDOM_TYPE_WORLD))
14988                        goto nla_put_failure;
14989        } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
14990                if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
14991                               NL80211_REGDOM_TYPE_CUSTOM_WORLD))
14992                        goto nla_put_failure;
14993        } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
14994                   request->intersect) {
14995                if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
14996                               NL80211_REGDOM_TYPE_INTERSECTION))
14997                        goto nla_put_failure;
14998        } else {
14999                if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
15000                               NL80211_REGDOM_TYPE_COUNTRY) ||
15001                    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
15002                                   request->alpha2))
15003                        goto nla_put_failure;
15004        }
15005
15006        if (request->wiphy_idx != WIPHY_IDX_INVALID) {
15007                struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
15008
15009                if (wiphy &&
15010                    nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
15011                        goto nla_put_failure;
15012
15013                if (wiphy &&
15014                    wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
15015                    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
15016                        goto nla_put_failure;
15017        }
15018
15019        return true;
15020
15021nla_put_failure:
15022        return false;
15023}
15024
15025/*
15026 * This can happen on global regulatory changes or device specific settings
15027 * based on custom regulatory domains.
15028 */
15029void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
15030                                     struct regulatory_request *request)
15031{
15032        struct sk_buff *msg;
15033        void *hdr;
15034
15035        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15036        if (!msg)
15037                return;
15038
15039        hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
15040        if (!hdr)
15041                goto nla_put_failure;
15042
15043        if (!nl80211_reg_change_event_fill(msg, request))
15044                goto nla_put_failure;
15045
15046        genlmsg_end(msg, hdr);
15047
15048        rcu_read_lock();
15049        genlmsg_multicast_allns(&nl80211_fam, msg, 0,
15050                                NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
15051        rcu_read_unlock();
15052
15053        return;
15054
15055nla_put_failure:
15056        nlmsg_free(msg);
15057}
15058
15059static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
15060                                    struct net_device *netdev,
15061                                    const u8 *buf, size_t len,
15062                                    enum nl80211_commands cmd, gfp_t gfp,
15063                                    int uapsd_queues, const u8 *req_ies,
15064                                    size_t req_ies_len)
15065{
15066        struct sk_buff *msg;
15067        void *hdr;
15068
15069        msg = nlmsg_new(100 + len + req_ies_len, gfp);
15070        if (!msg)
15071                return;
15072
15073        hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15074        if (!hdr) {
15075                nlmsg_free(msg);
15076                return;
15077        }
15078
15079        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15080            nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15081            nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15082            (req_ies &&
15083             nla_put(msg, NL80211_ATTR_REQ_IE, req_ies_len, req_ies)))
15084                goto nla_put_failure;
15085
15086        if (uapsd_queues >= 0) {
15087                struct nlattr *nla_wmm =
15088                        nla_nest_start_noflag(msg, NL80211_ATTR_STA_WME);
15089                if (!nla_wmm)
15090                        goto nla_put_failure;
15091
15092                if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
15093                               uapsd_queues))
15094                        goto nla_put_failure;
15095
15096                nla_nest_end(msg, nla_wmm);
15097        }
15098
15099        genlmsg_end(msg, hdr);
15100
15101        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15102                                NL80211_MCGRP_MLME, gfp);
15103        return;
15104
15105 nla_put_failure:
15106        nlmsg_free(msg);
15107}
15108
15109void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
15110                          struct net_device *netdev, const u8 *buf,
15111                          size_t len, gfp_t gfp)
15112{
15113        nl80211_send_mlme_event(rdev, netdev, buf, len,
15114                                NL80211_CMD_AUTHENTICATE, gfp, -1, NULL, 0);
15115}
15116
15117void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
15118                           struct net_device *netdev, const u8 *buf,
15119                           size_t len, gfp_t gfp, int uapsd_queues,
15120                           const u8 *req_ies, size_t req_ies_len)
15121{
15122        nl80211_send_mlme_event(rdev, netdev, buf, len,
15123                                NL80211_CMD_ASSOCIATE, gfp, uapsd_queues,
15124                                req_ies, req_ies_len);
15125}
15126
15127void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
15128                         struct net_device *netdev, const u8 *buf,
15129                         size_t len, gfp_t gfp)
15130{
15131        nl80211_send_mlme_event(rdev, netdev, buf, len,
15132                                NL80211_CMD_DEAUTHENTICATE, gfp, -1, NULL, 0);
15133}
15134
15135void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
15136                           struct net_device *netdev, const u8 *buf,
15137                           size_t len, gfp_t gfp)
15138{
15139        nl80211_send_mlme_event(rdev, netdev, buf, len,
15140                                NL80211_CMD_DISASSOCIATE, gfp, -1, NULL, 0);
15141}
15142
15143void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
15144                                  size_t len)
15145{
15146        struct wireless_dev *wdev = dev->ieee80211_ptr;
15147        struct wiphy *wiphy = wdev->wiphy;
15148        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15149        const struct ieee80211_mgmt *mgmt = (void *)buf;
15150        u32 cmd;
15151
15152        if (WARN_ON(len < 2))
15153                return;
15154
15155        if (ieee80211_is_deauth(mgmt->frame_control))
15156                cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
15157        else
15158                cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
15159
15160        trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
15161        nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1,
15162                                NULL, 0);
15163}
15164EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
15165
15166static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
15167                                      struct net_device *netdev, int cmd,
15168                                      const u8 *addr, gfp_t gfp)
15169{
15170        struct sk_buff *msg;
15171        void *hdr;
15172
15173        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15174        if (!msg)
15175                return;
15176
15177        hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15178        if (!hdr) {
15179                nlmsg_free(msg);
15180                return;
15181        }
15182
15183        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15184            nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15185            nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
15186            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
15187                goto nla_put_failure;
15188
15189        genlmsg_end(msg, hdr);
15190
15191        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15192                                NL80211_MCGRP_MLME, gfp);
15193        return;
15194
15195 nla_put_failure:
15196        nlmsg_free(msg);
15197}
15198
15199void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
15200                               struct net_device *netdev, const u8 *addr,
15201                               gfp_t gfp)
15202{
15203        nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
15204                                  addr, gfp);
15205}
15206
15207void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
15208                                struct net_device *netdev, const u8 *addr,
15209                                gfp_t gfp)
15210{
15211        nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
15212                                  addr, gfp);
15213}
15214
15215void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
15216                                 struct net_device *netdev,
15217                                 struct cfg80211_connect_resp_params *cr,
15218                                 gfp_t gfp)
15219{
15220        struct sk_buff *msg;
15221        void *hdr;
15222
15223        msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
15224                        cr->fils.kek_len + cr->fils.pmk_len +
15225                        (cr->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
15226        if (!msg)
15227                return;
15228
15229        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
15230        if (!hdr) {
15231                nlmsg_free(msg);
15232                return;
15233        }
15234
15235        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15236            nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15237            (cr->bssid &&
15238             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, cr->bssid)) ||
15239            nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
15240                        cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
15241                        cr->status) ||
15242            (cr->status < 0 &&
15243             (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
15244              nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
15245                          cr->timeout_reason))) ||
15246            (cr->req_ie &&
15247             nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
15248            (cr->resp_ie &&
15249             nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
15250                     cr->resp_ie)) ||
15251            (cr->fils.update_erp_next_seq_num &&
15252             nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
15253                         cr->fils.erp_next_seq_num)) ||
15254            (cr->status == WLAN_STATUS_SUCCESS &&
15255             ((cr->fils.kek &&
15256               nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
15257                       cr->fils.kek)) ||
15258              (cr->fils.pmk &&
15259               nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
15260              (cr->fils.pmkid &&
15261               nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
15262                goto nla_put_failure;
15263
15264        genlmsg_end(msg, hdr);
15265
15266        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15267                                NL80211_MCGRP_MLME, gfp);
15268        return;
15269
15270 nla_put_failure:
15271        nlmsg_free(msg);
15272}
15273
15274void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
15275                         struct net_device *netdev,
15276                         struct cfg80211_roam_info *info, gfp_t gfp)
15277{
15278        struct sk_buff *msg;
15279        void *hdr;
15280        const u8 *bssid = info->bss ? info->bss->bssid : info->bssid;
15281
15282        msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
15283                        info->fils.kek_len + info->fils.pmk_len +
15284                        (info->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
15285        if (!msg)
15286                return;
15287
15288        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
15289        if (!hdr) {
15290                nlmsg_free(msg);
15291                return;
15292        }
15293
15294        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15295            nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15296            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
15297            (info->req_ie &&
15298             nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
15299                     info->req_ie)) ||
15300            (info->resp_ie &&
15301             nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
15302                     info->resp_ie)) ||
15303            (info->fils.update_erp_next_seq_num &&
15304             nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
15305                         info->fils.erp_next_seq_num)) ||
15306            (info->fils.kek &&
15307             nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
15308                     info->fils.kek)) ||
15309            (info->fils.pmk &&
15310             nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
15311            (info->fils.pmkid &&
15312             nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
15313                goto nla_put_failure;
15314
15315        genlmsg_end(msg, hdr);
15316
15317        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15318                                NL80211_MCGRP_MLME, gfp);
15319        return;
15320
15321 nla_put_failure:
15322        nlmsg_free(msg);
15323}
15324
15325void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
15326                                  struct net_device *netdev, const u8 *bssid)
15327{
15328        struct sk_buff *msg;
15329        void *hdr;
15330
15331        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15332        if (!msg)
15333                return;
15334
15335        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
15336        if (!hdr) {
15337                nlmsg_free(msg);
15338                return;
15339        }
15340
15341        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15342            nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15343            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
15344                goto nla_put_failure;
15345
15346        genlmsg_end(msg, hdr);
15347
15348        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15349                                NL80211_MCGRP_MLME, GFP_KERNEL);
15350        return;
15351
15352 nla_put_failure:
15353        nlmsg_free(msg);
15354}
15355
15356void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
15357                               struct net_device *netdev, u16 reason,
15358                               const u8 *ie, size_t ie_len, bool from_ap)
15359{
15360        struct sk_buff *msg;
15361        void *hdr;
15362
15363        msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
15364        if (!msg)
15365                return;
15366
15367        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
15368        if (!hdr) {
15369                nlmsg_free(msg);
15370                return;
15371        }
15372
15373        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15374            nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15375            (reason &&
15376             nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
15377            (from_ap &&
15378             nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
15379            (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
15380                goto nla_put_failure;
15381
15382        genlmsg_end(msg, hdr);
15383
15384        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15385                                NL80211_MCGRP_MLME, GFP_KERNEL);
15386        return;
15387
15388 nla_put_failure:
15389        nlmsg_free(msg);
15390}
15391
15392void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
15393                             struct net_device *netdev, const u8 *bssid,
15394                             gfp_t gfp)
15395{
15396        struct sk_buff *msg;
15397        void *hdr;
15398
15399        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15400        if (!msg)
15401                return;
15402
15403        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
15404        if (!hdr) {
15405                nlmsg_free(msg);
15406                return;
15407        }
15408
15409        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15410            nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15411            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
15412                goto nla_put_failure;
15413
15414        genlmsg_end(msg, hdr);
15415
15416        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15417                                NL80211_MCGRP_MLME, gfp);
15418        return;
15419
15420 nla_put_failure:
15421        nlmsg_free(msg);
15422}
15423
15424void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
15425                                        const u8 *ie, u8 ie_len,
15426                                        int sig_dbm, gfp_t gfp)
15427{
15428        struct wireless_dev *wdev = dev->ieee80211_ptr;
15429        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15430        struct sk_buff *msg;
15431        void *hdr;
15432
15433        if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
15434                return;
15435
15436        trace_cfg80211_notify_new_peer_candidate(dev, addr);
15437
15438        msg = nlmsg_new(100 + ie_len, gfp);
15439        if (!msg)
15440                return;
15441
15442        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
15443        if (!hdr) {
15444                nlmsg_free(msg);
15445                return;
15446        }
15447
15448        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15449            nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15450            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
15451            (ie_len && ie &&
15452             nla_put(msg, NL80211_ATTR_IE, ie_len, ie)) ||
15453            (sig_dbm &&
15454             nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)))
15455                goto nla_put_failure;
15456
15457        genlmsg_end(msg, hdr);
15458
15459        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15460                                NL80211_MCGRP_MLME, gfp);
15461        return;
15462
15463 nla_put_failure:
15464        nlmsg_free(msg);
15465}
15466EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
15467
15468void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
15469                                 struct net_device *netdev, const u8 *addr,
15470                                 enum nl80211_key_type key_type, int key_id,
15471                                 const u8 *tsc, gfp_t gfp)
15472{
15473        struct sk_buff *msg;
15474        void *hdr;
15475
15476        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15477        if (!msg)
15478                return;
15479
15480        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
15481        if (!hdr) {
15482                nlmsg_free(msg);
15483                return;
15484        }
15485
15486        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15487            nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15488            (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
15489            nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
15490            (key_id != -1 &&
15491             nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
15492            (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
15493                goto nla_put_failure;
15494
15495        genlmsg_end(msg, hdr);
15496
15497        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15498                                NL80211_MCGRP_MLME, gfp);
15499        return;
15500
15501 nla_put_failure:
15502        nlmsg_free(msg);
15503}
15504
15505void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
15506                                    struct ieee80211_channel *channel_before,
15507                                    struct ieee80211_channel *channel_after)
15508{
15509        struct sk_buff *msg;
15510        void *hdr;
15511        struct nlattr *nl_freq;
15512
15513        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
15514        if (!msg)
15515                return;
15516
15517        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
15518        if (!hdr) {
15519                nlmsg_free(msg);
15520                return;
15521        }
15522
15523        /*
15524         * Since we are applying the beacon hint to a wiphy we know its
15525         * wiphy_idx is valid
15526         */
15527        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
15528                goto nla_put_failure;
15529
15530        /* Before */
15531        nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_BEFORE);
15532        if (!nl_freq)
15533                goto nla_put_failure;
15534
15535        if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
15536                goto nla_put_failure;
15537        nla_nest_end(msg, nl_freq);
15538
15539        /* After */
15540        nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_AFTER);
15541        if (!nl_freq)
15542                goto nla_put_failure;
15543
15544        if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
15545                goto nla_put_failure;
15546        nla_nest_end(msg, nl_freq);
15547
15548        genlmsg_end(msg, hdr);
15549
15550        rcu_read_lock();
15551        genlmsg_multicast_allns(&nl80211_fam, msg, 0,
15552                                NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
15553        rcu_read_unlock();
15554
15555        return;
15556
15557nla_put_failure:
15558        nlmsg_free(msg);
15559}
15560
15561static void nl80211_send_remain_on_chan_event(
15562        int cmd, struct cfg80211_registered_device *rdev,
15563        struct wireless_dev *wdev, u64 cookie,
15564        struct ieee80211_channel *chan,
15565        unsigned int duration, gfp_t gfp)
15566{
15567        struct sk_buff *msg;
15568        void *hdr;
15569
15570        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15571        if (!msg)
15572                return;
15573
15574        hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15575        if (!hdr) {
15576                nlmsg_free(msg);
15577                return;
15578        }
15579
15580        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15581            (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15582                                         wdev->netdev->ifindex)) ||
15583            nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15584                              NL80211_ATTR_PAD) ||
15585            nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
15586            nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
15587                        NL80211_CHAN_NO_HT) ||
15588            nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
15589                              NL80211_ATTR_PAD))
15590                goto nla_put_failure;
15591
15592        if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
15593            nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
15594                goto nla_put_failure;
15595
15596        genlmsg_end(msg, hdr);
15597
15598        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15599                                NL80211_MCGRP_MLME, gfp);
15600        return;
15601
15602 nla_put_failure:
15603        nlmsg_free(msg);
15604}
15605
15606void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
15607                               struct ieee80211_channel *chan,
15608                               unsigned int duration, gfp_t gfp)
15609{
15610        struct wiphy *wiphy = wdev->wiphy;
15611        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15612
15613        trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
15614        nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
15615                                          rdev, wdev, cookie, chan,
15616                                          duration, gfp);
15617}
15618EXPORT_SYMBOL(cfg80211_ready_on_channel);
15619
15620void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
15621                                        struct ieee80211_channel *chan,
15622                                        gfp_t gfp)
15623{
15624        struct wiphy *wiphy = wdev->wiphy;
15625        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15626
15627        trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
15628        nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
15629                                          rdev, wdev, cookie, chan, 0, gfp);
15630}
15631EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
15632
15633void cfg80211_tx_mgmt_expired(struct wireless_dev *wdev, u64 cookie,
15634                                        struct ieee80211_channel *chan,
15635                                        gfp_t gfp)
15636{
15637        struct wiphy *wiphy = wdev->wiphy;
15638        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15639
15640        trace_cfg80211_tx_mgmt_expired(wdev, cookie, chan);
15641        nl80211_send_remain_on_chan_event(NL80211_CMD_FRAME_WAIT_CANCEL,
15642                                          rdev, wdev, cookie, chan, 0, gfp);
15643}
15644EXPORT_SYMBOL(cfg80211_tx_mgmt_expired);
15645
15646void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
15647                      struct station_info *sinfo, gfp_t gfp)
15648{
15649        struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
15650        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15651        struct sk_buff *msg;
15652
15653        trace_cfg80211_new_sta(dev, mac_addr, sinfo);
15654
15655        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15656        if (!msg)
15657                return;
15658
15659        if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
15660                                 rdev, dev, mac_addr, sinfo) < 0) {
15661                nlmsg_free(msg);
15662                return;
15663        }
15664
15665        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15666                                NL80211_MCGRP_MLME, gfp);
15667}
15668EXPORT_SYMBOL(cfg80211_new_sta);
15669
15670void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
15671                            struct station_info *sinfo, gfp_t gfp)
15672{
15673        struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
15674        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15675        struct sk_buff *msg;
15676        struct station_info empty_sinfo = {};
15677
15678        if (!sinfo)
15679                sinfo = &empty_sinfo;
15680
15681        trace_cfg80211_del_sta(dev, mac_addr);
15682
15683        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15684        if (!msg) {
15685                cfg80211_sinfo_release_content(sinfo);
15686                return;
15687        }
15688
15689        if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
15690                                 rdev, dev, mac_addr, sinfo) < 0) {
15691                nlmsg_free(msg);
15692                return;
15693        }
15694
15695        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15696                                NL80211_MCGRP_MLME, gfp);
15697}
15698EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
15699
15700void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
15701                          enum nl80211_connect_failed_reason reason,
15702                          gfp_t gfp)
15703{
15704        struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
15705        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15706        struct sk_buff *msg;
15707        void *hdr;
15708
15709        msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
15710        if (!msg)
15711                return;
15712
15713        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
15714        if (!hdr) {
15715                nlmsg_free(msg);
15716                return;
15717        }
15718
15719        if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15720            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
15721            nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
15722                goto nla_put_failure;
15723
15724        genlmsg_end(msg, hdr);
15725
15726        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15727                                NL80211_MCGRP_MLME, gfp);
15728        return;
15729
15730 nla_put_failure:
15731        nlmsg_free(msg);
15732}
15733EXPORT_SYMBOL(cfg80211_conn_failed);
15734
15735static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
15736                                       const u8 *addr, gfp_t gfp)
15737{
15738        struct wireless_dev *wdev = dev->ieee80211_ptr;
15739        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15740        struct sk_buff *msg;
15741        void *hdr;
15742        u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
15743
15744        if (!nlportid)
15745                return false;
15746
15747        msg = nlmsg_new(100, gfp);
15748        if (!msg)
15749                return true;
15750
15751        hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15752        if (!hdr) {
15753                nlmsg_free(msg);
15754                return true;
15755        }
15756
15757        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15758            nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15759            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
15760                goto nla_put_failure;
15761
15762        genlmsg_end(msg, hdr);
15763        genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15764        return true;
15765
15766 nla_put_failure:
15767        nlmsg_free(msg);
15768        return true;
15769}
15770
15771bool cfg80211_rx_spurious_frame(struct net_device *dev,
15772                                const u8 *addr, gfp_t gfp)
15773{
15774        struct wireless_dev *wdev = dev->ieee80211_ptr;
15775        bool ret;
15776
15777        trace_cfg80211_rx_spurious_frame(dev, addr);
15778
15779        if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
15780                    wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
15781                trace_cfg80211_return_bool(false);
15782                return false;
15783        }
15784        ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
15785                                         addr, gfp);
15786        trace_cfg80211_return_bool(ret);
15787        return ret;
15788}
15789EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
15790
15791bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
15792                                        const u8 *addr, gfp_t gfp)
15793{
15794        struct wireless_dev *wdev = dev->ieee80211_ptr;
15795        bool ret;
15796
15797        trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
15798
15799        if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
15800                    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
15801                    wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
15802                trace_cfg80211_return_bool(false);
15803                return false;
15804        }
15805        ret = __nl80211_unexpected_frame(dev,
15806                                         NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
15807                                         addr, gfp);
15808        trace_cfg80211_return_bool(ret);
15809        return ret;
15810}
15811EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
15812
15813int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
15814                      struct wireless_dev *wdev, u32 nlportid,
15815                      int freq, int sig_dbm,
15816                      const u8 *buf, size_t len, u32 flags, gfp_t gfp)
15817{
15818        struct net_device *netdev = wdev->netdev;
15819        struct sk_buff *msg;
15820        void *hdr;
15821
15822        msg = nlmsg_new(100 + len, gfp);
15823        if (!msg)
15824                return -ENOMEM;
15825
15826        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
15827        if (!hdr) {
15828                nlmsg_free(msg);
15829                return -ENOMEM;
15830        }
15831
15832        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15833            (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15834                                        netdev->ifindex)) ||
15835            nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15836                              NL80211_ATTR_PAD) ||
15837            nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
15838            (sig_dbm &&
15839             nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
15840            nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15841            (flags &&
15842             nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
15843                goto nla_put_failure;
15844
15845        genlmsg_end(msg, hdr);
15846
15847        return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15848
15849 nla_put_failure:
15850        nlmsg_free(msg);
15851        return -ENOBUFS;
15852}
15853
15854void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
15855                             const u8 *buf, size_t len, bool ack, gfp_t gfp)
15856{
15857        struct wiphy *wiphy = wdev->wiphy;
15858        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15859        struct net_device *netdev = wdev->netdev;
15860        struct sk_buff *msg;
15861        void *hdr;
15862
15863        trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
15864
15865        msg = nlmsg_new(100 + len, gfp);
15866        if (!msg)
15867                return;
15868
15869        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
15870        if (!hdr) {
15871                nlmsg_free(msg);
15872                return;
15873        }
15874
15875        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15876            (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15877                                   netdev->ifindex)) ||
15878            nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15879                              NL80211_ATTR_PAD) ||
15880            nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15881            nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
15882                              NL80211_ATTR_PAD) ||
15883            (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
15884                goto nla_put_failure;
15885
15886        genlmsg_end(msg, hdr);
15887
15888        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15889                                NL80211_MCGRP_MLME, gfp);
15890        return;
15891
15892 nla_put_failure:
15893        nlmsg_free(msg);
15894}
15895EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
15896
15897static int __nl80211_rx_control_port(struct net_device *dev,
15898                                     struct sk_buff *skb,
15899                                     bool unencrypted, gfp_t gfp)
15900{
15901        struct wireless_dev *wdev = dev->ieee80211_ptr;
15902        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15903        struct ethhdr *ehdr = eth_hdr(skb);
15904        const u8 *addr = ehdr->h_source;
15905        u16 proto = be16_to_cpu(skb->protocol);
15906        struct sk_buff *msg;
15907        void *hdr;
15908        struct nlattr *frame;
15909
15910        u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
15911
15912        if (!nlportid)
15913                return -ENOENT;
15914
15915        msg = nlmsg_new(100 + skb->len, gfp);
15916        if (!msg)
15917                return -ENOMEM;
15918
15919        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
15920        if (!hdr) {
15921                nlmsg_free(msg);
15922                return -ENOBUFS;
15923        }
15924
15925        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15926            nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15927            nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15928                              NL80211_ATTR_PAD) ||
15929            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
15930            nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
15931            (unencrypted && nla_put_flag(msg,
15932                                         NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
15933                goto nla_put_failure;
15934
15935        frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
15936        if (!frame)
15937                goto nla_put_failure;
15938
15939        skb_copy_bits(skb, 0, nla_data(frame), skb->len);
15940        genlmsg_end(msg, hdr);
15941
15942        return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15943
15944 nla_put_failure:
15945        nlmsg_free(msg);
15946        return -ENOBUFS;
15947}
15948
15949bool cfg80211_rx_control_port(struct net_device *dev,
15950                              struct sk_buff *skb, bool unencrypted)
15951{
15952        int ret;
15953
15954        trace_cfg80211_rx_control_port(dev, skb, unencrypted);
15955        ret = __nl80211_rx_control_port(dev, skb, unencrypted, GFP_ATOMIC);
15956        trace_cfg80211_return_bool(ret == 0);
15957        return ret == 0;
15958}
15959EXPORT_SYMBOL(cfg80211_rx_control_port);
15960
15961static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
15962                                            const char *mac, gfp_t gfp)
15963{
15964        struct wireless_dev *wdev = dev->ieee80211_ptr;
15965        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15966        struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15967        void **cb;
15968
15969        if (!msg)
15970                return NULL;
15971
15972        cb = (void **)msg->cb;
15973
15974        cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
15975        if (!cb[0]) {
15976                nlmsg_free(msg);
15977                return NULL;
15978        }
15979
15980        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15981            nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
15982                goto nla_put_failure;
15983
15984        if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
15985                goto nla_put_failure;
15986
15987        cb[1] = nla_nest_start_noflag(msg, NL80211_ATTR_CQM);
15988        if (!cb[1])
15989                goto nla_put_failure;
15990
15991        cb[2] = rdev;
15992
15993        return msg;
15994 nla_put_failure:
15995        nlmsg_free(msg);
15996        return NULL;
15997}
15998
15999static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
16000{
16001        void **cb = (void **)msg->cb;
16002        struct cfg80211_registered_device *rdev = cb[2];
16003
16004        nla_nest_end(msg, cb[1]);
16005        genlmsg_end(msg, cb[0]);
16006
16007        memset(msg->cb, 0, sizeof(msg->cb));
16008
16009        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16010                                NL80211_MCGRP_MLME, gfp);
16011}
16012
16013void cfg80211_cqm_rssi_notify(struct net_device *dev,
16014                              enum nl80211_cqm_rssi_threshold_event rssi_event,
16015                              s32 rssi_level, gfp_t gfp)
16016{
16017        struct sk_buff *msg;
16018        struct wireless_dev *wdev = dev->ieee80211_ptr;
16019        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16020
16021        trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
16022
16023        if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
16024                    rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
16025                return;
16026
16027        if (wdev->cqm_config) {
16028                wdev->cqm_config->last_rssi_event_value = rssi_level;
16029
16030                cfg80211_cqm_rssi_update(rdev, dev);
16031
16032                if (rssi_level == 0)
16033                        rssi_level = wdev->cqm_config->last_rssi_event_value;
16034        }
16035
16036        msg = cfg80211_prepare_cqm(dev, NULL, gfp);
16037        if (!msg)
16038                return;
16039
16040        if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
16041                        rssi_event))
16042                goto nla_put_failure;
16043
16044        if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
16045                                      rssi_level))
16046                goto nla_put_failure;
16047
16048        cfg80211_send_cqm(msg, gfp);
16049
16050        return;
16051
16052 nla_put_failure:
16053        nlmsg_free(msg);
16054}
16055EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
16056
16057void cfg80211_cqm_txe_notify(struct net_device *dev,
16058                             const u8 *peer, u32 num_packets,
16059                             u32 rate, u32 intvl, gfp_t gfp)
16060{
16061        struct sk_buff *msg;
16062
16063        msg = cfg80211_prepare_cqm(dev, peer, gfp);
16064        if (!msg)
16065                return;
16066
16067        if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
16068                goto nla_put_failure;
16069
16070        if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
16071                goto nla_put_failure;
16072
16073        if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
16074                goto nla_put_failure;
16075
16076        cfg80211_send_cqm(msg, gfp);
16077        return;
16078
16079 nla_put_failure:
16080        nlmsg_free(msg);
16081}
16082EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
16083
16084void cfg80211_cqm_pktloss_notify(struct net_device *dev,
16085                                 const u8 *peer, u32 num_packets, gfp_t gfp)
16086{
16087        struct sk_buff *msg;
16088
16089        trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
16090
16091        msg = cfg80211_prepare_cqm(dev, peer, gfp);
16092        if (!msg)
16093                return;
16094
16095        if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
16096                goto nla_put_failure;
16097
16098        cfg80211_send_cqm(msg, gfp);
16099        return;
16100
16101 nla_put_failure:
16102        nlmsg_free(msg);
16103}
16104EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
16105
16106void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
16107{
16108        struct sk_buff *msg;
16109
16110        msg = cfg80211_prepare_cqm(dev, NULL, gfp);
16111        if (!msg)
16112                return;
16113
16114        if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
16115                goto nla_put_failure;
16116
16117        cfg80211_send_cqm(msg, gfp);
16118        return;
16119
16120 nla_put_failure:
16121        nlmsg_free(msg);
16122}
16123EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
16124
16125static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
16126                                     struct net_device *netdev, const u8 *bssid,
16127                                     const u8 *replay_ctr, gfp_t gfp)
16128{
16129        struct sk_buff *msg;
16130        struct nlattr *rekey_attr;
16131        void *hdr;
16132
16133        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16134        if (!msg)
16135                return;
16136
16137        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
16138        if (!hdr) {
16139                nlmsg_free(msg);
16140                return;
16141        }
16142
16143        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16144            nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16145            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
16146                goto nla_put_failure;
16147
16148        rekey_attr = nla_nest_start_noflag(msg, NL80211_ATTR_REKEY_DATA);
16149        if (!rekey_attr)
16150                goto nla_put_failure;
16151
16152        if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
16153                    NL80211_REPLAY_CTR_LEN, replay_ctr))
16154                goto nla_put_failure;
16155
16156        nla_nest_end(msg, rekey_attr);
16157
16158        genlmsg_end(msg, hdr);
16159
16160        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16161                                NL80211_MCGRP_MLME, gfp);
16162        return;
16163
16164 nla_put_failure:
16165        nlmsg_free(msg);
16166}
16167
16168void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
16169                               const u8 *replay_ctr, gfp_t gfp)
16170{
16171        struct wireless_dev *wdev = dev->ieee80211_ptr;
16172        struct wiphy *wiphy = wdev->wiphy;
16173        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16174
16175        trace_cfg80211_gtk_rekey_notify(dev, bssid);
16176        nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
16177}
16178EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
16179
16180static void
16181nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
16182                               struct net_device *netdev, int index,
16183                               const u8 *bssid, bool preauth, gfp_t gfp)
16184{
16185        struct sk_buff *msg;
16186        struct nlattr *attr;
16187        void *hdr;
16188
16189        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16190        if (!msg)
16191                return;
16192
16193        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
16194        if (!hdr) {
16195                nlmsg_free(msg);
16196                return;
16197        }
16198
16199        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16200            nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
16201                goto nla_put_failure;
16202
16203        attr = nla_nest_start_noflag(msg, NL80211_ATTR_PMKSA_CANDIDATE);
16204        if (!attr)
16205                goto nla_put_failure;
16206
16207        if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
16208            nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
16209            (preauth &&
16210             nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
16211                goto nla_put_failure;
16212
16213        nla_nest_end(msg, attr);
16214
16215        genlmsg_end(msg, hdr);
16216
16217        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16218                                NL80211_MCGRP_MLME, gfp);
16219        return;
16220
16221 nla_put_failure:
16222        nlmsg_free(msg);
16223}
16224
16225void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
16226                                     const u8 *bssid, bool preauth, gfp_t gfp)
16227{
16228        struct wireless_dev *wdev = dev->ieee80211_ptr;
16229        struct wiphy *wiphy = wdev->wiphy;
16230        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16231
16232        trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
16233        nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
16234}
16235EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
16236
16237static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
16238                                     struct net_device *netdev,
16239                                     struct cfg80211_chan_def *chandef,
16240                                     gfp_t gfp,
16241                                     enum nl80211_commands notif,
16242                                     u8 count)
16243{
16244        struct sk_buff *msg;
16245        void *hdr;
16246
16247        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16248        if (!msg)
16249                return;
16250
16251        hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
16252        if (!hdr) {
16253                nlmsg_free(msg);
16254                return;
16255        }
16256
16257        if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
16258                goto nla_put_failure;
16259
16260        if (nl80211_send_chandef(msg, chandef))
16261                goto nla_put_failure;
16262
16263        if ((notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) &&
16264            (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count)))
16265                        goto nla_put_failure;
16266
16267        genlmsg_end(msg, hdr);
16268
16269        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16270                                NL80211_MCGRP_MLME, gfp);
16271        return;
16272
16273 nla_put_failure:
16274        nlmsg_free(msg);
16275}
16276
16277void cfg80211_ch_switch_notify(struct net_device *dev,
16278                               struct cfg80211_chan_def *chandef)
16279{
16280        struct wireless_dev *wdev = dev->ieee80211_ptr;
16281        struct wiphy *wiphy = wdev->wiphy;
16282        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16283
16284        ASSERT_WDEV_LOCK(wdev);
16285
16286        trace_cfg80211_ch_switch_notify(dev, chandef);
16287
16288        wdev->chandef = *chandef;
16289        wdev->preset_chandef = *chandef;
16290
16291        if (wdev->iftype == NL80211_IFTYPE_STATION &&
16292            !WARN_ON(!wdev->current_bss))
16293                cfg80211_update_assoc_bss_entry(wdev, chandef->chan);
16294
16295        cfg80211_sched_dfs_chan_update(rdev);
16296
16297        nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
16298                                 NL80211_CMD_CH_SWITCH_NOTIFY, 0);
16299}
16300EXPORT_SYMBOL(cfg80211_ch_switch_notify);
16301
16302void cfg80211_ch_switch_started_notify(struct net_device *dev,
16303                                       struct cfg80211_chan_def *chandef,
16304                                       u8 count)
16305{
16306        struct wireless_dev *wdev = dev->ieee80211_ptr;
16307        struct wiphy *wiphy = wdev->wiphy;
16308        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16309
16310        trace_cfg80211_ch_switch_started_notify(dev, chandef);
16311
16312        nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
16313                                 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, count);
16314}
16315EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
16316
16317void
16318nl80211_radar_notify(struct cfg80211_registered_device *rdev,
16319                     const struct cfg80211_chan_def *chandef,
16320                     enum nl80211_radar_event event,
16321                     struct net_device *netdev, gfp_t gfp)
16322{
16323        struct sk_buff *msg;
16324        void *hdr;
16325
16326        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16327        if (!msg)
16328                return;
16329
16330        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
16331        if (!hdr) {
16332                nlmsg_free(msg);
16333                return;
16334        }
16335
16336        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
16337                goto nla_put_failure;
16338
16339        /* NOP and radar events don't need a netdev parameter */
16340        if (netdev) {
16341                struct wireless_dev *wdev = netdev->ieee80211_ptr;
16342
16343                if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16344                    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16345                                      NL80211_ATTR_PAD))
16346                        goto nla_put_failure;
16347        }
16348
16349        if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
16350                goto nla_put_failure;
16351
16352        if (nl80211_send_chandef(msg, chandef))
16353                goto nla_put_failure;
16354
16355        genlmsg_end(msg, hdr);
16356
16357        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16358                                NL80211_MCGRP_MLME, gfp);
16359        return;
16360
16361 nla_put_failure:
16362        nlmsg_free(msg);
16363}
16364
16365void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
16366                                       struct sta_opmode_info *sta_opmode,
16367                                       gfp_t gfp)
16368{
16369        struct sk_buff *msg;
16370        struct wireless_dev *wdev = dev->ieee80211_ptr;
16371        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16372        void *hdr;
16373
16374        if (WARN_ON(!mac))
16375                return;
16376
16377        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16378        if (!msg)
16379                return;
16380
16381        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
16382        if (!hdr) {
16383                nlmsg_free(msg);
16384                return;
16385        }
16386
16387        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
16388                goto nla_put_failure;
16389
16390        if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
16391                goto nla_put_failure;
16392
16393        if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
16394                goto nla_put_failure;
16395
16396        if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
16397            nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
16398                goto nla_put_failure;
16399
16400        if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
16401            nla_put_u8(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
16402                goto nla_put_failure;
16403
16404        if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
16405            nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
16406                goto nla_put_failure;
16407
16408        genlmsg_end(msg, hdr);
16409
16410        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16411                                NL80211_MCGRP_MLME, gfp);
16412
16413        return;
16414
16415nla_put_failure:
16416        nlmsg_free(msg);
16417}
16418EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
16419
16420void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
16421                           u64 cookie, bool acked, s32 ack_signal,
16422                           bool is_valid_ack_signal, gfp_t gfp)
16423{
16424        struct wireless_dev *wdev = dev->ieee80211_ptr;
16425        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16426        struct sk_buff *msg;
16427        void *hdr;
16428
16429        trace_cfg80211_probe_status(dev, addr, cookie, acked);
16430
16431        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16432
16433        if (!msg)
16434                return;
16435
16436        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
16437        if (!hdr) {
16438                nlmsg_free(msg);
16439                return;
16440        }
16441
16442        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16443            nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16444            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
16445            nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
16446                              NL80211_ATTR_PAD) ||
16447            (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
16448            (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
16449                                                ack_signal)))
16450                goto nla_put_failure;
16451
16452        genlmsg_end(msg, hdr);
16453
16454        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16455                                NL80211_MCGRP_MLME, gfp);
16456        return;
16457
16458 nla_put_failure:
16459        nlmsg_free(msg);
16460}
16461EXPORT_SYMBOL(cfg80211_probe_status);
16462
16463void cfg80211_report_obss_beacon(struct wiphy *wiphy,
16464                                 const u8 *frame, size_t len,
16465                                 int freq, int sig_dbm)
16466{
16467        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16468        struct sk_buff *msg;
16469        void *hdr;
16470        struct cfg80211_beacon_registration *reg;
16471
16472        trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
16473
16474        spin_lock_bh(&rdev->beacon_registrations_lock);
16475        list_for_each_entry(reg, &rdev->beacon_registrations, list) {
16476                msg = nlmsg_new(len + 100, GFP_ATOMIC);
16477                if (!msg) {
16478                        spin_unlock_bh(&rdev->beacon_registrations_lock);
16479                        return;
16480                }
16481
16482                hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
16483                if (!hdr)
16484                        goto nla_put_failure;
16485
16486                if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16487                    (freq &&
16488                     nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
16489                    (sig_dbm &&
16490                     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
16491                    nla_put(msg, NL80211_ATTR_FRAME, len, frame))
16492                        goto nla_put_failure;
16493
16494                genlmsg_end(msg, hdr);
16495
16496                genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
16497        }
16498        spin_unlock_bh(&rdev->beacon_registrations_lock);
16499        return;
16500
16501 nla_put_failure:
16502        spin_unlock_bh(&rdev->beacon_registrations_lock);
16503        nlmsg_free(msg);
16504}
16505EXPORT_SYMBOL(cfg80211_report_obss_beacon);
16506
16507#ifdef CONFIG_PM
16508static int cfg80211_net_detect_results(struct sk_buff *msg,
16509                                       struct cfg80211_wowlan_wakeup *wakeup)
16510{
16511        struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
16512        struct nlattr *nl_results, *nl_match, *nl_freqs;
16513        int i, j;
16514
16515        nl_results = nla_nest_start_noflag(msg,
16516                                           NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
16517        if (!nl_results)
16518                return -EMSGSIZE;
16519
16520        for (i = 0; i < nd->n_matches; i++) {
16521                struct cfg80211_wowlan_nd_match *match = nd->matches[i];
16522
16523                nl_match = nla_nest_start_noflag(msg, i);
16524                if (!nl_match)
16525                        break;
16526
16527                /* The SSID attribute is optional in nl80211, but for
16528                 * simplicity reasons it's always present in the
16529                 * cfg80211 structure.  If a driver can't pass the
16530                 * SSID, that needs to be changed.  A zero length SSID
16531                 * is still a valid SSID (wildcard), so it cannot be
16532                 * used for this purpose.
16533                 */
16534                if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
16535                            match->ssid.ssid)) {
16536                        nla_nest_cancel(msg, nl_match);
16537                        goto out;
16538                }
16539
16540                if (match->n_channels) {
16541                        nl_freqs = nla_nest_start_noflag(msg,
16542                                                         NL80211_ATTR_SCAN_FREQUENCIES);
16543                        if (!nl_freqs) {
16544                                nla_nest_cancel(msg, nl_match);
16545                                goto out;
16546                        }
16547
16548                        for (j = 0; j < match->n_channels; j++) {
16549                                if (nla_put_u32(msg, j, match->channels[j])) {
16550                                        nla_nest_cancel(msg, nl_freqs);
16551                                        nla_nest_cancel(msg, nl_match);
16552                                        goto out;
16553                                }
16554                        }
16555
16556                        nla_nest_end(msg, nl_freqs);
16557                }
16558
16559                nla_nest_end(msg, nl_match);
16560        }
16561
16562out:
16563        nla_nest_end(msg, nl_results);
16564        return 0;
16565}
16566
16567void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
16568                                   struct cfg80211_wowlan_wakeup *wakeup,
16569                                   gfp_t gfp)
16570{
16571        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16572        struct sk_buff *msg;
16573        void *hdr;
16574        int size = 200;
16575
16576        trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
16577
16578        if (wakeup)
16579                size += wakeup->packet_present_len;
16580
16581        msg = nlmsg_new(size, gfp);
16582        if (!msg)
16583                return;
16584
16585        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
16586        if (!hdr)
16587                goto free_msg;
16588
16589        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16590            nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16591                              NL80211_ATTR_PAD))
16592                goto free_msg;
16593
16594        if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
16595                                        wdev->netdev->ifindex))
16596                goto free_msg;
16597
16598        if (wakeup) {
16599                struct nlattr *reasons;
16600
16601                reasons = nla_nest_start_noflag(msg,
16602                                                NL80211_ATTR_WOWLAN_TRIGGERS);
16603                if (!reasons)
16604                        goto free_msg;
16605
16606                if (wakeup->disconnect &&
16607                    nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
16608                        goto free_msg;
16609                if (wakeup->magic_pkt &&
16610                    nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
16611                        goto free_msg;
16612                if (wakeup->gtk_rekey_failure &&
16613                    nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
16614                        goto free_msg;
16615                if (wakeup->eap_identity_req &&
16616                    nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
16617                        goto free_msg;
16618                if (wakeup->four_way_handshake &&
16619                    nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
16620                        goto free_msg;
16621                if (wakeup->rfkill_release &&
16622                    nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
16623                        goto free_msg;
16624
16625                if (wakeup->pattern_idx >= 0 &&
16626                    nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
16627                                wakeup->pattern_idx))
16628                        goto free_msg;
16629
16630                if (wakeup->tcp_match &&
16631                    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
16632                        goto free_msg;
16633
16634                if (wakeup->tcp_connlost &&
16635                    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
16636                        goto free_msg;
16637
16638                if (wakeup->tcp_nomoretokens &&
16639                    nla_put_flag(msg,
16640                                 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
16641                        goto free_msg;
16642
16643                if (wakeup->packet) {
16644                        u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
16645                        u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
16646
16647                        if (!wakeup->packet_80211) {
16648                                pkt_attr =
16649                                        NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
16650                                len_attr =
16651                                        NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
16652                        }
16653
16654                        if (wakeup->packet_len &&
16655                            nla_put_u32(msg, len_attr, wakeup->packet_len))
16656                                goto free_msg;
16657
16658                        if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
16659                                    wakeup->packet))
16660                                goto free_msg;
16661                }
16662
16663                if (wakeup->net_detect &&
16664                    cfg80211_net_detect_results(msg, wakeup))
16665                                goto free_msg;
16666
16667                nla_nest_end(msg, reasons);
16668        }
16669
16670        genlmsg_end(msg, hdr);
16671
16672        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16673                                NL80211_MCGRP_MLME, gfp);
16674        return;
16675
16676 free_msg:
16677        nlmsg_free(msg);
16678}
16679EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
16680#endif
16681
16682void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
16683                                enum nl80211_tdls_operation oper,
16684                                u16 reason_code, gfp_t gfp)
16685{
16686        struct wireless_dev *wdev = dev->ieee80211_ptr;
16687        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16688        struct sk_buff *msg;
16689        void *hdr;
16690
16691        trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
16692                                         reason_code);
16693
16694        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16695        if (!msg)
16696                return;
16697
16698        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
16699        if (!hdr) {
16700                nlmsg_free(msg);
16701                return;
16702        }
16703
16704        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16705            nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16706            nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
16707            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
16708            (reason_code > 0 &&
16709             nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
16710                goto nla_put_failure;
16711
16712        genlmsg_end(msg, hdr);
16713
16714        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16715                                NL80211_MCGRP_MLME, gfp);
16716        return;
16717
16718 nla_put_failure:
16719        nlmsg_free(msg);
16720}
16721EXPORT_SYMBOL(cfg80211_tdls_oper_request);
16722
16723static int nl80211_netlink_notify(struct notifier_block * nb,
16724                                  unsigned long state,
16725                                  void *_notify)
16726{
16727        struct netlink_notify *notify = _notify;
16728        struct cfg80211_registered_device *rdev;
16729        struct wireless_dev *wdev;
16730        struct cfg80211_beacon_registration *reg, *tmp;
16731
16732        if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
16733                return NOTIFY_DONE;
16734
16735        rcu_read_lock();
16736
16737        list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
16738                struct cfg80211_sched_scan_request *sched_scan_req;
16739
16740                list_for_each_entry_rcu(sched_scan_req,
16741                                        &rdev->sched_scan_req_list,
16742                                        list) {
16743                        if (sched_scan_req->owner_nlportid == notify->portid) {
16744                                sched_scan_req->nl_owner_dead = true;
16745                                schedule_work(&rdev->sched_scan_stop_wk);
16746                        }
16747                }
16748
16749                list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
16750                        cfg80211_mlme_unregister_socket(wdev, notify->portid);
16751
16752                        if (wdev->owner_nlportid == notify->portid) {
16753                                wdev->nl_owner_dead = true;
16754                                schedule_work(&rdev->destroy_work);
16755                        } else if (wdev->conn_owner_nlportid == notify->portid) {
16756                                schedule_work(&wdev->disconnect_wk);
16757                        }
16758
16759                        cfg80211_release_pmsr(wdev, notify->portid);
16760                }
16761
16762                spin_lock_bh(&rdev->beacon_registrations_lock);
16763                list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
16764                                         list) {
16765                        if (reg->nlportid == notify->portid) {
16766                                list_del(&reg->list);
16767                                kfree(reg);
16768                                break;
16769                        }
16770                }
16771                spin_unlock_bh(&rdev->beacon_registrations_lock);
16772        }
16773
16774        rcu_read_unlock();
16775
16776        /*
16777         * It is possible that the user space process that is controlling the
16778         * indoor setting disappeared, so notify the regulatory core.
16779         */
16780        regulatory_netlink_notify(notify->portid);
16781        return NOTIFY_OK;
16782}
16783
16784static struct notifier_block nl80211_netlink_notifier = {
16785        .notifier_call = nl80211_netlink_notify,
16786};
16787
16788void cfg80211_ft_event(struct net_device *netdev,
16789                       struct cfg80211_ft_event_params *ft_event)
16790{
16791        struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
16792        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16793        struct sk_buff *msg;
16794        void *hdr;
16795
16796        trace_cfg80211_ft_event(wiphy, netdev, ft_event);
16797
16798        if (!ft_event->target_ap)
16799                return;
16800
16801        msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
16802                        GFP_KERNEL);
16803        if (!msg)
16804                return;
16805
16806        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
16807        if (!hdr)
16808                goto out;
16809
16810        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16811            nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16812            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
16813                goto out;
16814
16815        if (ft_event->ies &&
16816            nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
16817                goto out;
16818        if (ft_event->ric_ies &&
16819            nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
16820                    ft_event->ric_ies))
16821                goto out;
16822
16823        genlmsg_end(msg, hdr);
16824
16825        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16826                                NL80211_MCGRP_MLME, GFP_KERNEL);
16827        return;
16828 out:
16829        nlmsg_free(msg);
16830}
16831EXPORT_SYMBOL(cfg80211_ft_event);
16832
16833void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
16834{
16835        struct cfg80211_registered_device *rdev;
16836        struct sk_buff *msg;
16837        void *hdr;
16838        u32 nlportid;
16839
16840        rdev = wiphy_to_rdev(wdev->wiphy);
16841        if (!rdev->crit_proto_nlportid)
16842                return;
16843
16844        nlportid = rdev->crit_proto_nlportid;
16845        rdev->crit_proto_nlportid = 0;
16846
16847        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16848        if (!msg)
16849                return;
16850
16851        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
16852        if (!hdr)
16853                goto nla_put_failure;
16854
16855        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16856            nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16857                              NL80211_ATTR_PAD))
16858                goto nla_put_failure;
16859
16860        genlmsg_end(msg, hdr);
16861
16862        genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
16863        return;
16864
16865 nla_put_failure:
16866        nlmsg_free(msg);
16867}
16868EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
16869
16870void nl80211_send_ap_stopped(struct wireless_dev *wdev)
16871{
16872        struct wiphy *wiphy = wdev->wiphy;
16873        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16874        struct sk_buff *msg;
16875        void *hdr;
16876
16877        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
16878        if (!msg)
16879                return;
16880
16881        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
16882        if (!hdr)
16883                goto out;
16884
16885        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16886            nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
16887            nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16888                              NL80211_ATTR_PAD))
16889                goto out;
16890
16891        genlmsg_end(msg, hdr);
16892
16893        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
16894                                NL80211_MCGRP_MLME, GFP_KERNEL);
16895        return;
16896 out:
16897        nlmsg_free(msg);
16898}
16899
16900int cfg80211_external_auth_request(struct net_device *dev,
16901                                   struct cfg80211_external_auth_params *params,
16902                                   gfp_t gfp)
16903{
16904        struct wireless_dev *wdev = dev->ieee80211_ptr;
16905        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16906        struct sk_buff *msg;
16907        void *hdr;
16908
16909        if (!wdev->conn_owner_nlportid)
16910                return -EINVAL;
16911
16912        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16913        if (!msg)
16914                return -ENOMEM;
16915
16916        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
16917        if (!hdr)
16918                goto nla_put_failure;
16919
16920        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16921            nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16922            nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
16923            nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
16924                        params->action) ||
16925            nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
16926            nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
16927                    params->ssid.ssid))
16928                goto nla_put_failure;
16929
16930        genlmsg_end(msg, hdr);
16931        genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
16932                        wdev->conn_owner_nlportid);
16933        return 0;
16934
16935 nla_put_failure:
16936        nlmsg_free(msg);
16937        return -ENOBUFS;
16938}
16939EXPORT_SYMBOL(cfg80211_external_auth_request);
16940
16941void cfg80211_update_owe_info_event(struct net_device *netdev,
16942                                    struct cfg80211_update_owe_info *owe_info,
16943                                    gfp_t gfp)
16944{
16945        struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
16946        struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16947        struct sk_buff *msg;
16948        void *hdr;
16949
16950        trace_cfg80211_update_owe_info_event(wiphy, netdev, owe_info);
16951
16952        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16953        if (!msg)
16954                return;
16955
16956        hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_UPDATE_OWE_INFO);
16957        if (!hdr)
16958                goto nla_put_failure;
16959
16960        if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16961            nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16962            nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, owe_info->peer))
16963                goto nla_put_failure;
16964
16965        if (!owe_info->ie_len ||
16966            nla_put(msg, NL80211_ATTR_IE, owe_info->ie_len, owe_info->ie))
16967                goto nla_put_failure;
16968
16969        genlmsg_end(msg, hdr);
16970
16971        genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16972                                NL80211_MCGRP_MLME, gfp);
16973        return;
16974
16975nla_put_failure:
16976        genlmsg_cancel(msg, hdr);
16977        nlmsg_free(msg);
16978}
16979EXPORT_SYMBOL(cfg80211_update_owe_info_event);
16980
16981/* initialisation/exit functions */
16982
16983int __init nl80211_init(void)
16984{
16985        int err;
16986
16987        err = genl_register_family(&nl80211_fam);
16988        if (err)
16989                return err;
16990
16991        err = netlink_register_notifier(&nl80211_netlink_notifier);
16992        if (err)
16993                goto err_out;
16994
16995        return 0;
16996 err_out:
16997        genl_unregister_family(&nl80211_fam);
16998        return err;
16999}
17000
17001void nl80211_exit(void)
17002{
17003        netlink_unregister_notifier(&nl80211_netlink_notifier);
17004        genl_unregister_family(&nl80211_fam);
17005}
17006