1 // SPDX-License-Identifier: GPL-2.0
2 /* drivers/net/wireless/virt_wifi.c
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.
7 * Copyright (C) 2018 Google, Inc.
9 * Author: schuffelen@google.com
12 #include <net/cfg80211.h>
13 #include <net/rtnetlink.h>
14 #include <linux/etherdevice.h>
15 #include <linux/module.h>
17 #include <net/cfg80211.h>
18 #include <net/rtnetlink.h>
19 #include <linux/etherdevice.h>
20 #include <linux/module.h>
22 static struct wiphy *common_wiphy;
24 struct virt_wifi_wiphy_priv {
25 struct delayed_work scan_result;
26 struct cfg80211_scan_request *scan_request;
30 static struct ieee80211_channel channel_2ghz = {
31 .band = NL80211_BAND_2GHZ,
37 static struct ieee80211_rate bitrates_2ghz[] = {
47 static struct ieee80211_supported_band band_2ghz = {
48 .channels = &channel_2ghz,
49 .bitrates = bitrates_2ghz,
50 .band = NL80211_BAND_2GHZ,
52 .n_bitrates = ARRAY_SIZE(bitrates_2ghz),
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,
63 .rx_mask = {0xff, 0xff},
64 .tx_params = IEEE80211_HT_MCS_TX_DEFINED,
69 static struct ieee80211_channel channel_5ghz = {
70 .band = NL80211_BAND_5GHZ,
76 static struct ieee80211_rate bitrates_5ghz[] = {
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)
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)
100 static struct ieee80211_supported_band band_5ghz = {
101 .channels = &channel_5ghz,
102 .bitrates = bitrates_5ghz,
103 .band = NL80211_BAND_5GHZ,
105 .n_bitrates = ARRAY_SIZE(bitrates_5ghz),
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,
114 .ampdu_density = 0x6,
116 .rx_mask = {0xff, 0xff},
117 .tx_params = IEEE80211_HT_MCS_TX_DEFINED,
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,
134 .rx_mcs_map = cpu_to_le16(RX_MCS_MAP),
135 .tx_mcs_map = cpu_to_le16(TX_MCS_MAP),
140 /* Assigned at module init. Guaranteed locally-administered and unicast. */
141 static u8 fake_router_bssid[ETH_ALEN] __ro_after_init = {};
143 /* Called with the rtnl lock held. */
144 static int virt_wifi_scan(struct wiphy *wiphy,
145 struct cfg80211_scan_request *request)
147 struct virt_wifi_wiphy_priv *priv = wiphy_priv(wiphy);
149 wiphy_debug(wiphy, "scan\n");
151 if (priv->scan_request || priv->being_deleted)
154 priv->scan_request = request;
155 schedule_delayed_work(&priv->scan_result, HZ * 2);
160 /* Acquires and releases the rdev BSS lock. */
161 static void virt_wifi_scan_result(struct work_struct *work)
168 .tag = WLAN_EID_SSID, .len = 8, .ssid = "VirtWifi",
170 struct cfg80211_bss *informed_bss;
171 struct virt_wifi_wiphy_priv *priv =
172 container_of(work, struct virt_wifi_wiphy_priv,
174 struct wiphy *wiphy = priv_to_wiphy(priv);
175 struct cfg80211_scan_info scan_info = { .aborted = false };
177 informed_bss = cfg80211_inform_bss(wiphy, &channel_5ghz,
178 CFG80211_BSS_FTYPE_PRESP,
181 WLAN_CAPABILITY_ESS, 0,
182 (void *)&ssid, sizeof(ssid),
183 DBM_TO_MBM(-50), GFP_KERNEL);
184 cfg80211_put_bss(wiphy, informed_bss);
186 /* Schedules work which acquires and releases the rtnl lock. */
187 cfg80211_scan_done(priv->scan_request, &scan_info);
188 priv->scan_request = NULL;
191 /* May acquire and release the rdev BSS lock. */
192 static void virt_wifi_cancel_scan(struct wiphy *wiphy)
194 struct virt_wifi_wiphy_priv *priv = wiphy_priv(wiphy);
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;
206 struct virt_wifi_netdev_priv {
207 struct delayed_work connect;
208 struct net_device *lowerdev;
209 struct net_device *upperdev;
212 u8 connect_requested_bss[ETH_ALEN];
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)
222 struct virt_wifi_netdev_priv *priv = netdev_priv(netdev);
225 if (priv->being_deleted || !priv->is_up)
228 could_schedule = schedule_delayed_work(&priv->connect, HZ * 2);
233 ether_addr_copy(priv->connect_requested_bss, sme->bssid);
235 eth_zero_addr(priv->connect_requested_bss);
237 wiphy_debug(wiphy, "connect\n");
242 /* Acquires and releases the rdev event lock. */
243 static void virt_wifi_connect_complete(struct work_struct *work)
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;
252 if (!priv->is_up || (has_addr && !right_addr))
253 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
255 priv->is_connected = true;
257 /* Schedules an event that acquires the rtnl lock. */
258 cfg80211_connect_result(priv->upperdev, requested_bss, NULL, 0, NULL, 0,
260 netif_carrier_on(priv->upperdev);
263 /* May acquire and release the rdev event lock. */
264 static void virt_wifi_cancel_connect(struct net_device *netdev)
266 struct virt_wifi_netdev_priv *priv = netdev_priv(netdev);
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,
274 WLAN_STATUS_UNSPECIFIED_FAILURE,
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,
283 struct virt_wifi_netdev_priv *priv = netdev_priv(netdev);
285 if (priv->being_deleted)
288 wiphy_debug(wiphy, "disconnect\n");
289 virt_wifi_cancel_connect(netdev);
291 cfg80211_disconnected(netdev, reason_code, NULL, 0, true, GFP_KERNEL);
292 priv->is_connected = false;
293 netif_carrier_off(netdev);
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)
302 struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
304 wiphy_debug(wiphy, "get_station\n");
306 if (!priv->is_connected || !ether_addr_equal(mac, fake_router_bssid))
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_ */
317 sinfo->txrate = (struct rate_info) {
318 .legacy = 10, /* units are 100kbit/s */
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)
327 struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
329 wiphy_debug(wiphy, "dump_station\n");
331 if (idx != 0 || !priv->is_connected)
334 ether_addr_copy(mac, fake_router_bssid);
335 return virt_wifi_get_station(wiphy, dev, fake_router_bssid, sinfo);
338 static const struct cfg80211_ops virt_wifi_cfg80211_ops = {
339 .scan = virt_wifi_scan,
341 .connect = virt_wifi_connect,
342 .disconnect = virt_wifi_disconnect,
344 .get_station = virt_wifi_get_station,
345 .dump_station = virt_wifi_dump_station,
348 /* Acquires and releases the rtnl lock. */
349 static struct wiphy *virt_wifi_make_wiphy(void)
352 struct virt_wifi_wiphy_priv *priv;
355 wiphy = wiphy_new(&virt_wifi_cfg80211_ops, sizeof(*priv));
360 wiphy->max_scan_ssids = 4;
361 wiphy->max_scan_ie_len = 1000;
362 wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
364 wiphy->bands[NL80211_BAND_2GHZ] = &band_2ghz;
365 wiphy->bands[NL80211_BAND_5GHZ] = &band_5ghz;
366 wiphy->bands[NL80211_BAND_60GHZ] = NULL;
368 wiphy->regulatory_flags = REGULATORY_WIPHY_SELF_MANAGED;
369 wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
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);
376 err = wiphy_register(wiphy);
385 /* Acquires and releases the rtnl lock. */
386 static void virt_wifi_destroy_wiphy(struct wiphy *wiphy)
388 struct virt_wifi_wiphy_priv *priv;
390 WARN(!wiphy, "%s called with null wiphy", __func__);
394 priv = wiphy_priv(wiphy);
395 priv->being_deleted = true;
396 virt_wifi_cancel_scan(wiphy);
398 if (wiphy->registered)
399 wiphy_unregister(wiphy);
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)
407 struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
410 if (!priv->is_connected) {
412 return NET_XMIT_DROP;
415 skb->dev = priv->lowerdev;
416 return dev_queue_xmit(skb);
419 /* Called with rtnl lock held. */
420 static int virt_wifi_net_device_open(struct net_device *dev)
422 struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
428 /* Called with rtnl lock held. */
429 static int virt_wifi_net_device_stop(struct net_device *dev)
431 struct virt_wifi_netdev_priv *n_priv = netdev_priv(dev);
432 struct virt_wifi_wiphy_priv *w_priv;
434 n_priv->is_up = false;
436 if (!dev->ieee80211_ptr)
438 w_priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
440 virt_wifi_cancel_scan(dev->ieee80211_ptr->wiphy);
441 virt_wifi_cancel_connect(dev);
442 netif_carrier_off(dev);
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,
453 /* Invoked as part of rtnl lock release. */
454 static void virt_wifi_net_device_destructor(struct net_device *dev)
456 /* Delayed past dellink to allow nl80211 to react to the device being
459 kfree(dev->ieee80211_ptr);
460 dev->ieee80211_ptr = NULL;
464 /* No lock interaction. */
465 static void virt_wifi_setup(struct net_device *dev)
468 dev->netdev_ops = &virt_wifi_ops;
469 dev->priv_destructor = virt_wifi_net_device_destructor;
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)
475 struct sk_buff *skb = *pskb;
476 struct virt_wifi_netdev_priv *priv =
477 rcu_dereference(skb->dev->rx_handler_data);
479 if (!priv->is_connected)
480 return RX_HANDLER_PASS;
482 /* GFP_ATOMIC because this is a packet interrupt handler. */
483 skb = skb_share_check(skb, GFP_ATOMIC);
485 dev_err(&priv->upperdev->dev, "can't skb_share_check\n");
486 return RX_HANDLER_CONSUMED;
490 skb->dev = priv->upperdev;
491 skb->pkt_type = PACKET_HOST;
492 return RX_HANDLER_ANOTHER;
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 struct netlink_ext_ack *extack)
500 struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
506 netif_carrier_off(dev);
508 priv->upperdev = dev;
509 priv->lowerdev = __dev_get_by_index(src_net,
510 nla_get_u32(tb[IFLA_LINK]));
515 dev->mtu = priv->lowerdev->mtu;
516 else if (dev->mtu > priv->lowerdev->mtu)
519 err = netdev_rx_handler_register(priv->lowerdev, virt_wifi_rx_handler,
522 dev_err(&priv->lowerdev->dev,
523 "can't netdev_rx_handler_register: %d\n", err);
527 eth_hw_addr_inherit(dev, priv->lowerdev);
528 netif_stacked_transfer_operstate(priv->lowerdev, dev);
530 SET_NETDEV_DEV(dev, &priv->lowerdev->dev);
531 dev->ieee80211_ptr = kzalloc(sizeof(*dev->ieee80211_ptr), GFP_KERNEL);
533 if (!dev->ieee80211_ptr) {
538 dev->ieee80211_ptr->iftype = NL80211_IFTYPE_STATION;
539 dev->ieee80211_ptr->wiphy = common_wiphy;
541 err = register_netdevice(dev);
543 dev_err(&priv->lowerdev->dev, "can't register_netdevice: %d\n",
545 goto free_wireless_dev;
548 err = netdev_upper_dev_link(priv->lowerdev, dev, extack);
550 dev_err(&priv->lowerdev->dev, "can't netdev_upper_dev_link: %d\n",
552 goto unregister_netdev;
555 priv->being_deleted = false;
556 priv->is_connected = false;
558 INIT_DELAYED_WORK(&priv->connect, virt_wifi_connect_complete);
562 unregister_netdevice(dev);
564 kfree(dev->ieee80211_ptr);
565 dev->ieee80211_ptr = NULL;
567 netdev_rx_handler_unregister(priv->lowerdev);
572 /* Called with rtnl lock held. */
573 static void virt_wifi_dellink(struct net_device *dev,
574 struct list_head *head)
576 struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
578 if (dev->ieee80211_ptr)
579 virt_wifi_cancel_scan(dev->ieee80211_ptr->wiphy);
581 priv->being_deleted = true;
582 virt_wifi_cancel_connect(dev);
583 netif_carrier_off(dev);
585 netdev_rx_handler_unregister(priv->lowerdev);
586 netdev_upper_dev_unlink(priv->lowerdev, dev);
588 unregister_netdevice_queue(dev, head);
590 /* Deleting the wiphy is handled in the module destructor. */
593 static struct rtnl_link_ops virt_wifi_link_ops = {
595 .setup = virt_wifi_setup,
596 .newlink = virt_wifi_newlink,
597 .dellink = virt_wifi_dellink,
598 .priv_size = sizeof(struct virt_wifi_netdev_priv),
601 /* Acquires and releases the rtnl lock. */
602 static int __init virt_wifi_init_module(void)
606 /* Guaranteed to be locallly-administered and not multicast. */
607 eth_random_addr(fake_router_bssid);
609 common_wiphy = virt_wifi_make_wiphy();
613 err = rtnl_link_register(&virt_wifi_link_ops);
615 virt_wifi_destroy_wiphy(common_wiphy);
620 /* Acquires and releases the rtnl lock. */
621 static void __exit virt_wifi_cleanup_module(void)
623 /* Will delete any devices that depend on the wiphy. */
624 rtnl_link_unregister(&virt_wifi_link_ops);
625 virt_wifi_destroy_wiphy(common_wiphy);
628 module_init(virt_wifi_init_module);
629 module_exit(virt_wifi_cleanup_module);
631 MODULE_LICENSE("GPL v2");
632 MODULE_AUTHOR("Cody Schuffelen <schuffelen@google.com>");
633 MODULE_DESCRIPTION("Driver for a wireless wrapper of ethernet devices");
634 MODULE_ALIAS_RTNL_LINK("virt_wifi");