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