Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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