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