virt_wifi.c 18 KB

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