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