^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* drivers/net/wireless/virt_wifi.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * A fake implementation of cfg80211_ops that can be tacked on to an ethernet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * net_device to make it appear as a wireless connection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2018 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Author: schuffelen@google.com
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <net/cfg80211.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <net/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/math64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <net/virt_wifi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static struct wiphy *common_wiphy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct virt_wifi_wiphy_priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct delayed_work scan_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct cfg80211_scan_request *scan_request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) bool being_deleted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct virt_wifi_network_simulation *network_simulation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static struct ieee80211_channel channel_2ghz = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) .band = NL80211_BAND_2GHZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) .center_freq = 2432,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) .hw_value = 2432,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) .max_power = 20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static struct ieee80211_rate bitrates_2ghz[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) { .bitrate = 10 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) { .bitrate = 20 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) { .bitrate = 55 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) { .bitrate = 110 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) { .bitrate = 60 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) { .bitrate = 120 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) { .bitrate = 240 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static struct ieee80211_supported_band band_2ghz = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) .channels = &channel_2ghz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) .bitrates = bitrates_2ghz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) .band = NL80211_BAND_2GHZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .n_channels = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .n_bitrates = ARRAY_SIZE(bitrates_2ghz),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) .ht_cap = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .ht_supported = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) .cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) IEEE80211_HT_CAP_GRN_FLD |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) IEEE80211_HT_CAP_SGI_20 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) IEEE80211_HT_CAP_SGI_40 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) IEEE80211_HT_CAP_DSSSCCK40,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) .ampdu_factor = 0x3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) .ampdu_density = 0x6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) .mcs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) .rx_mask = {0xff, 0xff},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) .tx_params = IEEE80211_HT_MCS_TX_DEFINED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static struct ieee80211_channel channel_5ghz = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .band = NL80211_BAND_5GHZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) .center_freq = 5240,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .hw_value = 5240,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) .max_power = 20,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static struct ieee80211_rate bitrates_5ghz[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) { .bitrate = 60 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) { .bitrate = 120 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) { .bitrate = 240 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #define RX_MCS_MAP (IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) IEEE80211_VHT_MCS_SUPPORT_0_9 << 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #define TX_MCS_MAP (IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) IEEE80211_VHT_MCS_SUPPORT_0_9 << 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static struct ieee80211_supported_band band_5ghz = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .channels = &channel_5ghz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .bitrates = bitrates_5ghz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .band = NL80211_BAND_5GHZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .n_channels = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) .n_bitrates = ARRAY_SIZE(bitrates_5ghz),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) .ht_cap = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) .ht_supported = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) .cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) IEEE80211_HT_CAP_GRN_FLD |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) IEEE80211_HT_CAP_SGI_20 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) IEEE80211_HT_CAP_SGI_40 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) IEEE80211_HT_CAP_DSSSCCK40,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) .ampdu_factor = 0x3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .ampdu_density = 0x6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .mcs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .rx_mask = {0xff, 0xff},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .tx_params = IEEE80211_HT_MCS_TX_DEFINED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) .vht_cap = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) .vht_supported = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) .cap = IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) IEEE80211_VHT_CAP_RXLDPC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) IEEE80211_VHT_CAP_SHORT_GI_80 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) IEEE80211_VHT_CAP_SHORT_GI_160 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) IEEE80211_VHT_CAP_TXSTBC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) IEEE80211_VHT_CAP_RXSTBC_1 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) IEEE80211_VHT_CAP_RXSTBC_2 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) IEEE80211_VHT_CAP_RXSTBC_3 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) IEEE80211_VHT_CAP_RXSTBC_4 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) .vht_mcs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) .rx_mcs_map = cpu_to_le16(RX_MCS_MAP),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .tx_mcs_map = cpu_to_le16(TX_MCS_MAP),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /* Assigned at module init. Guaranteed locally-administered and unicast. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static u8 fake_router_bssid[ETH_ALEN] __ro_after_init = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static void virt_wifi_inform_bss(struct wiphy *wiphy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) u64 tsf = div_u64(ktime_get_boottime_ns(), 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct cfg80211_bss *informed_bss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) u8 tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) u8 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) u8 ssid[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) } __packed ssid = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) .tag = WLAN_EID_SSID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) .len = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) .ssid = "VirtWifi",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) informed_bss = cfg80211_inform_bss(wiphy, &channel_5ghz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) CFG80211_BSS_FTYPE_PRESP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) fake_router_bssid, tsf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) WLAN_CAPABILITY_ESS, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) (void *)&ssid, sizeof(ssid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) DBM_TO_MBM(-50), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) cfg80211_put_bss(wiphy, informed_bss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /* Called with the rtnl lock held. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static int virt_wifi_scan(struct wiphy *wiphy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct cfg80211_scan_request *request)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct virt_wifi_wiphy_priv *priv = wiphy_priv(wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) wiphy_debug(wiphy, "scan\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (priv->scan_request || priv->being_deleted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) priv->scan_request = request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) schedule_delayed_work(&priv->scan_result, HZ * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (priv->network_simulation &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) priv->network_simulation->notify_scan_trigger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) priv->network_simulation->notify_scan_trigger(wiphy, request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /* Acquires and releases the rdev BSS lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static void virt_wifi_scan_result(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct virt_wifi_wiphy_priv *priv =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) container_of(work, struct virt_wifi_wiphy_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) scan_result.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct wiphy *wiphy = priv_to_wiphy(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct cfg80211_scan_info scan_info = { .aborted = false };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) virt_wifi_inform_bss(wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if(priv->network_simulation &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) priv->network_simulation->generate_virt_scan_result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if(priv->network_simulation->generate_virt_scan_result(wiphy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) wiphy_err(wiphy, "Fail to generater the simulated scan result.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /* Schedules work which acquires and releases the rtnl lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) cfg80211_scan_done(priv->scan_request, &scan_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) priv->scan_request = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /* May acquire and release the rdev BSS lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static void virt_wifi_cancel_scan(struct wiphy *wiphy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct virt_wifi_wiphy_priv *priv = wiphy_priv(wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) cancel_delayed_work_sync(&priv->scan_result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /* Clean up dangling callbacks if necessary. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (priv->scan_request) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct cfg80211_scan_info scan_info = { .aborted = true };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /* Schedules work which acquires and releases the rtnl lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) cfg80211_scan_done(priv->scan_request, &scan_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) priv->scan_request = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct virt_wifi_netdev_priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct delayed_work connect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct net_device *lowerdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct net_device *upperdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) u32 tx_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) u32 tx_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) u8 connect_requested_bss[ETH_ALEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) bool is_up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) bool is_connected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) bool being_deleted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /* Called with the rtnl lock held. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static int virt_wifi_connect(struct wiphy *wiphy, struct net_device *netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct cfg80211_connect_params *sme)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) struct virt_wifi_netdev_priv *priv = netdev_priv(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) bool could_schedule;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (priv->being_deleted || !priv->is_up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) could_schedule = schedule_delayed_work(&priv->connect, HZ * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (!could_schedule)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (sme->bssid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) ether_addr_copy(priv->connect_requested_bss, sme->bssid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) virt_wifi_inform_bss(wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) eth_zero_addr(priv->connect_requested_bss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) wiphy_debug(wiphy, "connect\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /* Acquires and releases the rdev event lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static void virt_wifi_connect_complete(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) struct virt_wifi_netdev_priv *priv =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) container_of(work, struct virt_wifi_netdev_priv, connect.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) u8 *requested_bss = priv->connect_requested_bss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) bool right_addr = ether_addr_equal(requested_bss, fake_router_bssid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) u16 status = WLAN_STATUS_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (is_zero_ether_addr(requested_bss))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) requested_bss = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (!priv->is_up || (requested_bss && !right_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) status = WLAN_STATUS_UNSPECIFIED_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) priv->is_connected = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /* Schedules an event that acquires the rtnl lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) cfg80211_connect_result(priv->upperdev, requested_bss, NULL, 0, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) status, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) netif_carrier_on(priv->upperdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) /* May acquire and release the rdev event lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static void virt_wifi_cancel_connect(struct net_device *netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) struct virt_wifi_netdev_priv *priv = netdev_priv(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /* If there is work pending, clean up dangling callbacks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (cancel_delayed_work_sync(&priv->connect)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) /* Schedules an event that acquires the rtnl lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) cfg80211_connect_result(priv->upperdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) priv->connect_requested_bss, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) WLAN_STATUS_UNSPECIFIED_FAILURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) /* Called with the rtnl lock held. Acquires the rdev event lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static int virt_wifi_disconnect(struct wiphy *wiphy, struct net_device *netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) u16 reason_code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct virt_wifi_netdev_priv *priv = netdev_priv(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (priv->being_deleted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) wiphy_debug(wiphy, "disconnect\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) virt_wifi_cancel_connect(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) cfg80211_disconnected(netdev, reason_code, NULL, 0, true, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) priv->is_connected = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) netif_carrier_off(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) /* Called with the rtnl lock held. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static int virt_wifi_get_station(struct wiphy *wiphy, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) const u8 *mac, struct station_info *sinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) wiphy_debug(wiphy, "get_station\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (!priv->is_connected || !ether_addr_equal(mac, fake_router_bssid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) sinfo->filled = BIT_ULL(NL80211_STA_INFO_TX_PACKETS) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) BIT_ULL(NL80211_STA_INFO_TX_FAILED) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) BIT_ULL(NL80211_STA_INFO_SIGNAL) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) sinfo->tx_packets = priv->tx_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) sinfo->tx_failed = priv->tx_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /* For CFG80211_SIGNAL_TYPE_MBM, value is expressed in _dBm_ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) sinfo->signal = -50;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) sinfo->txrate = (struct rate_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) .legacy = 10, /* units are 100kbit/s */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) /* Called with the rtnl lock held. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static int virt_wifi_dump_station(struct wiphy *wiphy, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) int idx, u8 *mac, struct station_info *sinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) wiphy_debug(wiphy, "dump_station\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (idx != 0 || !priv->is_connected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) ether_addr_copy(mac, fake_router_bssid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return virt_wifi_get_station(wiphy, dev, fake_router_bssid, sinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) static const struct cfg80211_ops virt_wifi_cfg80211_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) .scan = virt_wifi_scan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) .connect = virt_wifi_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) .disconnect = virt_wifi_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) .get_station = virt_wifi_get_station,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) .dump_station = virt_wifi_dump_station,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) /* Acquires and releases the rtnl lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) static struct wiphy *virt_wifi_make_wiphy(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) struct wiphy *wiphy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) struct virt_wifi_wiphy_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) wiphy = wiphy_new(&virt_wifi_cfg80211_ops, sizeof(*priv));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (!wiphy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) wiphy->max_scan_ssids = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) wiphy->max_scan_ie_len = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) wiphy->bands[NL80211_BAND_2GHZ] = &band_2ghz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) wiphy->bands[NL80211_BAND_5GHZ] = &band_5ghz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) wiphy->bands[NL80211_BAND_60GHZ] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) priv = wiphy_priv(wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) priv->being_deleted = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) priv->scan_request = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) priv->network_simulation = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) INIT_DELAYED_WORK(&priv->scan_result, virt_wifi_scan_result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) err = wiphy_register(wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) wiphy_free(wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return wiphy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) /* Acquires and releases the rtnl lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) static void virt_wifi_destroy_wiphy(struct wiphy *wiphy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) struct virt_wifi_wiphy_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) WARN(!wiphy, "%s called with null wiphy", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (!wiphy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) priv = wiphy_priv(wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) priv->being_deleted = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) virt_wifi_cancel_scan(wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (wiphy->registered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) wiphy_unregister(wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) wiphy_free(wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) /* Enters and exits a RCU-bh critical section. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) static netdev_tx_t virt_wifi_start_xmit(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) priv->tx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (!priv->is_connected) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) priv->tx_failed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) return NET_XMIT_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) skb->dev = priv->lowerdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) return dev_queue_xmit(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) /* Called with rtnl lock held. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) static int virt_wifi_net_device_open(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) struct virt_wifi_wiphy_priv *w_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) priv->is_up = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) w_priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if(w_priv->network_simulation &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) w_priv->network_simulation->notify_device_open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) w_priv->network_simulation->notify_device_open(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) /* Called with rtnl lock held. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) static int virt_wifi_net_device_stop(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) struct virt_wifi_netdev_priv *n_priv = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) struct virt_wifi_wiphy_priv *w_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) n_priv->is_up = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (!dev->ieee80211_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) w_priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) virt_wifi_cancel_scan(dev->ieee80211_ptr->wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) virt_wifi_cancel_connect(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) netif_carrier_off(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (w_priv->network_simulation &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) w_priv->network_simulation->notify_device_stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) w_priv->network_simulation->notify_device_stop(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) static int virt_wifi_net_device_get_iflink(const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) return priv->lowerdev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) static const struct net_device_ops virt_wifi_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) .ndo_start_xmit = virt_wifi_start_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) .ndo_open = virt_wifi_net_device_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) .ndo_stop = virt_wifi_net_device_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) .ndo_get_iflink = virt_wifi_net_device_get_iflink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) /* Invoked as part of rtnl lock release. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) static void virt_wifi_net_device_destructor(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) /* Delayed past dellink to allow nl80211 to react to the device being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) * deleted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) kfree(dev->ieee80211_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) dev->ieee80211_ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) /* No lock interaction. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) static void virt_wifi_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) ether_setup(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) dev->netdev_ops = &virt_wifi_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) dev->needs_free_netdev = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) /* Called in a RCU read critical section from netif_receive_skb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) static rx_handler_result_t virt_wifi_rx_handler(struct sk_buff **pskb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) struct sk_buff *skb = *pskb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) struct virt_wifi_netdev_priv *priv =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) rcu_dereference(skb->dev->rx_handler_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (!priv->is_connected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) return RX_HANDLER_PASS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) /* GFP_ATOMIC because this is a packet interrupt handler. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) skb = skb_share_check(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) dev_err(&priv->upperdev->dev, "can't skb_share_check\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) return RX_HANDLER_CONSUMED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) *pskb = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) skb->dev = priv->upperdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) skb->pkt_type = PACKET_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) return RX_HANDLER_ANOTHER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) /* Called with rtnl lock held. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static int virt_wifi_newlink(struct net *src_net, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) if (!tb[IFLA_LINK])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) netif_carrier_off(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) priv->upperdev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) priv->lowerdev = __dev_get_by_index(src_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) nla_get_u32(tb[IFLA_LINK]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (!priv->lowerdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) if (!tb[IFLA_MTU])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) dev->mtu = priv->lowerdev->mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) else if (dev->mtu > priv->lowerdev->mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) err = netdev_rx_handler_register(priv->lowerdev, virt_wifi_rx_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) dev_err(&priv->lowerdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) "can't netdev_rx_handler_register: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) eth_hw_addr_inherit(dev, priv->lowerdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) netif_stacked_transfer_operstate(priv->lowerdev, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) SET_NETDEV_DEV(dev, &priv->lowerdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) dev->ieee80211_ptr = kzalloc(sizeof(*dev->ieee80211_ptr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) if (!dev->ieee80211_ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) goto remove_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) dev->ieee80211_ptr->iftype = NL80211_IFTYPE_STATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) dev->ieee80211_ptr->wiphy = common_wiphy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) err = register_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) dev_err(&priv->lowerdev->dev, "can't register_netdevice: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) goto free_wireless_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) err = netdev_upper_dev_link(priv->lowerdev, dev, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) dev_err(&priv->lowerdev->dev, "can't netdev_upper_dev_link: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) goto unregister_netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) dev->priv_destructor = virt_wifi_net_device_destructor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) priv->being_deleted = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) priv->is_connected = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) priv->is_up = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) INIT_DELAYED_WORK(&priv->connect, virt_wifi_connect_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) __module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) unregister_netdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) unregister_netdevice(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) free_wireless_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) kfree(dev->ieee80211_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) dev->ieee80211_ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) remove_handler:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) netdev_rx_handler_unregister(priv->lowerdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) /* Called with rtnl lock held. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) static void virt_wifi_dellink(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if (dev->ieee80211_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) virt_wifi_cancel_scan(dev->ieee80211_ptr->wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) priv->being_deleted = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) virt_wifi_cancel_connect(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) netif_carrier_off(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) netdev_rx_handler_unregister(priv->lowerdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) netdev_upper_dev_unlink(priv->lowerdev, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) unregister_netdevice_queue(dev, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) /* Deleting the wiphy is handled in the module destructor. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) static struct rtnl_link_ops virt_wifi_link_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) .kind = "virt_wifi",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) .setup = virt_wifi_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) .newlink = virt_wifi_newlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) .dellink = virt_wifi_dellink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) .priv_size = sizeof(struct virt_wifi_netdev_priv),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) static bool netif_is_virt_wifi_dev(const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) return rcu_access_pointer(dev->rx_handler) == virt_wifi_rx_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) static int virt_wifi_event(struct notifier_block *this, unsigned long event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) struct net_device *lower_dev = netdev_notifier_info_to_dev(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) struct virt_wifi_netdev_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) struct net_device *upper_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) LIST_HEAD(list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) if (!netif_is_virt_wifi_dev(lower_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) switch (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) case NETDEV_UNREGISTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) priv = rtnl_dereference(lower_dev->rx_handler_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) upper_dev = priv->upperdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) upper_dev->rtnl_link_ops->dellink(upper_dev, &list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) unregister_netdevice_many(&list_kill);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) static struct notifier_block virt_wifi_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) .notifier_call = virt_wifi_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) /* Acquires and releases the rtnl lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) static int __init virt_wifi_init_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) /* Guaranteed to be locallly-administered and not multicast. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) eth_random_addr(fake_router_bssid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) err = register_netdevice_notifier(&virt_wifi_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) common_wiphy = virt_wifi_make_wiphy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) if (!common_wiphy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) goto notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) err = rtnl_link_register(&virt_wifi_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) goto destroy_wiphy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) destroy_wiphy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) virt_wifi_destroy_wiphy(common_wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) notifier:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) unregister_netdevice_notifier(&virt_wifi_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) /* Acquires and releases the rtnl lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) static void __exit virt_wifi_cleanup_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) /* Will delete any devices that depend on the wiphy. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) rtnl_link_unregister(&virt_wifi_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) virt_wifi_destroy_wiphy(common_wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) unregister_netdevice_notifier(&virt_wifi_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) int virt_wifi_register_network_simulation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) (struct virt_wifi_network_simulation *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) struct virt_wifi_wiphy_priv *priv = wiphy_priv(common_wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) if (priv->network_simulation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) priv->network_simulation = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) EXPORT_SYMBOL_GPL(virt_wifi_register_network_simulation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) int virt_wifi_unregister_network_simulation(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) struct virt_wifi_wiphy_priv *priv = wiphy_priv(common_wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if(!priv->network_simulation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) priv->network_simulation = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) EXPORT_SYMBOL_GPL(virt_wifi_unregister_network_simulation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) module_init(virt_wifi_init_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) module_exit(virt_wifi_cleanup_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) MODULE_AUTHOR("Cody Schuffelen <schuffelen@google.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) MODULE_DESCRIPTION("Driver for a wireless wrapper of ethernet devices");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) MODULE_ALIAS_RTNL_LINK("virt_wifi");