linux/drivers/net/wireless/mac80211_hwsim.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
   4 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
   5 * Copyright (c) 2011, Javier Lopez <jlopex@gmail.com>
   6 * Copyright (c) 2016 - 2017 Intel Deutschland GmbH
   7 * Copyright (C) 2018 - 2022 Intel Corporation
   8 */
   9
  10/*
  11 * TODO:
  12 * - Add TSF sync and fix IBSS beacon transmission by adding
  13 *   competition for "air time" at TBTT
  14 * - RX filtering based on filter configuration (data->rx_filter)
  15 */
  16
  17#include <linux/list.h>
  18#include <linux/slab.h>
  19#include <linux/spinlock.h>
  20#include <net/dst.h>
  21#include <net/xfrm.h>
  22#include <net/mac80211.h>
  23#include <net/ieee80211_radiotap.h>
  24#include <linux/if_arp.h>
  25#include <linux/rtnetlink.h>
  26#include <linux/etherdevice.h>
  27#include <linux/platform_device.h>
  28#include <linux/debugfs.h>
  29#include <linux/module.h>
  30#include <linux/ktime.h>
  31#include <net/genetlink.h>
  32#include <net/net_namespace.h>
  33#include <net/netns/generic.h>
  34#include <linux/rhashtable.h>
  35#include <linux/nospec.h>
  36#include <linux/virtio.h>
  37#include <linux/virtio_ids.h>
  38#include <linux/virtio_config.h>
  39#include "mac80211_hwsim.h"
  40
  41#define WARN_QUEUE 100
  42#define MAX_QUEUE 200
  43
  44MODULE_AUTHOR("Jouni Malinen");
  45MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
  46MODULE_LICENSE("GPL");
  47
  48static int radios = 2;
  49module_param(radios, int, 0444);
  50MODULE_PARM_DESC(radios, "Number of simulated radios");
  51
  52static int channels = 1;
  53module_param(channels, int, 0444);
  54MODULE_PARM_DESC(channels, "Number of concurrent channels");
  55
  56static bool paged_rx = false;
  57module_param(paged_rx, bool, 0644);
  58MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones");
  59
  60static bool rctbl = false;
  61module_param(rctbl, bool, 0444);
  62MODULE_PARM_DESC(rctbl, "Handle rate control table");
  63
  64static bool support_p2p_device = true;
  65module_param(support_p2p_device, bool, 0444);
  66MODULE_PARM_DESC(support_p2p_device, "Support P2P-Device interface type");
  67
  68/**
  69 * enum hwsim_regtest - the type of regulatory tests we offer
  70 *
  71 * These are the different values you can use for the regtest
  72 * module parameter. This is useful to help test world roaming
  73 * and the driver regulatory_hint() call and combinations of these.
  74 * If you want to do specific alpha2 regulatory domain tests simply
  75 * use the userspace regulatory request as that will be respected as
  76 * well without the need of this module parameter. This is designed
  77 * only for testing the driver regulatory request, world roaming
  78 * and all possible combinations.
  79 *
  80 * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
  81 *      this is the default value.
  82 * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
  83 *      hint, only one driver regulatory hint will be sent as such the
  84 *      secondary radios are expected to follow.
  85 * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
  86 *      request with all radios reporting the same regulatory domain.
  87 * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
  88 *      different regulatory domains requests. Expected behaviour is for
  89 *      an intersection to occur but each device will still use their
  90 *      respective regulatory requested domains. Subsequent radios will
  91 *      use the resulting intersection.
  92 * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
  93 *      this by using a custom beacon-capable regulatory domain for the first
  94 *      radio. All other device world roam.
  95 * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
  96 *      domain requests. All radios will adhere to this custom world regulatory
  97 *      domain.
  98 * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
  99 *      domain requests. The first radio will adhere to the first custom world
 100 *      regulatory domain, the second one to the second custom world regulatory
 101 *      domain. All other devices will world roam.
 102 * @HWSIM_REGTEST_STRICT_FOLLOW: Used for testing strict regulatory domain
 103 *      settings, only the first radio will send a regulatory domain request
 104 *      and use strict settings. The rest of the radios are expected to follow.
 105 * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
 106 *      settings. All radios will adhere to this.
 107 * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
 108 *      domain settings, combined with secondary driver regulatory domain
 109 *      settings. The first radio will get a strict regulatory domain setting
 110 *      using the first driver regulatory request and the second radio will use
 111 *      non-strict settings using the second driver regulatory request. All
 112 *      other devices should follow the intersection created between the
 113 *      first two.
 114 * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
 115 *      at least 6 radios for a complete test. We will test in this order:
 116 *      1 - driver custom world regulatory domain
 117 *      2 - second custom world regulatory domain
 118 *      3 - first driver regulatory domain request
 119 *      4 - second driver regulatory domain request
 120 *      5 - strict regulatory domain settings using the third driver regulatory
 121 *          domain request
 122 *      6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
 123 *                 regulatory requests.
 124 */
 125enum hwsim_regtest {
 126        HWSIM_REGTEST_DISABLED = 0,
 127        HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
 128        HWSIM_REGTEST_DRIVER_REG_ALL = 2,
 129        HWSIM_REGTEST_DIFF_COUNTRY = 3,
 130        HWSIM_REGTEST_WORLD_ROAM = 4,
 131        HWSIM_REGTEST_CUSTOM_WORLD = 5,
 132        HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
 133        HWSIM_REGTEST_STRICT_FOLLOW = 7,
 134        HWSIM_REGTEST_STRICT_ALL = 8,
 135        HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
 136        HWSIM_REGTEST_ALL = 10,
 137};
 138
 139/* Set to one of the HWSIM_REGTEST_* values above */
 140static int regtest = HWSIM_REGTEST_DISABLED;
 141module_param(regtest, int, 0444);
 142MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
 143
 144static const char *hwsim_alpha2s[] = {
 145        "FI",
 146        "AL",
 147        "US",
 148        "DE",
 149        "JP",
 150        "AL",
 151};
 152
 153static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
 154        .n_reg_rules = 5,
 155        .alpha2 =  "99",
 156        .reg_rules = {
 157                REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
 158                REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
 159                REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
 160                REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
 161                REG_RULE(5855-10, 5925+10, 40, 0, 33, 0),
 162        }
 163};
 164
 165static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
 166        .n_reg_rules = 3,
 167        .alpha2 =  "99",
 168        .reg_rules = {
 169                REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
 170                REG_RULE(5725-10, 5850+10, 40, 0, 30,
 171                         NL80211_RRF_NO_IR),
 172                REG_RULE(5855-10, 5925+10, 40, 0, 33, 0),
 173        }
 174};
 175
 176static const struct ieee80211_regdomain hwsim_world_regdom_custom_03 = {
 177        .n_reg_rules = 6,
 178        .alpha2 =  "99",
 179        .reg_rules = {
 180                REG_RULE(2412 - 10, 2462 + 10, 40, 0, 20, 0),
 181                REG_RULE(2484 - 10, 2484 + 10, 40, 0, 20, 0),
 182                REG_RULE(5150 - 10, 5240 + 10, 40, 0, 30, 0),
 183                REG_RULE(5745 - 10, 5825 + 10, 40, 0, 30, 0),
 184                REG_RULE(5855 - 10, 5925 + 10, 40, 0, 33, 0),
 185                REG_RULE(5955 - 10, 7125 + 10, 320, 0, 33, 0),
 186        }
 187};
 188
 189static const struct ieee80211_regdomain *hwsim_world_regdom_custom[] = {
 190        &hwsim_world_regdom_custom_01,
 191        &hwsim_world_regdom_custom_02,
 192        &hwsim_world_regdom_custom_03,
 193};
 194
 195struct hwsim_vif_priv {
 196        u32 magic;
 197        u8 bssid[ETH_ALEN];
 198        bool assoc;
 199        bool bcn_en;
 200        u16 aid;
 201};
 202
 203#define HWSIM_VIF_MAGIC 0x69537748
 204
 205static inline void hwsim_check_magic(struct ieee80211_vif *vif)
 206{
 207        struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
 208        WARN(vp->magic != HWSIM_VIF_MAGIC,
 209             "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
 210             vif, vp->magic, vif->addr, vif->type, vif->p2p);
 211}
 212
 213static inline void hwsim_set_magic(struct ieee80211_vif *vif)
 214{
 215        struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
 216        vp->magic = HWSIM_VIF_MAGIC;
 217}
 218
 219static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
 220{
 221        struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
 222        vp->magic = 0;
 223}
 224
 225struct hwsim_sta_priv {
 226        u32 magic;
 227};
 228
 229#define HWSIM_STA_MAGIC 0x6d537749
 230
 231static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
 232{
 233        struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
 234        WARN_ON(sp->magic != HWSIM_STA_MAGIC);
 235}
 236
 237static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
 238{
 239        struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
 240        sp->magic = HWSIM_STA_MAGIC;
 241}
 242
 243static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
 244{
 245        struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
 246        sp->magic = 0;
 247}
 248
 249struct hwsim_chanctx_priv {
 250        u32 magic;
 251};
 252
 253#define HWSIM_CHANCTX_MAGIC 0x6d53774a
 254
 255static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
 256{
 257        struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
 258        WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
 259}
 260
 261static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
 262{
 263        struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
 264        cp->magic = HWSIM_CHANCTX_MAGIC;
 265}
 266
 267static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
 268{
 269        struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
 270        cp->magic = 0;
 271}
 272
 273static unsigned int hwsim_net_id;
 274
 275static DEFINE_IDA(hwsim_netgroup_ida);
 276
 277struct hwsim_net {
 278        int netgroup;
 279        u32 wmediumd;
 280};
 281
 282static inline int hwsim_net_get_netgroup(struct net *net)
 283{
 284        struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
 285
 286        return hwsim_net->netgroup;
 287}
 288
 289static inline int hwsim_net_set_netgroup(struct net *net)
 290{
 291        struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
 292
 293        hwsim_net->netgroup = ida_simple_get(&hwsim_netgroup_ida,
 294                                             0, 0, GFP_KERNEL);
 295        return hwsim_net->netgroup >= 0 ? 0 : -ENOMEM;
 296}
 297
 298static inline u32 hwsim_net_get_wmediumd(struct net *net)
 299{
 300        struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
 301
 302        return hwsim_net->wmediumd;
 303}
 304
 305static inline void hwsim_net_set_wmediumd(struct net *net, u32 portid)
 306{
 307        struct hwsim_net *hwsim_net = net_generic(net, hwsim_net_id);
 308
 309        hwsim_net->wmediumd = portid;
 310}
 311
 312static struct class *hwsim_class;
 313
 314static struct net_device *hwsim_mon; /* global monitor netdev */
 315
 316#define CHAN2G(_freq)  { \
 317        .band = NL80211_BAND_2GHZ, \
 318        .center_freq = (_freq), \
 319        .hw_value = (_freq), \
 320}
 321
 322#define CHAN5G(_freq) { \
 323        .band = NL80211_BAND_5GHZ, \
 324        .center_freq = (_freq), \
 325        .hw_value = (_freq), \
 326}
 327
 328#define CHAN6G(_freq) { \
 329        .band = NL80211_BAND_6GHZ, \
 330        .center_freq = (_freq), \
 331        .hw_value = (_freq), \
 332}
 333
 334static const struct ieee80211_channel hwsim_channels_2ghz[] = {
 335        CHAN2G(2412), /* Channel 1 */
 336        CHAN2G(2417), /* Channel 2 */
 337        CHAN2G(2422), /* Channel 3 */
 338        CHAN2G(2427), /* Channel 4 */
 339        CHAN2G(2432), /* Channel 5 */
 340        CHAN2G(2437), /* Channel 6 */
 341        CHAN2G(2442), /* Channel 7 */
 342        CHAN2G(2447), /* Channel 8 */
 343        CHAN2G(2452), /* Channel 9 */
 344        CHAN2G(2457), /* Channel 10 */
 345        CHAN2G(2462), /* Channel 11 */
 346        CHAN2G(2467), /* Channel 12 */
 347        CHAN2G(2472), /* Channel 13 */
 348        CHAN2G(2484), /* Channel 14 */
 349};
 350
 351static const struct ieee80211_channel hwsim_channels_5ghz[] = {
 352        CHAN5G(5180), /* Channel 36 */
 353        CHAN5G(5200), /* Channel 40 */
 354        CHAN5G(5220), /* Channel 44 */
 355        CHAN5G(5240), /* Channel 48 */
 356
 357        CHAN5G(5260), /* Channel 52 */
 358        CHAN5G(5280), /* Channel 56 */
 359        CHAN5G(5300), /* Channel 60 */
 360        CHAN5G(5320), /* Channel 64 */
 361
 362        CHAN5G(5500), /* Channel 100 */
 363        CHAN5G(5520), /* Channel 104 */
 364        CHAN5G(5540), /* Channel 108 */
 365        CHAN5G(5560), /* Channel 112 */
 366        CHAN5G(5580), /* Channel 116 */
 367        CHAN5G(5600), /* Channel 120 */
 368        CHAN5G(5620), /* Channel 124 */
 369        CHAN5G(5640), /* Channel 128 */
 370        CHAN5G(5660), /* Channel 132 */
 371        CHAN5G(5680), /* Channel 136 */
 372        CHAN5G(5700), /* Channel 140 */
 373
 374        CHAN5G(5745), /* Channel 149 */
 375        CHAN5G(5765), /* Channel 153 */
 376        CHAN5G(5785), /* Channel 157 */
 377        CHAN5G(5805), /* Channel 161 */
 378        CHAN5G(5825), /* Channel 165 */
 379        CHAN5G(5845), /* Channel 169 */
 380
 381        CHAN5G(5855), /* Channel 171 */
 382        CHAN5G(5860), /* Channel 172 */
 383        CHAN5G(5865), /* Channel 173 */
 384        CHAN5G(5870), /* Channel 174 */
 385
 386        CHAN5G(5875), /* Channel 175 */
 387        CHAN5G(5880), /* Channel 176 */
 388        CHAN5G(5885), /* Channel 177 */
 389        CHAN5G(5890), /* Channel 178 */
 390        CHAN5G(5895), /* Channel 179 */
 391        CHAN5G(5900), /* Channel 180 */
 392        CHAN5G(5905), /* Channel 181 */
 393
 394        CHAN5G(5910), /* Channel 182 */
 395        CHAN5G(5915), /* Channel 183 */
 396        CHAN5G(5920), /* Channel 184 */
 397        CHAN5G(5925), /* Channel 185 */
 398};
 399
 400static const struct ieee80211_channel hwsim_channels_6ghz[] = {
 401        CHAN6G(5955), /* Channel 1 */
 402        CHAN6G(5975), /* Channel 5 */
 403        CHAN6G(5995), /* Channel 9 */
 404        CHAN6G(6015), /* Channel 13 */
 405        CHAN6G(6035), /* Channel 17 */
 406        CHAN6G(6055), /* Channel 21 */
 407        CHAN6G(6075), /* Channel 25 */
 408        CHAN6G(6095), /* Channel 29 */
 409        CHAN6G(6115), /* Channel 33 */
 410        CHAN6G(6135), /* Channel 37 */
 411        CHAN6G(6155), /* Channel 41 */
 412        CHAN6G(6175), /* Channel 45 */
 413        CHAN6G(6195), /* Channel 49 */
 414        CHAN6G(6215), /* Channel 53 */
 415        CHAN6G(6235), /* Channel 57 */
 416        CHAN6G(6255), /* Channel 61 */
 417        CHAN6G(6275), /* Channel 65 */
 418        CHAN6G(6295), /* Channel 69 */
 419        CHAN6G(6315), /* Channel 73 */
 420        CHAN6G(6335), /* Channel 77 */
 421        CHAN6G(6355), /* Channel 81 */
 422        CHAN6G(6375), /* Channel 85 */
 423        CHAN6G(6395), /* Channel 89 */
 424        CHAN6G(6415), /* Channel 93 */
 425        CHAN6G(6435), /* Channel 97 */
 426        CHAN6G(6455), /* Channel 181 */
 427        CHAN6G(6475), /* Channel 105 */
 428        CHAN6G(6495), /* Channel 109 */
 429        CHAN6G(6515), /* Channel 113 */
 430        CHAN6G(6535), /* Channel 117 */
 431        CHAN6G(6555), /* Channel 121 */
 432        CHAN6G(6575), /* Channel 125 */
 433        CHAN6G(6595), /* Channel 129 */
 434        CHAN6G(6615), /* Channel 133 */
 435        CHAN6G(6635), /* Channel 137 */
 436        CHAN6G(6655), /* Channel 141 */
 437        CHAN6G(6675), /* Channel 145 */
 438        CHAN6G(6695), /* Channel 149 */
 439        CHAN6G(6715), /* Channel 153 */
 440        CHAN6G(6735), /* Channel 157 */
 441        CHAN6G(6755), /* Channel 161 */
 442        CHAN6G(6775), /* Channel 165 */
 443        CHAN6G(6795), /* Channel 169 */
 444        CHAN6G(6815), /* Channel 173 */
 445        CHAN6G(6835), /* Channel 177 */
 446        CHAN6G(6855), /* Channel 181 */
 447        CHAN6G(6875), /* Channel 185 */
 448        CHAN6G(6895), /* Channel 189 */
 449        CHAN6G(6915), /* Channel 193 */
 450        CHAN6G(6935), /* Channel 197 */
 451        CHAN6G(6955), /* Channel 201 */
 452        CHAN6G(6975), /* Channel 205 */
 453        CHAN6G(6995), /* Channel 209 */
 454        CHAN6G(7015), /* Channel 213 */
 455        CHAN6G(7035), /* Channel 217 */
 456        CHAN6G(7055), /* Channel 221 */
 457        CHAN6G(7075), /* Channel 225 */
 458        CHAN6G(7095), /* Channel 229 */
 459        CHAN6G(7115), /* Channel 233 */
 460};
 461
 462#define NUM_S1G_CHANS_US 51
 463static struct ieee80211_channel hwsim_channels_s1g[NUM_S1G_CHANS_US];
 464
 465static const struct ieee80211_sta_s1g_cap hwsim_s1g_cap = {
 466        .s1g = true,
 467        .cap = { S1G_CAP0_SGI_1MHZ | S1G_CAP0_SGI_2MHZ,
 468                 0,
 469                 0,
 470                 S1G_CAP3_MAX_MPDU_LEN,
 471                 0,
 472                 S1G_CAP5_AMPDU,
 473                 0,
 474                 S1G_CAP7_DUP_1MHZ,
 475                 S1G_CAP8_TWT_RESPOND | S1G_CAP8_TWT_REQUEST,
 476                 0},
 477        .nss_mcs = { 0xfc | 1, /* MCS 7 for 1 SS */
 478        /* RX Highest Supported Long GI Data Rate 0:7 */
 479                     0,
 480        /* RX Highest Supported Long GI Data Rate 0:7 */
 481        /* TX S1G MCS Map 0:6 */
 482                     0xfa,
 483        /* TX S1G MCS Map :7 */
 484        /* TX Highest Supported Long GI Data Rate 0:6 */
 485                     0x80,
 486        /* TX Highest Supported Long GI Data Rate 7:8 */
 487        /* Rx Single spatial stream and S1G-MCS Map for 1MHz */
 488        /* Tx Single spatial stream and S1G-MCS Map for 1MHz */
 489                     0 },
 490};
 491
 492static void hwsim_init_s1g_channels(struct ieee80211_channel *chans)
 493{
 494        int ch, freq;
 495
 496        for (ch = 0; ch < NUM_S1G_CHANS_US; ch++) {
 497                freq = 902000 + (ch + 1) * 500;
 498                chans[ch].band = NL80211_BAND_S1GHZ;
 499                chans[ch].center_freq = KHZ_TO_MHZ(freq);
 500                chans[ch].freq_offset = freq % 1000;
 501                chans[ch].hw_value = ch + 1;
 502        }
 503}
 504
 505static const struct ieee80211_rate hwsim_rates[] = {
 506        { .bitrate = 10 },
 507        { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 508        { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 509        { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 510        { .bitrate = 60 },
 511        { .bitrate = 90 },
 512        { .bitrate = 120 },
 513        { .bitrate = 180 },
 514        { .bitrate = 240 },
 515        { .bitrate = 360 },
 516        { .bitrate = 480 },
 517        { .bitrate = 540 }
 518};
 519
 520#define DEFAULT_RX_RSSI -50
 521
 522static const u32 hwsim_ciphers[] = {
 523        WLAN_CIPHER_SUITE_WEP40,
 524        WLAN_CIPHER_SUITE_WEP104,
 525        WLAN_CIPHER_SUITE_TKIP,
 526        WLAN_CIPHER_SUITE_CCMP,
 527        WLAN_CIPHER_SUITE_CCMP_256,
 528        WLAN_CIPHER_SUITE_GCMP,
 529        WLAN_CIPHER_SUITE_GCMP_256,
 530        WLAN_CIPHER_SUITE_AES_CMAC,
 531        WLAN_CIPHER_SUITE_BIP_CMAC_256,
 532        WLAN_CIPHER_SUITE_BIP_GMAC_128,
 533        WLAN_CIPHER_SUITE_BIP_GMAC_256,
 534};
 535
 536#define OUI_QCA 0x001374
 537#define QCA_NL80211_SUBCMD_TEST 1
 538enum qca_nl80211_vendor_subcmds {
 539        QCA_WLAN_VENDOR_ATTR_TEST = 8,
 540        QCA_WLAN_VENDOR_ATTR_MAX = QCA_WLAN_VENDOR_ATTR_TEST
 541};
 542
 543static const struct nla_policy
 544hwsim_vendor_test_policy[QCA_WLAN_VENDOR_ATTR_MAX + 1] = {
 545        [QCA_WLAN_VENDOR_ATTR_MAX] = { .type = NLA_U32 },
 546};
 547
 548static int mac80211_hwsim_vendor_cmd_test(struct wiphy *wiphy,
 549                                          struct wireless_dev *wdev,
 550                                          const void *data, int data_len)
 551{
 552        struct sk_buff *skb;
 553        struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_MAX + 1];
 554        int err;
 555        u32 val;
 556
 557        err = nla_parse_deprecated(tb, QCA_WLAN_VENDOR_ATTR_MAX, data,
 558                                   data_len, hwsim_vendor_test_policy, NULL);
 559        if (err)
 560                return err;
 561        if (!tb[QCA_WLAN_VENDOR_ATTR_TEST])
 562                return -EINVAL;
 563        val = nla_get_u32(tb[QCA_WLAN_VENDOR_ATTR_TEST]);
 564        wiphy_dbg(wiphy, "%s: test=%u\n", __func__, val);
 565
 566        /* Send a vendor event as a test. Note that this would not normally be
 567         * done within a command handler, but rather, based on some other
 568         * trigger. For simplicity, this command is used to trigger the event
 569         * here.
 570         *
 571         * event_idx = 0 (index in mac80211_hwsim_vendor_commands)
 572         */
 573        skb = cfg80211_vendor_event_alloc(wiphy, wdev, 100, 0, GFP_KERNEL);
 574        if (skb) {
 575                /* skb_put() or nla_put() will fill up data within
 576                 * NL80211_ATTR_VENDOR_DATA.
 577                 */
 578
 579                /* Add vendor data */
 580                nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 1);
 581
 582                /* Send the event - this will call nla_nest_end() */
 583                cfg80211_vendor_event(skb, GFP_KERNEL);
 584        }
 585
 586        /* Send a response to the command */
 587        skb = cfg80211_vendor_cmd_alloc_reply_skb(wiphy, 10);
 588        if (!skb)
 589                return -ENOMEM;
 590
 591        /* skb_put() or nla_put() will fill up data within
 592         * NL80211_ATTR_VENDOR_DATA
 593         */
 594        nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_TEST, val + 2);
 595
 596        return cfg80211_vendor_cmd_reply(skb);
 597}
 598
 599static struct wiphy_vendor_command mac80211_hwsim_vendor_commands[] = {
 600        {
 601                .info = { .vendor_id = OUI_QCA,
 602                          .subcmd = QCA_NL80211_SUBCMD_TEST },
 603                .flags = WIPHY_VENDOR_CMD_NEED_NETDEV,
 604                .doit = mac80211_hwsim_vendor_cmd_test,
 605                .policy = hwsim_vendor_test_policy,
 606                .maxattr = QCA_WLAN_VENDOR_ATTR_MAX,
 607        }
 608};
 609
 610/* Advertise support vendor specific events */
 611static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events[] = {
 612        { .vendor_id = OUI_QCA, .subcmd = 1 },
 613};
 614
 615static DEFINE_SPINLOCK(hwsim_radio_lock);
 616static LIST_HEAD(hwsim_radios);
 617static struct rhashtable hwsim_radios_rht;
 618static int hwsim_radio_idx;
 619static int hwsim_radios_generation = 1;
 620
 621static struct platform_driver mac80211_hwsim_driver = {
 622        .driver = {
 623                .name = "mac80211_hwsim",
 624        },
 625};
 626
 627struct mac80211_hwsim_data {
 628        struct list_head list;
 629        struct rhash_head rht;
 630        struct ieee80211_hw *hw;
 631        struct device *dev;
 632        struct ieee80211_supported_band bands[NUM_NL80211_BANDS];
 633        struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
 634        struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
 635        struct ieee80211_channel channels_6ghz[ARRAY_SIZE(hwsim_channels_6ghz)];
 636        struct ieee80211_channel channels_s1g[ARRAY_SIZE(hwsim_channels_s1g)];
 637        struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
 638        struct ieee80211_iface_combination if_combination;
 639        struct ieee80211_iface_limit if_limits[3];
 640        int n_if_limits;
 641
 642        u32 ciphers[ARRAY_SIZE(hwsim_ciphers)];
 643
 644        struct mac_address addresses[2];
 645        struct ieee80211_chanctx_conf *chanctx;
 646        int channels, idx;
 647        bool use_chanctx;
 648        bool destroy_on_close;
 649        u32 portid;
 650        char alpha2[2];
 651        const struct ieee80211_regdomain *regd;
 652
 653        struct ieee80211_channel *tmp_chan;
 654        struct ieee80211_channel *roc_chan;
 655        u32 roc_duration;
 656        struct delayed_work roc_start;
 657        struct delayed_work roc_done;
 658        struct delayed_work hw_scan;
 659        struct cfg80211_scan_request *hw_scan_request;
 660        struct ieee80211_vif *hw_scan_vif;
 661        int scan_chan_idx;
 662        u8 scan_addr[ETH_ALEN];
 663        struct {
 664                struct ieee80211_channel *channel;
 665                unsigned long next_start, start, end;
 666        } survey_data[ARRAY_SIZE(hwsim_channels_2ghz) +
 667                      ARRAY_SIZE(hwsim_channels_5ghz) +
 668                      ARRAY_SIZE(hwsim_channels_6ghz)];
 669
 670        struct ieee80211_channel *channel;
 671        enum nl80211_chan_width bw;
 672        u64 beacon_int  /* beacon interval in us */;
 673        unsigned int rx_filter;
 674        bool started, idle, scanning;
 675        struct mutex mutex;
 676        struct hrtimer beacon_timer;
 677        enum ps_mode {
 678                PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
 679        } ps;
 680        bool ps_poll_pending;
 681        struct dentry *debugfs;
 682
 683        uintptr_t pending_cookie;
 684        struct sk_buff_head pending;    /* packets pending */
 685        /*
 686         * Only radios in the same group can communicate together (the
 687         * channel has to match too). Each bit represents a group. A
 688         * radio can be in more than one group.
 689         */
 690        u64 group;
 691
 692        /* group shared by radios created in the same netns */
 693        int netgroup;
 694        /* wmediumd portid responsible for netgroup of this radio */
 695        u32 wmediumd;
 696
 697        /* difference between this hw's clock and the real clock, in usecs */
 698        s64 tsf_offset;
 699        s64 bcn_delta;
 700        /* absolute beacon transmission time. Used to cover up "tx" delay. */
 701        u64 abs_bcn_ts;
 702
 703        /* Stats */
 704        u64 tx_pkts;
 705        u64 rx_pkts;
 706        u64 tx_bytes;
 707        u64 rx_bytes;
 708        u64 tx_dropped;
 709        u64 tx_failed;
 710
 711        /* RSSI in rx status of the receiver */
 712        int rx_rssi;
 713};
 714
 715static const struct rhashtable_params hwsim_rht_params = {
 716        .nelem_hint = 2,
 717        .automatic_shrinking = true,
 718        .key_len = ETH_ALEN,
 719        .key_offset = offsetof(struct mac80211_hwsim_data, addresses[1]),
 720        .head_offset = offsetof(struct mac80211_hwsim_data, rht),
 721};
 722
 723struct hwsim_radiotap_hdr {
 724        struct ieee80211_radiotap_header hdr;
 725        __le64 rt_tsft;
 726        u8 rt_flags;
 727        u8 rt_rate;
 728        __le16 rt_channel;
 729        __le16 rt_chbitmask;
 730} __packed;
 731
 732struct hwsim_radiotap_ack_hdr {
 733        struct ieee80211_radiotap_header hdr;
 734        u8 rt_flags;
 735        u8 pad;
 736        __le16 rt_channel;
 737        __le16 rt_chbitmask;
 738} __packed;
 739
 740/* MAC80211_HWSIM netlink family */
 741static struct genl_family hwsim_genl_family;
 742
 743enum hwsim_multicast_groups {
 744        HWSIM_MCGRP_CONFIG,
 745};
 746
 747static const struct genl_multicast_group hwsim_mcgrps[] = {
 748        [HWSIM_MCGRP_CONFIG] = { .name = "config", },
 749};
 750
 751/* MAC80211_HWSIM netlink policy */
 752
 753static const struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
 754        [HWSIM_ATTR_ADDR_RECEIVER] = NLA_POLICY_ETH_ADDR_COMPAT,
 755        [HWSIM_ATTR_ADDR_TRANSMITTER] = NLA_POLICY_ETH_ADDR_COMPAT,
 756        [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
 757                               .len = IEEE80211_MAX_DATA_LEN },
 758        [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
 759        [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
 760        [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
 761        [HWSIM_ATTR_TX_INFO] = { .type = NLA_BINARY,
 762                                 .len = IEEE80211_TX_MAX_RATES *
 763                                        sizeof(struct hwsim_tx_rate)},
 764        [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
 765        [HWSIM_ATTR_CHANNELS] = { .type = NLA_U32 },
 766        [HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
 767        [HWSIM_ATTR_REG_HINT_ALPHA2] = { .type = NLA_STRING, .len = 2 },
 768        [HWSIM_ATTR_REG_CUSTOM_REG] = { .type = NLA_U32 },
 769        [HWSIM_ATTR_REG_STRICT_REG] = { .type = NLA_FLAG },
 770        [HWSIM_ATTR_SUPPORT_P2P_DEVICE] = { .type = NLA_FLAG },
 771        [HWSIM_ATTR_USE_CHANCTX] = { .type = NLA_FLAG },
 772        [HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE] = { .type = NLA_FLAG },
 773        [HWSIM_ATTR_RADIO_NAME] = { .type = NLA_STRING },
 774        [HWSIM_ATTR_NO_VIF] = { .type = NLA_FLAG },
 775        [HWSIM_ATTR_FREQ] = { .type = NLA_U32 },
 776        [HWSIM_ATTR_TX_INFO_FLAGS] = { .type = NLA_BINARY },
 777        [HWSIM_ATTR_PERM_ADDR] = NLA_POLICY_ETH_ADDR_COMPAT,
 778        [HWSIM_ATTR_IFTYPE_SUPPORT] = { .type = NLA_U32 },
 779        [HWSIM_ATTR_CIPHER_SUPPORT] = { .type = NLA_BINARY },
 780};
 781
 782#if IS_REACHABLE(CONFIG_VIRTIO)
 783
 784/* MAC80211_HWSIM virtio queues */
 785static struct virtqueue *hwsim_vqs[HWSIM_NUM_VQS];
 786static bool hwsim_virtio_enabled;
 787static DEFINE_SPINLOCK(hwsim_virtio_lock);
 788
 789static void hwsim_virtio_rx_work(struct work_struct *work);
 790static DECLARE_WORK(hwsim_virtio_rx, hwsim_virtio_rx_work);
 791
 792static int hwsim_tx_virtio(struct mac80211_hwsim_data *data,
 793                           struct sk_buff *skb)
 794{
 795        struct scatterlist sg[1];
 796        unsigned long flags;
 797        int err;
 798
 799        spin_lock_irqsave(&hwsim_virtio_lock, flags);
 800        if (!hwsim_virtio_enabled) {
 801                err = -ENODEV;
 802                goto out_free;
 803        }
 804
 805        sg_init_one(sg, skb->head, skb_end_offset(skb));
 806        err = virtqueue_add_outbuf(hwsim_vqs[HWSIM_VQ_TX], sg, 1, skb,
 807                                   GFP_ATOMIC);
 808        if (err)
 809                goto out_free;
 810        virtqueue_kick(hwsim_vqs[HWSIM_VQ_TX]);
 811        spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
 812        return 0;
 813
 814out_free:
 815        spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
 816        nlmsg_free(skb);
 817        return err;
 818}
 819#else
 820/* cause a linker error if this ends up being needed */
 821extern int hwsim_tx_virtio(struct mac80211_hwsim_data *data,
 822                           struct sk_buff *skb);
 823#define hwsim_virtio_enabled false
 824#endif
 825
 826static int hwsim_get_chanwidth(enum nl80211_chan_width bw)
 827{
 828        switch (bw) {
 829        case NL80211_CHAN_WIDTH_20_NOHT:
 830        case NL80211_CHAN_WIDTH_20:
 831                return 20;
 832        case NL80211_CHAN_WIDTH_40:
 833                return 40;
 834        case NL80211_CHAN_WIDTH_80:
 835                return 80;
 836        case NL80211_CHAN_WIDTH_80P80:
 837        case NL80211_CHAN_WIDTH_160:
 838                return 160;
 839        case NL80211_CHAN_WIDTH_320:
 840                return 320;
 841        case NL80211_CHAN_WIDTH_5:
 842                return 5;
 843        case NL80211_CHAN_WIDTH_10:
 844                return 10;
 845        case NL80211_CHAN_WIDTH_1:
 846                return 1;
 847        case NL80211_CHAN_WIDTH_2:
 848                return 2;
 849        case NL80211_CHAN_WIDTH_4:
 850                return 4;
 851        case NL80211_CHAN_WIDTH_8:
 852                return 8;
 853        case NL80211_CHAN_WIDTH_16:
 854                return 16;
 855        }
 856
 857        return INT_MAX;
 858}
 859
 860static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
 861                                    struct sk_buff *skb,
 862                                    struct ieee80211_channel *chan);
 863
 864/* sysfs attributes */
 865static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
 866{
 867        struct mac80211_hwsim_data *data = dat;
 868        struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
 869        struct sk_buff *skb;
 870        struct ieee80211_pspoll *pspoll;
 871
 872        if (!vp->assoc)
 873                return;
 874
 875        wiphy_dbg(data->hw->wiphy,
 876                  "%s: send PS-Poll to %pM for aid %d\n",
 877                  __func__, vp->bssid, vp->aid);
 878
 879        skb = dev_alloc_skb(sizeof(*pspoll));
 880        if (!skb)
 881                return;
 882        pspoll = skb_put(skb, sizeof(*pspoll));
 883        pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
 884                                            IEEE80211_STYPE_PSPOLL |
 885                                            IEEE80211_FCTL_PM);
 886        pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
 887        memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
 888        memcpy(pspoll->ta, mac, ETH_ALEN);
 889
 890        rcu_read_lock();
 891        mac80211_hwsim_tx_frame(data->hw, skb,
 892                                rcu_dereference(vif->chanctx_conf)->def.chan);
 893        rcu_read_unlock();
 894}
 895
 896static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
 897                                struct ieee80211_vif *vif, int ps)
 898{
 899        struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
 900        struct sk_buff *skb;
 901        struct ieee80211_hdr *hdr;
 902
 903        if (!vp->assoc)
 904                return;
 905
 906        wiphy_dbg(data->hw->wiphy,
 907                  "%s: send data::nullfunc to %pM ps=%d\n",
 908                  __func__, vp->bssid, ps);
 909
 910        skb = dev_alloc_skb(sizeof(*hdr));
 911        if (!skb)
 912                return;
 913        hdr = skb_put(skb, sizeof(*hdr) - ETH_ALEN);
 914        hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
 915                                         IEEE80211_STYPE_NULLFUNC |
 916                                         IEEE80211_FCTL_TODS |
 917                                         (ps ? IEEE80211_FCTL_PM : 0));
 918        hdr->duration_id = cpu_to_le16(0);
 919        memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
 920        memcpy(hdr->addr2, mac, ETH_ALEN);
 921        memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
 922
 923        rcu_read_lock();
 924        mac80211_hwsim_tx_frame(data->hw, skb,
 925                                rcu_dereference(vif->chanctx_conf)->def.chan);
 926        rcu_read_unlock();
 927}
 928
 929
 930static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
 931                                   struct ieee80211_vif *vif)
 932{
 933        struct mac80211_hwsim_data *data = dat;
 934        hwsim_send_nullfunc(data, mac, vif, 1);
 935}
 936
 937static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
 938                                      struct ieee80211_vif *vif)
 939{
 940        struct mac80211_hwsim_data *data = dat;
 941        hwsim_send_nullfunc(data, mac, vif, 0);
 942}
 943
 944static int hwsim_fops_ps_read(void *dat, u64 *val)
 945{
 946        struct mac80211_hwsim_data *data = dat;
 947        *val = data->ps;
 948        return 0;
 949}
 950
 951static int hwsim_fops_ps_write(void *dat, u64 val)
 952{
 953        struct mac80211_hwsim_data *data = dat;
 954        enum ps_mode old_ps;
 955
 956        if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
 957            val != PS_MANUAL_POLL)
 958                return -EINVAL;
 959
 960        if (val == PS_MANUAL_POLL) {
 961                if (data->ps != PS_ENABLED)
 962                        return -EINVAL;
 963                local_bh_disable();
 964                ieee80211_iterate_active_interfaces_atomic(
 965                        data->hw, IEEE80211_IFACE_ITER_NORMAL,
 966                        hwsim_send_ps_poll, data);
 967                local_bh_enable();
 968                return 0;
 969        }
 970        old_ps = data->ps;
 971        data->ps = val;
 972
 973        local_bh_disable();
 974        if (old_ps == PS_DISABLED && val != PS_DISABLED) {
 975                ieee80211_iterate_active_interfaces_atomic(
 976                        data->hw, IEEE80211_IFACE_ITER_NORMAL,
 977                        hwsim_send_nullfunc_ps, data);
 978        } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
 979                ieee80211_iterate_active_interfaces_atomic(
 980                        data->hw, IEEE80211_IFACE_ITER_NORMAL,
 981                        hwsim_send_nullfunc_no_ps, data);
 982        }
 983        local_bh_enable();
 984
 985        return 0;
 986}
 987
 988DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
 989                         "%llu\n");
 990
 991static int hwsim_write_simulate_radar(void *dat, u64 val)
 992{
 993        struct mac80211_hwsim_data *data = dat;
 994
 995        ieee80211_radar_detected(data->hw);
 996
 997        return 0;
 998}
 999
