blob: d255147e8af655e6682b745ae7452ff3d636f944 [file] [log] [blame]
Cody Schuffelen1ea58e12018-11-20 19:14:49 -08001// 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
22static struct wiphy *common_wiphy;
23
24struct virt_wifi_wiphy_priv {
25 struct delayed_work scan_result;
26 struct cfg80211_scan_request *scan_request;
27 bool being_deleted;
28};
29
30static struct ieee80211_channel channel_2ghz = {
31 .band = NL80211_BAND_2GHZ,
32 .center_freq = 2432,
33 .hw_value = 2432,
34 .max_power = 20,
35};
36
37static 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
47static 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
69static struct ieee80211_channel channel_5ghz = {
70 .band = NL80211_BAND_5GHZ,
71 .center_freq = 5240,
72 .hw_value = 5240,
73 .max_power = 20,
74};
75
76static 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
100static 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. */
141static u8 fake_router_bssid[ETH_ALEN] __ro_after_init = {};
142
143/* Called with the rtnl lock held. */
144static 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. */
161static 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. */
192static 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
206struct 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. */
219static 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. */
243static 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. */
264static 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. */
280static 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. */
299static 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. */
324static 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
338static 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. */
349static 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
Cody Schuffelen1ea58e12018-11-20 19:14:49 -0800368 wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
369
370 priv = wiphy_priv(wiphy);
371 priv->being_deleted = false;
372 priv->scan_request = NULL;
373 INIT_DELAYED_WORK(&priv->scan_result, virt_wifi_scan_result);
374
375 err = wiphy_register(wiphy);
376 if (err < 0) {
377 wiphy_free(wiphy);
378 return NULL;
379 }
380
381 return wiphy;
382}
383
384/* Acquires and releases the rtnl lock. */
385static void virt_wifi_destroy_wiphy(struct wiphy *wiphy)
386{
387 struct virt_wifi_wiphy_priv *priv;
388
389 WARN(!wiphy, "%s called with null wiphy", __func__);
390 if (!wiphy)
391 return;
392
393 priv = wiphy_priv(wiphy);
394 priv->being_deleted = true;
395 virt_wifi_cancel_scan(wiphy);
396
397 if (wiphy->registered)
398 wiphy_unregister(wiphy);
399 wiphy_free(wiphy);
400}
401
402/* Enters and exits a RCU-bh critical section. */
403static netdev_tx_t virt_wifi_start_xmit(struct sk_buff *skb,
404 struct net_device *dev)
405{
406 struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
407
408 priv->tx_packets++;
409 if (!priv->is_connected) {
410 priv->tx_failed++;
411 return NET_XMIT_DROP;
412 }
413
414 skb->dev = priv->lowerdev;
415 return dev_queue_xmit(skb);
416}
417
418/* Called with rtnl lock held. */
419static int virt_wifi_net_device_open(struct net_device *dev)
420{
421 struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
422
423 priv->is_up = true;
424 return 0;
425}
426
427/* Called with rtnl lock held. */
428static int virt_wifi_net_device_stop(struct net_device *dev)
429{
430 struct virt_wifi_netdev_priv *n_priv = netdev_priv(dev);
431 struct virt_wifi_wiphy_priv *w_priv;
432
433 n_priv->is_up = false;
434
435 if (!dev->ieee80211_ptr)
436 return 0;
437 w_priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
438
439 virt_wifi_cancel_scan(dev->ieee80211_ptr->wiphy);
440 virt_wifi_cancel_connect(dev);
441 netif_carrier_off(dev);
442
443 return 0;
444}
445
446static const struct net_device_ops virt_wifi_ops = {
447 .ndo_start_xmit = virt_wifi_start_xmit,
448 .ndo_open = virt_wifi_net_device_open,
449 .ndo_stop = virt_wifi_net_device_stop,
450};
451
452/* Invoked as part of rtnl lock release. */
453static void virt_wifi_net_device_destructor(struct net_device *dev)
454{
455 /* Delayed past dellink to allow nl80211 to react to the device being
456 * deleted.
457 */
458 kfree(dev->ieee80211_ptr);
459 dev->ieee80211_ptr = NULL;
460 free_netdev(dev);
461}
462
463/* No lock interaction. */
464static void virt_wifi_setup(struct net_device *dev)
465{
466 ether_setup(dev);
467 dev->netdev_ops = &virt_wifi_ops;
468 dev->destructor = virt_wifi_net_device_destructor;
469}
470
471/* Called in a RCU read critical section from netif_receive_skb */
472static rx_handler_result_t virt_wifi_rx_handler(struct sk_buff **pskb)
473{
474 struct sk_buff *skb = *pskb;
475 struct virt_wifi_netdev_priv *priv =
476 rcu_dereference(skb->dev->rx_handler_data);
477
478 if (!priv->is_connected)
479 return RX_HANDLER_PASS;
480
481 /* GFP_ATOMIC because this is a packet interrupt handler. */
482 skb = skb_share_check(skb, GFP_ATOMIC);
483 if (!skb) {
484 dev_err(&priv->upperdev->dev, "can't skb_share_check\n");
485 return RX_HANDLER_CONSUMED;
486 }
487
488 *pskb = skb;
489 skb->dev = priv->upperdev;
490 skb->pkt_type = PACKET_HOST;
491 return RX_HANDLER_ANOTHER;
492}
493
494/* Called with rtnl lock held. */
495static int virt_wifi_newlink(struct net *src_net, struct net_device *dev,
496 struct nlattr *tb[], struct nlattr *data[])
497{
498 struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
499 int err;
500
501 if (!tb[IFLA_LINK])
502 return -EINVAL;
503
504 netif_carrier_off(dev);
505
506 priv->upperdev = dev;
507 priv->lowerdev = __dev_get_by_index(src_net,
508 nla_get_u32(tb[IFLA_LINK]));
509
510 if (!priv->lowerdev)
511 return -ENODEV;
512 if (!tb[IFLA_MTU])
513 dev->mtu = priv->lowerdev->mtu;
514 else if (dev->mtu > priv->lowerdev->mtu)
515 return -EINVAL;
516
517 err = netdev_rx_handler_register(priv->lowerdev, virt_wifi_rx_handler,
518 priv);
519 if (err) {
520 dev_err(&priv->lowerdev->dev,
521 "can't netdev_rx_handler_register: %d\n", err);
522 return err;
523 }
524
525 eth_hw_addr_inherit(dev, priv->lowerdev);
526 netif_stacked_transfer_operstate(priv->lowerdev, dev);
527
528 SET_NETDEV_DEV(dev, &priv->lowerdev->dev);
529 dev->ieee80211_ptr = kzalloc(sizeof(*dev->ieee80211_ptr), GFP_KERNEL);
530
Wei Yongjun4ffb1972019-01-18 07:29:52 +0000531 if (!dev->ieee80211_ptr) {
532 err = -ENOMEM;
Cody Schuffelen1ea58e12018-11-20 19:14:49 -0800533 goto remove_handler;
Wei Yongjun4ffb1972019-01-18 07:29:52 +0000534 }
Cody Schuffelen1ea58e12018-11-20 19:14:49 -0800535
536 dev->ieee80211_ptr->iftype = NL80211_IFTYPE_STATION;
537 dev->ieee80211_ptr->wiphy = common_wiphy;
538
539 err = register_netdevice(dev);
540 if (err) {
541 dev_err(&priv->lowerdev->dev, "can't register_netdevice: %d\n",
542 err);
543 goto free_wireless_dev;
544 }
545
546 err = netdev_upper_dev_link(priv->lowerdev, dev);
547 if (err) {
548 dev_err(&priv->lowerdev->dev, "can't netdev_upper_dev_link: %d\n",
549 err);
550 goto unregister_netdev;
551 }
552
553 priv->being_deleted = false;
554 priv->is_connected = false;
555 priv->is_up = false;
556 INIT_DELAYED_WORK(&priv->connect, virt_wifi_connect_complete);
557
558 return 0;
559unregister_netdev:
560 unregister_netdevice(dev);
561free_wireless_dev:
562 kfree(dev->ieee80211_ptr);
563 dev->ieee80211_ptr = NULL;
564remove_handler:
565 netdev_rx_handler_unregister(priv->lowerdev);
566
567 return err;
568}
569
570/* Called with rtnl lock held. */
571static void virt_wifi_dellink(struct net_device *dev,
572 struct list_head *head)
573{
574 struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
575
576 if (dev->ieee80211_ptr)
577 virt_wifi_cancel_scan(dev->ieee80211_ptr->wiphy);
578
579 priv->being_deleted = true;
580 virt_wifi_cancel_connect(dev);
581 netif_carrier_off(dev);
582
583 netdev_rx_handler_unregister(priv->lowerdev);
584 netdev_upper_dev_unlink(priv->lowerdev, dev);
585
586 unregister_netdevice_queue(dev, head);
587
588 /* Deleting the wiphy is handled in the module destructor. */
589}
590
591static struct rtnl_link_ops virt_wifi_link_ops = {
592 .kind = "virt_wifi",
593 .setup = virt_wifi_setup,
594 .newlink = virt_wifi_newlink,
595 .dellink = virt_wifi_dellink,
596 .priv_size = sizeof(struct virt_wifi_netdev_priv),
597};
598
599/* Acquires and releases the rtnl lock. */
600static int __init virt_wifi_init_module(void)
601{
602 int err;
603
604 /* Guaranteed to be locallly-administered and not multicast. */
605 eth_random_addr(fake_router_bssid);
606
607 common_wiphy = virt_wifi_make_wiphy();
608 if (!common_wiphy)
609 return -ENOMEM;
610
611 err = rtnl_link_register(&virt_wifi_link_ops);
612 if (err)
613 virt_wifi_destroy_wiphy(common_wiphy);
614
615 return err;
616}
617
618/* Acquires and releases the rtnl lock. */
619static void __exit virt_wifi_cleanup_module(void)
620{
621 /* Will delete any devices that depend on the wiphy. */
622 rtnl_link_unregister(&virt_wifi_link_ops);
623 virt_wifi_destroy_wiphy(common_wiphy);
624}
625
626module_init(virt_wifi_init_module);
627module_exit(virt_wifi_cleanup_module);
628
629MODULE_LICENSE("GPL v2");
630MODULE_AUTHOR("Cody Schuffelen <schuffelen@google.com>");
631MODULE_DESCRIPTION("Driver for a wireless wrapper of ethernet devices");
632MODULE_ALIAS_RTNL_LINK("virt_wifi");