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.
12 #include <net/cfg80211.h>
13 #include <net/rtnetlink.h>
14 #include <linux/etherdevice.h>
15 #include <linux/math64.h>
16 #include <linux/module.h>
18 static struct wiphy *common_wiphy;
20 struct virt_wifi_wiphy_priv {
21 struct delayed_work scan_result;
22 struct cfg80211_scan_request *scan_request;
26 static struct ieee80211_channel channel_2ghz = {
27 .band = NL80211_BAND_2GHZ,
33 static struct ieee80211_rate bitrates_2ghz[] = {
43 static struct ieee80211_supported_band band_2ghz = {
44 .channels = &channel_2ghz,
45 .bitrates = bitrates_2ghz,
46 .band = NL80211_BAND_2GHZ,
48 .n_bitrates = ARRAY_SIZE(bitrates_2ghz),
51 .cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
52 IEEE80211_HT_CAP_GRN_FLD |
53 IEEE80211_HT_CAP_SGI_20 |
54 IEEE80211_HT_CAP_SGI_40 |
55 IEEE80211_HT_CAP_DSSSCCK40,
59 .rx_mask = {0xff, 0xff},
60 .tx_params = IEEE80211_HT_MCS_TX_DEFINED,
65 static struct ieee80211_channel channel_5ghz = {
66 .band = NL80211_BAND_5GHZ,
72 static struct ieee80211_rate bitrates_5ghz[] = {
78 #define RX_MCS_MAP (IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 | \
79 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 | \
80 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 | \
81 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 | \
82 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 | \
83 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 | \
84 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 | \
85 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14)
87 #define TX_MCS_MAP (IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 | \
88 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 | \
89 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 | \
90 IEEE80211_VHT_MCS_SUPPORT_0_9 << 6 | \
91 IEEE80211_VHT_MCS_SUPPORT_0_9 << 8 | \
92 IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 | \
93 IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 | \
94 IEEE80211_VHT_MCS_SUPPORT_0_9 << 14)
96 static struct ieee80211_supported_band band_5ghz = {
97 .channels = &channel_5ghz,
98 .bitrates = bitrates_5ghz,
99 .band = NL80211_BAND_5GHZ,
101 .n_bitrates = ARRAY_SIZE(bitrates_5ghz),
103 .ht_supported = true,
104 .cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
105 IEEE80211_HT_CAP_GRN_FLD |
106 IEEE80211_HT_CAP_SGI_20 |
107 IEEE80211_HT_CAP_SGI_40 |
108 IEEE80211_HT_CAP_DSSSCCK40,
110 .ampdu_density = 0x6,
112 .rx_mask = {0xff, 0xff},
113 .tx_params = IEEE80211_HT_MCS_TX_DEFINED,
117 .vht_supported = true,
118 .cap = IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
119 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
120 IEEE80211_VHT_CAP_RXLDPC |
121 IEEE80211_VHT_CAP_SHORT_GI_80 |
122 IEEE80211_VHT_CAP_SHORT_GI_160 |
123 IEEE80211_VHT_CAP_TXSTBC |
124 IEEE80211_VHT_CAP_RXSTBC_1 |
125 IEEE80211_VHT_CAP_RXSTBC_2 |
126 IEEE80211_VHT_CAP_RXSTBC_3 |
127 IEEE80211_VHT_CAP_RXSTBC_4 |
128 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
130 .rx_mcs_map = cpu_to_le16(RX_MCS_MAP),
131 .tx_mcs_map = cpu_to_le16(TX_MCS_MAP),
136 /* Assigned at module init. Guaranteed locally-administered and unicast. */
137 static u8 fake_router_bssid[ETH_ALEN] __ro_after_init = {};
139 /* Called with the rtnl lock held. */
140 static int virt_wifi_scan(struct wiphy *wiphy,
141 struct cfg80211_scan_request *request)
143 struct virt_wifi_wiphy_priv *priv = wiphy_priv(wiphy);
145 wiphy_debug(wiphy, "scan\n");
147 if (priv->scan_request || priv->being_deleted)
150 priv->scan_request = request;
151 schedule_delayed_work(&priv->scan_result, HZ * 2);
156 /* Acquires and releases the rdev BSS lock. */
157 static void virt_wifi_scan_result(struct work_struct *work)
164 .tag = WLAN_EID_SSID, .len = 8, .ssid = "VirtWifi",
166 struct cfg80211_bss *informed_bss;
167 struct virt_wifi_wiphy_priv *priv =
168 container_of(work, struct virt_wifi_wiphy_priv,
170 struct wiphy *wiphy = priv_to_wiphy(priv);
171 struct cfg80211_scan_info scan_info = { .aborted = false };
172 u64 tsf = div_u64(ktime_get_boottime_ns(), 1000);
174 informed_bss = cfg80211_inform_bss(wiphy, &channel_5ghz,
175 CFG80211_BSS_FTYPE_PRESP,
176 fake_router_bssid, tsf,
177 WLAN_CAPABILITY_ESS, 0,
178 (void *)&ssid, sizeof(ssid),
179 DBM_TO_MBM(-50), GFP_KERNEL);
180 cfg80211_put_bss(wiphy, informed_bss);
182 /* Schedules work which acquires and releases the rtnl lock. */
183 cfg80211_scan_done(priv->scan_request, &scan_info);
184 priv->scan_request = NULL;
187 /* May acquire and release the rdev BSS lock. */
188 static void virt_wifi_cancel_scan(struct wiphy *wiphy)
190 struct virt_wifi_wiphy_priv *priv = wiphy_priv(wiphy);
192 cancel_delayed_work_sync(&priv->scan_result);
193 /* Clean up dangling callbacks if necessary. */
194 if (priv->scan_request) {
195 struct cfg80211_scan_info scan_info = { .aborted = true };
196 /* Schedules work which acquires and releases the rtnl lock. */
197 cfg80211_scan_done(priv->scan_request, &scan_info);
198 priv->scan_request = NULL;
202 struct virt_wifi_netdev_priv {
203 struct delayed_work connect;
204 struct net_device *lowerdev;
205 struct net_device *upperdev;
208 u8 connect_requested_bss[ETH_ALEN];
214 /* Called with the rtnl lock held. */
215 static int virt_wifi_connect(struct wiphy *wiphy, struct net_device *netdev,
216 struct cfg80211_connect_params *sme)
218 struct virt_wifi_netdev_priv *priv = netdev_priv(netdev);
221 if (priv->being_deleted || !priv->is_up)
224 could_schedule = schedule_delayed_work(&priv->connect, HZ * 2);
229 ether_addr_copy(priv->connect_requested_bss, sme->bssid);
231 eth_zero_addr(priv->connect_requested_bss);
233 wiphy_debug(wiphy, "connect\n");
238 /* Acquires and releases the rdev event lock. */
239 static void virt_wifi_connect_complete(struct work_struct *work)
241 struct virt_wifi_netdev_priv *priv =
242 container_of(work, struct virt_wifi_netdev_priv, connect.work);
243 u8 *requested_bss = priv->connect_requested_bss;
244 bool has_addr = !is_zero_ether_addr(requested_bss);
245 bool right_addr = ether_addr_equal(requested_bss, fake_router_bssid);
246 u16 status = WLAN_STATUS_SUCCESS;
248 if (!priv->is_up || (has_addr && !right_addr))
249 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
251 priv->is_connected = true;
253 /* Schedules an event that acquires the rtnl lock. */
254 cfg80211_connect_result(priv->upperdev, requested_bss, NULL, 0, NULL, 0,
256 netif_carrier_on(priv->upperdev);
259 /* May acquire and release the rdev event lock. */
260 static void virt_wifi_cancel_connect(struct net_device *netdev)
262 struct virt_wifi_netdev_priv *priv = netdev_priv(netdev);
264 /* If there is work pending, clean up dangling callbacks. */
265 if (cancel_delayed_work_sync(&priv->connect)) {
266 /* Schedules an event that acquires the rtnl lock. */
267 cfg80211_connect_result(priv->upperdev,
268 priv->connect_requested_bss, NULL, 0,
270 WLAN_STATUS_UNSPECIFIED_FAILURE,
275 /* Called with the rtnl lock held. Acquires the rdev event lock. */
276 static int virt_wifi_disconnect(struct wiphy *wiphy, struct net_device *netdev,
279 struct virt_wifi_netdev_priv *priv = netdev_priv(netdev);
281 if (priv->being_deleted)
284 wiphy_debug(wiphy, "disconnect\n");
285 virt_wifi_cancel_connect(netdev);
287 cfg80211_disconnected(netdev, reason_code, NULL, 0, true, GFP_KERNEL);
288 priv->is_connected = false;
289 netif_carrier_off(netdev);
294 /* Called with the rtnl lock held. */
295 static int virt_wifi_get_station(struct wiphy *wiphy, struct net_device *dev,
296 const u8 *mac, struct station_info *sinfo)
298 struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
300 wiphy_debug(wiphy, "get_station\n");
302 if (!priv->is_connected || !ether_addr_equal(mac, fake_router_bssid))
305 sinfo->filled = BIT_ULL(NL80211_STA_INFO_TX_PACKETS) |
306 BIT_ULL(NL80211_STA_INFO_TX_FAILED) |
307 BIT_ULL(NL80211_STA_INFO_SIGNAL) |
308 BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
309 sinfo->tx_packets = priv->tx_packets;
310 sinfo->tx_failed = priv->tx_failed;
311 /* For CFG80211_SIGNAL_TYPE_MBM, value is expressed in _dBm_ */
313 sinfo->txrate = (struct rate_info) {
314 .legacy = 10, /* units are 100kbit/s */
319 /* Called with the rtnl lock held. */
320 static int virt_wifi_dump_station(struct wiphy *wiphy, struct net_device *dev,
321 int idx, u8 *mac, struct station_info *sinfo)
323 struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
325 wiphy_debug(wiphy, "dump_station\n");
327 if (idx != 0 || !priv->is_connected)
330 ether_addr_copy(mac, fake_router_bssid);
331 return virt_wifi_get_station(wiphy, dev, fake_router_bssid, sinfo);
334 static const struct cfg80211_ops virt_wifi_cfg80211_ops = {
335 .scan = virt_wifi_scan,
337 .connect = virt_wifi_connect,
338 .disconnect = virt_wifi_disconnect,
340 .get_station = virt_wifi_get_station,
341 .dump_station = virt_wifi_dump_station,
344 /* Acquires and releases the rtnl lock. */
345 static struct wiphy *virt_wifi_make_wiphy(void)
348 struct virt_wifi_wiphy_priv *priv;
351 wiphy = wiphy_new(&virt_wifi_cfg80211_ops, sizeof(*priv));
356 wiphy->max_scan_ssids = 4;
357 wiphy->max_scan_ie_len = 1000;
358 wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
360 wiphy->bands[NL80211_BAND_2GHZ] = &band_2ghz;
361 wiphy->bands[NL80211_BAND_5GHZ] = &band_5ghz;
362 wiphy->bands[NL80211_BAND_60GHZ] = NULL;
364 wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
366 priv = wiphy_priv(wiphy);
367 priv->being_deleted = false;
368 priv->scan_request = NULL;
369 INIT_DELAYED_WORK(&priv->scan_result, virt_wifi_scan_result);
371 err = wiphy_register(wiphy);
380 /* Acquires and releases the rtnl lock. */
381 static void virt_wifi_destroy_wiphy(struct wiphy *wiphy)
383 struct virt_wifi_wiphy_priv *priv;
385 WARN(!wiphy, "%s called with null wiphy", __func__);
389 priv = wiphy_priv(wiphy);
390 priv->being_deleted = true;
391 virt_wifi_cancel_scan(wiphy);
393 if (wiphy->registered)
394 wiphy_unregister(wiphy);
398 /* Enters and exits a RCU-bh critical section. */
399 static netdev_tx_t virt_wifi_start_xmit(struct sk_buff *skb,
400 struct net_device *dev)
402 struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
405 if (!priv->is_connected) {
407 return NET_XMIT_DROP;
410 skb->dev = priv->lowerdev;
411 return dev_queue_xmit(skb);
414 /* Called with rtnl lock held. */
415 static int virt_wifi_net_device_open(struct net_device *dev)
417 struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
423 /* Called with rtnl lock held. */
424 static int virt_wifi_net_device_stop(struct net_device *dev)
426 struct virt_wifi_netdev_priv *n_priv = netdev_priv(dev);
428 n_priv->is_up = false;
430 if (!dev->ieee80211_ptr)
433 virt_wifi_cancel_scan(dev->ieee80211_ptr->wiphy);
434 virt_wifi_cancel_connect(dev);
435 netif_carrier_off(dev);
440 static int virt_wifi_net_device_get_iflink(const struct net_device *dev)
442 struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
444 return priv->lowerdev->ifindex;
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 .ndo_get_iflink = virt_wifi_net_device_get_iflink,
454 /* Invoked as part of rtnl lock release. */
455 static void virt_wifi_net_device_destructor(struct net_device *dev)
457 /* Delayed past dellink to allow nl80211 to react to the device being
460 kfree(dev->ieee80211_ptr);
461 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->needs_free_netdev = true;
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 dev->priv_destructor = virt_wifi_net_device_destructor;
556 priv->being_deleted = false;
557 priv->is_connected = false;
559 INIT_DELAYED_WORK(&priv->connect, virt_wifi_connect_complete);
560 __module_get(THIS_MODULE);
564 unregister_netdevice(dev);
566 kfree(dev->ieee80211_ptr);
567 dev->ieee80211_ptr = NULL;
569 netdev_rx_handler_unregister(priv->lowerdev);
574 /* Called with rtnl lock held. */
575 static void virt_wifi_dellink(struct net_device *dev,
576 struct list_head *head)
578 struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
580 if (dev->ieee80211_ptr)
581 virt_wifi_cancel_scan(dev->ieee80211_ptr->wiphy);
583 priv->being_deleted = true;
584 virt_wifi_cancel_connect(dev);
585 netif_carrier_off(dev);
587 netdev_rx_handler_unregister(priv->lowerdev);
588 netdev_upper_dev_unlink(priv->lowerdev, dev);
590 unregister_netdevice_queue(dev, head);
591 module_put(THIS_MODULE);
593 /* Deleting the wiphy is handled in the module destructor. */
596 static struct rtnl_link_ops virt_wifi_link_ops = {
598 .setup = virt_wifi_setup,
599 .newlink = virt_wifi_newlink,
600 .dellink = virt_wifi_dellink,
601 .priv_size = sizeof(struct virt_wifi_netdev_priv),
604 static bool netif_is_virt_wifi_dev(const struct net_device *dev)
606 return rcu_access_pointer(dev->rx_handler) == virt_wifi_rx_handler;
609 static int virt_wifi_event(struct notifier_block *this, unsigned long event,
612 struct net_device *lower_dev = netdev_notifier_info_to_dev(ptr);
613 struct virt_wifi_netdev_priv *priv;
614 struct net_device *upper_dev;
615 LIST_HEAD(list_kill);
617 if (!netif_is_virt_wifi_dev(lower_dev))
621 case NETDEV_UNREGISTER:
622 priv = rtnl_dereference(lower_dev->rx_handler_data);
626 upper_dev = priv->upperdev;
628 upper_dev->rtnl_link_ops->dellink(upper_dev, &list_kill);
629 unregister_netdevice_many(&list_kill);
636 static struct notifier_block virt_wifi_notifier = {
637 .notifier_call = virt_wifi_event,
640 /* Acquires and releases the rtnl lock. */
641 static int __init virt_wifi_init_module(void)
645 /* Guaranteed to be locallly-administered and not multicast. */
646 eth_random_addr(fake_router_bssid);
648 err = register_netdevice_notifier(&virt_wifi_notifier);
653 common_wiphy = virt_wifi_make_wiphy();
657 err = rtnl_link_register(&virt_wifi_link_ops);
664 virt_wifi_destroy_wiphy(common_wiphy);
666 unregister_netdevice_notifier(&virt_wifi_notifier);
670 /* Acquires and releases the rtnl lock. */
671 static void __exit virt_wifi_cleanup_module(void)
673 /* Will delete any devices that depend on the wiphy. */
674 rtnl_link_unregister(&virt_wifi_link_ops);
675 virt_wifi_destroy_wiphy(common_wiphy);
676 unregister_netdevice_notifier(&virt_wifi_notifier);
679 module_init(virt_wifi_init_module);
680 module_exit(virt_wifi_cleanup_module);
682 MODULE_LICENSE("GPL v2");
684 MODULE_DESCRIPTION("Driver for a wireless wrapper of ethernet devices");
685 MODULE_ALIAS_RTNL_LINK("virt_wifi");