• 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 = IEEE80211_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 = IEEE80211_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 = IEEE80211_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 = IEEE80211_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 
176 	informed_bss = cfg80211_inform_bss(wiphy, &channel_5ghz,
177 					   CFG80211_BSS_FTYPE_PRESP,
178 					   fake_router_bssid,
179 					   ktime_get_boot_ns(),
180 					   WLAN_CAPABILITY_ESS, 0,
181 					   (void *)&ssid, sizeof(ssid),
182 					   DBM_TO_MBM(-50), GFP_KERNEL);
183 	cfg80211_put_bss(wiphy, informed_bss);
184 
185 	/* Schedules work which acquires and releases the rtnl lock. */
186 	cfg80211_scan_done(priv->scan_request, false);
187 	priv->scan_request = NULL;
188 }
189 
190 /* May acquire and release the rdev BSS lock. */
virt_wifi_cancel_scan(struct wiphy * wiphy)191 static void virt_wifi_cancel_scan(struct wiphy *wiphy)
192 {
193 	struct virt_wifi_wiphy_priv *priv = wiphy_priv(wiphy);
194 
195 	cancel_delayed_work_sync(&priv->scan_result);
196 	/* Clean up dangling callbacks if necessary. */
197 	if (priv->scan_request) {
198 		/* Schedules work which acquires and releases the rtnl lock. */
199 		cfg80211_scan_done(priv->scan_request, true);
200 		priv->scan_request = NULL;
201 	}
202 }
203 
204 struct virt_wifi_netdev_priv {
205 	struct delayed_work connect;
206 	struct net_device *lowerdev;
207 	struct net_device *upperdev;
208 	u32 tx_packets;
209 	u32 tx_failed;
210 	u8 connect_requested_bss[ETH_ALEN];
211 	bool is_up;
212 	bool is_connected;
213 	bool being_deleted;
214 };
215 
216 /* Called with the rtnl lock held. */
virt_wifi_connect(struct wiphy * wiphy,struct net_device * netdev,struct cfg80211_connect_params * sme)217 static int virt_wifi_connect(struct wiphy *wiphy, struct net_device *netdev,
218 			     struct cfg80211_connect_params *sme)
219 {
220 	struct virt_wifi_netdev_priv *priv = netdev_priv(netdev);
221 	bool could_schedule;
222 
223 	if (priv->being_deleted || !priv->is_up)
224 		return -EBUSY;
225 
226 	could_schedule = schedule_delayed_work(&priv->connect, HZ * 2);
227 	if (!could_schedule)
228 		return -EBUSY;
229 
230 	if (sme->bssid)
231 		ether_addr_copy(priv->connect_requested_bss, sme->bssid);
232 	else
233 		eth_zero_addr(priv->connect_requested_bss);
234 
235 	wiphy_debug(wiphy, "connect\n");
236 
237 	return 0;
238 }
239 
240 /* Acquires and releases the rdev event lock. */
virt_wifi_connect_complete(struct work_struct * work)241 static void virt_wifi_connect_complete(struct work_struct *work)
242 {
243 	struct virt_wifi_netdev_priv *priv =
244 		container_of(work, struct virt_wifi_netdev_priv, connect.work);
245 	u8 *requested_bss = priv->connect_requested_bss;
246 	bool has_addr = !is_zero_ether_addr(requested_bss);
247 	bool right_addr = ether_addr_equal(requested_bss, fake_router_bssid);
248 	u16 status = WLAN_STATUS_SUCCESS;
249 
250 	if (!priv->is_up || (has_addr && !right_addr))
251 		status = WLAN_STATUS_UNSPECIFIED_FAILURE;
252 	else
253 		priv->is_connected = true;
254 
255 	/* Schedules an event that acquires the rtnl lock. */
256 	cfg80211_connect_result(priv->upperdev, requested_bss, NULL, 0, NULL, 0,
257 				status, GFP_KERNEL);
258 	netif_carrier_on(priv->upperdev);
259 }
260 
261 /* May acquire and release the rdev event lock. */
virt_wifi_cancel_connect(struct net_device * netdev)262 static void virt_wifi_cancel_connect(struct net_device *netdev)
263 {
264 	struct virt_wifi_netdev_priv *priv = netdev_priv(netdev);
265 
266 	/* If there is work pending, clean up dangling callbacks. */
267 	if (cancel_delayed_work_sync(&priv->connect)) {
268 		/* Schedules an event that acquires the rtnl lock. */
269 		cfg80211_connect_result(priv->upperdev,
270 					priv->connect_requested_bss, NULL, 0,
271 					NULL, 0,
272 					WLAN_STATUS_UNSPECIFIED_FAILURE,
273 					GFP_KERNEL);
274 	}
275 }
276 
277 /* Called with the rtnl lock held. Acquires the rdev event lock. */
virt_wifi_disconnect(struct wiphy * wiphy,struct net_device * netdev,u16 reason_code)278 static int virt_wifi_disconnect(struct wiphy *wiphy, struct net_device *netdev,
279 				u16 reason_code)
280 {
281 	struct virt_wifi_netdev_priv *priv = netdev_priv(netdev);
282 
283 	if (priv->being_deleted)
284 		return -EBUSY;
285 
286 	wiphy_debug(wiphy, "disconnect\n");
287 	virt_wifi_cancel_connect(netdev);
288 
289 	cfg80211_disconnected(netdev, reason_code, NULL, 0, true, GFP_KERNEL);
290 	priv->is_connected = false;
291 	netif_carrier_off(netdev);
292 
293 	return 0;
294 }
295 
296 /* Called with the rtnl lock held. */
virt_wifi_get_station(struct wiphy * wiphy,struct net_device * dev,const u8 * mac,struct station_info * sinfo)297 static int virt_wifi_get_station(struct wiphy *wiphy, struct net_device *dev,
298 				 const u8 *mac, struct station_info *sinfo)
299 {
300 	struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
301 
302 	wiphy_debug(wiphy, "get_station\n");
303 
304 	if (!priv->is_connected || !ether_addr_equal(mac, fake_router_bssid))
305 		return -ENOENT;
306 
307 	sinfo->filled = BIT_ULL(NL80211_STA_INFO_TX_PACKETS) |
308 		BIT_ULL(NL80211_STA_INFO_TX_FAILED) |
309 		BIT_ULL(NL80211_STA_INFO_SIGNAL) |
310 		BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
311 	sinfo->tx_packets = priv->tx_packets;
312 	sinfo->tx_failed = priv->tx_failed;
313 	/* For CFG80211_SIGNAL_TYPE_MBM, value is expressed in _dBm_ */
314 	sinfo->signal = -50;
315 	sinfo->txrate = (struct rate_info) {
316 		.legacy = 10, /* units are 100kbit/s */
317 	};
318 	return 0;
319 }
320 
321 /* 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)322 static int virt_wifi_dump_station(struct wiphy *wiphy, struct net_device *dev,
323 				  int idx, u8 *mac, struct station_info *sinfo)
324 {
325 	struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
326 
327 	wiphy_debug(wiphy, "dump_station\n");
328 
329 	if (idx != 0 || !priv->is_connected)
330 		return -ENOENT;
331 
332 	ether_addr_copy(mac, fake_router_bssid);
333 	return virt_wifi_get_station(wiphy, dev, fake_router_bssid, sinfo);
334 }
335 
336 static const struct cfg80211_ops virt_wifi_cfg80211_ops = {
337 	.scan = virt_wifi_scan,
338 
339 	.connect = virt_wifi_connect,
340 	.disconnect = virt_wifi_disconnect,
341 
342 	.get_station = virt_wifi_get_station,
343 	.dump_station = virt_wifi_dump_station,
344 };
345 
346 /* Acquires and releases the rtnl lock. */
virt_wifi_make_wiphy(void)347 static struct wiphy *virt_wifi_make_wiphy(void)
348 {
349 	struct wiphy *wiphy;
350 	struct virt_wifi_wiphy_priv *priv;
351 	int err;
352 
353 	wiphy = wiphy_new(&virt_wifi_cfg80211_ops, sizeof(*priv));
354 
355 	if (!wiphy)
356 		return NULL;
357 
358 	wiphy->max_scan_ssids = 4;
359 	wiphy->max_scan_ie_len = 1000;
360 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
361 
362 	wiphy->bands[IEEE80211_BAND_2GHZ] = &band_2ghz;
363 	wiphy->bands[IEEE80211_BAND_5GHZ] = &band_5ghz;
364 	wiphy->bands[IEEE80211_BAND_60GHZ] = NULL;
365 
366 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
367 
368 	priv = wiphy_priv(wiphy);
369 	priv->being_deleted = false;
370 	priv->scan_request = NULL;
371 	INIT_DELAYED_WORK(&priv->scan_result, virt_wifi_scan_result);
372 
373 	err = wiphy_register(wiphy);
374 	if (err < 0) {
375 		wiphy_free(wiphy);
376 		return NULL;
377 	}
378 
379 	return wiphy;
380 }
381 
382 /* Acquires and releases the rtnl lock. */
virt_wifi_destroy_wiphy(struct wiphy * wiphy)383 static void virt_wifi_destroy_wiphy(struct wiphy *wiphy)
384 {
385 	struct virt_wifi_wiphy_priv *priv;
386 
387 	WARN(!wiphy, "%s called with null wiphy", __func__);
388 	if (!wiphy)
389 		return;
390 
391 	priv = wiphy_priv(wiphy);
392 	priv->being_deleted = true;
393 	virt_wifi_cancel_scan(wiphy);
394 
395 	if (wiphy->registered)
396 		wiphy_unregister(wiphy);
397 	wiphy_free(wiphy);
398 }
399 
400 /* Enters and exits a RCU-bh critical section. */
virt_wifi_start_xmit(struct sk_buff * skb,struct net_device * dev)401 static netdev_tx_t virt_wifi_start_xmit(struct sk_buff *skb,
402 					struct net_device *dev)
403 {
404 	struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
405 
406 	priv->tx_packets++;
407 	if (!priv->is_connected) {
408 		priv->tx_failed++;
409 		return NET_XMIT_DROP;
410 	}
411 
412 	skb->dev = priv->lowerdev;
413 	return dev_queue_xmit(skb);
414 }
415 
416 /* Called with rtnl lock held. */
virt_wifi_net_device_open(struct net_device * dev)417 static int virt_wifi_net_device_open(struct net_device *dev)
418 {
419 	struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
420 
421 	priv->is_up = true;
422 	return 0;
423 }
424 
425 /* Called with rtnl lock held. */
virt_wifi_net_device_stop(struct net_device * dev)426 static int virt_wifi_net_device_stop(struct net_device *dev)
427 {
428 	struct virt_wifi_netdev_priv *n_priv = netdev_priv(dev);
429 	struct virt_wifi_wiphy_priv *w_priv;
430 
431 	n_priv->is_up = false;
432 
433 	if (!dev->ieee80211_ptr)
434 		return 0;
435 	w_priv = wiphy_priv(dev->ieee80211_ptr->wiphy);
436 
437 	virt_wifi_cancel_scan(dev->ieee80211_ptr->wiphy);
438 	virt_wifi_cancel_connect(dev);
439 	netif_carrier_off(dev);
440 
441 	return 0;
442 }
443 
444 static const struct net_device_ops virt_wifi_ops = {
445 	.ndo_start_xmit = virt_wifi_start_xmit,
446 	.ndo_open = virt_wifi_net_device_open,
447 	.ndo_stop = virt_wifi_net_device_stop,
448 };
449 
450 /* Invoked as part of rtnl lock release. */
virt_wifi_net_device_destructor(struct net_device * dev)451 static void virt_wifi_net_device_destructor(struct net_device *dev)
452 {
453 	/* Delayed past dellink to allow nl80211 to react to the device being
454 	 * deleted.
455 	 */
456 	kfree(dev->ieee80211_ptr);
457 	dev->ieee80211_ptr = NULL;
458 	free_netdev(dev);
459 }
460 
461 /* No lock interaction. */
virt_wifi_setup(struct net_device * dev)462 static void virt_wifi_setup(struct net_device *dev)
463 {
464 	ether_setup(dev);
465 	dev->netdev_ops = &virt_wifi_ops;
466 	dev->destructor = virt_wifi_net_device_destructor;
467 }
468 
469 /* Called in a RCU read critical section from netif_receive_skb */
virt_wifi_rx_handler(struct sk_buff ** pskb)470 static rx_handler_result_t virt_wifi_rx_handler(struct sk_buff **pskb)
471 {
472 	struct sk_buff *skb = *pskb;
473 	struct virt_wifi_netdev_priv *priv =
474 		rcu_dereference(skb->dev->rx_handler_data);
475 
476 	if (!priv->is_connected)
477 		return RX_HANDLER_PASS;
478 
479 	/* GFP_ATOMIC because this is a packet interrupt handler. */
480 	skb = skb_share_check(skb, GFP_ATOMIC);
481 	if (!skb) {
482 		dev_err(&priv->upperdev->dev, "can't skb_share_check\n");
483 		return RX_HANDLER_CONSUMED;
484 	}
485 
486 	*pskb = skb;
487 	skb->dev = priv->upperdev;
488 	skb->pkt_type = PACKET_HOST;
489 	return RX_HANDLER_ANOTHER;
490 }
491 
492 /* Called with rtnl lock held. */
virt_wifi_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[])493 static int virt_wifi_newlink(struct net *src_net, struct net_device *dev,
494 			     struct nlattr *tb[], struct nlattr *data[])
495 {
496 	struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
497 	int err;
498 
499 	if (!tb[IFLA_LINK])
500 		return -EINVAL;
501 
502 	netif_carrier_off(dev);
503 
504 	priv->upperdev = dev;
505 	priv->lowerdev = __dev_get_by_index(src_net,
506 					    nla_get_u32(tb[IFLA_LINK]));
507 
508 	if (!priv->lowerdev)
509 		return -ENODEV;
510 	if (!tb[IFLA_MTU])
511 		dev->mtu = priv->lowerdev->mtu;
512 	else if (dev->mtu > priv->lowerdev->mtu)
513 		return -EINVAL;
514 
515 	err = netdev_rx_handler_register(priv->lowerdev, virt_wifi_rx_handler,
516 					 priv);
517 	if (err) {
518 		dev_err(&priv->lowerdev->dev,
519 			"can't netdev_rx_handler_register: %d\n", err);
520 		return err;
521 	}
522 
523 	eth_hw_addr_inherit(dev, priv->lowerdev);
524 	netif_stacked_transfer_operstate(priv->lowerdev, dev);
525 
526 	SET_NETDEV_DEV(dev, &priv->lowerdev->dev);
527 	dev->ieee80211_ptr = kzalloc(sizeof(*dev->ieee80211_ptr), GFP_KERNEL);
528 
529 	if (!dev->ieee80211_ptr)
530 		goto remove_handler;
531 
532 	dev->ieee80211_ptr->iftype = NL80211_IFTYPE_STATION;
533 	dev->ieee80211_ptr->wiphy = common_wiphy;
534 
535 	err = register_netdevice(dev);
536 	if (err) {
537 		dev_err(&priv->lowerdev->dev, "can't register_netdevice: %d\n",
538 			err);
539 		goto free_wireless_dev;
540 	}
541 
542 	err = netdev_upper_dev_link(priv->lowerdev, dev);
543 	if (err) {
544 		dev_err(&priv->lowerdev->dev, "can't netdev_upper_dev_link: %d\n",
545 			err);
546 		goto unregister_netdev;
547 	}
548 
549 	priv->being_deleted = false;
550 	priv->is_connected = false;
551 	priv->is_up = false;
552 	INIT_DELAYED_WORK(&priv->connect, virt_wifi_connect_complete);
553 
554 	return 0;
555 unregister_netdev:
556 	unregister_netdevice(dev);
557 free_wireless_dev:
558 	kfree(dev->ieee80211_ptr);
559 	dev->ieee80211_ptr = NULL;
560 remove_handler:
561 	netdev_rx_handler_unregister(priv->lowerdev);
562 
563 	return err;
564 }
565 
566 /* Called with rtnl lock held. */
virt_wifi_dellink(struct net_device * dev,struct list_head * head)567 static void virt_wifi_dellink(struct net_device *dev,
568 			      struct list_head *head)
569 {
570 	struct virt_wifi_netdev_priv *priv = netdev_priv(dev);
571 
572 	if (dev->ieee80211_ptr)
573 		virt_wifi_cancel_scan(dev->ieee80211_ptr->wiphy);
574 
575 	priv->being_deleted = true;
576 	virt_wifi_cancel_connect(dev);
577 	netif_carrier_off(dev);
578 
579 	netdev_rx_handler_unregister(priv->lowerdev);
580 	netdev_upper_dev_unlink(priv->lowerdev, dev);
581 
582 	unregister_netdevice_queue(dev, head);
583 
584 	/* Deleting the wiphy is handled in the module destructor. */
585 }
586 
587 static struct rtnl_link_ops virt_wifi_link_ops = {
588 	.kind		= "virt_wifi",
589 	.setup		= virt_wifi_setup,
590 	.newlink	= virt_wifi_newlink,
591 	.dellink	= virt_wifi_dellink,
592 	.priv_size	= sizeof(struct virt_wifi_netdev_priv),
593 };
594 
595 /* Acquires and releases the rtnl lock. */
virt_wifi_init_module(void)596 static int __init virt_wifi_init_module(void)
597 {
598 	int err;
599 
600 	/* Guaranteed to be locallly-administered and not multicast. */
601 	eth_random_addr(fake_router_bssid);
602 
603 	common_wiphy = virt_wifi_make_wiphy();
604 	if (!common_wiphy)
605 		return -ENOMEM;
606 
607 	err = rtnl_link_register(&virt_wifi_link_ops);
608 	if (err)
609 		virt_wifi_destroy_wiphy(common_wiphy);
610 
611 	return err;
612 }
613 
614 /* Acquires and releases the rtnl lock. */
virt_wifi_cleanup_module(void)615 static void __exit virt_wifi_cleanup_module(void)
616 {
617 	/* Will delete any devices that depend on the wiphy. */
618 	rtnl_link_unregister(&virt_wifi_link_ops);
619 	virt_wifi_destroy_wiphy(common_wiphy);
620 }
621 
622 module_init(virt_wifi_init_module);
623 module_exit(virt_wifi_cleanup_module);
624 
625 MODULE_LICENSE("GPL v2");
626 MODULE_AUTHOR("Cody Schuffelen <schuffelen@google.com>");
627 MODULE_DESCRIPTION("Driver for a wireless wrapper of ethernet devices");
628 MODULE_ALIAS_RTNL_LINK("virt_wifi");
629