1000DEFINE_DEBUGFS_ATTRIBUTE(hwsim_simulate_radar, NULL,
1001                         hwsim_write_simulate_radar, "%llu\n");
1002
1003static int hwsim_fops_group_read(void *dat, u64 *val)
1004{
1005        struct mac80211_hwsim_data *data = dat;
1006        *val = data->group;
1007        return 0;
1008}
1009
1010static int hwsim_fops_group_write(void *dat, u64 val)
1011{
1012        struct mac80211_hwsim_data *data = dat;
1013        data->group = val;
1014        return 0;
1015}
1016
1017DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_group,
1018                         hwsim_fops_group_read, hwsim_fops_group_write,
1019                         "%llx\n");
1020
1021static int hwsim_fops_rx_rssi_read(void *dat, u64 *val)
1022{
1023        struct mac80211_hwsim_data *data = dat;
1024        *val = data->rx_rssi;
1025        return 0;
1026}
1027
1028static int hwsim_fops_rx_rssi_write(void *dat, u64 val)
1029{
1030        struct mac80211_hwsim_data *data = dat;
1031        int rssi = (int)val;
1032
1033        if (rssi >= 0 || rssi < -100)
1034                return -EINVAL;
1035
1036        data->rx_rssi = rssi;
1037        return 0;
1038}
1039
1040DEFINE_DEBUGFS_ATTRIBUTE(hwsim_fops_rx_rssi,
1041                         hwsim_fops_rx_rssi_read, hwsim_fops_rx_rssi_write,
1042                         "%lld\n");
1043
1044static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
1045                                        struct net_device *dev)
1046{
1047        /* TODO: allow packet injection */
1048        dev_kfree_skb(skb);
1049        return NETDEV_TX_OK;
1050}
1051
1052static inline u64 mac80211_hwsim_get_tsf_raw(void)
1053{
1054        return ktime_to_us(ktime_get_real());
1055}
1056
1057static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
1058{
1059        u64 now = mac80211_hwsim_get_tsf_raw();
1060        return cpu_to_le64(now + data->tsf_offset);
1061}
1062
1063static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
1064                                  struct ieee80211_vif *vif)
1065{
1066        struct mac80211_hwsim_data *data = hw->priv;
1067        return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
1068}
1069
1070static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
1071                struct ieee80211_vif *vif, u64 tsf)
1072{
1073        struct mac80211_hwsim_data *data = hw->priv;
1074        u64 now = mac80211_hwsim_get_tsf(hw, vif);
1075        u32 bcn_int = data->beacon_int;
1076        u64 delta = abs(tsf - now);
1077
1078        /* adjust after beaconing with new timestamp at old TBTT */
1079        if (tsf > now) {
1080                data->tsf_offset += delta;
1081                data->bcn_delta = do_div(delta, bcn_int);
1082        } else {
1083                data->tsf_offset -= delta;
1084                data->bcn_delta = -(s64)do_div(delta, bcn_int);
1085        }
1086}
1087
1088static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
1089                                      struct sk_buff *tx_skb,
1090                                      struct ieee80211_channel *chan)
1091{
1092        struct mac80211_hwsim_data *data = hw->priv;
1093        struct sk_buff *skb;
1094        struct hwsim_radiotap_hdr *hdr;
1095        u16 flags, bitrate;
1096        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
1097        struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
1098
1099        if (!txrate)
1100                bitrate = 0;
1101        else
1102                bitrate = txrate->bitrate;
1103
1104        if (!netif_running(hwsim_mon))
1105                return;
1106
1107        skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
1108        if (skb == NULL)
1109                return;
1110
1111        hdr = skb_push(skb, sizeof(*hdr));
1112        hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
1113        hdr->hdr.it_pad = 0;
1114        hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
1115        hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
1116                                          (1 << IEEE80211_RADIOTAP_RATE) |
1117                                          (1 << IEEE80211_RADIOTAP_TSFT) |
1118                                          (1 << IEEE80211_RADIOTAP_CHANNEL));
1119        hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
1120        hdr->rt_flags = 0;
1121        hdr->rt_rate = bitrate / 5;
1122        hdr->rt_channel = cpu_to_le16(chan->center_freq);
1123        flags = IEEE80211_CHAN_2GHZ;
1124        if (txrate && txrate->flags & IEEE80211_RATE_ERP_G)
1125                flags |= IEEE80211_CHAN_OFDM;
1126        else
1127                flags |= IEEE80211_CHAN_CCK;
1128        hdr->rt_chbitmask = cpu_to_le16(flags);
1129
1130        skb->dev = hwsim_mon;
1131        skb_reset_mac_header(skb);
1132        skb->ip_summed = CHECKSUM_UNNECESSARY;
1133        skb->pkt_type = PACKET_OTHERHOST;
1134        skb->protocol = htons(ETH_P_802_2);
1135        memset(skb->cb, 0, sizeof(skb->cb));
1136        netif_rx(skb);
1137}
1138
1139
1140static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
1141                                       const u8 *addr)
1142{
1143        struct sk_buff *skb;
1144        struct hwsim_radiotap_ack_hdr *hdr;
1145        u16 flags;
1146        struct ieee80211_hdr *hdr11;
1147
1148        if (!netif_running(hwsim_mon))
1149                return;
1150
1151        skb = dev_alloc_skb(100);
1152        if (skb == NULL)
1153                return;
1154
1155        hdr = skb_put(skb, sizeof(*hdr));
1156        hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
1157        hdr->hdr.it_pad = 0;
1158        hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
1159        hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
1160                                          (1 << IEEE80211_RADIOTAP_CHANNEL));
1161        hdr->rt_flags = 0;
1162        hdr->pad = 0;
1163        hdr->rt_channel = cpu_to_le16(chan->center_freq);
1164        flags = IEEE80211_CHAN_2GHZ;
1165        hdr->rt_chbitmask = cpu_to_le16(flags);
1166
1167        hdr11 = skb_put(skb, 10);
1168        hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
1169                                           IEEE80211_STYPE_ACK);
1170        hdr11->duration_id = cpu_to_le16(0);
1171        memcpy(hdr11->addr1, addr, ETH_ALEN);
1172
1173        skb->dev = hwsim_mon;
1174        skb_reset_mac_header(skb);
1175        skb->ip_summed = CHECKSUM_UNNECESSARY;
1176        skb->pkt_type = PACKET_OTHERHOST;
1177        skb->protocol = htons(ETH_P_802_2);
1178        memset(skb->cb, 0, sizeof(skb->cb));
1179        netif_rx(skb);
1180}
1181
1182struct mac80211_hwsim_addr_match_data {
1183        u8 addr[ETH_ALEN];
1184        bool ret;
1185};
1186
1187static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
1188                                     struct ieee80211_vif *vif)
1189{
1190        struct mac80211_hwsim_addr_match_data *md = data;
1191
1192        if (memcmp(mac, md->addr, ETH_ALEN) == 0)
1193                md->ret = true;
1194}
1195
1196static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
1197                                      const u8 *addr)
1198{
1199        struct mac80211_hwsim_addr_match_data md = {
1200                .ret = false,
1201        };
1202
1203        if (data->scanning && memcmp(addr, data->scan_addr, ETH_ALEN) == 0)
1204                return true;
1205
1206        memcpy(md.addr, addr, ETH_ALEN);
1207
1208        ieee80211_iterate_active_interfaces_atomic(data->hw,
1209                                                   IEEE80211_IFACE_ITER_NORMAL,
1210                                                   mac80211_hwsim_addr_iter,
1211                                                   &md);
1212
1213        return md.ret;
1214}
1215
1216static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
1217                           struct sk_buff *skb)
1218{
1219        switch (data->ps) {
1220        case PS_DISABLED:
1221                return true;
1222        case PS_ENABLED:
1223                return false;
1224        case PS_AUTO_POLL:
1225                /* TODO: accept (some) Beacons by default and other frames only
1226                 * if pending PS-Poll has been sent */
1227                return true;
1228        case PS_MANUAL_POLL:
1229                /* Allow unicast frames to own address if there is a pending
1230                 * PS-Poll */
1231                if (data->ps_poll_pending &&
1232                    mac80211_hwsim_addr_match(data, skb->data + 4)) {
1233                        data->ps_poll_pending = false;
1234                        return true;
1235                }
1236                return false;
1237        }
1238
1239        return true;
1240}
1241
1242static int hwsim_unicast_netgroup(struct mac80211_hwsim_data *data,
1243                                  struct sk_buff *skb, int portid)
1244{
1245        struct net *net;
1246        bool found = false;
1247        int res = -ENOENT;
1248
1249        rcu_read_lock();
1250        for_each_net_rcu(net) {
1251                if (data->netgroup == hwsim_net_get_netgroup(net)) {
1252                        res = genlmsg_unicast(net, skb, portid);
1253                        found = true;
1254                        break;
1255                }
1256        }
1257        rcu_read_unlock();
1258
1259        if (!found)
1260                nlmsg_free(skb);
1261
1262        return res;
1263}
1264
1265static void mac80211_hwsim_config_mac_nl(struct ieee80211_hw *hw,
1266                                         const u8 *addr, bool add)
1267{
1268        struct mac80211_hwsim_data *data = hw->priv;
1269        u32 _portid = READ_ONCE(data->wmediumd);
1270        struct sk_buff *skb;
1271        void *msg_head;
1272
1273        if (!_portid && !hwsim_virtio_enabled)
1274                return;
1275
1276        skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1277        if (!skb)
1278                return;
1279
1280        msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1281                               add ? HWSIM_CMD_ADD_MAC_ADDR :
1282                                     HWSIM_CMD_DEL_MAC_ADDR);
1283        if (!msg_head) {
1284                pr_debug("mac80211_hwsim: problem with msg_head\n");
1285                goto nla_put_failure;
1286        }
1287
1288        if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1289                    ETH_ALEN, data->addresses[1].addr))
1290                goto nla_put_failure;
1291
1292        if (nla_put(skb, HWSIM_ATTR_ADDR_RECEIVER, ETH_ALEN, addr))
1293                goto nla_put_failure;
1294
1295        genlmsg_end(skb, msg_head);
1296
1297        if (hwsim_virtio_enabled)
1298                hwsim_tx_virtio(data, skb);
1299        else
1300                hwsim_unicast_netgroup(data, skb, _portid);
1301        return;
1302nla_put_failure:
1303        nlmsg_free(skb);
1304}
1305
1306static inline u16 trans_tx_rate_flags_ieee2hwsim(struct ieee80211_tx_rate *rate)
1307{
1308        u16 result = 0;
1309
1310        if (rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
1311                result |= MAC80211_HWSIM_TX_RC_USE_RTS_CTS;
1312        if (rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1313                result |= MAC80211_HWSIM_TX_RC_USE_CTS_PROTECT;
1314        if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1315                result |= MAC80211_HWSIM_TX_RC_USE_SHORT_PREAMBLE;
1316        if (rate->flags & IEEE80211_TX_RC_MCS)
1317                result |= MAC80211_HWSIM_TX_RC_MCS;
1318        if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
1319                result |= MAC80211_HWSIM_TX_RC_GREEN_FIELD;
1320        if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1321                result |= MAC80211_HWSIM_TX_RC_40_MHZ_WIDTH;
1322        if (rate->flags & IEEE80211_TX_RC_DUP_DATA)
1323                result |= MAC80211_HWSIM_TX_RC_DUP_DATA;
1324        if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
1325                result |= MAC80211_HWSIM_TX_RC_SHORT_GI;
1326        if (rate->flags & IEEE80211_TX_RC_VHT_MCS)
1327                result |= MAC80211_HWSIM_TX_RC_VHT_MCS;
1328        if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1329                result |= MAC80211_HWSIM_TX_RC_80_MHZ_WIDTH;
1330        if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1331                result |= MAC80211_HWSIM_TX_RC_160_MHZ_WIDTH;
1332
1333        return result;
1334}
1335
1336static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
1337                                       struct sk_buff *my_skb,
1338                                       int dst_portid,
1339                                       struct ieee80211_channel *channel)
1340{
1341        struct sk_buff *skb;
1342        struct mac80211_hwsim_data *data = hw->priv;
1343        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
1344        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
1345        void *msg_head;
1346        unsigned int hwsim_flags = 0;
1347        int i;
1348        struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
1349        struct hwsim_tx_rate_flag tx_attempts_flags[IEEE80211_TX_MAX_RATES];
1350        uintptr_t cookie;
1351
1352        if (data->ps != PS_DISABLED)
1353                hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1354        /* If the queue contains MAX_QUEUE skb's drop some */
1355        if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
1356                /* Dropping until WARN_QUEUE level */
1357                while (skb_queue_len(&data->pending) >= WARN_QUEUE) {
1358                        ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1359                        data->tx_dropped++;
1360                }
1361        }
1362
1363        skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1364        if (skb == NULL)
1365                goto nla_put_failure;
1366
1367        msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
1368                               HWSIM_CMD_FRAME);
1369        if (msg_head == NULL) {
1370                pr_debug("mac80211_hwsim: problem with msg_head\n");
1371                goto nla_put_failure;
1372        }
1373
1374        if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
1375                    ETH_ALEN, data->addresses[1].addr))
1376                goto nla_put_failure;
1377
1378        /* We get the skb->data */
1379        if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
1380                goto nla_put_failure;
1381
1382        /* We get the flags for this transmission, and we translate them to
1383           wmediumd flags  */
1384
1385        if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
1386                hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
1387
1388        if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1389                hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
1390
1391        if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
1392                goto nla_put_failure;
1393
1394        if (nla_put_u32(skb, HWSIM_ATTR_FREQ, channel->center_freq))
1395                goto nla_put_failure;
1396
1397        /* We get the tx control (rate and retries) info*/
1398
1399        for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1400                tx_attempts[i].idx = info->status.rates[i].idx;
1401                tx_attempts_flags[i].idx = info->status.rates[i].idx;
1402                tx_attempts[i].count = info->status.rates[i].count;
1403                tx_attempts_flags[i].flags =
1404                                trans_tx_rate_flags_ieee2hwsim(
1405                                                &info->status.rates[i]);
1406        }
1407
1408        if (nla_put(skb, HWSIM_ATTR_TX_INFO,
1409                    sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
1410                    tx_attempts))
1411                goto nla_put_failure;
1412
1413        if (nla_put(skb, HWSIM_ATTR_TX_INFO_FLAGS,
1414                    sizeof(struct hwsim_tx_rate_flag) * IEEE80211_TX_MAX_RATES,
1415                    tx_attempts_flags))
1416                goto nla_put_failure;
1417
1418        /* We create a cookie to identify this skb */
1419        data->pending_cookie++;
1420        cookie = data->pending_cookie;
1421        info->rate_driver_data[0] = (void *)cookie;
1422        if (nla_put_u64_64bit(skb, HWSIM_ATTR_COOKIE, cookie, HWSIM_ATTR_PAD))
1423                goto nla_put_failure;
1424
1425        genlmsg_end(skb, msg_head);
1426
1427        if (hwsim_virtio_enabled) {
1428                if (hwsim_tx_virtio(data, skb))
1429                        goto err_free_txskb;
1430        } else {
1431                if (hwsim_unicast_netgroup(data, skb, dst_portid))
1432                        goto err_free_txskb;
1433        }
1434
1435        /* Enqueue the packet */
1436        skb_queue_tail(&data->pending, my_skb);
1437        data->tx_pkts++;
1438        data->tx_bytes += my_skb->len;
1439        return;
1440
1441nla_put_failure:
1442        nlmsg_free(skb);
1443err_free_txskb:
1444        pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
1445        ieee80211_free_txskb(hw, my_skb);
1446        data->tx_failed++;
1447}
1448
1449static bool hwsim_chans_compat(struct ieee80211_channel *c1,
1450                               struct ieee80211_channel *c2)
1451{
1452        if (!c1 || !c2)
1453                return false;
1454
1455        return c1->center_freq == c2->center_freq;
1456}
1457
1458struct tx_iter_data {
1459        struct ieee80211_channel *channel;
1460        bool receive;
1461};
1462
1463static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
1464                                   struct ieee80211_vif *vif)
1465{
1466        struct tx_iter_data *data = _data;
1467
1468        if (!vif->chanctx_conf)
1469                return;
1470
1471        if (!hwsim_chans_compat(data->channel,
1472                                rcu_dereference(vif->chanctx_conf)->def.chan))
1473                return;
1474
1475        data->receive = true;
1476}
1477
1478static void mac80211_hwsim_add_vendor_rtap(struct sk_buff *skb)
1479{
1480        /*
1481         * To enable this code, #define the HWSIM_RADIOTAP_OUI,
1482         * e.g. like this:
1483         * #define HWSIM_RADIOTAP_OUI "\x02\x00\x00"
1484         * (but you should use a valid OUI, not that)
1485         *
1486         * If anyone wants to 'donate' a radiotap OUI/subns code
1487         * please send a patch removing this #ifdef and changing
1488         * the values accordingly.
1489         */
1490#ifdef HWSIM_RADIOTAP_OUI
1491        struct ieee80211_vendor_radiotap *rtap;
1492
1493        /*
1494         * Note that this code requires the headroom in the SKB
1495         * that was allocated earlier.
1496         */
1497        rtap = skb_push(skb, sizeof(*rtap) + 8 + 4);
1498        rtap->oui[0] = HWSIM_RADIOTAP_OUI[0];
1499        rtap->oui[1] = HWSIM_RADIOTAP_OUI[1];
1500        rtap->oui[2] = HWSIM_RADIOTAP_OUI[2];
1501        rtap->subns = 127;
1502
1503        /*
1504         * Radiotap vendor namespaces can (and should) also be
1505         * split into fields by using the standard radiotap
1506         * presence bitmap mechanism. Use just BIT(0) here for
1507         * the presence bitmap.
1508         */
1509        rtap->present = BIT(0);
1510        /* We have 8 bytes of (dummy) data */
1511        rtap->len = 8;
1512        /* For testing, also require it to be aligned */
1513        rtap->align = 8;
1514        /* And also test that padding works, 4 bytes */
1515        rtap->pad = 4;
1516        /* push the data */
1517        memcpy(rtap->data, "ABCDEFGH", 8);
1518        /* make sure to clear padding, mac80211 doesn't */
1519        memset(rtap->data + 8, 0, 4);
1520
1521        IEEE80211_SKB_RXCB(skb)->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
1522#endif
1523}
1524
1525static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
1526                                          struct sk_buff *skb,
1527                                          struct ieee80211_channel *chan)
1528{
1529        struct mac80211_hwsim_data *data = hw->priv, *data2;
1530        bool ack = false;
1531        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1532        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1533        struct ieee80211_rx_status rx_status;
1534        u64 now;
1535
1536        memset(&rx_status, 0, sizeof(rx_status));
1537        rx_status.flag |= RX_FLAG_MACTIME_START;
1538        rx_status.freq = chan->center_freq;
1539        rx_status.freq_offset = chan->freq_offset ? 1 : 0;
1540        rx_status.band = chan->band;
1541        if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
1542                rx_status.rate_idx =
1543                        ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
1544                rx_status.nss =
1545                        ieee80211_rate_get_vht_nss(&info->control.rates[0]);
1546                rx_status.encoding = RX_ENC_VHT;
1547        } else {
1548                rx_status.rate_idx = info->control.rates[0].idx;
1549                if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
1550                        rx_status.encoding = RX_ENC_HT;
1551        }
1552        if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1553                rx_status.bw = RATE_INFO_BW_40;
1554        else if (info->control.rates[0].flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1555                rx_status.bw = RATE_INFO_BW_80;
1556        else if (info->control.rates[0].flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1557                rx_status.bw = RATE_INFO_BW_160;
1558        else
1559                rx_status.bw = RATE_INFO_BW_20;
1560        if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
1561                rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
1562        /* TODO: simulate optional packet loss */
1563        rx_status.signal = data->rx_rssi;
1564        if (info->control.vif)
1565                rx_status.signal += info->control.vif->bss_conf.txpower;
1566
1567        if (data->ps != PS_DISABLED)
1568                hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1569
1570        /* release the skb's source info */
1571        skb_orphan(skb);
1572        skb_dst_drop(skb);
1573        skb->mark = 0;
1574        skb_ext_reset(skb);
1575        nf_reset_ct(skb);
1576
1577        /*
1578         * Get absolute mactime here so all HWs RX at the "same time", and
1579         * absolute TX time for beacon mactime so the timestamp matches.
1580         * Giving beacons a different mactime than non-beacons looks messy, but
1581         * it helps the Toffset be exact and a ~10us mactime discrepancy
1582         * probably doesn't really matter.
1583         */
1584        if (ieee80211_is_beacon(hdr->frame_control) ||
1585            ieee80211_is_probe_resp(hdr->frame_control)) {
1586                rx_status.boottime_ns = ktime_get_boottime_ns();
1587                now = data->abs_bcn_ts;
1588        } else {
1589                now = mac80211_hwsim_get_tsf_raw();
1590        }
1591
1592        /* Copy skb to all enabled radios that are on the current frequency */
1593        spin_lock(&hwsim_radio_lock);
1594        list_for_each_entry(data2, &hwsim_radios, list) {
1595                struct sk_buff *nskb;
1596                struct tx_iter_data tx_iter_data = {
1597                        .receive = false,
1598                        .channel = chan,
1599                };
1600
1601                if (data == data2)
1602                        continue;
1603
1604                if (!data2->started || (data2->idle && !data2->tmp_chan) ||
1605                    !hwsim_ps_rx_ok(data2, skb))
1606                        continue;
1607
1608                if (!(data->group & data2->group))
1609                        continue;
1610
1611                if (data->netgroup != data2->netgroup)
1612                        continue;
1613
1614                if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
1615                    !hwsim_chans_compat(chan, data2->channel)) {
1616                        ieee80211_iterate_active_interfaces_atomic(
1617                                data2->hw, IEEE80211_IFACE_ITER_NORMAL,
1618                                mac80211_hwsim_tx_iter, &tx_iter_data);
1619                        if (!tx_iter_data.receive)
1620                                continue;
1621                }
1622
1623                /*
1624                 * reserve some space for our vendor and the normal
1625                 * radiotap header, since we're copying anyway
1626                 */
1627                if (skb->len < PAGE_SIZE && paged_rx) {
1628                        struct page *page = alloc_page(GFP_ATOMIC);
1629
1630                        if (!page)
1631                                continue;
1632
1633                        nskb = dev_alloc_skb(128);
1634                        if (!nskb) {
1635                                __free_page(page);
1636                                continue;
1637                        }
1638
1639                        memcpy(page_address(page), skb->data, skb->len);
1640                        skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
1641                } else {
1642                        nskb = skb_copy(skb, GFP_ATOMIC);
1643                        if (!nskb)
1644                                continue;
1645                }
1646
1647                if (mac80211_hwsim_addr_match(data2, hdr->addr1))
1648                        ack = true;
1649
1650                rx_status.mactime = now + data2->tsf_offset;
1651
1652                memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
1653
1654                mac80211_hwsim_add_vendor_rtap(nskb);
1655
1656                data2->rx_pkts++;
1657                data2->rx_bytes += nskb->len;
1658                ieee80211_rx_irqsafe(data2->hw, nskb);
1659        }
1660        spin_unlock(&hwsim_radio_lock);
1661
1662        return ack;
1663}
1664
1665static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
1666                              struct ieee80211_tx_control *control,
1667                              struct sk_buff *skb)
1668{
1669        struct mac80211_hwsim_data *data = hw->priv;
1670        struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1671        struct ieee80211_hdr *hdr = (void *)skb->data;
1672        struct ieee80211_chanctx_conf *chanctx_conf;
1673        struct ieee80211_channel *channel;
1674        bool ack;
1675        enum nl80211_chan_width confbw = NL80211_CHAN_WIDTH_20_NOHT;
1676        u32 _portid, i;
1677
1678        if (WARN_ON(skb->len < 10)) {
1679                /* Should not happen; just a sanity check for addr1 use */
1680                ieee80211_free_txskb(hw, skb);
1681                return;
1682        }
1683
1684        if (!data->use_chanctx) {
1685                channel = data->channel;
1686                confbw = data->bw;
1687        } else if (txi->hw_queue == 4) {
1688                channel = data->tmp_chan;
1689        } else {
1690                chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
1691                if (chanctx_conf) {
1692                        channel = chanctx_conf->def.chan;
1693                        confbw = chanctx_conf->def.width;
1694                } else {
1695                        channel = NULL;
1696                }
1697        }
1698
1699        if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
1700                ieee80211_free_txskb(hw, skb);
1701                return;
1702        }
1703
1704        if (data->idle && !data->tmp_chan) {
1705                wiphy_dbg(hw->wiphy, "Trying to TX when idle - reject\n");
1706                ieee80211_free_txskb(hw, skb);
1707                return;
1708        }
1709
1710        if (txi->control.vif)
1711                hwsim_check_magic(txi->control.vif);
1712        if (control->sta)
1713                hwsim_check_sta_magic(control->sta);
1714
1715        if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1716                ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
1717                                       txi->control.rates,
1718                                       ARRAY_SIZE(txi->control.rates));
1719
1720        for (i = 0; i < ARRAY_SIZE(txi->control.rates); i++) {
1721                u16 rflags = txi->control.rates[i].flags;
1722                /* initialize to data->bw for 5/10 MHz handling */
1723                enum nl80211_chan_width bw = data->bw;
1724
1725                if (txi->control.rates[i].idx == -1)
1726                        break;
1727
1728                if (rflags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1729                        bw = NL80211_CHAN_WIDTH_40;
1730                else if (rflags & IEEE80211_TX_RC_80_MHZ_WIDTH)
1731                        bw = NL80211_CHAN_WIDTH_80;
1732                else if (rflags & IEEE80211_TX_RC_160_MHZ_WIDTH)
1733                        bw = NL80211_CHAN_WIDTH_160;
1734
1735                if (WARN_ON(hwsim_get_chanwidth(bw) > hwsim_get_chanwidth(confbw)))
1736                        return;
1737        }
1738
1739        if (skb->len >= 24 + 8 &&
1740            ieee80211_is_probe_resp(hdr->frame_control)) {
1741                /* fake header transmission time */
1742                struct ieee80211_mgmt *mgmt;
1743                struct ieee80211_rate *txrate;
1744                /* TODO: get MCS */
1745                int bitrate = 100;
1746                u64 ts;
1747
1748                mgmt = (struct ieee80211_mgmt *)skb->data;
1749                txrate = ieee80211_get_tx_rate(hw, txi);
1750                if (txrate)
1751                        bitrate = txrate->bitrate;
1752                ts = mac80211_hwsim_get_tsf_raw();
1753                mgmt->u.probe_resp.timestamp =
1754                        cpu_to_le64(ts + data->tsf_offset +
1755                                    24 * 8 * 10 / bitrate);
1756        }
1757
1758        mac80211_hwsim_monitor_rx(hw, skb, channel);
1759
1760        /* wmediumd mode check */
1761        _portid = READ_ONCE(data->wmediumd);
1762
1763        if (_portid || hwsim_virtio_enabled)
1764                return mac80211_hwsim_tx_frame_nl(hw, skb, _portid, channel);
1765
1766        /* NO wmediumd detected, perfect medium simulation */
1767        data->tx_pkts++;
1768        data->tx_bytes += skb->len;
1769        ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
1770
1771        if (ack && skb->len >= 16)
1772                mac80211_hwsim_monitor_ack(channel, hdr->addr2);
1773
1774        ieee80211_tx_info_clear_status(txi);
1775
1776        /* frame was transmitted at most favorable rate at first attempt */
1777        txi->control.rates[0].count = 1;
1778        txi->control.rates[1].idx = -1;
1779
1780        if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
1781                txi->flags |= IEEE80211_TX_STAT_ACK;
1782        ieee80211_tx_status_irqsafe(hw, skb);
1783}
1784
1785
1786static int mac80211_hwsim_start(struct ieee80211_hw *hw)
1787{
1788        struct mac80211_hwsim_data *data = hw->priv;
1789        wiphy_dbg(hw->wiphy, "%s\n", __func__);
1790        data->started = true;
1791        return 0;
1792}
1793
1794
1795static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
1796{
1797        struct mac80211_hwsim_data *data = hw->priv;
1798
1799        data->started = false;
1800        hrtimer_cancel(&data->beacon_timer);
1801
1802        while (!skb_queue_empty(&data->pending))
1803                ieee80211_free_txskb(hw, skb_dequeue(&data->pending));
1804
1805        wiphy_dbg(hw->wiphy, "%s\n", __func__);
1806}
1807
1808
1809static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
1810                                        struct ieee80211_vif *vif)
1811{
1812        wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1813                  __func__, ieee80211_vif_type_p2p(vif),
1814                  vif->addr);
1815        hwsim_set_magic(vif);
1816
1817        if (vif->type != NL80211_IFTYPE_MONITOR)
1818                mac80211_hwsim_config_mac_nl(hw, vif->addr, true);
1819
1820        vif->cab_queue = 0;
1821        vif->hw_queue[IEEE80211_AC_VO] = 0;
1822        vif->hw_queue[IEEE80211_AC_VI] = 1;
1823        vif->hw_queue[IEEE80211_AC_BE] = 2;
1824        vif->hw_queue[IEEE80211_AC_BK] = 3;
1825
1826        return 0;
1827}
1828
1829
1830static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
1831                                           struct ieee80211_vif *vif,
1832                                           enum nl80211_iftype newtype,
1833                                           bool newp2p)
1834{
1835        newtype = ieee80211_iftype_p2p(newtype, newp2p);
1836        wiphy_dbg(hw->wiphy,
1837                  "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
1838                  __func__, ieee80211_vif_type_p2p(vif),
1839                    newtype, vif->addr);
1840        hwsim_check_magic(vif);
1841
1842        /*
1843         * interface may change from non-AP to AP in
1844         * which case this needs to be set up again
1845         */
1846        vif->cab_queue = 0;
1847
1848        return 0;
1849}
1850
1851static void mac80211_hwsim_remove_interface(
1852        struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1853{
1854        wiphy_dbg(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
1855                  __func__, ieee80211_vif_type_p2p(vif),
1856                  vif->addr);
1857        hwsim_check_magic(vif);
1858        hwsim_clear_magic(vif);
1859        if (vif->type != NL80211_IFTYPE_MONITOR)
1860                mac80211_hwsim_config_mac_nl(hw, vif->addr, false);
1861}
1862
1863static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
1864                                    struct sk_buff *skb,
1865                                    struct ieee80211_channel *chan)
1866{
1867        struct mac80211_hwsim_data *data = hw->priv;
1868        u32 _pid = READ_ONCE(data->wmediumd);
1869
1870        if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE)) {
1871                struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
1872                ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
1873                                       txi->control.rates,
1874                                       ARRAY_SIZE(txi->control.rates));
1875        }
1876
1877        mac80211_hwsim_monitor_rx(hw, skb, chan);
1878
1879        if (_pid || hwsim_virtio_enabled)
1880                return mac80211_hwsim_tx_frame_nl(hw, skb, _pid, chan);
1881
1882        data->tx_pkts++;
1883        data->tx_bytes += skb->len;
1884        mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
1885        dev_kfree_skb(skb);
1886}
1887
1888static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
1889                                     struct ieee80211_vif *vif)
1890{
1891        struct mac80211_hwsim_data *data = arg;
1892        struct ieee80211_hw *hw = data->hw;
1893        struct ieee80211_tx_info *info;
1894        struct ieee80211_rate *txrate;
1895        struct ieee80211_mgmt *mgmt;
1896        struct sk_buff *skb;
1897        /* TODO: get MCS */
1898        int bitrate = 100;
1899
1900        hwsim_check_magic(vif);
1901
1902        if (vif->type != NL80211_IFTYPE_AP &&
1903            vif->type != NL80211_IFTYPE_MESH_POINT &&
1904            vif->type != NL80211_IFTYPE_ADHOC &&
1905            vif->type != NL80211_IFTYPE_OCB)
1906                return;
1907
1908        skb = ieee80211_beacon_get(hw, vif);
1909        if (skb == NULL)
1910                return;
1911        info = IEEE80211_SKB_CB(skb);
1912        if (ieee80211_hw_check(hw, SUPPORTS_RC_TABLE))
1913                ieee80211_get_tx_rates(vif, NULL, skb,
1914                                       info->control.rates,
1915                                       ARRAY_SIZE(info->control.rates));
1916
1917        txrate = ieee80211_get_tx_rate(hw, info);
1918        if (txrate)
1919                bitrate = txrate->bitrate;
1920
1921        mgmt = (struct ieee80211_mgmt *) skb->data;
1922        /* fake header transmission time */
1923        data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
1924        if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
1925                struct ieee80211_ext *ext = (void *) mgmt;
1926
1927                ext->u.s1g_beacon.timestamp = cpu_to_le32(data->abs_bcn_ts +
1928                                                          data->tsf_offset +
1929                                                          10 * 8 * 10 /
1930                                                          bitrate);
1931        } else {
1932                mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
1933                                                       data->tsf_offset +
1934                                                       24 * 8 * 10 /
1935                                                       bitrate);
1936        }
1937
1938        mac80211_hwsim_tx_frame(hw, skb,
1939                                rcu_dereference(vif->chanctx_conf)->def.chan);
1940
1941        while ((skb = ieee80211_get_buffered_bc(hw, vif)) != NULL) {
1942                mac80211_hwsim_tx_frame(hw, skb,
1943                                rcu_dereference(vif->chanctx_conf)->def.chan);
1944        }
1945
1946        if (vif->csa_active && ieee80211_beacon_cntdwn_is_complete(vif))
1947                ieee80211_csa_finish(vif);
1948}
1949
1950static enum hrtimer_restart
1951mac80211_hwsim_beacon(struct hrtimer *timer)
1952{
1953        struct mac80211_hwsim_data *data =
1954                container_of(timer, struct mac80211_hwsim_data, beacon_timer);
1955        struct ieee80211_hw *hw = data->hw;
1956        u64 bcn_int = data->beacon_int;
1957
1958        if (!data->started)
1959                return HRTIMER_NORESTART;
1960
1961        ieee80211_iterate_active_interfaces_atomic(
1962                hw, IEEE80211_IFACE_ITER_NORMAL,
1963                mac80211_hwsim_beacon_tx, data);
1964
1965        /* beacon at new TBTT + beacon interval */
1966        if (data->bcn_delta) {
1967                bcn_int -= data->bcn_delta;
1968                data->bcn_delta = 0;
1969        }
1970        hrtimer_forward_now(&data->beacon_timer,
1971                            ns_to_ktime(bcn_int * NSEC_PER_USEC));
1972        return HRTIMER_RESTART;
1973}
1974
1975static const char * const hwsim_chanwidths[] = {
1976        [NL80211_CHAN_WIDTH_5] = "ht5",
1977        [NL80211_CHAN_WIDTH_10] = "ht10",
1978        [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
1979        [NL80211_CHAN_WIDTH_20] = "ht20",
1980        [NL80211_CHAN_WIDTH_40] = "ht40",
1981        [NL80211_CHAN_WIDTH_80] = "vht80",
1982        [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
1983        [NL80211_CHAN_WIDTH_160] = "vht160",
1984        [NL80211_CHAN_WIDTH_1] = "1MHz",
1985        [NL80211_CHAN_WIDTH_2] = "2MHz",
1986        [NL80211_CHAN_WIDTH_4] = "4MHz",
1987        [NL80211_CHAN_WIDTH_8] = "8MHz",
1988        [NL80211_CHAN_WIDTH_16] = "16MHz",
1989};
1990
1991static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
1992{
1993        struct mac80211_hwsim_data *data = hw->priv;
1994        struct ieee80211_conf *conf = &hw->conf;
1995        static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
1996                [IEEE80211_SMPS_AUTOMATIC] = "auto",
1997                [IEEE80211_SMPS_OFF] = "off",
1998                [IEEE80211_SMPS_STATIC] = "static",
1999                [IEEE80211_SMPS_DYNAMIC] = "dynamic",
2000        };
2001        int idx;
2002
2003        if (conf->chandef.chan)
2004                wiphy_dbg(hw->wiphy,
2005                          "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
2006                          __func__,
2007                          conf->chandef.chan->center_freq,
2008                          conf->chandef.center_freq1,
2009                          conf->chandef.center_freq2,
2010                          hwsim_chanwidths[conf->chandef.width],
2011                          !!(conf->flags & IEEE80211_CONF_IDLE),
2012                          !!(conf->flags & IEEE80211_CONF_PS),
2013                          smps_modes[conf->smps_mode]);
2014        else
2015                wiphy_dbg(hw->wiphy,
2016                          "%s (freq=0 idle=%d ps=%d smps=%s)\n",
2017                          __func__,
2018                          !!(conf->flags & IEEE80211_CONF_IDLE),
2019                          !!(conf->flags & IEEE80211_CONF_PS),
2020                          smps_modes[conf->smps_mode]);
2021
2022        data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
2023
2024        WARN_ON(conf->chandef.chan && data->use_chanctx);
2025
2026        mutex_lock(&data->mutex);
2027        if (data->scanning && conf->chandef.chan) {
2028                for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
2029                        if (data->survey_data[idx].channel == data->channel) {
2030                                data->survey_data[idx].start =
2031                                        data->survey_data[idx].next_start;
2032                                data->survey_data[idx].end = jiffies;
2033                                break;
2034                        }
2035                }
2036
2037                data->channel = conf->chandef.chan;
2038                data->bw = conf->chandef.width;
2039
2040                for (idx = 0; idx < ARRAY_SIZE(data->survey_data); idx++) {
2041                        if (data->survey_data[idx].channel &&
2042                            data->survey_data[idx].channel != data->channel)
2043                                continue;
2044                        data->survey_data[idx].channel = data->channel;
2045                        data->survey_data[idx].next_start = jiffies;
2046                        break;
2047                }
2048        } else {
2049                data->channel = conf->chandef.chan;
2050                data->bw = conf->chandef.width;
2051        }
2052        mutex_unlock(&data->mutex);
2053
2054        if (!data->started || !data->beacon_int)
2055                hrtimer_cancel(&data->beacon_timer);
2056        else if (!hrtimer_is_queued(&data->beacon_timer)) {
2057                u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
2058                u32 bcn_int = data->beacon_int;
2059                u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
2060
2061                hrtimer_start(&data->beacon_timer,
2062                              ns_to_ktime(until_tbtt * NSEC_PER_USEC),
2063                              HRTIMER_MODE_REL_SOFT);
2064        }
2065
2066        return 0;
2067}
2068
2069
2070static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
2071                                            unsigned int changed_flags,
2072                                            unsigned int *total_flags,u64 multicast)
2073{
2074        struct mac80211_hwsim_data *data = hw->priv;
2075
2076        wiphy_dbg(hw->wiphy, "%s\n", __func__);
2077
2078        data->rx_filter = 0;
2079        if (*total_flags & FIF_ALLMULTI)
2080                data->rx_filter |= FIF_ALLMULTI;
2081        if (*total_flags & FIF_MCAST_ACTION)
2082                data->rx_filter |= FIF_MCAST_ACTION;
2083
2084        *total_flags = data->rx_filter;
2085}
2086
2087static void mac80211_hwsim_bcn_en_iter(void *data, u8 *mac,
2088                                       struct ieee80211_vif *vif)
2089{
2090        unsigned int *count = data;
2091        struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
2092
2093        if (vp->bcn_en)
2094                (*count)++;
2095}
2096
2097static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
2098                                            struct ieee80211_vif *vif,
2099                                            struct ieee80211_bss_conf *info,
2100                                            u32 changed)
2101{
2102        struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
2103        struct mac80211_hwsim_data *data = hw->priv;
2104
2105        hwsim_check_magic(vif);
2106
2107        wiphy_dbg(hw->wiphy, "%s(changed=0x%x vif->addr=%pM)\n",
2108                  __func__, changed, vif->addr);
2109
2110        if (changed & BSS_CHANGED_BSSID) {
2111                wiphy_dbg(hw->wiphy, "%s: BSSID changed: %pM\n",
2112                          __func__, info->bssid);
2113                memcpy(vp->bssid, info->bssid, ETH_ALEN);
2114        }
2115
2116        if (changed & BSS_CHANGED_ASSOC) {
2117                wiphy_dbg(hw->wiphy, "  ASSOC: assoc=%d aid=%d\n",
2118                          info->assoc, info->aid);
2119                vp->assoc = info->assoc;
2120                vp->aid = info->aid;
2121        }
2122
2123        if (changed & BSS_CHANGED_BEACON_ENABLED) {
2124                wiphy_dbg(hw->wiphy, "  BCN EN: %d (BI=%u)\n",
2125                          info->enable_beacon, info->beacon_int);
2126                vp->bcn_en = info->enable_beacon;
2127                if (data->started &&
2128                    !hrtimer_is_queued(&data->beacon_timer) &&
2129                    info->enable_beacon) {
2130                        u64 tsf, until_tbtt;
2131                        u32 bcn_int;
2132                        data->beacon_int = info->beacon_int * 1024;
2133                        tsf = mac80211_hwsim_get_tsf(hw, vif);
2134                        bcn_int = data->beacon_int;
2135                        until_tbtt = bcn_int - do_div(tsf, bcn_int);
2136
2137                        hrtimer_start(&data->beacon_timer,
2138                                      ns_to_ktime(until_tbtt * NSEC_PER_USEC),
2139                                      HRTIMER_MODE_REL_SOFT);
2140                } else if (!info->enable_beacon) {
2141                        unsigned int count = 0;
2142                        ieee80211_iterate_active_interfaces_atomic(
2143                                data->hw, IEEE80211_IFACE_ITER_NORMAL,
2144                                mac80211_hwsim_bcn_en_iter, &count);
2145                        wiphy_dbg(hw->wiphy, "  beaconing vifs remaining: %u",
2146                                  count);
2147                        if (count == 0) {
2148                                hrtimer_cancel(&data->beacon_timer);
2149                                data->beacon_int = 0;
2150                        }
2151                }
2152        }
2153
2154        if (changed & BSS_CHANGED_ERP_CTS_PROT) {
2155                wiphy_dbg(hw->wiphy, "  ERP_CTS_PROT: %d\n",
2156                          info->use_cts_prot);
2157        }
2158
2159        if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2160                wiphy_dbg(hw->wiphy, "  ERP_PREAMBLE: %d\n",
2161                          info->use_short_preamble);
2162        }
2163
2164        if (changed & BSS_CHANGED_ERP_SLOT) {
2165                wiphy_dbg(hw->wiphy, "  ERP_SLOT: %d\n", info->use_short_slot);
2166        }
2167
2168        if (changed & BSS_CHANGED_HT) {
2169                wiphy_dbg(hw->wiphy, "  HT: op_mode=0x%x\n",
2170                          info->ht_operation_mode);
2171        }
2172
2173        if (changed & BSS_CHANGED_BASIC_RATES) {
2174                wiphy_dbg(hw->wiphy, "  BASIC_RATES: 0x%llx\n",
2175                          (unsigned long long) info->basic_rates);
2176        }
2177
2178        if (changed & BSS_CHANGED_TXPOWER)
2179                wiphy_dbg(hw->wiphy, "  TX Power: %d dBm\n", info->txpower);
2180}
2181
2182static void
2183mac80211_hwsim_sta_rc_update(struct ieee80211_hw *hw,
2184                             struct ieee80211_vif *vif,
2185                             struct ieee80211_sta *sta,
2186                             u32 changed)
2187{
2188        struct mac80211_hwsim_data *data = hw->priv;
2189        u32 bw = U32_MAX;
2190        enum nl80211_chan_width confbw = NL80211_CHAN_WIDTH_20_NOHT;
2191
2192        switch (sta->bandwidth) {
2193#define C(_bw) case IEEE80211_STA_RX_BW_##_bw: bw = _bw; break
2194        C(20);
2195        C(40);
2196        C(80);
2197        C(160);
2198        C(320);
2199#undef C
2200        }
2201
2202        if (!data->use_chanctx) {
2203                confbw = data->bw;
2204        } else {
2205                struct ieee80211_chanctx_conf *chanctx_conf;
2206
2207                rcu_read_lock();
2208                chanctx_conf = rcu_dereference(vif->chanctx_conf);
2209
2210                if (!WARN_ON(!chanctx_conf))
2211                        confbw = chanctx_conf->def.width;
2212                rcu_read_unlock();
2213        }
2214
2215        WARN(bw > hwsim_get_chanwidth(confbw),
2216             "intf %pM: bad STA %pM bandwidth %d MHz (%d) > channel config %d MHz (%d)\n",
2217             vif->addr, sta->addr, bw, sta->bandwidth,
2218             hwsim_get_chanwidth(data->bw), data->bw);
2219}
2220
2221static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
2222                                  struct ieee80211_vif *vif,
2223                                  struct ieee80211_sta *sta)
2224{
2225        hwsim_check_magic(vif);
2226        hwsim_set_sta_magic(sta);
2227        mac80211_hwsim_sta_rc_update(hw, vif, sta, 0);
2228
2229        return 0;
2230}
2231
2232static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
2233                                     struct ieee80211_vif *vif,
2234                                     struct ieee80211_sta *sta)
2235{
2236        hwsim_check_magic(vif);
2237        hwsim_clear_sta_magic(sta);
2238
2239        return 0;
2240}
2241
2242static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
2243                                      struct ieee80211_vif *vif,
2244                                      enum sta_notify_cmd cmd,
2245                                      struct ieee80211_sta *sta)
2246{
2247        hwsim_check_magic(vif);
2248
2249        switch (cmd) {
2250        case STA_NOTIFY_SLEEP:
2251        case STA_NOTIFY_AWAKE:
2252                /* TODO: make good use of these flags */
2253                break;
2254        default:
2255                WARN(1, "Invalid sta notify: %d\n", cmd);
2256                break;
2257        }
2258}
2259
2260static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
2261                                  struct ieee80211_sta *sta,
2262                                  bool set)
2263{
2264        hwsim_check_sta_magic(sta);
2265        return 0;
2266}
2267
2268static int mac80211_hwsim_conf_tx(
2269        struct ieee80211_hw *hw,
2270        struct ieee80211_vif *vif, u16 queue,
2271        const struct ieee80211_tx_queue_params *params)
2272{
2273        wiphy_dbg(hw->wiphy,
2274                  "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
2275                  __func__, queue,
2276                  params->txop, params->cw_min,
2277                  params->cw_max, params->aifs);
2278        return 0;
2279}
2280
2281static int mac80211_hwsim_get_survey(struct ieee80211_hw *hw, int idx,
2282                                     struct survey_info *survey)
2283{
2284        struct mac80211_hwsim_data *hwsim = hw->priv;
2285
2286        if (idx < 0 || idx >= ARRAY_SIZE(hwsim->survey_data))
2287                return -ENOENT;
2288
2289        mutex_lock(&hwsim->mutex);
2290        survey->channel = hwsim->survey_data[idx].channel;
2291        if (!survey->channel) {
2292                mutex_unlock(&hwsim->mutex);
2293                return -ENOENT;
2294        }
2295
2296        /*
2297         * Magically conjured dummy values --- this is only ok for simulated hardware.
2298         *
2299         * A real driver which cannot determine real values noise MUST NOT
2300         * report any, especially not a magically conjured ones :-)
2301         */
2302        survey->filled = SURVEY_INFO_NOISE_DBM |
2303                         SURVEY_INFO_TIME |
2304                         SURVEY_INFO_TIME_BUSY;
2305        survey->noise = -92;
2306        survey->time =
2307                jiffies_to_msecs(hwsim->survey_data[idx].end -
2308                                 hwsim->survey_data[idx].start);
2309        /* report 12.5% of channel time is used */
2310        survey->time_busy = survey->time/8;
2311        mutex_unlock(&hwsim->mutex);
2312
2313        return 0;
2314}
2315
2316#ifdef CONFIG_NL80211_TESTMODE
2317/*
2318 * This section contains example code for using netlink
2319 * attributes with the testmode command in nl80211.
2320 */
2321
2322/* These enums need to be kept in sync with userspace */
2323enum hwsim_testmode_attr {
2324        __HWSIM_TM_ATTR_INVALID = 0,
2325        HWSIM_TM_ATTR_CMD       = 1,
2326        HWSIM_TM_ATTR_PS        = 2,
2327
2328        /* keep last */
2329        __HWSIM_TM_ATTR_AFTER_LAST,
2330        HWSIM_TM_ATTR_MAX       = __HWSIM_TM_ATTR_AFTER_LAST - 1
2331};
2332
2333enum hwsim_testmode_cmd {
2334        HWSIM_TM_CMD_SET_PS             = 0,
2335        HWSIM_TM_CMD_GET_PS             = 1,
2336        HWSIM_TM_CMD_STOP_QUEUES        = 2,
2337        HWSIM_TM_CMD_WAKE_QUEUES        = 3,
2338};
2339
2340static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
2341        [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
2342        [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
2343};
2344
2345static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
2346                                       struct ieee80211_vif *vif,
2347                                       void *data, int len)
2348{
2349        struct mac80211_hwsim_data *hwsim = hw->priv;
2350        struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
2351        struct sk_buff *skb;
2352        int err, ps;
2353
2354        err = nla_parse_deprecated(tb, HWSIM_TM_ATTR_MAX, data, len,
2355                                   hwsim_testmode_policy, NULL);
2356        if (err)
2357                return err;
2358
2359        if (!tb[HWSIM_TM_ATTR_CMD])
2360                return -EINVAL;
2361
2362        switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
2363        case HWSIM_TM_CMD_SET_PS:
2364                if (!tb[HWSIM_TM_ATTR_PS])
2365                        return -EINVAL;
2366                ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
2367                return hwsim_fops_ps_write(hwsim, ps);
2368        case HWSIM_TM_CMD_GET_PS:
2369                skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
2370                                                nla_total_size(sizeof(u32)));
2371                if (!skb)
2372                        return -ENOMEM;
2373                if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
2374                        goto nla_put_failure;
2375                return cfg80211_testmode_reply(skb);
2376        case HWSIM_TM_CMD_STOP_QUEUES:
2377                ieee80211_stop_queues(hw);
2378                return 0;
2379        case HWSIM_TM_CMD_WAKE_QUEUES:
2380                ieee80211_wake_queues(hw);
2381                return 0;
2382        default:
2383                return -EOPNOTSUPP;
2384        }
2385
2386 nla_put_failure:
2387        kfree_skb(skb);
2388        return -ENOBUFS;
2389}
2390#endif
2391
2392static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
2393                                       struct ieee80211_vif *vif,
2394                                       struct ieee80211_ampdu_params *params)
2395{
2396        struct ieee80211_sta *sta = params->sta;
2397        enum ieee80211_ampdu_mlme_action action = params->action;
2398        u16 tid = params->tid;
2399
2400        switch (action) {
2401        case IEEE80211_AMPDU_TX_START:
2402                return IEEE80211_AMPDU_TX_START_IMMEDIATE;
2403        case IEEE80211_AMPDU_TX_STOP_CONT:
2404        case IEEE80211_AMPDU_TX_STOP_FLUSH:
2405        case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
2406                ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2407                break;
2408        case IEEE80211_AMPDU_TX_OPERATIONAL:
2409                break;
2410        case IEEE80211_AMPDU_RX_START:
2411        case IEEE80211_AMPDU_RX_STOP:
2412                break;
2413        default:
2414                return -EOPNOTSUPP;
2415        }
2416
2417        return 0;
2418}
2419
2420static void mac80211_hwsim_flush(struct ieee80211_hw *hw,
2421                                 struct ieee80211_vif *vif,
2422                                 u32 queues, bool drop)
2423{
2424        /* Not implemented, queues only on kernel side */
2425}
2426
2427static void hw_scan_work(struct work_struct *work)
2428{
2429        struct mac80211_hwsim_data *hwsim =
2430                container_of(work, struct mac80211_hwsim_data, hw_scan.work);
2431        struct cfg80211_scan_request *req = hwsim->hw_scan_request;
2432        int dwell, i;
2433
2434        mutex_lock(&hwsim->mutex);
2435        if (hwsim->scan_chan_idx >= req->n_channels) {
2436                struct cfg80211_scan_info info = {
2437                        .aborted = false,
2438                };
2439
2440                wiphy_dbg(hwsim->hw->wiphy, "hw scan complete\n");
2441                ieee80211_scan_completed(hwsim->hw, &info);
2442                hwsim->hw_scan_request = NULL;
2443                hwsim->hw_scan_vif = NULL;
2444                hwsim->tmp_chan = NULL;
2445                mutex_unlock(&hwsim->mutex);
2446                mac80211_hwsim_config_mac_nl(hwsim->hw, hwsim->scan_addr,
2447                                             false);
2448                return;
2449        }
2450
2451        wiphy_dbg(hwsim->hw->wiphy, "hw scan %d MHz\n",
2452                  req->channels[hwsim->scan_chan_idx]->center_freq);
2453
2454        hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
2455        if (hwsim->tmp_chan->flags & (IEEE80211_CHAN_NO_IR |
2456                                      IEEE80211_CHAN_RADAR) ||
2457            !req->n_ssids) {
2458                dwell = 120;
2459        } else {
2460                dwell = 30;
2461                /* send probes */
2462                for (i = 0; i < req->n_ssids; i++) {
2463                        struct sk_buff *probe;
2464                        struct ieee80211_mgmt *mgmt;
2465
2466                        probe = ieee80211_probereq_get(hwsim->hw,
2467                                                       hwsim->scan_addr,
2468                                                       req->ssids[i].ssid,
2469                                                       req->ssids[i].ssid_len,
2470                                                       req->ie_len);
2471                        if (!probe)
2472                                continue;
2473
2474                        mgmt = (struct ieee80211_mgmt *) probe->data;
2475                        memcpy(mgmt->da, req->bssid, ETH_ALEN);
2476                        memcpy(mgmt->bssid, req->bssid, ETH_ALEN);
2477
2478                        if (req->ie_len)
2479                                skb_put_data(probe, req->ie, req->ie_len);
2480
2481                        rcu_read_lock();
2482                        if (!ieee80211_tx_prepare_skb(hwsim->hw,
2483                                                      hwsim->hw_scan_vif,
2484                                                      probe,
2485                                                      hwsim->tmp_chan->band,
2486                                                      NULL)) {
2487                                rcu_read_unlock();
2488                                kfree_skb(probe);
2489                                continue;
2490                        }
2491
2492                        local_bh_disable();
2493                        mac80211_hwsim_tx_frame(hwsim->hw, probe,
2494                                                hwsim->tmp_chan);
2495                        rcu_read_unlock();
2496                        local_bh_enable();
2497                }
2498        }
2499        ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
2500                                     msecs_to_jiffies(dwell));
2501        hwsim->survey_data[hwsim->scan_chan_idx].channel = hwsim->tmp_chan;
2502        hwsim->survey_data[hwsim->scan_chan_idx].start = jiffies;
2503        hwsim->survey_data[hwsim->scan_chan_idx].end =
2504                jiffies + msecs_to_jiffies(dwell);
2505        hwsim->scan_chan_idx++;
2506        mutex_unlock(&hwsim->mutex);
2507}
2508
2509static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
2510                                  struct ieee80211_vif *vif,
2511                                  struct ieee80211_scan_request *hw_req)
2512{
2513        struct mac80211_hwsim_data *hwsim = hw->priv;
2514        struct cfg80211_scan_request *req = &hw_req->req;
2515
2516        mutex_lock(&hwsim->mutex);
2517        if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2518                mutex_unlock(&hwsim->mutex);
2519                return -EBUSY;
2520        }
2521        hwsim->hw_scan_request = req;
2522        hwsim->hw_scan_vif = vif;
2523        hwsim->scan_chan_idx = 0;
2524        if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
2525                get_random_mask_addr(hwsim->scan_addr,
2526                                     hw_req->req.mac_addr,
2527                                     hw_req->req.mac_addr_mask);
2528        else
2529                memcpy(hwsim->scan_addr, vif->addr, ETH_ALEN);
2530        memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2531        mutex_unlock(&hwsim->mutex);
2532
2533        mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
2534        wiphy_dbg(hw->wiphy, "hwsim hw_scan request\n");
2535
2536        ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
2537
2538        return 0;
2539}
2540
2541static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
2542                                          struct ieee80211_vif *vif)
2543{
2544        struct mac80211_hwsim_data *hwsim = hw->priv;
2545        struct cfg80211_scan_info info = {
2546                .aborted = true,
2547        };
2548
2549        wiphy_dbg(hw->wiphy, "hwsim cancel_hw_scan\n");
2550
2551        cancel_delayed_work_sync(&hwsim->hw_scan);
2552
2553        mutex_lock(&hwsim->mutex);
2554        ieee80211_scan_completed(hwsim->hw, &info);
2555        hwsim->tmp_chan = NULL;
2556        hwsim->hw_scan_request = NULL;
2557        hwsim->hw_scan_vif = NULL;
2558        mutex_unlock(&hwsim->mutex);
2559}
2560
2561static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw,
2562                                   struct ieee80211_vif *vif,
2563                                   const u8 *mac_addr)
2564{
2565        struct mac80211_hwsim_data *hwsim = hw->priv;
2566
2567        mutex_lock(&hwsim->mutex);
2568
2569        if (hwsim->scanning) {
2570                pr_debug("two hwsim sw_scans detected!\n");
2571                goto out;
2572        }
2573
2574        pr_debug("hwsim sw_scan request, prepping stuff\n");
2575
2576        memcpy(hwsim->scan_addr, mac_addr, ETH_ALEN);
2577        mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, true);
2578        hwsim->scanning = true;
2579        memset(hwsim->survey_data, 0, sizeof(hwsim->survey_data));
2580
2581out:
2582        mutex_unlock(&hwsim->mutex);
2583}
2584
2585static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw,
2586                                            struct ieee80211_vif *vif)
2587{
2588        struct mac80211_hwsim_data *hwsim = hw->priv;
2589
2590        mutex_lock(&hwsim->mutex);
2591
2592        pr_debug("hwsim sw_scan_complete\n");
2593        hwsim->scanning = false;
2594        mac80211_hwsim_config_mac_nl(hw, hwsim->scan_addr, false);
2595        eth_zero_addr(hwsim->scan_addr);
2596
2597        mutex_unlock(&hwsim->mutex);
2598}
2599
2600static void hw_roc_start(struct work_struct *work)
2601{
2602        struct mac80211_hwsim_data *hwsim =
2603                container_of(work, struct mac80211_hwsim_data, roc_start.work);
2604
2605        mutex_lock(&hwsim->mutex);
2606
2607        wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC begins\n");
2608        hwsim->tmp_chan = hwsim->roc_chan;
2609        ieee80211_ready_on_channel(hwsim->hw);
2610
2611        ieee80211_queue_delayed_work(hwsim->hw, &hwsim->roc_done,
2612                                     msecs_to_jiffies(hwsim->roc_duration));
2613
2614        mutex_unlock(&hwsim->mutex);
2615}
2616
2617static void hw_roc_done(struct work_struct *work)
2618{
2619        struct mac80211_hwsim_data *hwsim =
2620                container_of(work, struct mac80211_hwsim_data, roc_done.work);
2621
2622        mutex_lock(&hwsim->mutex);
2623        ieee80211_remain_on_channel_expired(hwsim->hw);
2624        hwsim->tmp_chan = NULL;
2625        mutex_unlock(&hwsim->mutex);
2626
2627        wiphy_dbg(hwsim->hw->wiphy, "hwsim ROC expired\n");
2628}
2629
2630static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
2631                              struct ieee80211_vif *vif,
2632                              struct ieee80211_channel *chan,
2633                              int duration,
2634                              enum ieee80211_roc_type type)
2635{
2636        struct mac80211_hwsim_data *hwsim = hw->priv;
2637
2638        mutex_lock(&hwsim->mutex);
2639        if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
2640                mutex_unlock(&hwsim->mutex);
2641                return -EBUSY;
2642        }
2643
2644        hwsim->roc_chan = chan;
2645        hwsim->roc_duration = duration;
2646        mutex_unlock(&hwsim->mutex);
2647
2648        wiphy_dbg(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
2649                  chan->center_freq, duration);
2650        ieee80211_queue_delayed_work(hw, &hwsim->roc_start, HZ/50);
2651
2652        return 0;
2653}
2654
2655static int mac80211_hwsim_croc(struct ieee80211_hw *hw,
2656                               struct ieee80211_vif *vif)
2657{
2658        struct mac80211_hwsim_data *hwsim = hw->priv;
2659
2660        cancel_delayed_work_sync(&hwsim->roc_start);
2661        cancel_delayed_work_sync(&hwsim->roc_done);
2662
2663        mutex_lock(&hwsim->mutex);
2664        hwsim->tmp_chan = NULL;
2665        mutex_unlock(&hwsim->mutex);
2666
2667        wiphy_dbg(hw->wiphy, "hwsim ROC canceled\n");
2668
2669        return 0;
2670}
2671
2672static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
2673                                      struct ieee80211_chanctx_conf *ctx)
2674{
2675        struct mac80211_hwsim_data *hwsim = hw->priv;
2676
2677        mutex_lock(&hwsim->mutex);
2678        hwsim->chanctx = ctx;
2679        mutex_unlock(&hwsim->mutex);
2680        hwsim_set_chanctx_magic(ctx);
2681        wiphy_dbg(hw->wiphy,
2682                  "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2683                  ctx->def.chan->center_freq, ctx->def.width,
2684                  ctx->def.center_freq1, ctx->def.center_freq2);
2685        return 0;
2686}
2687
2688static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
2689                                          struct ieee80211_chanctx_conf *ctx)
2690{
2691        struct mac80211_hwsim_data *hwsim = hw->priv;
2692
2693        mutex_lock(&hwsim->mutex);
2694        hwsim->chanctx = NULL;
2695        mutex_unlock(&hwsim->mutex);
2696        wiphy_dbg(hw->wiphy,
2697                  "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2698                  ctx->def.chan->center_freq, ctx->def.width,
2699                  ctx->def.center_freq1, ctx->def.center_freq2);
2700        hwsim_check_chanctx_magic(ctx);
2701        hwsim_clear_chanctx_magic(ctx);
2702}
2703
2704static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
2705                                          struct ieee80211_chanctx_conf *ctx,
2706                                          u32 changed)
2707{
2708        struct mac80211_hwsim_data *hwsim = hw->priv;
2709
2710        mutex_lock(&hwsim->mutex);
2711        hwsim->chanctx = ctx;
2712        mutex_unlock(&hwsim->mutex);
2713        hwsim_check_chanctx_magic(ctx);
2714        wiphy_dbg(hw->wiphy,
2715                  "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
2716                  ctx->def.chan->center_freq, ctx->def.width,
2717                  ctx->def.center_freq1, ctx->def.center_freq2);
2718}
2719
2720static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
2721                                             struct ieee80211_vif *vif,
2722                                             struct ieee80211_chanctx_conf *ctx)
2723{
2724        hwsim_check_magic(vif);
2725        hwsim_check_chanctx_magic(ctx);
2726
2727        return 0;
2728}
2729
2730static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
2731                                                struct ieee80211_vif *vif,
2732                                                struct ieee80211_chanctx_conf *ctx)
2733{
2734        hwsim_check_magic(vif);
2735        hwsim_check_chanctx_magic(ctx);
2736}
2737
2738static const char mac80211_hwsim_gstrings_stats[][ETH_GSTRING_LEN] = {
2739        "tx_pkts_nic",
2740        "tx_bytes_nic",
2741        "rx_pkts_nic",
2742        "rx_bytes_nic",
2743        "d_tx_dropped",
2744        "d_tx_failed",
2745        "d_ps_mode",
2746        "d_group",
2747};
2748
2749#define MAC80211_HWSIM_SSTATS_LEN ARRAY_SIZE(mac80211_hwsim_gstrings_stats)
2750
2751static void mac80211_hwsim_get_et_strings(struct ieee80211_hw *hw,
2752                                          struct ieee80211_vif *vif,
2753                                          u32 sset, u8 *data)
2754{
2755        if (sset == ETH_SS_STATS)
2756                memcpy(data, *mac80211_hwsim_gstrings_stats,
2757                       sizeof(mac80211_hwsim_gstrings_stats));
2758}
2759
2760static int mac80211_hwsim_get_et_sset_count(struct ieee80211_hw *hw,
2761                                            struct ieee80211_vif *vif, int sset)
2762{
2763        if (sset == ETH_SS_STATS)
2764                return MAC80211_HWSIM_SSTATS_LEN;
2765        return 0;
2766}
2767
2768static void mac80211_hwsim_get_et_stats(struct ieee80211_hw *hw,
2769                                        struct ieee80211_vif *vif,
2770                                        struct ethtool_stats *stats, u64 *data)
2771{
2772        struct mac80211_hwsim_data *ar = hw->priv;
2773        int i = 0;
2774
2775        data[i++] = ar->tx_pkts;
2776        data[i++] = ar->tx_bytes;
2777        data[i++] = ar->rx_pkts;
2778        data[i++] = ar->rx_bytes;
2779        data[i++] = ar->tx_dropped;
2780        data[i++] = ar->tx_failed;
2781        data[i++] = ar->ps;
2782        data[i++] = ar->group;
2783
2784        WARN_ON(i != MAC80211_HWSIM_SSTATS_LEN);
2785}
2786
2787static int mac80211_hwsim_tx_last_beacon(struct ieee80211_hw *hw)
2788{
2789        return 1;
2790}
2791
2792#define HWSIM_COMMON_OPS                                        \
2793        .tx = mac80211_hwsim_tx,                                \
2794        .start = mac80211_hwsim_start,                          \
2795        .stop = mac80211_hwsim_stop,                            \
2796        .add_interface = mac80211_hwsim_add_interface,          \
2797        .change_interface = mac80211_hwsim_change_interface,    \
2798        .remove_interface = mac80211_hwsim_remove_interface,    \
2799        .config = mac80211_hwsim_config,                        \
2800        .configure_filter = mac80211_hwsim_configure_filter,    \
2801        .bss_info_changed = mac80211_hwsim_bss_info_changed,    \
2802        .tx_last_beacon = mac80211_hwsim_tx_last_beacon,        \
2803        .sta_add = mac80211_hwsim_sta_add,                      \
2804        .sta_remove = mac80211_hwsim_sta_remove,                \
2805        .sta_notify = mac80211_hwsim_sta_notify,                \
2806        .sta_rc_update = mac80211_hwsim_sta_rc_update,          \
2807        .set_tim = mac80211_hwsim_set_tim,                      \
2808        .conf_tx = mac80211_hwsim_conf_tx,                      \
2809        .get_survey = mac80211_hwsim_get_survey,                \
2810        CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)      \
2811        .ampdu_action = mac80211_hwsim_ampdu_action,            \
2812        .flush = mac80211_hwsim_flush,                          \
2813        .get_tsf = mac80211_hwsim_get_tsf,                      \
2814        .set_tsf = mac80211_hwsim_set_tsf,                      \
2815        .get_et_sset_count = mac80211_hwsim_get_et_sset_count,  \
2816        .get_et_stats = mac80211_hwsim_get_et_stats,            \
2817        .get_et_strings = mac80211_hwsim_get_et_strings,
2818
2819static const struct ieee80211_ops mac80211_hwsim_ops = {
2820        HWSIM_COMMON_OPS
2821        .sw_scan_start = mac80211_hwsim_sw_scan,
2822        .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
2823};
2824
2825static const struct ieee80211_ops mac80211_hwsim_mchan_ops = {
2826        HWSIM_COMMON_OPS
2827        .hw_scan = mac80211_hwsim_hw_scan,
2828        .cancel_hw_scan = mac80211_hwsim_cancel_hw_scan,
2829        .sw_scan_start = NULL,
2830        .sw_scan_complete = NULL,
2831        .remain_on_channel = mac80211_hwsim_roc,
2832        .cancel_remain_on_channel = mac80211_hwsim_croc,
2833        .add_chanctx = mac80211_hwsim_add_chanctx,
2834        .remove_chanctx = mac80211_hwsim_remove_chanctx,
2835        .change_chanctx = mac80211_hwsim_change_chanctx,
2836        .assign_vif_chanctx = mac80211_hwsim_assign_vif_chanctx,
2837        .unassign_vif_chanctx = mac80211_hwsim_unassign_vif_chanctx,
2838};
2839
2840struct hwsim_new_radio_params {
2841        unsigned int channels;
2842        const char *reg_alpha2;
2843        const struct ieee80211_regdomain *regd;
2844        bool reg_strict;
2845        bool p2p_device;
2846        bool use_chanctx;
2847        bool destroy_on_close;
2848        const char *hwname;
2849        bool no_vif;
2850        const u8 *perm_addr;
2851        u32 iftypes;
2852        u32 *ciphers;
2853        u8 n_ciphers;
2854};
2855
2856static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
2857                                   struct genl_info *info)
2858{
2859        if (info)
2860                genl_notify(&hwsim_genl_family, mcast_skb, info,
2861                            HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2862        else
2863                genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
2864                                  HWSIM_MCGRP_CONFIG, GFP_KERNEL);
2865}
2866
2867static int append_radio_msg(struct sk_buff *skb, int id,
2868                            struct hwsim_new_radio_params *param)
2869{
2870        int ret;
2871
2872        ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
2873        if (ret < 0)
2874                return ret;
2875
2876        if (param->channels) {
2877                ret = nla_put_u32(skb, HWSIM_ATTR_CHANNELS, param->channels);
2878                if (ret < 0)
2879                        return ret;
2880        }
2881
2882        if (param->reg_alpha2) {
2883                ret = nla_put(skb, HWSIM_ATTR_REG_HINT_ALPHA2, 2,
2884                              param->reg_alpha2);
2885                if (ret < 0)
2886                        return ret;
2887        }
2888
2889        if (param->regd) {
2890                int i;
2891
2892                for (i = 0; i < ARRAY_SIZE(hwsim_world_regdom_custom); i++) {
2893                        if (hwsim_world_regdom_custom[i] != param->regd)
2894                                continue;
2895
2896                        ret = nla_put_u32(skb, HWSIM_ATTR_REG_CUSTOM_REG, i);
2897                        if (ret < 0)
2898                                return ret;
2899                        break;
2900                }
2901        }
2902
2903        if (param->reg_strict) {
2904                ret = nla_put_flag(skb, HWSIM_ATTR_REG_STRICT_REG);
2905                if (ret < 0)
2906                        return ret;
2907        }
2908
2909        if (param->p2p_device) {
2910                ret = nla_put_flag(skb, HWSIM_ATTR_SUPPORT_P2P_DEVICE);
2911                if (ret < 0)
2912                        return ret;
2913        }
2914
2915        if (param->use_chanctx) {
2916                ret = nla_put_flag(skb, HWSIM_ATTR_USE_CHANCTX);
2917                if (ret < 0)
2918                        return ret;
2919        }
2920
2921        if (param->hwname) {
2922                ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME,
2923                              strlen(param->hwname), param->hwname);
2924                if (ret < 0)
2925                        return ret;
2926        }
2927
2928        return 0;
2929}
2930
2931static void hwsim_mcast_new_radio(int id, struct genl_info *info,
2932                                  struct hwsim_new_radio_params *param)
2933{
2934        struct sk_buff *mcast_skb;
2935        void *data;
2936
2937        mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2938        if (!mcast_skb)
2939                return;
2940
2941        data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
2942                           HWSIM_CMD_NEW_RADIO);
2943        if (!data)
2944                goto out_err;
2945
2946        if (append_radio_msg(mcast_skb, id, param) < 0)
2947                goto out_err;
2948
2949        genlmsg_end(mcast_skb, data);
2950
2951        hwsim_mcast_config_msg(mcast_skb, info);
2952        return;
2953
2954out_err:
2955        nlmsg_free(mcast_skb);
2956}
2957
2958static const struct ieee80211_sband_iftype_data sband_capa_2ghz[] = {
2959        {
2960                .types_mask = BIT(NL80211_IFTYPE_STATION) |
2961                              BIT(NL80211_IFTYPE_AP),
2962                .he_cap = {
2963                        .has_he = true,
2964                        .he_cap_elem = {
2965                                .mac_cap_info[0] =
2966                                        IEEE80211_HE_MAC_CAP0_HTC_HE,
2967                                .mac_cap_info[1] =
2968                                        IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
2969                                        IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
2970                                .mac_cap_info[2] =
2971                                        IEEE80211_HE_MAC_CAP2_BSR |
2972                                        IEEE80211_HE_MAC_CAP2_MU_CASCADING |
2973                                        IEEE80211_HE_MAC_CAP2_ACK_EN,
2974                                .mac_cap_info[3] =
2975                                        IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
2976                                        IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
2977                                .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
2978                                .phy_cap_info[1] =
2979                                        IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
2980                                        IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
2981                                        IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
2982                                        IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
2983                                .phy_cap_info[2] =
2984                                        IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
2985                                        IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
2986                                        IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
2987                                        IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
2988                                        IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
2989
2990                                /* Leave all the other PHY capability bytes
2991                                 * unset, as DCM, beam forming, RU and PPE
2992                                 * threshold information are not supported
2993                                 */
2994                        },
2995                        .he_mcs_nss_supp = {
2996                                .rx_mcs_80 = cpu_to_le16(0xfffa),
2997                                .tx_mcs_80 = cpu_to_le16(0xfffa),
2998                                .rx_mcs_160 = cpu_to_le16(0xffff),
2999                                .tx_mcs_160 = cpu_to_le16(0xffff),
3000                                .rx_mcs_80p80 = cpu_to_le16(0xffff),
3001                                .tx_mcs_80p80 = cpu_to_le16(0xffff),
3002                        },
3003                },
3004                .eht_cap = {
3005                        .has_eht = true,
3006                        .eht_cap_elem = {
3007                                .mac_cap_info[0] =
3008                                        IEEE80211_EHT_MAC_CAP0_NSEP_PRIO_ACCESS |
3009                                        IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
3010                                        IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
3011                                .phy_cap_info[0] =
3012                                        IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
3013                                        IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
3014                                        IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
3015                                        IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
3016                                        IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE,
3017                                .phy_cap_info[3] =
3018                                        IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
3019                                        IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
3020                                        IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
3021                                        IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
3022                                        IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
3023                                        IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
3024                                        IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
3025                                .phy_cap_info[4] =
3026                                        IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
3027                                        IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
3028                                        IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
3029                                        IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
3030                                        IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
3031                                .phy_cap_info[5] =
3032                                        IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
3033                                        IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
3034                                        IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
3035                                        IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
3036                                        IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
3037                                        IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
3038                                .phy_cap_info[6] =
3039                                        IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
3040                                        IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK,
3041                                .phy_cap_info[7] =
3042                                        IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW,
3043                        },
3044
3045                        /* For all MCS and bandwidth, set 8 NSS for both Tx and
3046                         * Rx
3047                         */
3048                        .eht_mcs_nss_supp = {
3049                                /*
3050                                 * Since B0, B1, B2 and B3 are not set in
3051                                 * the supported channel width set field in the
3052                                 * HE PHY capabilities information field the
3053                                 * device is a 20MHz only device on 2.4GHz band.
3054                                 */
3055                                .only_20mhz = {
3056                                        .rx_tx_mcs7_max_nss = 0x88,
3057                                        .rx_tx_mcs9_max_nss = 0x88,
3058                                        .rx_tx_mcs11_max_nss = 0x88,
3059                                        .rx_tx_mcs13_max_nss = 0x88,
3060                                },
3061                        },
3062                        /* PPE threshold information is not supported */
3063                },
3064        },
3065#ifdef CONFIG_MAC80211_MESH
3066        {
3067                .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
3068                .he_cap = {
3069                        .has_he = true,
3070                        .he_cap_elem = {
3071                                .mac_cap_info[0] =
3072                                        IEEE80211_HE_MAC_CAP0_HTC_HE,
3073                                .mac_cap_info[1] =
3074                                        IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
3075                                .mac_cap_info[2] =
3076                                        IEEE80211_HE_MAC_CAP2_ACK_EN,
3077                                .mac_cap_info[3] =
3078                                        IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
3079                                        IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
3080                                .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
3081                                .phy_cap_info[1] =
3082                                        IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
3083                                        IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
3084                                        IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
3085                                        IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
3086                                .phy_cap_info[2] = 0,
3087
3088                                /* Leave all the other PHY capability bytes
3089                                 * unset, as DCM, beam forming, RU and PPE
3090                                 * threshold information are not supported
3091                                 */
3092                        },
3093                        .he_mcs_nss_supp = {
3094                                .rx_mcs_80 = cpu_to_le16(0xfffa),
3095                                .tx_mcs_80 = cpu_to_le16(0xfffa),
3096                                .rx_mcs_160 = cpu_to_le16(0xffff),
3097                                .tx_mcs_160 = cpu_to_le16(0xffff),
3098                                .rx_mcs_80p80 = cpu_to_le16(0xffff),
3099                                .tx_mcs_80p80 = cpu_to_le16(0xffff),
3100                        },
3101                },
3102        },
3103#endif
3104};
3105
3106static const struct ieee80211_sband_iftype_data sband_capa_5ghz[] = {
3107        {
3108                /* TODO: should we support other types, e.g., P2P?*/
3109                .types_mask = BIT(NL80211_IFTYPE_STATION) |
3110                              BIT(NL80211_IFTYPE_AP),
3111                .he_cap = {
3112                        .has_he = true,
3113                        .he_cap_elem = {
3114                                .mac_cap_info[0] =
3115                                        IEEE80211_HE_MAC_CAP0_HTC_HE,
3116                                .mac_cap_info[1] =
3117                                        IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
3118                                        IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
3119                                .mac_cap_info[2] =
3120                                        IEEE80211_HE_MAC_CAP2_BSR |
3121                                        IEEE80211_HE_MAC_CAP2_MU_CASCADING |
3122                                        IEEE80211_HE_MAC_CAP2_ACK_EN,
3123                                .mac_cap_info[3] =
3124                                        IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
3125                                        IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
3126                                .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
3127                                .phy_cap_info[0] =
3128                                        IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
3129                                        IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
3130                                        IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
3131                                .phy_cap_info[1] =
3132                                        IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
3133                                        IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
3134                                        IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
3135                                        IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
3136                                .phy_cap_info[2] =
3137                                        IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
3138                                        IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
3139                                        IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
3140                                        IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
3141                                        IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
3142
3143                                /* Leave all the other PHY capability bytes
3144                                 * unset, as DCM, beam forming, RU and PPE
3145                                 * threshold information are not supported
3146                                 */
3147                        },
3148                        .he_mcs_nss_supp = {
3149                                .rx_mcs_80 = cpu_to_le16(0xfffa),
3150                                .tx_mcs_80 = cpu_to_le16(0xfffa),
3151                                .rx_mcs_160 = cpu_to_le16(0xfffa),
3152                                .tx_mcs_160 = cpu_to_le16(0xfffa),
3153                                .rx_mcs_80p80 = cpu_to_le16(0xfffa),
3154                                .tx_mcs_80p80 = cpu_to_le16(0xfffa),
3155                        },
3156                },
3157                .eht_cap = {
3158                        .has_eht = true,
3159                        .eht_cap_elem = {
3160                                .mac_cap_info[0] =
3161                                        IEEE80211_EHT_MAC_CAP0_NSEP_PRIO_ACCESS |
3162                                        IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
3163                                        IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
3164                                .phy_cap_info[0] =
3165                                        IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
3166                                        IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
3167                                        IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
3168                                        IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
3169                                        IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE |
3170                                        IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK,
3171                                .phy_cap_info[1] =
3172                                        IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK |
3173                                        IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK,
3174                                .phy_cap_info[2] =
3175                                        IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK |
3176                                        IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK,
3177                                .phy_cap_info[3] =
3178                                        IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
3179                                        IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
3180                                        IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
3181                                        IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
3182                                        IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
3183                                        IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
3184                                        IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
3185                                .phy_cap_info[4] =
3186                                        IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
3187                                        IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
3188                                        IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
3189                                        IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
3190                                        IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
3191                                .phy_cap_info[5] =
3192                                        IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
3193                                        IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
3194                                        IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
3195                                        IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
3196                                        IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
3197                                        IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
3198                                .phy_cap_info[6] =
3199                                        IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
3200                                        IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK,
3201                                .phy_cap_info[7] =
3202                                        IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW |
3203                                        IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
3204                                        IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
3205                                        IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
3206                                        IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ,
3207                        },
3208
3209                        /* For all MCS and bandwidth, set 8 NSS for both Tx and
3210                         * Rx
3211                         */
3212                        .eht_mcs_nss_supp = {
3213                                /*
3214                                 * As B1 and B2 are set in the supported
3215                                 * channel width set field in the HE PHY
3216                                 * capabilities information field include all
3217                                 * the following MCS/NSS.
3218                                 */
3219                                .bw._80 = {
3220                                        .rx_tx_mcs9_max_nss = 0x88,
3221                                        .rx_tx_mcs11_max_nss = 0x88,
3222                                        .rx_tx_mcs13_max_nss = 0x88,
3223                                },
3224                                .bw._160 = {
3225                                        .rx_tx_mcs9_max_nss = 0x88,
3226                                        .rx_tx_mcs11_max_nss = 0x88,
3227                                        .rx_tx_mcs13_max_nss = 0x88,
3228                                },
3229                        },
3230                        /* PPE threshold information is not supported */
3231                },
3232        },
3233#ifdef CONFIG_MAC80211_MESH
3234        {
3235                /* TODO: should we support other types, e.g., IBSS?*/
3236                .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
3237                .he_cap = {
3238                        .has_he = true,
3239                        .he_cap_elem = {
3240                                .mac_cap_info[0] =
3241                                        IEEE80211_HE_MAC_CAP0_HTC_HE,
3242                                .mac_cap_info[1] =
3243                                        IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
3244                                .mac_cap_info[2] =
3245                                        IEEE80211_HE_MAC_CAP2_ACK_EN,
3246                                .mac_cap_info[3] =
3247                                        IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
3248                                        IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
3249                                .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
3250                                .phy_cap_info[0] =
3251                                        IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
3252                                        IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
3253                                        IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
3254                                .phy_cap_info[1] =
3255                                        IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
3256                                        IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
3257                                        IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
3258                                        IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
3259                                .phy_cap_info[2] = 0,
3260
3261                                /* Leave all the other PHY capability bytes
3262                                 * unset, as DCM, beam forming, RU and PPE
3263                                 * threshold information are not supported
3264                                 */
3265                        },
3266                        .he_mcs_nss_supp = {
3267                                .rx_mcs_80 = cpu_to_le16(0xfffa),
3268                                .tx_mcs_80 = cpu_to_le16(0xfffa),
3269                                .rx_mcs_160 = cpu_to_le16(0xfffa),
3270                                .tx_mcs_160 = cpu_to_le16(0xfffa),
3271                                .rx_mcs_80p80 = cpu_to_le16(0xfffa),
3272                                .tx_mcs_80p80 = cpu_to_le16(0xfffa),
3273                        },
3274                },
3275        },
3276#endif
3277};
3278
3279static const struct ieee80211_sband_iftype_data sband_capa_6ghz[] = {
3280        {
3281                /* TODO: should we support other types, e.g., P2P?*/
3282                .types_mask = BIT(NL80211_IFTYPE_STATION) |
3283                              BIT(NL80211_IFTYPE_AP),
3284                .he_6ghz_capa = {
3285                        .capa = cpu_to_le16(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START |
3286                                            IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP |
3287                                            IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN |
3288                                            IEEE80211_HE_6GHZ_CAP_SM_PS |
3289                                            IEEE80211_HE_6GHZ_CAP_RD_RESPONDER |
3290                                            IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS |
3291                                            IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS),
3292                },
3293                .he_cap = {
3294                        .has_he = true,
3295                        .he_cap_elem = {
3296                                .mac_cap_info[0] =
3297                                        IEEE80211_HE_MAC_CAP0_HTC_HE,
3298                                .mac_cap_info[1] =
3299                                        IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US |
3300                                        IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
3301                                .mac_cap_info[2] =
3302                                        IEEE80211_HE_MAC_CAP2_BSR |
3303                                        IEEE80211_HE_MAC_CAP2_MU_CASCADING |
3304                                        IEEE80211_HE_MAC_CAP2_ACK_EN,
3305                                .mac_cap_info[3] =
3306                                        IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
3307                                        IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
3308                                .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
3309                                .phy_cap_info[0] =
3310                                        IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
3311                                        IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
3312                                        IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
3313                                .phy_cap_info[1] =
3314                                        IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
3315                                        IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
3316                                        IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
3317                                        IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
3318                                .phy_cap_info[2] =
3319                                        IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
3320                                        IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
3321                                        IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
3322                                        IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
3323                                        IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO,
3324
3325                                /* Leave all the other PHY capability bytes
3326                                 * unset, as DCM, beam forming, RU and PPE
3327                                 * threshold information are not supported
3328                                 */
3329                        },
3330                        .he_mcs_nss_supp = {
3331                                .rx_mcs_80 = cpu_to_le16(0xfffa),
3332                                .tx_mcs_80 = cpu_to_le16(0xfffa),
3333                                .rx_mcs_160 = cpu_to_le16(0xfffa),
3334                                .tx_mcs_160 = cpu_to_le16(0xfffa),
3335                                .rx_mcs_80p80 = cpu_to_le16(0xfffa),
3336                                .tx_mcs_80p80 = cpu_to_le16(0xfffa),
3337                        },
3338                },
3339                .eht_cap = {
3340                        .has_eht = true,
3341                        .eht_cap_elem = {
3342                                .mac_cap_info[0] =
3343                                        IEEE80211_EHT_MAC_CAP0_NSEP_PRIO_ACCESS |
3344                                        IEEE80211_EHT_MAC_CAP0_OM_CONTROL |
3345                                        IEEE80211_EHT_MAC_CAP0_TRIG_TXOP_SHARING_MODE1,
3346                                .phy_cap_info[0] =
3347                                        IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ |
3348                                        IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ |
3349                                        IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
3350                                        IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO |
3351                                        IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
3352                                        IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE |
3353                                        IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK,
3354                                .phy_cap_info[1] =
3355                                        IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK |
3356                                        IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK |
3357                                        IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_320MHZ_MASK,
3358                                .phy_cap_info[2] =
3359                                        IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK |
3360                                        IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK |
3361                                        IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_320MHZ_MASK,
3362                                .phy_cap_info[3] =
3363                                        IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
3364                                        IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
3365                                        IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
3366                                        IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
3367                                        IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
3368                                        IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
3369                                        IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK,
3370                                .phy_cap_info[4] =
3371                                        IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
3372                                        IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
3373                                        IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
3374                                        IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI |
3375                                        IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK,
3376                                .phy_cap_info[5] =
3377                                        IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
3378                                        IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
3379                                        IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
3380                                        IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT |
3381                                        IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK |
3382                                        IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK,
3383                                .phy_cap_info[6] =
3384                                        IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK |
3385                                        IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK |
3386                                        IEEE80211_EHT_PHY_CAP6_EHT_DUP_6GHZ_SUPP,
3387                                .phy_cap_info[7] =
3388                                        IEEE80211_EHT_PHY_CAP7_20MHZ_STA_RX_NDP_WIDER_BW |
3389                                        IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
3390                                        IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
3391                                        IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
3392                                        IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
3393                                        IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
3394                                        IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ,
3395                        },
3396
3397                        /* For all MCS and bandwidth, set 8 NSS for both Tx and
3398                         * Rx
3399                         */
3400                        .eht_mcs_nss_supp = {
3401                                /*
3402                                 * As B1 and B2 are set in the supported
3403                                 * channel width set field in the HE PHY
3404                                 * capabilities information field and 320MHz in
3405                                 * 6GHz is supported include all the following
3406                                 * MCS/NSS.
3407                                 */
3408                                .bw._80 = {
3409                                        .rx_tx_mcs9_max_nss = 0x88,
3410                                        .rx_tx_mcs11_max_nss = 0x88,
3411                                        .rx_tx_mcs13_max_nss = 0x88,
3412                                },
3413                                .bw._160 = {
3414                                        .rx_tx_mcs9_max_nss = 0x88,
3415                                        .rx_tx_mcs11_max_nss = 0x88,
3416                                        .rx_tx_mcs13_max_nss = 0x88,
3417                                },
3418                                .bw._320 = {
3419                                        .rx_tx_mcs9_max_nss = 0x88,
3420                                        .rx_tx_mcs11_max_nss = 0x88,
3421                                        .rx_tx_mcs13_max_nss = 0x88,
3422                                },
3423                        },
3424                        /* PPE threshold information is not supported */
3425                },
3426        },
3427#ifdef CONFIG_MAC80211_MESH
3428        {
3429                /* TODO: should we support other types, e.g., IBSS?*/
3430                .types_mask = BIT(NL80211_IFTYPE_MESH_POINT),
3431                .he_6ghz_capa = {
3432                        .capa = cpu_to_le16(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START |
3433                                            IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP |
3434                                            IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN |
3435                                            IEEE80211_HE_6GHZ_CAP_SM_PS |
3436                                            IEEE80211_HE_6GHZ_CAP_RD_RESPONDER |
3437                                            IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS |
3438                                            IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS),
3439                },
3440                .he_cap = {
3441                        .has_he = true,
3442                        .he_cap_elem = {
3443                                .mac_cap_info[0] =
3444                                        IEEE80211_HE_MAC_CAP0_HTC_HE,
3445                                .mac_cap_info[1] =
3446                                        IEEE80211_HE_MAC_CAP1_MULTI_TID_AGG_RX_QOS_8,
3447                                .mac_cap_info[2] =
3448                                        IEEE80211_HE_MAC_CAP2_ACK_EN,
3449                                .mac_cap_info[3] =
3450                                        IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
3451                                        IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3,
3452                                .mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU,
3453                                .phy_cap_info[0] =
3454                                        IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
3455                                        IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
3456                                        IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G,
3457                                .phy_cap_info[1] =
3458                                        IEEE80211_HE_PHY_CAP1_PREAMBLE_PUNC_RX_MASK |
3459                                        IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
3460                                        IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD |
3461                                        IEEE80211_HE_PHY_CAP1_MIDAMBLE_RX_TX_MAX_NSTS,
3462                                .phy_cap_info[2] = 0,
3463
3464                                /* Leave all the other PHY capability bytes
3465                                 * unset, as DCM, beam forming, RU and PPE
3466                                 * threshold information are not supported
3467                                 */
3468                        },
3469                        .he_mcs_nss_supp = {
3470                                .rx_mcs_80 = cpu_to_le16(0xfffa),
3471                                .tx_mcs_80 = cpu_to_le16(0xfffa),
3472                                .rx_mcs_160 = cpu_to_le16(0xfffa),
3473                                .tx_mcs_160 = cpu_to_le16(0xfffa),
3474                                .rx_mcs_80p80 = cpu_to_le16(0xfffa),
3475                                .tx_mcs_80p80 = cpu_to_le16(0xfffa),
3476                        },
3477                },
3478        },
3479#endif
3480};
3481
3482static void mac80211_hwsim_sband_capab(struct ieee80211_supported_band *sband)
3483{
3484        u16 n_iftype_data;
3485
3486        if (sband->band == NL80211_BAND_2GHZ) {
3487                n_iftype_data = ARRAY_SIZE(sband_capa_2ghz);
3488                sband->iftype_data =
3489                        (struct ieee80211_sband_iftype_data *)sband_capa_2ghz;
3490        } else if (sband->band == NL80211_BAND_5GHZ) {
3491                n_iftype_data = ARRAY_SIZE(sband_capa_5ghz);
3492                sband->iftype_data =
3493                        (struct ieee80211_sband_iftype_data *)sband_capa_5ghz;
3494        } else if (sband->band == NL80211_BAND_6GHZ) {
3495                n_iftype_data = ARRAY_SIZE(sband_capa_6ghz);
3496                sband->iftype_data =
3497                        (struct ieee80211_sband_iftype_data *)sband_capa_6ghz;
3498        } else {
3499                return;
3500        }
3501
3502        sband->n_iftype_data = n_iftype_data;
3503}
3504
3505#ifdef CONFIG_MAC80211_MESH
3506#define HWSIM_MESH_BIT BIT(NL80211_IFTYPE_MESH_POINT)
3507#else
3508#define HWSIM_MESH_BIT 0
3509#endif
3510
3511#define HWSIM_DEFAULT_IF_LIMIT \
3512        (BIT(NL80211_IFTYPE_STATION) | \
3513         BIT(NL80211_IFTYPE_P2P_CLIENT) | \
3514         BIT(NL80211_IFTYPE_AP) | \
3515         BIT(NL80211_IFTYPE_P2P_GO) | \
3516         HWSIM_MESH_BIT)
3517
3518#define HWSIM_IFTYPE_SUPPORT_MASK \
3519        (BIT(NL80211_IFTYPE_STATION) | \
3520         BIT(NL80211_IFTYPE_AP) | \
3521         BIT(NL80211_IFTYPE_P2P_CLIENT) | \
3522         BIT(NL80211_IFTYPE_P2P_GO) | \
3523         BIT(NL80211_IFTYPE_ADHOC) | \
3524         BIT(NL80211_IFTYPE_MESH_POINT) | \
3525         BIT(NL80211_IFTYPE_OCB))
3526
3527static int mac80211_hwsim_new_radio(struct genl_info *info,
3528                                    struct hwsim_new_radio_params *param)
3529{
3530        int err;
3531        u8 addr[ETH_ALEN];
3532        struct mac80211_hwsim_data *data;
3533        struct ieee80211_hw *hw;
3534        enum nl80211_band band;
3535        const struct ieee80211_ops *ops = &mac80211_hwsim_ops;
3536        struct net *net;
3537        int idx, i;
3538        int n_limits = 0;
3539
3540        if (WARN_ON(param->channels > 1 && !param->use_chanctx))
3541                return -EINVAL;
3542
3543        spin_lock_bh(&hwsim_radio_lock);
3544        idx = hwsim_radio_idx++;
3545        spin_unlock_bh(&hwsim_radio_lock);
3546
3547        if (param->use_chanctx)
3548                ops = &mac80211_hwsim_mchan_ops;
3549        hw = ieee80211_alloc_hw_nm(sizeof(*data), ops, param->hwname);
3550        if (!hw) {
3551                pr_debug("mac80211_hwsim: ieee80211_alloc_hw failed\n");
3552                err = -ENOMEM;
3553                goto failed;
3554        }
3555
3556        /* ieee80211_alloc_hw_nm may have used a default name */
3557        param->hwname = wiphy_name(hw->wiphy);
3558
3559        if (info)
3560                net = genl_info_net(info);
3561        else
3562                net = &init_net;
3563        wiphy_net_set(hw->wiphy, net);
3564
3565        data = hw->priv;
3566        data->hw = hw;
3567
3568        data->dev = device_create(hwsim_class, NULL, 0, hw, "hwsim%d", idx);
3569        if (IS_ERR(data->dev)) {
3570                printk(KERN_DEBUG
3571                       "mac80211_hwsim: device_create failed (%ld)\n",
3572                       PTR_ERR(data->dev));
3573                err = -ENOMEM;
3574                goto failed_drvdata;
3575        }
3576        data->dev->driver = &mac80211_hwsim_driver.driver;
3577        err = device_bind_driver(data->dev);
3578        if (err != 0) {
3579                pr_debug("mac80211_hwsim: device_bind_driver failed (%d)\n",
3580                       err);
3581                goto failed_bind;
3582        }
3583
3584        skb_queue_head_init(&data->pending);
3585
3586        SET_IEEE80211_DEV(hw, data->dev);
3587        if (!param->perm_addr) {
3588                eth_zero_addr(addr);
3589                addr[0] = 0x02;
3590                addr[3] = idx >> 8;
3591                addr[4] = idx;
3592                memcpy(data->addresses[0].addr, addr, ETH_ALEN);
3593                /* Why need here second address ? */
3594                memcpy(data->addresses[1].addr, addr, ETH_ALEN);
3595                data->addresses[1].addr[0] |= 0x40;
3596                hw->wiphy->n_addresses = 2;
3597                hw->wiphy->addresses = data->addresses;
3598                /* possible address clash is checked at hash table insertion */
3599        } else {
3600                memcpy(data->addresses[0].addr, param->perm_addr, ETH_ALEN);
3601                /* compatibility with automatically generated mac addr */
3602                memcpy(data->addresses[1].addr, param->perm_addr, ETH_ALEN);
3603                hw->wiphy->n_addresses = 2;
3604                hw->wiphy->addresses = data->addresses;
3605        }
3606
3607        data->channels = param->channels;
3608        data->use_chanctx = param->use_chanctx;
3609        data->idx = idx;
3610        data->destroy_on_close = param->destroy_on_close;
3611        if (info)
3612                data->portid = info->snd_portid;
3613
3614        /* setup interface limits, only on interface types we support */
3615        if (param->iftypes & BIT(NL80211_IFTYPE_ADHOC)) {
3616                data->if_limits[n_limits].max = 1;
3617                data->if_limits[n_limits].types = BIT(NL80211_IFTYPE_ADHOC);
3618                n_limits++;
3619        }
3620
3621        if (param->iftypes & HWSIM_DEFAULT_IF_LIMIT) {
3622                data->if_limits[n_limits].max = 2048;
3623                /*
3624                 * For this case, we may only support a subset of
3625                 * HWSIM_DEFAULT_IF_LIMIT, therefore we only want to add the
3626                 * bits that both param->iftype & HWSIM_DEFAULT_IF_LIMIT have.
3627                 */
3628                data->if_limits[n_limits].types =
3629                                        HWSIM_DEFAULT_IF_LIMIT & param->iftypes;
3630                n_limits++;
3631        }
3632
3633        if (param->iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
3634                data->if_limits[n_limits].max = 1;
3635                data->if_limits[n_limits].types =
3636                                                BIT(NL80211_IFTYPE_P2P_DEVICE);
3637                n_limits++;
3638        }
3639
3640        if (data->use_chanctx) {
3641                hw->wiphy->max_scan_ssids = 255;
3642                hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
3643                hw->wiphy->max_remain_on_channel_duration = 1000;
3644                data->if_combination.radar_detect_widths = 0;
3645                data->if_combination.num_different_channels = data->channels;
3646                data->chanctx = NULL;
3647        } else {
3648                data->if_combination.num_different_channels = 1;
3649                data->if_combination.radar_detect_widths =
3650                                        BIT(NL80211_CHAN_WIDTH_5) |
3651                                        BIT(NL80211_CHAN_WIDTH_10) |
3652                                        BIT(NL80211_CHAN_WIDTH_20_NOHT) |
3653                                        BIT(NL80211_CHAN_WIDTH_20) |
3654                                        BIT(NL80211_CHAN_WIDTH_40) |
3655                                        BIT(NL80211_CHAN_WIDTH_80) |
3656                                        BIT(NL80211_CHAN_WIDTH_160);
3657        }
3658
3659        if (!n_limits) {
3660                err = -EINVAL;
3661                goto failed_hw;
3662        }
3663
3664        data->if_combination.max_interfaces = 0;
3665        for (i = 0; i < n_limits; i++)
3666                data->if_combination.max_interfaces +=
3667                        data->if_limits[i].max;
3668
3669        data->if_combination.n_limits = n_limits;
3670        data->if_combination.limits = data->if_limits;
3671
3672        /*
3673         * If we actually were asked to support combinations,
3674         * advertise them - if there's only a single thing like
3675         * only IBSS then don't advertise it as combinations.
3676         */
3677        if (data->if_combination.max_interfaces > 1) {
3678                hw->wiphy->iface_combinations = &data->if_combination;
3679                hw->wiphy->n_iface_combinations = 1;
3680        }
3681
3682        if (param->ciphers) {
3683                memcpy(data->ciphers, param->ciphers,
3684                       param->n_ciphers * sizeof(u32));
3685                hw->wiphy->cipher_suites = data->ciphers;
3686                hw->wiphy->n_cipher_suites = param->n_ciphers;
3687        }
3688
3689        data->rx_rssi = DEFAULT_RX_RSSI;
3690
3691        INIT_DELAYED_WORK(&data->roc_start, hw_roc_start);
3692        INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
3693        INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
3694
3695        hw->queues = 5;
3696        hw->offchannel_tx_hw_queue = 4;
3697
3698        ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
3699        ieee80211_hw_set(hw, CHANCTX_STA_CSA);
3700        ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
3701        ieee80211_hw_set(hw, QUEUE_CONTROL);
3702        ieee80211_hw_set(hw, WANT_MONITOR_VIF);
3703        ieee80211_hw_set(hw, AMPDU_AGGREGATION);
3704        ieee80211_hw_set(hw, MFP_CAPABLE);
3705        ieee80211_hw_set(hw, SIGNAL_DBM);
3706        ieee80211_hw_set(hw, SUPPORTS_PS);
3707        ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
3708        ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
3709        ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
3710        ieee80211_hw_set(hw, TDLS_WIDER_BW);
3711        if (rctbl)
3712                ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
3713        ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
3714
3715        hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
3716        hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
3717                            WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
3718                            WIPHY_FLAG_AP_UAPSD |
3719                            WIPHY_FLAG_SUPPORTS_5_10_MHZ |
3720                            WIPHY_FLAG_HAS_CHANNEL_SWITCH;
3721        hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR |
3722                               NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
3723                               NL80211_FEATURE_STATIC_SMPS |
3724                               NL80211_FEATURE_DYNAMIC_SMPS |
3725                               NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
3726        wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
3727        wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
3728        wiphy_ext_feature_set(hw->wiphy,
3729                              NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS);
3730        wiphy_ext_feature_set(hw->wiphy,
3731                              NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
3732
3733        hw->wiphy->interface_modes = param->iftypes;
3734
3735        /* ask mac80211 to reserve space for magic */
3736        hw->vif_data_size = sizeof(struct hwsim_vif_priv);
3737        hw->sta_data_size = sizeof(struct hwsim_sta_priv);
3738        hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
3739
3740        memcpy(data->channels_2ghz, hwsim_channels_2ghz,
3741                sizeof(hwsim_channels_2ghz));
3742        memcpy(data->channels_5ghz, hwsim_channels_5ghz,
3743                sizeof(hwsim_channels_5ghz));
3744        memcpy(data->channels_6ghz, hwsim_channels_6ghz,
3745                sizeof(hwsim_channels_6ghz));
3746        memcpy(data->channels_s1g, hwsim_channels_s1g,
3747               sizeof(hwsim_channels_s1g));
3748        memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
3749
3750        for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
3751                struct ieee80211_supported_band *sband = &data->bands[band];
3752
3753                sband->band = band;
3754
3755                switch (band) {
3756                case NL80211_BAND_2GHZ:
3757                        sband->channels = data->channels_2ghz;
3758                        sband->n_channels = ARRAY_SIZE(hwsim_channels_2ghz);
3759                        sband->bitrates = data->rates;
3760                        sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
3761                        break;
3762                case NL80211_BAND_5GHZ:
3763                        sband->channels = data->channels_5ghz;
3764                        sband->n_channels = ARRAY_SIZE(hwsim_channels_5ghz);
3765                        sband->bitrates = data->rates + 4;
3766                        sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
3767
3768                        sband->vht_cap.vht_supported = true;
3769                        sband->vht_cap.cap =
3770                                IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
3771                                IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
3772                                IEEE80211_VHT_CAP_RXLDPC |
3773                                IEEE80211_VHT_CAP_SHORT_GI_80 |
3774                                IEEE80211_VHT_CAP_SHORT_GI_160 |
3775                                IEEE80211_VHT_CAP_TXSTBC |
3776                                IEEE80211_VHT_CAP_RXSTBC_4 |
3777                                IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
3778                        sband->vht_cap.vht_mcs.rx_mcs_map =
3779                                cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
3780                                            IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
3781                                            IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
3782                                            IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 |
3783                                            IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 |
3784                                            IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
3785                                            IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
3786                                            IEEE80211_VHT_MCS_SUPPORT_0_9 << 14);
3787                        sband->vht_cap.vht_mcs.tx_mcs_map =
3788                                sband->vht_cap.vht_mcs.rx_mcs_map;
3789                        break;
3790                case NL80211_BAND_6GHZ:
3791                        sband->channels = data->channels_6ghz;
3792                        sband->n_channels = ARRAY_SIZE(hwsim_channels_6ghz);
3793                        sband->bitrates = data->rates + 4;
3794                        sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
3795                        break;
3796                case NL80211_BAND_S1GHZ:
3797                        memcpy(&sband->s1g_cap, &hwsim_s1g_cap,
3798                               sizeof(sband->s1g_cap));
3799                        sband->channels = data->channels_s1g;
3800                        sband->n_channels = ARRAY_SIZE(hwsim_channels_s1g);
3801                        break;
3802                default:
3803                        continue;
3804                }
3805
3806                if (band != NL80211_BAND_6GHZ){
3807                        sband->ht_cap.ht_supported = true;
3808                        sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
3809                                            IEEE80211_HT_CAP_GRN_FLD |
3810                                            IEEE80211_HT_CAP_SGI_20 |
3811                                            IEEE80211_HT_CAP_SGI_40 |
3812                                            IEEE80211_HT_CAP_DSSSCCK40;
3813                        sband->ht_cap.ampdu_factor = 0x3;
3814                        sband->ht_cap.ampdu_density = 0x6;
3815                        memset(&sband->ht_cap.mcs, 0,
3816                               sizeof(sband->ht_cap.mcs));
3817                        sband->ht_cap.mcs.rx_mask[0] = 0xff;
3818                        sband->ht_cap.mcs.rx_mask[1] = 0xff;
3819                        sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
3820                }
3821
3822                mac80211_hwsim_sband_capab(sband);
3823
3824                hw->wiphy->bands[band] = sband;
3825        }
3826
3827        /* By default all radios belong to the first group */
3828        data->group = 1;
3829        mutex_init(&data->mutex);
3830
3831        data->netgroup = hwsim_net_get_netgroup(net);
3832        data->wmediumd = hwsim_net_get_wmediumd(net);
3833
3834        /* Enable frame retransmissions for lossy channels */
3835        hw->max_rates = 4;
3836        hw->max_rate_tries = 11;
3837
3838        hw->wiphy->vendor_commands = mac80211_hwsim_vendor_commands;
3839        hw->wiphy->n_vendor_commands =
3840                ARRAY_SIZE(mac80211_hwsim_vendor_commands);
3841        hw->wiphy->vendor_events = mac80211_hwsim_vendor_events;
3842        hw->wiphy->n_vendor_events = ARRAY_SIZE(mac80211_hwsim_vendor_events);
3843
3844        if (param->reg_strict)
3845                hw->wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
3846        if (param->regd) {
3847                data->regd = param->regd;
3848                hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
3849                wiphy_apply_custom_regulatory(hw->wiphy, param->regd);
3850                /* give the regulatory workqueue a chance to run */
3851                schedule_timeout_interruptible(1);
3852        }
3853
3854        if (param->no_vif)
3855                ieee80211_hw_set(hw, NO_AUTO_VIF);
3856
3857        wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
3858
3859        hrtimer_init(&data->beacon_timer, CLOCK_MONOTONIC,
3860                     HRTIMER_MODE_ABS_SOFT);
3861        data->beacon_timer.function = mac80211_hwsim_beacon;
3862
3863        err = ieee80211_register_hw(hw);
3864        if (err < 0) {
3865                pr_debug("mac80211_hwsim: ieee80211_register_hw failed (%d)\n",
3866                       err);
3867                goto failed_hw;
3868        }
3869
3870        wiphy_dbg(hw->wiphy, "hwaddr %pM registered\n", hw->wiphy->perm_addr);
3871
3872        if (param->reg_alpha2) {
3873                data->alpha2[0] = param->reg_alpha2[0];
3874                data->alpha2[1] = param->reg_alpha2[1];
3875                regulatory_hint(hw->wiphy, param->reg_alpha2);
3876        }
3877
3878        data->debugfs = debugfs_create_dir("hwsim", hw->wiphy->debugfsdir);
3879        debugfs_create_file("ps", 0666, data->debugfs, data, &hwsim_fops_ps);
3880        debugfs_create_file("group", 0666, data->debugfs, data,
3881                            &hwsim_fops_group);
3882        debugfs_create_file("rx_rssi", 0666, data->debugfs, data,
3883                            &hwsim_fops_rx_rssi);
3884        if (!data->use_chanctx)
3885                debugfs_create_file("dfs_simulate_radar", 0222,
3886                                    data->debugfs,
3887                                    data, &hwsim_simulate_radar);
3888
3889        spin_lock_bh(&hwsim_radio_lock);
3890        err = rhashtable_insert_fast(&hwsim_radios_rht, &data->rht,
3891                                     hwsim_rht_params);
3892        if (err < 0) {
3893                if (info) {
3894                        GENL_SET_ERR_MSG(info, "perm addr already present");
3895                        NL_SET_BAD_ATTR(info->extack,
3896                                        info->attrs[HWSIM_ATTR_PERM_ADDR]);
3897                }
3898                spin_unlock_bh(&hwsim_radio_lock);
3899                goto failed_final_insert;
3900        }
3901
3902        list_add_tail(&data->list, &hwsim_radios);
3903        hwsim_radios_generation++;
3904        spin_unlock_bh(&hwsim_radio_lock);
3905
3906        hwsim_mcast_new_radio(idx, info, param);
3907
3908        return idx;
3909
3910failed_final_insert:
3911        debugfs_remove_recursive(data->debugfs);
3912        ieee80211_unregister_hw(data->hw);
3913failed_hw:
3914        device_release_driver(data->dev);
3915failed_bind:
3916        device_unregister(data->dev);
3917failed_drvdata:
3918        ieee80211_free_hw(hw);
3919failed:
3920        return err;
3921}
3922
3923static void hwsim_mcast_del_radio(int id, const char *hwname,
3924                                  struct genl_info *info)
3925{
3926        struct sk_buff *skb;
3927        void *data;
3928        int ret;
3929
3930        skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3931        if (!skb)
3932                return;
3933
3934        data = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
3935                           HWSIM_CMD_DEL_RADIO);
3936        if (!data)
3937                goto error;
3938
3939        ret = nla_put_u32(skb, HWSIM_ATTR_RADIO_ID, id);
3940        if (ret < 0)
3941                goto error;
3942
3943        ret = nla_put(skb, HWSIM_ATTR_RADIO_NAME, strlen(hwname),
3944                      hwname);
3945        if (ret < 0)
3946                goto error;
3947
3948        genlmsg_end(skb, data);
3949
3950        hwsim_mcast_config_msg(skb, info);
3951
3952        return;
3953
3954error:
3955        nlmsg_free(skb);
3956}
3957
3958static void mac80211_hwsim_del_radio(struct mac80211_hwsim_data *data,
3959                                     const char *hwname,
3960                                     struct genl_info *info)
3961{
3962        hwsim_mcast_del_radio(data->idx, hwname, info);
3963        debugfs_remove_recursive(data->debugfs);
3964        ieee80211_unregister_hw(data->hw);
3965        device_release_driver(data->dev);
3966        device_unregister(data->dev);
3967        ieee80211_free_hw(data->hw);
3968}
3969
3970static int mac80211_hwsim_get_radio(struct sk_buff *skb,
3971                                    struct mac80211_hwsim_data *data,
3972                                    u32 portid, u32 seq,
3973                                    struct netlink_callback *cb, int flags)
3974{
3975        void *hdr;
3976        struct hwsim_new_radio_params param = { };
3977        int res = -EMSGSIZE;
3978
3979        hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
3980                          HWSIM_CMD_GET_RADIO);
3981        if (!hdr)
3982                return -EMSGSIZE;
3983
3984        if (cb)
3985                genl_dump_check_consistent(cb, hdr);
3986
3987        if (data->alpha2[0] && data->alpha2[1])
3988                param.reg_alpha2 = data->alpha2;
3989
3990        param.reg_strict = !!(data->hw->wiphy->regulatory_flags &
3991                                        REGULATORY_STRICT_REG);
3992        param.p2p_device = !!(data->hw->wiphy->interface_modes &
3993                                        BIT(NL80211_IFTYPE_P2P_DEVICE));
3994        param.use_chanctx = data->use_chanctx;
3995        param.regd = data->regd;
3996        param.channels = data->channels;
3997        param.hwname = wiphy_name(data->hw->wiphy);
3998
3999        res = append_radio_msg(skb, data->idx, &param);
4000        if (res < 0)
4001                goto out_err;
4002
4003        genlmsg_end(skb, hdr);
4004        return 0;
4005
4006out_err:
4007        genlmsg_cancel(skb, hdr);
4008        return res;
4009}
4010
4011static void mac80211_hwsim_free(void)
4012{
4013        struct mac80211_hwsim_data *data;
4014
4015        spin_lock_bh(&hwsim_radio_lock);
4016        while ((data = list_first_entry_or_null(&hwsim_radios,
4017                                                struct mac80211_hwsim_data,
4018                                                list))) {
4019                list_del(&data->list);
4020                spin_unlock_bh(&hwsim_radio_lock);
4021                mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
4022                                         NULL);
4023                spin_lock_bh(&hwsim_radio_lock);
4024        }
4025        spin_unlock_bh(&hwsim_radio_lock);
4026        class_destroy(hwsim_class);
4027}
4028
4029static const struct net_device_ops hwsim_netdev_ops = {
4030        .ndo_start_xmit         = hwsim_mon_xmit,
4031        .ndo_set_mac_address    = eth_mac_addr,
4032        .ndo_validate_addr      = eth_validate_addr,
4033};
4034
4035static void hwsim_mon_setup(struct net_device *dev)
4036{
4037        u8 addr[ETH_ALEN];
4038
4039        dev->netdev_ops = &hwsim_netdev_ops;
4040        dev->needs_free_netdev = true;
4041        ether_setup(dev);
4042        dev->priv_flags |= IFF_NO_QUEUE;
4043        dev->type = ARPHRD_IEEE80211_RADIOTAP;
4044        eth_zero_addr(addr);
4045        addr[0] = 0x12;
4046        eth_hw_addr_set(dev, addr);
4047}
4048
4049static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(const u8 *addr)
4050{
4051        return rhashtable_lookup_fast(&hwsim_radios_rht,
4052                                      addr,
4053                                      hwsim_rht_params);
4054}
4055
4056static void hwsim_register_wmediumd(struct net *net, u32 portid)
4057{
4058        struct mac80211_hwsim_data *data;
4059
4060        hwsim_net_set_wmediumd(net, portid);
4061
4062        spin_lock_bh(&hwsim_radio_lock);
4063        list_for_each_entry(data, &hwsim_radios, list) {
4064                if (data->netgroup == hwsim_net_get_netgroup(net))
4065                        data->wmediumd = portid;
4066        }
4067        spin_unlock_bh(&hwsim_radio_lock);
4068}
4069
4070static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
4071                                           struct genl_info *info)
4072{
4073
4074        struct ieee80211_hdr *hdr;
4075        struct mac80211_hwsim_data *data2;
4076        struct ieee80211_tx_info *txi;
4077        struct hwsim_tx_rate *tx_attempts;
4078        u64 ret_skb_cookie;
4079        struct sk_buff *skb, *tmp;
4080        const u8 *src;
4081        unsigned int hwsim_flags;
4082        int i;
4083        bool found = false;
4084
4085        if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
4086            !info->attrs[HWSIM_ATTR_FLAGS] ||
4087            !info->attrs[HWSIM_ATTR_COOKIE] ||
4088            !info->attrs[HWSIM_ATTR_SIGNAL] ||
4089            !info->attrs[HWSIM_ATTR_TX_INFO])
4090                goto out;
4091
4092        src = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
4093        hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
4094        ret_skb_cookie = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
4095
4096        data2 = get_hwsim_data_ref_from_addr(src);
4097        if (!data2)
4098                goto out;
4099
4100        if (!hwsim_virtio_enabled) {
4101                if (hwsim_net_get_netgroup(genl_info_net(info)) !=
4102                    data2->netgroup)
4103                        goto out;
4104
4105                if (info->snd_portid != data2->wmediumd)
4106                        goto out;
4107        }
4108
4109        /* look for the skb matching the cookie passed back from user */
4110        skb_queue_walk_safe(&data2->pending, skb, tmp) {
4111                u64 skb_cookie;
4112
4113                txi = IEEE80211_SKB_CB(skb);
4114                skb_cookie = (u64)(uintptr_t)txi->rate_driver_data[0];
4115
4116                if (skb_cookie == ret_skb_cookie) {
4117                        skb_unlink(skb, &data2->pending);
4118                        found = true;
4119                        break;
4120                }
4121        }
4122
4123        /* not found */
4124        if (!found)
4125                goto out;
4126
4127        /* Tx info received because the frame was broadcasted on user space,
4128         so we get all the necessary info: tx attempts and skb control buff */
4129
4130        tx_attempts = (struct hwsim_tx_rate *)nla_data(
4131                       info->attrs[HWSIM_ATTR_TX_INFO]);
4132
4133        /* now send back TX status */
4134        txi = IEEE80211_SKB_CB(skb);
4135
4136        ieee80211_tx_info_clear_status(txi);
4137
4138        for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
4139                txi->status.rates[i].idx = tx_attempts[i].idx;
4140                txi->status.rates[i].count = tx_attempts[i].count;
4141        }
4142
4143        txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
4144
4145        if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
4146           (hwsim_flags & HWSIM_TX_STAT_ACK)) {
4147                if (skb->len >= 16) {
4148                        hdr = (struct ieee80211_hdr *) skb->data;
4149                        mac80211_hwsim_monitor_ack(data2->channel,
4150                                                   hdr->addr2);
4151                }
4152                txi->flags |= IEEE80211_TX_STAT_ACK;
4153        }
4154
4155        if (hwsim_flags & HWSIM_TX_CTL_NO_ACK)
4156                txi->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
4157
4158        ieee80211_tx_status_irqsafe(data2->hw, skb);
4159        return 0;
4160out:
4161        return -EINVAL;
4162
4163}
4164
4165static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
4166                                          struct genl_info *info)
4167{
4168        struct mac80211_hwsim_data *data2;
4169        struct ieee80211_rx_status rx_status;
4170        struct ieee80211_hdr *hdr;
4171        const u8 *dst;
4172        int frame_data_len;
4173        void *frame_data;
4174        struct sk_buff *skb = NULL;
4175        struct ieee80211_channel *channel = NULL;
4176
4177        if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
4178            !info->attrs[HWSIM_ATTR_FRAME] ||
4179            !info->attrs[HWSIM_ATTR_RX_RATE] ||
4180            !info->attrs[HWSIM_ATTR_SIGNAL])
4181                goto out;
4182
4183        dst = (void *)nla_data(info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
4184        frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
4185        frame_data = (void *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
4186
4187        /* Allocate new skb here */
4188        skb = alloc_skb(frame_data_len, GFP_KERNEL);
4189        if (skb == NULL)
4190                goto err;
4191
4192        if (frame_data_len > IEEE80211_MAX_DATA_LEN)
4193                goto err;
4194
4195        /* Copy the data */
4196        skb_put_data(skb, frame_data, frame_data_len);
4197
4198        data2 = get_hwsim_data_ref_from_addr(dst);
4199        if (!data2)
4200                goto out;
4201
4202        if (data2->use_chanctx) {
4203                if (data2->tmp_chan)
4204                        channel = data2->tmp_chan;
4205                else if (data2->chanctx)
4206                        channel = data2->chanctx->def.chan;
4207        } else {
4208                channel = data2->channel;
4209        }
4210        if (!channel)
4211                goto out;
4212
4213        if (!hwsim_virtio_enabled) {
4214                if (hwsim_net_get_netgroup(genl_info_net(info)) !=
4215                    data2->netgroup)
4216                        goto out;
4217
4218                if (info->snd_portid != data2->wmediumd)
4219                        goto out;
4220        }
4221
4222        /* check if radio is configured properly */
4223
4224        if ((data2->idle && !data2->tmp_chan) || !data2->started)
4225                goto out;
4226
4227        /* A frame is received from user space */
4228        memset(&rx_status, 0, sizeof(rx_status));
4229        if (info->attrs[HWSIM_ATTR_FREQ]) {
4230                /* throw away off-channel packets, but allow both the temporary
4231                 * ("hw" scan/remain-on-channel) and regular channel, since the
4232                 * internal datapath also allows this
4233                 */
4234                mutex_lock(&data2->mutex);
4235                rx_status.freq = nla_get_u32(info->attrs[HWSIM_ATTR_FREQ]);
4236
4237                if (rx_status.freq != channel->center_freq) {
4238                        mutex_unlock(&data2->mutex);
4239                        goto out;
4240                }
4241                mutex_unlock(&data2->mutex);
4242        } else {
4243                rx_status.freq = channel->center_freq;
4244        }
4245
4246        rx_status.band = channel->band;
4247        rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
4248        rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
4249
4250        hdr = (void *)skb->data;
4251
4252        if (ieee80211_is_beacon(hdr->frame_control) ||
4253            ieee80211_is_probe_resp(hdr->frame_control))
4254                rx_status.boottime_ns = ktime_get_boottime_ns();
4255
4256        memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
4257        data2->rx_pkts++;
4258        data2->rx_bytes += skb->len;
4259        ieee80211_rx_irqsafe(data2->hw, skb);
4260
4261        return 0;
4262err:
4263        pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
4264out:
4265        dev_kfree_skb(skb);
4266        return -EINVAL;
4267}
4268
4269static int hwsim_register_received_nl(struct sk_buff *skb_2,
4270                                      struct genl_info *info)
4271{
4272        struct net *net = genl_info_net(info);
4273        struct mac80211_hwsim_data *data;
4274        int chans = 1;
4275
4276        spin_lock_bh(&hwsim_radio_lock);
4277        list_for_each_entry(data, &hwsim_radios, list)
4278                chans = max(chans, data->channels);
4279        spin_unlock_bh(&hwsim_radio_lock);
4280
4281        /* In the future we should revise the userspace API and allow it
4282         * to set a flag that it does support multi-channel, then we can
4283         * let this pass conditionally on the flag.
4284         * For current userspace, prohibit it since it won't work right.
4285         */
4286        if (chans > 1)
4287                return -EOPNOTSUPP;
4288
4289        if (hwsim_net_get_wmediumd(net))
4290                return -EBUSY;
4291
4292        hwsim_register_wmediumd(net, info->snd_portid);
4293
4294        pr_debug("mac80211_hwsim: received a REGISTER, "
4295               "switching to wmediumd mode with pid %d\n", info->snd_portid);
4296
4297        return 0;
4298}
4299
4300/* ensures ciphers only include ciphers listed in 'hwsim_ciphers' array */
4301static bool hwsim_known_ciphers(const u32 *ciphers, int n_ciphers)
4302{
4303        int i;
4304
4305        for (i = 0; i < n_ciphers; i++) {
4306                int j;
4307                int found = 0;
4308
4309                for (j = 0; j < ARRAY_SIZE(hwsim_ciphers); j++) {
4310                        if (ciphers[i] == hwsim_ciphers[j]) {
4311                                found = 1;
4312                                break;
4313                        }
4314                }
4315
4316                if (!found)
4317                        return false;
4318        }
4319
4320        return true;
4321}
4322
4323static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
4324{
4325        struct hwsim_new_radio_params param = { 0 };
4326        const char *hwname = NULL;
4327        int ret;
4328
4329        param.reg_strict = info->attrs[HWSIM_ATTR_REG_STRICT_REG];
4330        param.p2p_device = info->attrs[HWSIM_ATTR_SUPPORT_P2P_DEVICE];
4331        param.channels = channels;
4332        param.destroy_on_close =
4333                info->attrs[HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE];
4334
4335        if (info->attrs[HWSIM_ATTR_CHANNELS])
4336                param.channels = nla_get_u32(info->attrs[HWSIM_ATTR_CHANNELS]);
4337
4338        if (param.channels < 1) {
4339                GENL_SET_ERR_MSG(info, "must have at least one channel");
4340                return -EINVAL;
4341        }
4342
4343        if (info->attrs[HWSIM_ATTR_NO_VIF])
4344                param.no_vif = true;
4345
4346        if (info->attrs[HWSIM_ATTR_USE_CHANCTX])
4347                param.use_chanctx = true;
4348        else
4349                param.use_chanctx = (param.channels > 1);
4350
4351        if (info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2])
4352                param.reg_alpha2 =
4353                        nla_data(info->attrs[HWSIM_ATTR_REG_HINT_ALPHA2]);
4354
4355        if (info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]) {
4356                u32 idx = nla_get_u32(info->attrs[HWSIM_ATTR_REG_CUSTOM_REG]);
4357
4358                if (idx >= ARRAY_SIZE(hwsim_world_regdom_custom))
4359                        return -EINVAL;
4360
4361                idx = array_index_nospec(idx,
4362                                         ARRAY_SIZE(hwsim_world_regdom_custom));
4363                param.regd = hwsim_world_regdom_custom[idx];
4364        }
4365
4366        if (info->attrs[HWSIM_ATTR_PERM_ADDR]) {
4367                if (!is_valid_ether_addr(
4368                                nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]))) {
4369                        GENL_SET_ERR_MSG(info,"MAC is no valid source addr");
4370                        NL_SET_BAD_ATTR(info->extack,
4371                                        info->attrs[HWSIM_ATTR_PERM_ADDR]);
4372                        return -EINVAL;
4373                }
4374
4375                param.perm_addr = nla_data(info->attrs[HWSIM_ATTR_PERM_ADDR]);
4376        }
4377
4378        if (info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]) {
4379                param.iftypes =
4380                        nla_get_u32(info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT]);
4381
4382                if (param.iftypes & ~HWSIM_IFTYPE_SUPPORT_MASK) {
4383                        NL_SET_ERR_MSG_ATTR(info->extack,
4384                                            info->attrs[HWSIM_ATTR_IFTYPE_SUPPORT],
4385                                            "cannot support more iftypes than kernel");
4386                        return -EINVAL;
4387                }
4388        } else {
4389                param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
4390        }
4391
4392        /* ensure both flag and iftype support is honored */
4393        if (param.p2p_device ||
4394            param.iftypes & BIT(NL80211_IFTYPE_P2P_DEVICE)) {
4395                param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
4396                param.p2p_device = true;
4397        }
4398
4399        if (info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]) {
4400                u32 len = nla_len(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
4401
4402                param.ciphers =
4403                        nla_data(info->attrs[HWSIM_ATTR_CIPHER_SUPPORT]);
4404
4405                if (len % sizeof(u32)) {
4406                        NL_SET_ERR_MSG_ATTR(info->extack,
4407                                            info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
4408                                            "bad cipher list length");
4409                        return -EINVAL;
4410                }
4411
4412                param.n_ciphers = len / sizeof(u32);
4413
4414                if (param.n_ciphers > ARRAY_SIZE(hwsim_ciphers)) {
4415                        NL_SET_ERR_MSG_ATTR(info->extack,
4416                                            info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
4417                                            "too many ciphers specified");
4418                        return -EINVAL;
4419                }
4420
4421                if (!hwsim_known_ciphers(param.ciphers, param.n_ciphers)) {
4422                        NL_SET_ERR_MSG_ATTR(info->extack,
4423                                            info->attrs[HWSIM_ATTR_CIPHER_SUPPORT],
4424                                            "unsupported ciphers specified");
4425                        return -EINVAL;
4426                }
4427        }
4428
4429        if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
4430                hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
4431                                  nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
4432                                  GFP_KERNEL);
4433                if (!hwname)
4434                        return -ENOMEM;
4435                param.hwname = hwname;
4436        }
4437
4438        ret = mac80211_hwsim_new_radio(info, &param);
4439        kfree(hwname);
4440        return ret;
4441}
4442
4443static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
4444{
4445        struct mac80211_hwsim_data *data;
4446        s64 idx = -1;
4447        const char *hwname = NULL;
4448
4449        if (info->attrs[HWSIM_ATTR_RADIO_ID]) {
4450                idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
4451        } else if (info->attrs[HWSIM_ATTR_RADIO_NAME]) {
4452                hwname = kstrndup((char *)nla_data(info->attrs[HWSIM_ATTR_RADIO_NAME]),
4453                                  nla_len(info->attrs[HWSIM_ATTR_RADIO_NAME]),
4454                                  GFP_KERNEL);
4455                if (!hwname)
4456                        return -ENOMEM;
4457        } else
4458                return -EINVAL;
4459
4460        spin_lock_bh(&hwsim_radio_lock);
4461        list_for_each_entry(data, &hwsim_radios, list) {
4462                if (idx >= 0) {
4463                        if (data->idx != idx)
4464                                continue;
4465                } else {
4466                        if (!hwname ||
4467                            strcmp(hwname, wiphy_name(data->hw->wiphy)))
4468                                continue;
4469                }
4470
4471                if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
4472                        continue;
4473
4474                list_del(&data->list);
4475                rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
4476                                       hwsim_rht_params);
4477                hwsim_radios_generation++;
4478                spin_unlock_bh(&hwsim_radio_lock);
4479                mac80211_hwsim_del_radio(data, wiphy_name(data->hw->wiphy),
4480                                         info);
4481                kfree(hwname);
4482                return 0;
4483        }
4484        spin_unlock_bh(&hwsim_radio_lock);
4485
4486        kfree(hwname);
4487        return -ENODEV;
4488}
4489
4490static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
4491{
4492        struct mac80211_hwsim_data *data;
4493        struct sk_buff *skb;
4494        int idx, res = -ENODEV;
4495
4496        if (!info->attrs[HWSIM_ATTR_RADIO_ID])
4497                return -EINVAL;
4498        idx = nla_get_u32(info->attrs[HWSIM_ATTR_RADIO_ID]);
4499
4500        spin_lock_bh(&hwsim_radio_lock);
4501        list_for_each_entry(data, &hwsim_radios, list) {
4502                if (data->idx != idx)
4503                        continue;
4504
4505                if (!net_eq(wiphy_net(data->hw->wiphy), genl_info_net(info)))
4506                        continue;
4507
4508                skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
4509                if (!skb) {
4510                        res = -ENOMEM;
4511                        goto out_err;
4512                }
4513
4514                res = mac80211_hwsim_get_radio(skb, data, info->snd_portid,
4515                                               info->snd_seq, NULL, 0);
4516                if (res < 0) {
4517                        nlmsg_free(skb);
4518                        goto out_err;
4519                }
4520
4521                res = genlmsg_reply(skb, info);
4522                break;
4523        }
4524
4525out_err:
4526        spin_unlock_bh(&hwsim_radio_lock);
4527
4528        return res;
4529}
4530
4531static int hwsim_dump_radio_nl(struct sk_buff *skb,
4532                               struct netlink_callback *cb)
4533{
4534        int last_idx = cb->args[0] - 1;
4535        struct mac80211_hwsim_data *data = NULL;
4536        int res = 0;
4537        void *hdr;
4538
4539        spin_lock_bh(&hwsim_radio_lock);
4540        cb->seq = hwsim_radios_generation;
4541
4542        if (last_idx >= hwsim_radio_idx-1)
4543                goto done;
4544
4545        list_for_each_entry(data, &hwsim_radios, list) {
4546                if (data->idx <= last_idx)
4547                        continue;
4548
4549                if (!net_eq(wiphy_net(data->hw->wiphy), sock_net(skb->sk)))
4550                        continue;
4551
4552                res = mac80211_hwsim_get_radio(skb, data,
4553                                               NETLINK_CB(cb->skb).portid,
4554                                               cb->nlh->nlmsg_seq, cb,
4555                                               NLM_F_MULTI);
4556                if (res < 0)
4557                        break;
4558
4559                last_idx = data->idx;
4560        }
4561
4562        cb->args[0] = last_idx + 1;
4563
4564        /* list changed, but no new element sent, set interrupted flag */
4565        if (skb->len == 0 && cb->prev_seq && cb->seq != cb->prev_seq) {
4566                hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
4567                                  cb->nlh->nlmsg_seq, &hwsim_genl_family,
4568                                  NLM_F_MULTI, HWSIM_CMD_GET_RADIO);
4569                if (hdr) {
4570                        genl_dump_check_consistent(cb, hdr);
4571                        genlmsg_end(skb, hdr);
4572                } else {
4573                        res = -EMSGSIZE;
4574                }
4575        }
4576
4577done:
4578        spin_unlock_bh(&hwsim_radio_lock);
4579        return res ?: skb->len;
4580}
4581
4582/* Generic Netlink operations array */
4583static const struct genl_small_ops hwsim_ops[] = {
4584        {
4585                .cmd = HWSIM_CMD_REGISTER,
4586                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4587                .doit = hwsim_register_received_nl,
4588                .flags = GENL_UNS_ADMIN_PERM,
4589        },
4590        {
4591                .cmd = HWSIM_CMD_FRAME,
4592                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4593                .doit = hwsim_cloned_frame_received_nl,
4594        },
4595        {
4596                .cmd = HWSIM_CMD_TX_INFO_FRAME,
4597                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4598                .doit = hwsim_tx_info_frame_received_nl,
4599        },
4600        {
4601                .cmd = HWSIM_CMD_NEW_RADIO,
4602                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4603                .doit = hwsim_new_radio_nl,
4604                .flags = GENL_UNS_ADMIN_PERM,
4605        },
4606        {
4607                .cmd = HWSIM_CMD_DEL_RADIO,
4608                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4609                .doit = hwsim_del_radio_nl,
4610                .flags = GENL_UNS_ADMIN_PERM,
4611        },
4612        {
4613                .cmd = HWSIM_CMD_GET_RADIO,
4614                .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4615                .doit = hwsim_get_radio_nl,
4616                .dumpit = hwsim_dump_radio_nl,
4617        },
4618};
4619
4620static struct genl_family hwsim_genl_family __ro_after_init = {
4621        .name = "MAC80211_HWSIM",
4622        .version = 1,
4623        .maxattr = HWSIM_ATTR_MAX,
4624        .policy = hwsim_genl_policy,
4625        .netnsok = true,
4626        .module = THIS_MODULE,
4627        .small_ops = hwsim_ops,
4628        .n_small_ops = ARRAY_SIZE(hwsim_ops),
4629        .mcgrps = hwsim_mcgrps,
4630        .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
4631};
4632
4633static void remove_user_radios(u32 portid)
4634{
4635        struct mac80211_hwsim_data *entry, *tmp;
4636        LIST_HEAD(list);
4637
4638        spin_lock_bh(&hwsim_radio_lock);
4639        list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
4640                if (entry->destroy_on_close && entry->portid == portid) {
4641                        list_move(&entry->list, &list);
4642                        rhashtable_remove_fast(&hwsim_radios_rht, &entry->rht,
4643                                               hwsim_rht_params);
4644                        hwsim_radios_generation++;
4645                }
4646        }
4647        spin_unlock_bh(&hwsim_radio_lock);
4648
4649        list_for_each_entry_safe(entry, tmp, &list, list) {
4650                list_del(&entry->list);
4651                mac80211_hwsim_del_radio(entry, wiphy_name(entry->hw->wiphy),
4652                                         NULL);
4653        }
4654}
4655
4656static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
4657                                         unsigned long state,
4658                                         void *_notify)
4659{
4660        struct netlink_notify *notify = _notify;
4661
4662        if (state != NETLINK_URELEASE)
4663                return NOTIFY_DONE;
4664
4665        remove_user_radios(notify->portid);
4666
4667        if (notify->portid == hwsim_net_get_wmediumd(notify->net)) {
4668                printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
4669                       " socket, switching to perfect channel medium\n");
4670                hwsim_register_wmediumd(notify->net, 0);
4671        }
4672        return NOTIFY_DONE;
4673
4674}
4675
4676static struct notifier_block hwsim_netlink_notifier = {
4677        .notifier_call = mac80211_hwsim_netlink_notify,
4678};
4679
4680static int __init hwsim_init_netlink(void)
4681{
4682        int rc;
4683
4684        printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
4685
4686        rc = genl_register_family(&hwsim_genl_family);
4687        if (rc)
4688                goto failure;
4689
4690        rc = netlink_register_notifier(&hwsim_netlink_notifier);
4691        if (rc) {
4692                genl_unregister_family(&hwsim_genl_family);
4693                goto failure;
4694        }
4695
4696        return 0;
4697
4698failure:
4699        pr_debug("mac80211_hwsim: error occurred in %s\n", __func__);
4700        return -EINVAL;
4701}
4702
4703static __net_init int hwsim_init_net(struct net *net)
4704{
4705        return hwsim_net_set_netgroup(net);
4706}
4707
4708static void __net_exit hwsim_exit_net(struct net *net)
4709{
4710        struct mac80211_hwsim_data *data, *tmp;
4711        LIST_HEAD(list);
4712
4713        spin_lock_bh(&hwsim_radio_lock);
4714        list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
4715                if (!net_eq(wiphy_net(data->hw->wiphy), net))
4716                        continue;
4717
4718                /* Radios created in init_net are returned to init_net. */
4719                if (data->netgroup == hwsim_net_get_netgroup(&init_net))
4720                        continue;
4721
4722                list_move(&data->list, &list);
4723                rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
4724                                       hwsim_rht_params);
4725                hwsim_radios_generation++;
4726        }
4727        spin_unlock_bh(&hwsim_radio_lock);
4728
4729        list_for_each_entry_safe(data, tmp, &list, list) {
4730                list_del(&data->list);
4731                mac80211_hwsim_del_radio(data,
4732                                         wiphy_name(data->hw->wiphy),
4733                                         NULL);
4734        }
4735
4736        ida_simple_remove(&hwsim_netgroup_ida, hwsim_net_get_netgroup(net));
4737}
4738
4739static struct pernet_operations hwsim_net_ops = {
4740        .init = hwsim_init_net,
4741        .exit = hwsim_exit_net,
4742        .id   = &hwsim_net_id,
4743        .size = sizeof(struct hwsim_net),
4744};
4745
4746static void hwsim_exit_netlink(void)
4747{
4748        /* unregister the notifier */
4749        netlink_unregister_notifier(&hwsim_netlink_notifier);
4750        /* unregister the family */
4751        genl_unregister_family(&hwsim_genl_family);
4752}
4753
4754#if IS_REACHABLE(CONFIG_VIRTIO)
4755static void hwsim_virtio_tx_done(struct virtqueue *vq)
4756{
4757        unsigned int len;
4758        struct sk_buff *skb;
4759        unsigned long flags;
4760
4761        spin_lock_irqsave(&hwsim_virtio_lock, flags);
4762        while ((skb = virtqueue_get_buf(vq, &len)))
4763                nlmsg_free(skb);
4764        spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4765}
4766
4767static int hwsim_virtio_handle_cmd(struct sk_buff *skb)
4768{
4769        struct nlmsghdr *nlh;
4770        struct genlmsghdr *gnlh;
4771        struct nlattr *tb[HWSIM_ATTR_MAX + 1];
4772        struct genl_info info = {};
4773        int err;
4774
4775        nlh = nlmsg_hdr(skb);
4776        gnlh = nlmsg_data(nlh);
4777        err = genlmsg_parse(nlh, &hwsim_genl_family, tb, HWSIM_ATTR_MAX,
4778                            hwsim_genl_policy, NULL);
4779        if (err) {
4780                pr_err_ratelimited("hwsim: genlmsg_parse returned %d\n", err);
4781                return err;
4782        }
4783
4784        info.attrs = tb;
4785
4786        switch (gnlh->cmd) {
4787        case HWSIM_CMD_FRAME:
4788                hwsim_cloned_frame_received_nl(skb, &info);
4789                break;
4790        case HWSIM_CMD_TX_INFO_FRAME:
4791                hwsim_tx_info_frame_received_nl(skb, &info);
4792                break;
4793        default:
4794                pr_err_ratelimited("hwsim: invalid cmd: %d\n", gnlh->cmd);
4795                return -EPROTO;
4796        }
4797        return 0;
4798}
4799
4800static void hwsim_virtio_rx_work(struct work_struct *work)
4801{
4802        struct virtqueue *vq;
4803        unsigned int len;
4804        struct sk_buff *skb;
4805        struct scatterlist sg[1];
4806        int err;
4807        unsigned long flags;
4808
4809        spin_lock_irqsave(&hwsim_virtio_lock, flags);
4810        if (!hwsim_virtio_enabled)
4811                goto out_unlock;
4812
4813        skb = virtqueue_get_buf(hwsim_vqs[HWSIM_VQ_RX], &len);
4814        if (!skb)
4815                goto out_unlock;
4816        spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4817
4818        skb->data = skb->head;
4819        skb_set_tail_pointer(skb, len);
4820        hwsim_virtio_handle_cmd(skb);
4821
4822        spin_lock_irqsave(&hwsim_virtio_lock, flags);
4823        if (!hwsim_virtio_enabled) {
4824                nlmsg_free(skb);
4825                goto out_unlock;
4826        }
4827        vq = hwsim_vqs[HWSIM_VQ_RX];
4828        sg_init_one(sg, skb->head, skb_end_offset(skb));
4829        err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_ATOMIC);
4830        if (WARN(err, "virtqueue_add_inbuf returned %d\n", err))
4831                nlmsg_free(skb);
4832        else
4833                virtqueue_kick(vq);
4834        schedule_work(&hwsim_virtio_rx);
4835
4836out_unlock:
4837        spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4838}
4839
4840static void hwsim_virtio_rx_done(struct virtqueue *vq)
4841{
4842        schedule_work(&hwsim_virtio_rx);
4843}
4844
4845static int init_vqs(struct virtio_device *vdev)
4846{
4847        vq_callback_t *callbacks[HWSIM_NUM_VQS] = {
4848                [HWSIM_VQ_TX] = hwsim_virtio_tx_done,
4849                [HWSIM_VQ_RX] = hwsim_virtio_rx_done,
4850        };
4851        const char *names[HWSIM_NUM_VQS] = {
4852                [HWSIM_VQ_TX] = "tx",
4853                [HWSIM_VQ_RX] = "rx",
4854        };
4855
4856        return virtio_find_vqs(vdev, HWSIM_NUM_VQS,
4857                               hwsim_vqs, callbacks, names, NULL);
4858}
4859
4860static int fill_vq(struct virtqueue *vq)
4861{
4862        int i, err;
4863        struct sk_buff *skb;
4864        struct scatterlist sg[1];
4865
4866        for (i = 0; i < virtqueue_get_vring_size(vq); i++) {
4867                skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4868                if (!skb)
4869                        return -ENOMEM;
4870
4871                sg_init_one(sg, skb->head, skb_end_offset(skb));
4872                err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_KERNEL);
4873                if (err) {
4874                        nlmsg_free(skb);
4875                        return err;
4876                }
4877        }
4878        virtqueue_kick(vq);
4879        return 0;
4880}
4881
4882static void remove_vqs(struct virtio_device *vdev)
4883{
4884        int i;
4885
4886        virtio_reset_device(vdev);
4887
4888        for (i = 0; i < ARRAY_SIZE(hwsim_vqs); i++) {
4889                struct virtqueue *vq = hwsim_vqs[i];
4890                struct sk_buff *skb;
4891
4892                while ((skb = virtqueue_detach_unused_buf(vq)))
4893                        nlmsg_free(skb);
4894        }
4895
4896        vdev->config->del_vqs(vdev);
4897}
4898
4899static int hwsim_virtio_probe(struct virtio_device *vdev)
4900{
4901        int err;
4902        unsigned long flags;
4903
4904        spin_lock_irqsave(&hwsim_virtio_lock, flags);
4905        if (hwsim_virtio_enabled) {
4906                spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4907                return -EEXIST;
4908        }
4909        spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4910
4911        err = init_vqs(vdev);
4912        if (err)
4913                return err;
4914
4915        err = fill_vq(hwsim_vqs[HWSIM_VQ_RX]);
4916        if (err)
4917                goto out_remove;
4918
4919        spin_lock_irqsave(&hwsim_virtio_lock, flags);
4920        hwsim_virtio_enabled = true;
4921        spin_unlock_irqrestore(&hwsim_virtio_lock, flags);
4922
4923        schedule_work(&hwsim_virtio_rx);
4924        return 0;
4925
4926out_remove:
4927        remove_vqs(vdev);
4928        return err;
4929}
4930
4931static void hwsim_virtio_remove(struct virtio_device *vdev)
4932{
4933        hwsim_virtio_enabled = false;
4934
4935        cancel_work_sync(&hwsim_virtio_rx);
4936
4937        remove_vqs(vdev);
4938}
4939
4940/* MAC80211_HWSIM virtio device id table */
4941static const struct virtio_device_id id_table[] = {
4942        { VIRTIO_ID_MAC80211_HWSIM, VIRTIO_DEV_ANY_ID },
4943        { 0 }
4944};
4945MODULE_DEVICE_TABLE(virtio, id_table);
4946
4947static struct virtio_driver virtio_hwsim = {
4948        .driver.name = KBUILD_MODNAME,
4949        .driver.owner = THIS_MODULE,
4950        .id_table = id_table,
4951        .probe = hwsim_virtio_probe,
4952        .remove = hwsim_virtio_remove,
4953};
4954
4955static int hwsim_register_virtio_driver(void)
4956{
4957        return register_virtio_driver(&virtio_hwsim);
4958}
4959
4960static void hwsim_unregister_virtio_driver(void)
4961{
4962        unregister_virtio_driver(&virtio_hwsim);
4963}
4964#else
4965static inline int hwsim_register_virtio_driver(void)
4966{
4967        return 0;
4968}
4969
4970static inline void hwsim_unregister_virtio_driver(void)
4971{
4972}
4973#endif
4974
4975static int __init init_mac80211_hwsim(void)
4976{
4977        int i, err;
4978
4979        if (radios < 0 || radios > 100)
4980                return -EINVAL;
4981
4982        if (channels < 1)
4983                return -EINVAL;
4984
4985        err = rhashtable_init(&hwsim_radios_rht, &hwsim_rht_params);
4986        if (err)
4987                return err;
4988
4989        err = register_pernet_device(&hwsim_net_ops);
4990        if (err)
4991                goto out_free_rht;
4992
4993        err = platform_driver_register(&mac80211_hwsim_driver);
4994        if (err)
4995                goto out_unregister_pernet;
4996
4997        err = hwsim_init_netlink();
4998        if (err)
4999                goto out_unregister_driver;
5000
5001        err = hwsim_register_virtio_driver();
5002        if (err)
5003                goto out_exit_netlink;
5004
5005        hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
5006        if (IS_ERR(hwsim_class)) {
5007                err = PTR_ERR(hwsim_class);
5008                goto out_exit_virtio;
5009        }
5010
5011        hwsim_init_s1g_channels(hwsim_channels_s1g);
5012
5013        for (i = 0; i < radios; i++) {
5014                struct hwsim_new_radio_params param = { 0 };
5015
5016                param.channels = channels;
5017
5018                switch (regtest) {
5019                case HWSIM_REGTEST_DIFF_COUNTRY:
5020                        if (i < ARRAY_SIZE(hwsim_alpha2s))
5021                                param.reg_alpha2 = hwsim_alpha2s[i];
5022                        break;
5023                case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
5024                        if (!i)
5025                                param.reg_alpha2 = hwsim_alpha2s[0];
5026                        break;
5027                case HWSIM_REGTEST_STRICT_ALL:
5028                        param.reg_strict = true;
5029                        fallthrough;
5030                case HWSIM_REGTEST_DRIVER_REG_ALL:
5031                        param.reg_alpha2 = hwsim_alpha2s[0];
5032                        break;
5033                case HWSIM_REGTEST_WORLD_ROAM:
5034                        if (i == 0)
5035                                param.regd = &hwsim_world_regdom_custom_01;
5036                        break;
5037                case HWSIM_REGTEST_CUSTOM_WORLD:
5038                        param.regd = &hwsim_world_regdom_custom_01;
5039                        break;
5040                case HWSIM_REGTEST_CUSTOM_WORLD_2:
5041                        if (i == 0)
5042                                param.regd = &hwsim_world_regdom_custom_01;
5043                        else if (i == 1)
5044                                param.regd = &hwsim_world_regdom_custom_02;
5045                        break;
5046                case HWSIM_REGTEST_STRICT_FOLLOW:
5047                        if (i == 0) {
5048                                param.reg_strict = true;
5049                                param.reg_alpha2 = hwsim_alpha2s[0];
5050                        }
5051                        break;
5052                case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
5053                        if (i == 0) {
5054                                param.reg_strict = true;
5055                                param.reg_alpha2 = hwsim_alpha2s[0];
5056                        } else if (i == 1) {
5057                                param.reg_alpha2 = hwsim_alpha2s[1];
5058                        }
5059                        break;
5060                case HWSIM_REGTEST_ALL:
5061                        switch (i) {
5062                        case 0:
5063                                param.regd = &hwsim_world_regdom_custom_01;
5064                                break;
5065                        case 1:
5066                                param.regd = &hwsim_world_regdom_custom_02;
5067                                break;
5068                        case 2:
5069                                param.reg_alpha2 = hwsim_alpha2s[0];
5070                                break;
5071                        case 3:
5072                                param.reg_alpha2 = hwsim_alpha2s[1];
5073                                break;
5074                        case 4:
5075                                param.reg_strict = true;
5076                                param.reg_alpha2 = hwsim_alpha2s[2];
5077                                break;
5078                        }
5079                        break;
5080                default:
5081                        break;
5082                }
5083
5084                param.p2p_device = support_p2p_device;
5085                param.use_chanctx = channels > 1;
5086                param.iftypes = HWSIM_IFTYPE_SUPPORT_MASK;
5087                if (param.p2p_device)
5088                        param.iftypes |= BIT(NL80211_IFTYPE_P2P_DEVICE);
5089
5090                err = mac80211_hwsim_new_radio(NULL, &param);
5091                if (err < 0)
5092                        goto out_free_radios;
5093        }
5094
5095        hwsim_mon = alloc_netdev(0, "hwsim%d", NET_NAME_UNKNOWN,
5096                                 hwsim_mon_setup);
5097        if (hwsim_mon == NULL) {
5098                err = -ENOMEM;
5099                goto out_free_radios;
5100        }
5101
5102        rtnl_lock();
5103        err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
5104        if (err < 0) {
5105                rtnl_unlock();
5106                goto out_free_mon;
5107        }
5108
5109        err = register_netdevice(hwsim_mon);
5110        if (err < 0) {
5111                rtnl_unlock();
5112                goto out_free_mon;
5113        }
5114        rtnl_unlock();
5115
5116        return 0;
5117
5118out_free_mon:
5119        free_netdev(hwsim_mon);
5120out_free_radios:
5121        mac80211_hwsim_free();
5122out_exit_virtio:
5123        hwsim_unregister_virtio_driver();
5124out_exit_netlink:
5125        hwsim_exit_netlink();
5126out_unregister_driver:
5127        platform_driver_unregister(&mac80211_hwsim_driver);
5128out_unregister_pernet:
5129        unregister_pernet_device(&hwsim_net_ops);
5130out_free_rht:
5131        rhashtable_destroy(&hwsim_radios_rht);
5132        return err;
5133}
5134module_init(init_mac80211_hwsim);
5135
5136static void __exit exit_mac80211_hwsim(void)
5137{
5138        pr_debug("mac80211_hwsim: unregister radios\n");
5139
5140        hwsim_unregister_virtio_driver();
5141        hwsim_exit_netlink();
5142
5143        mac80211_hwsim_free();
5144
5145        rhashtable_destroy(&hwsim_radios_rht);
5146        unregister_netdev(hwsim_mon);
5147        platform_driver_unregister(&mac80211_hwsim_driver);
5148        unregister_pernet_device(&hwsim_net_ops);
5149}
5150module_exit(exit_mac80211_hwsim);
5151