virt_wifi.c 18 KB

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