• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include <net/cfg80211.h>
18 #include <net/rtnetlink.h>
19 #include <linux/etherdevice.h>
20 #include <linux/module.h>
21 
22 static struct wiphy *common_wiphy;
23 
24 struct virt_wifi_wiphy_priv {
25 	struct delayed_work scan_result;
26 	struct cfg80211_scan_request *scan_request;
27 	bool being_deleted;
28 };
29 
30 static struct ieee80211_channel channel_2ghz = {
31 	.band = NL80211_BAND_2GHZ,
32 	.center_freq = 2432,
33 	.hw_value = 2432,
34 	.max_power = 20,
35 };
36 
37 static 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 
47 static 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 
69 static struct ieee80211_channel channel_5ghz = {
70 	.band = NL80211_BAND_5GHZ,
71 	.center_freq = 5240,
72 	.hw_value = 5240,
73 	.max_power = 20,
74 };
75 
76 static 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 
100 static 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. */
141 static u8 fake_router_bssid[ETH_ALEN] __ro_after_init = {};
142 
143 /* Called with the rtnl lock held. */
virt_wifi_scan(struct wiphy * wiphy,struct cfg80211_scan_request * request)144 static 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. */
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_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. */
virt_wifi_cancel_scan(struct wiphy * wiphy)192 static 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 
206 struct 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. */
virt_wifi_connect(struct wiphy * wiphy,struct net_device * netdev,struct cfg80211_connect_params * sme)219 static 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. */
virt_wifi_connect_complete(struct work_struct * work)243 static 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. */
virt_wifi_cancel_connect(struct net_device * netdev)264 static 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. */
virt_wifi_disconnect(struct wiphy * wiphy,struct net_device * netdev,u16 reason_code)280 static 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. */
virt_wifi_get_station(struct wiphy * wiphy,struct net_device * dev,const u8 * mac,struct station_info * sinfo)299 static 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. */
virt_wifi_dump_station(struct wiphy * wiphy,struct net_device * dev,int idx,u8 * mac,struct station_info * sinfo)324 static 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 
338 static 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. */
virt_wifi_make_wiphy(void)349 static 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 
368 	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. */
virt_wifi_destroy_wiphy(struct wiphy * wiphy)385 static 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. */
virt_wifi_start_xmit(struct sk_buff * skb,struct net_device * dev)403 static 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. */
virt_wifi_net_device_open(struct net_device * dev)419 static 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. */
virt_wifi_net_device_stop(struct net_device * dev)428 static 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 
446 static 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. */
virt_wifi_net_device_destructor(struct net_device * dev)453 static 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. */
virt_wifi_setup(struct net_device * dev)464 static void virt_wifi_setup(struct net_device *dev)
465 {
466 	ether_setup(dev);
467 	dev->netdev_ops = &virt_wifi_ops;
468 	dev->priv_destructor = virt_wifi_net_device_destructor;
469 }
470 
471 /* Called in a RCU read critical section from netif_receive_skb */
virt_wifi_rx_handler(struct sk_buff ** pskb)472 static 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. */
virt_wifi_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)495 static int virt_wifi_newlink(struct net *src_net, struct net_device *dev,
496 			     struct nlattr *tb[], struct nlattr *data[],
497 			     struct netlink_ext_ack *extack)
498 {
499 	struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
500 	int err;
501 
502 	if (!tb[IFLA_LINK])
503 		return -EINVAL;
504 
505 	netif_carrier_off(dev);
506 
507 	priv->upperdev = dev;
508 	priv->lowerdev = __dev_get_by_index(src_net,
509 					    nla_get_u32(tb[IFLA_LINK]));
510 
511 	if (!priv->lowerdev)
512 		return -ENODEV;
513 	if (!tb[IFLA_MTU])
514 		dev->mtu = priv->lowerdev->mtu;
515 	else if (dev->mtu > priv->lowerdev->mtu)
516 		return -EINVAL;
517 
518 	err = netdev_rx_handler_register(priv->lowerdev, virt_wifi_rx_handler,
519 					 priv);
520 	if (err) {
521 		dev_err(&priv->lowerdev->dev,
522 			"can't netdev_rx_handler_register: %d\n", err);
523 		return err;
524 	}
525 
526 	eth_hw_addr_inherit(dev, priv->lowerdev);
527 	netif_stacked_transfer_operstate(priv->lowerdev, dev);
528 
529 	SET_NETDEV_DEV(dev, &priv->lowerdev->dev);
530 	dev->ieee80211_ptr = kzalloc(sizeof(*dev->ieee80211_ptr), GFP_KERNEL);
531 
532 	if (!dev->ieee80211_ptr) {
533 		err = -ENOMEM;
534 		goto remove_handler;
535 	}
536 
537 	dev->ieee80211_ptr->iftype = NL80211_IFTYPE_STATION;
538 	dev->ieee80211_ptr->wiphy = common_wiphy;
539 
540 	err = register_netdevice(dev);
541 	if (err) {
542 		dev_err(&priv->lowerdev->dev, "can't register_netdevice: %d\n",
543 			err);
544 		goto free_wireless_dev;
545 	}
546 
547 	err = netdev_upper_dev_link(priv->lowerdev, dev);
548 	if (err) {
549 		dev_err(&priv->lowerdev->dev, "can't netdev_upper_dev_link: %d\n",
550 			err);
551 		goto unregister_netdev;
552 	}
553 
554 	priv->being_deleted = false;
555 	priv->is_connected = false;
556 	priv->is_up = false;
557 	INIT_DELAYED_WORK(&priv->connect, virt_wifi_connect_complete);
558 
559 	return 0;
560 unregister_netdev:
561 	unregister_netdevice(dev);
562 free_wireless_dev:
563 	kfree(dev->ieee80211_ptr);
564 	dev->ieee80211_ptr = NULL;
565 remove_handler:
566 	netdev_rx_handler_unregister(priv->lowerdev);
567 
568 	return err;
569 }
570 
571 /* Called with rtnl lock held. */
virt_wifi_dellink(struct net_device * dev,struct list_head * head)572 static void virt_wifi_dellink(struct net_device *dev,
573 			      struct list_head *head)
574 {
575 	struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
576 
577 	if (dev->ieee80211_ptr)
578 		virt_wifi_cancel_scan(dev->ieee80211_ptr->wiphy);
579 
580 	priv->being_deleted = true;
581 	virt_wifi_cancel_connect(dev);
582 	netif_carrier_off(dev);
583 
584 	netdev_rx_handler_unregister(priv->lowerdev);
585 	netdev_upper_dev_unlink(priv->lowerdev, dev);
586 
587 	unregister_netdevice_queue(dev, head);
588 
589 	/* Deleting the wiphy is handled in the module destructor. */
590 }
591 
592 static struct rtnl_link_ops virt_wifi_link_ops = {
593 	.kind		= "virt_wifi",
594 	.setup		= virt_wifi_setup,
595 	.newlink	= virt_wifi_newlink,
596 	.dellink	= virt_wifi_dellink,
597 	.priv_size	= sizeof(struct virt_wifi_netdev_priv),
598 };
599 
600 /* Acquires and releases the rtnl lock. */
virt_wifi_init_module(void)601 static int __init virt_wifi_init_module(void)
602 {
603 	int err;
604 
605 	/* Guaranteed to be locallly-administered and not multicast. */
606 	eth_random_addr(fake_router_bssid);
607 
608 	common_wiphy = virt_wifi_make_wiphy();
609 	if (!common_wiphy)
610 		return -ENOMEM;
611 
612 	err = rtnl_link_register(&virt_wifi_link_ops);
613 	if (err)
614 		virt_wifi_destroy_wiphy(common_wiphy);
615 
616 	return err;
617 }
618 
619 /* Acquires and releases the rtnl lock. */
virt_wifi_cleanup_module(void)620 static void __exit virt_wifi_cleanup_module(void)
621 {
622 	/* Will delete any devices that depend on the wiphy. */
623 	rtnl_link_unregister(&virt_wifi_link_ops);
624 	virt_wifi_destroy_wiphy(common_wiphy);
625 }
626 
627 module_init(virt_wifi_init_module);
628 module_exit(virt_wifi_cleanup_module);
629 
630 MODULE_LICENSE("GPL v2");
631 MODULE_AUTHOR("Cody Schuffelen <schuffelen@google.com>");
632 MODULE_DESCRIPTION("Driver for a wireless wrapper of ethernet devices");
633 MODULE_ALIAS_RTNL_LINK("virt_wifi");
634