wireless-drivers: rtnetlink wifi simulation device
Subsystems (3)
PATCH v2 1/1 wireless-drivers: rtnetlink wifi simulation device
This device takes over an existing network device and produces a new one that appears like a wireless connection, returning enough canned responses to nl80211 to satisfy a standard network manager. If necessary, it can also be set up one step removed from an existing network device, such as through a vlan/80211Q or macvlan connection to not disrupt the existing network interface.
To use it to wrap a bare ethernet connection:
ip link set eth0 down
ip link set eth0 name buried_eth0
ip link set buried_eth0 up
ip link add link buried_eth0 name wlan0 type virt_wifi
eth0 is renamed to buried_eth0 to avoid a network manager trying to manage it, as the original network link will become unusuable and only the wireless wrapper will be functional. This can also be combined with vlan or macvlan links on top of eth0 to share the network between distinct links, but that requires support outside the machine for accepting vlan-tagged packets or packets from multiple MAC addresses.
This is being used for Google's Remote Android Virtual Device project, which runs Android devices in virtual machines. The standard network interfaces provided inside the virtual machines are all ethernet. However, Android is not interested in ethernet devices and would rather connect to a wireless interface. This patch allows the virtual machine guest to treat one of its network connections as wireless rather than ethernet, satisfying Android's network connection requirements.
We believe this is a generally useful driver for simulating wireless network connections in other environments where a wireless connection is desired by some userspace process but is not available. Future work can also include exporting the wireless control plane to userspace, so the device can configure the behavior of the simulated wireless network itself.
This is distinct from other testing efforts such as mac80211_hwsim by being a cfg80211 device instead of mac80211 device, allowing straight pass-through on the data plane instead of forcing packaging of ethernet data into mac80211 frames.
---
Original change: https://lore.kernel.org/lkml/[email protected]/
First review: https://lore.kernel.org/lkml/[email protected]/
Thanks for the detailed review! I believe I've addressed all comments.
The problem we've experienced with routing the hwsim virtual medium from the inside the VM to outside the VM is this required running hwsim on the host kernel as well. This is too intrusive for our use-case, whereas wifi that exists completely inside the VM kernel is much more palatable.
drivers/net/wireless/Kconfig | 7 + drivers/net/wireless/Makefile | 2 + drivers/net/wireless/virt_wifi.c | 530 +++++++++++++++++++++++++++++++ 3 files changed, 539 insertions(+) create mode 100644 drivers/net/wireless/virt_wifi.c
drivers/net/wireless/Kconfig +7 -0
@@ -114,4 +114,11 @@ config USB_NET_RNDIS_WLAN If you choose to build a module, it'll be called rndis_wlan. +config VIRT_WIFI + tristate "Wifi wrapper for ethernet drivers" + default n + ---help--- + This option adds support for ethernet connections to appear as if they + are wifi connections through a special rtnetlink device. + endif # WLAN
drivers/net/wireless/Makefile +2 -0
@@ -27,3 +27,5 @@ obj-$(CONFIG_PCMCIA_WL3501) += wl3501_cs.o obj-$(CONFIG_USB_NET_RNDIS_WLAN) += rndis_wlan.o obj-$(CONFIG_MAC80211_HWSIM) += mac80211_hwsim.o + +obj-$(CONFIG_VIRT_WIFI) += virt_wifi.o
drivers/net/wireless/virt_wifi.c new file +530 -0
530 lines not on this page · raw message
Signature · 1 line
2.19.0.605.g01d371f741-goog
Quoted 45 lines · Cody Schuffelen
This device takes over an existing network device and produces a new one that appears like a wireless connection, returning enough canned responses to nl80211 to satisfy a standard network manager. If necessary, it can also be set up one step removed from an existing network device, such as through a vlan/80211Q or macvlan connection to not disrupt the existing network interface.
To use it to wrap a bare ethernet connection:
ip link set eth0 down
ip link set eth0 nameburied_eth0
ip link setburied_eth0up
ip link add linkburied_eth0name wlan0 typevirt_wifieth0 is renamed to
buried_eth0to avoid a network manager trying to manage it, as the original network link will become unusuable and only the wireless wrapper will be functional. This can also be combined with vlan or macvlan links on top of eth0 to share the network between distinct links, but that requires support outside the machine for accepting vlan-tagged packets or packets from multiple MAC addresses.This is being used for Google's Remote Android Virtual Device project, which runs Android devices in virtual machines. The standard network interfaces provided inside the virtual machines are all ethernet. However, Android is not interested in ethernet devices and would rather connect to a wireless interface. This patch allows the virtual machine guest to treat one of its network connections as wireless rather than ethernet, satisfying Android's network connection requirements.
We believe this is a generally useful driver for simulating wireless network connections in other environments where a wireless connection is desired by some userspace process but is not available. Future work can also include exporting the wireless control plane to userspace, so the device can configure the behavior of the simulated wireless network itself.
This is distinct from other testing efforts such as
mac80211_hwsimby being a cfg80211 device instead of mac80211 device, allowing straight pass-through on the data plane instead of forcing packaging of ethernet data into mac80211 frames.Signed-off-by A. Cody Schuffelen <[email protected]>Acked-by Greg Hartman <[email protected]>Signed-off-by Cody Schuffelen <[email protected]>
Quoted 5 lines
---
Original change: https://lore.kernel.org/lkml/[email protected]/
First review: https://lore.kernel.org/lkml/[email protected]/Thanks for the detailed review! I believe I've addressed all comments.
BTW, this will likely go through mac80211-next, so don't add net-next to the subject to avoid Dave getting confused. But no need to resend because of this.
Signature · 1 line
Kalle Valo
Quoted 45 lines · Cody Schuffelen
This device takes over an existing network device and produces a new one that appears like a wireless connection, returning enough canned responses to nl80211 to satisfy a standard network manager. If necessary, it can also be set up one step removed from an existing network device, such as through a vlan/80211Q or macvlan connection to not disrupt the existing network interface.
To use it to wrap a bare ethernet connection:
ip link set eth0 down
ip link set eth0 nameburied_eth0
ip link setburied_eth0up
ip link add linkburied_eth0name wlan0 typevirt_wifieth0 is renamed to
buried_eth0to avoid a network manager trying to manage it, as the original network link will become unusuable and only the wireless wrapper will be functional. This can also be combined with vlan or macvlan links on top of eth0 to share the network between distinct links, but that requires support outside the machine for accepting vlan-tagged packets or packets from multiple MAC addresses.This is being used for Google's Remote Android Virtual Device project, which runs Android devices in virtual machines. The standard network interfaces provided inside the virtual machines are all ethernet. However, Android is not interested in ethernet devices and would rather connect to a wireless interface. This patch allows the virtual machine guest to treat one of its network connections as wireless rather than ethernet, satisfying Android's network connection requirements.
We believe this is a generally useful driver for simulating wireless network connections in other environments where a wireless connection is desired by some userspace process but is not available. Future work can also include exporting the wireless control plane to userspace, so the device can configure the behavior of the simulated wireless network itself.
This is distinct from other testing efforts such as
mac80211_hwsimby being a cfg80211 device instead of mac80211 device, allowing straight pass-through on the data plane instead of forcing packaging of ethernet data into mac80211 frames.Signed-off-by A. Cody Schuffelen <[email protected]>Acked-by Greg Hartman <[email protected]>Signed-off-by Cody Schuffelen <[email protected]>
[...]
drivers/net/wireless/Kconfig config USB_NET_RNDIS_WLAN · line 119
Quoted 9 lines
drivers/net/wireless/Kconfig +3
-0@@ -114,4 +114,11 @@ config USB_NET_RNDIS_WLAN If you choose to build a module, it'll be called rndis_wlan. +config VIRT_WIFI + tristate "Wifi wrapper for ethernet drivers" + default n
Isn't 'n' the default so you don't need to explicitly set that?
Signature · 1 line
Kalle Valo
Johannes Berg
maintainerQuoted 7 lines · Cody Schuffelen
ip link set eth0 down
ip link set eth0 nameburied_eth0
ip link setburied_eth0up
ip link add linkburied_eth0name wlan0 typevirt_wifieth0 is renamed to
buried_eth0to avoid a network manager trying to manage it,
I feel you should remove this "buried" thing at least from the plain instructions - first of all, I'm not convinced it actually *works* in general (network managers are able to find it anyway, right? perhaps android's has some rules?), and it's not really necessary from a kernel POV.
Perhaps add a note like "you may have to rename or otherwise hide the eth0 from your connection manager" (also "NetworkManager" is a specific software, so "network manager" is confusing - I actually first thought you meant NM).
Anyway, just a thought.
Quoted 1 line
Thanks for the detailed review! I believe I've addressed all comments.
Thanks!
Quoted 4 lines
The problem we've experienced with routing the hwsim virtual medium from the inside the VM to outside the VM is this required running hwsim on the host kernel as well. This is too intrusive for our use-case, whereas wifi that exists completely inside the VM kernel is much more palatable.
Fair enough. I still think it'd be worthwhile in the long run because then you could start multiple APs to simulate roaming etc. without having to implement all of this here.
IOW - I don't think we should extend this much. As is, I think it's fine, but I wouldn't want to see extensions like "have two scan result", "let userspace control the RSSI of the scan results to force 'roaming'" etc.
drivers/net/wireless/virt_wifi.c line 32
Quoted 4 lines
drivers/net/wireless/virt_wifi.c new file +4
-0+static struct ieee80211_channel channel_2ghz = { + .band =NL80211_BAND_2GHZ , + .center_freq =5500 , + .hw_value =5500 ,
That doesn't seem right - a 2 GHz channel that's really 5.5 GHz?
Review
Quoted 1 line
(quoted diff) +1
-0+ .max_power = 5500,
That seems more like a copy/paste bug rather than intentional?
Quoted 7 lines
drivers/net/wireless/virt_wifi.c new file +7
-0+static struct ieee80211_rate bitrates_2ghz[] = { + { + .bitrate =10 , + }, { + .bitrate =20 , + }, { + .bitrate =55 ,
drivers/net/wireless/virt_wifi.c line 56
Quoted 10 lines
drivers/net/wireless/virt_wifi.c new file +10
-0+ }, { + .bitrate = 60, + }, { + .bitrate = 110, + }, { + .bitrate = 120, + }, { + .bitrate = 240, + }, +};
That's ... strangely formatted? I guess we'd usually write
{ .bitrate = 10 },
{ .bitrate = 20 },
...drivers/net/wireless/virt_wifi.c line 63
Quoted 6 lines
drivers/net/wireless/virt_wifi.c new file +6
-0+static struct ieee80211_supported_band band_2ghz = { + .channels = &channel_2ghz, + .bitrates = bitrates_2ghz, + .band =NL80211_BAND_2GHZ , + .n_channels =1 , + .n_bitrates =7 ,
Please use ARRAY_SIZE()
drivers/net/wireless/virt_wifi.c line 69
Quoted 6 lines
drivers/net/wireless/virt_wifi.c new file +6
-0+ .ht_cap = { + .ht_supported = true, + }, + .vht_cap = { + .vht_supported = true, + },
That looks _really_ bare - you may want to copy something sane to here.
drivers/net/wireless/virt_wifi.c line 79
Quoted 5 lines
drivers/net/wireless/virt_wifi.c new file +4
-0+}; + +static struct ieee80211_channel channel_5ghz = { + .max_power =5500 ,
same here - max_power 5500?
drivers/net/wireless/virt_wifi.c line 91
Quoted 12 lines
drivers/net/wireless/virt_wifi.c new file +12
-0+ .max_reg_power = 9999, +}; + +static struct ieee80211_rate bitrates_5ghz[] = { + { + .bitrate =60 , + }, { + .bitrate =120 , + }, { + .bitrate =240 , + }, +};
Same comment here regarding formatting.
drivers/net/wireless/virt_wifi.c line 98
Quoted 6 lines
drivers/net/wireless/virt_wifi.c new file +6
-0+static struct ieee80211_supported_band band_5ghz = { + .channels = &channel_5ghz, + .bitrates = bitrates_5ghz, + .band =NL80211_BAND_5GHZ , + .n_channels =1 , + .n_bitrates =3 ,
and ARRAY_SIZE
drivers/net/wireless/virt_wifi.c line 69
Quoted 6 lines
drivers/net/wireless/virt_wifi.c new file +6
-0+ .ht_cap = { + .ht_supported = true, + }, + .vht_cap = { + .vht_supported = true, + },
and HT/VHT
drivers/net/wireless/virt_wifi.c line 108
Quoted 2 lines
drivers/net/wireless/virt_wifi.c new file +2
-0+/** Assigned at module init. Guaranteed locally-administered and unicast. */ +static u8 fake_router_bssid[ETH_ALEN ] = {};
Maybe make that __ro_after_init?
It doesn't matter that much, but is a good signal.
Quoted 8 lines
drivers/net/wireless/virt_wifi.c new file +8
-0+static void virt_wifi_scan_result (struct work_struct *work) +{ +char ssid[] ="__VirtWifi" ; +struct cfg80211_bss *informed_bss; +struct virt_wifi_priv *priv = + container_of(work, struct virt_wifi_priv, + scan_result.work); +struct wiphy *wiphy =priv_to_wiphy (priv);
drivers/net/wireless/virt_wifi.c line 143
Quoted 10 lines
drivers/net/wireless/virt_wifi.c new file +10
-0+struct cfg80211_inform_bss mock_inform_bss = { + .chan = &channel_5ghz, + .scan_width =NL80211_BSS_CHAN_WIDTH_20 , + .signal =-60 , + .boottime_ns =ktime_get_boot_ns (), + }; + + ssid[0 ] =WLAN_EID_SSID ; +/* size of the array minus null terminator, length byte, tag byte */ + ssid[1 ] =sizeof (ssid) -3 ;
Seems like you could make that const?
struct {
u8 tag;
u8 len;
u8 ssid[8];
} ssid = { .tag = 0, .len = 8, .ssid = "VirtWifi" };or something like that?
Hmm, maybe that doesn't work.. whatever, not really important.
Quoted 7 lines
drivers/net/wireless/virt_wifi.c new file +7
-0+ informed_bss =cfg80211_inform_bss_data (wiphy, &mock_inform_bss, +CFG80211_BSS_FTYPE_PRESP , + fake_router_bssid, + mock_inform_bss.boottime_ns, +WLAN_CAPABILITY_ESS ,0 , ssid, +/* Truncate before the null. */ +sizeof (ssid) -1 ,GFP_KERNEL );
drivers/net/wireless/virt_wifi.c line 161
Quoted 10 lines
drivers/net/wireless/virt_wifi.c new file +10
-0+cfg80211_put_bss (wiphy, informed_bss); + + informed_bss =cfg80211_inform_bss_data (wiphy, &mock_inform_bss, +CFG80211_BSS_FTYPE_BEACON , + fake_router_bssid, + mock_inform_bss.boottime_ns, +WLAN_CAPABILITY_ESS ,0 , ssid, +/* Truncate before the null. */ +sizeof (ssid) -1 ,GFP_KERNEL ); +cfg80211_put_bss (wiphy, informed_bss);
Hmm, what's the point of doing it twice? You don't really need to receive both PRESP/BEACON, just one is sufficient.
Review
Quoted 2 lines
(quoted diff) +2
-0+ schedule_delayed_work(&priv->scan_complete, HZ * 2); +}
I don't think you need to make that async again.
drivers/net/wireless/virt_wifi.c line 186
Quoted 11 lines
drivers/net/wireless/virt_wifi.c new file +11
-0+static int virt_wifi_connect (struct wiphy *wiphy,struct net_device *netdev, +struct cfg80211_connect_params *sme) +{ +struct virt_wifi_priv *priv =wiphy_priv (wiphy); +bool could_schedule; + +if (priv->being_deleted) +return -EBUSY ; + +if (sme->bssid && !ether_addr_equal (sme->bssid, fake_router_bssid)) +return -EINVAL ;
If you wanted to be more "real", you'd accept this and then check it in the connect worker, rejecting it there if mismatched.
Quoted 5 lines
drivers/net/wireless/virt_wifi.c new file +5
-0+static int virt_wifi_get_station (struct wiphy *wiphy,struct net_device *dev, +const u8 *mac,struct station_info *sinfo) +{ +wiphy_debug (wiphy,"get_station\n" ); + sinfo->filled = BIT(NL80211_STA_INFO_TX_PACKETS) |
drivers/net/wireless/virt_wifi.c line 244
Quoted 10 lines
drivers/net/wireless/virt_wifi.c new file +10
-0+BIT (NL80211_STA_INFO_TX_FAILED ) |BIT (NL80211_STA_INFO_SIGNAL ) | +BIT (NL80211_STA_INFO_TX_BITRATE ); + sinfo->tx_packets =1 ; + sinfo->tx_failed =0 ; + sinfo->signal =-60 ; + sinfo->txrate = (struct rate_info ) { + .legacy =10 ,/* units are 100kbit/s */ + }; +return 0 ; +}
You should check that mac is fake_router_bssid, and otherwise return not found (-ENOENT), IIRC.
drivers/net/wireless/virt_wifi.c line 328
Quoted 8 lines
drivers/net/wireless/virt_wifi.c new file +8
-0+static netdev_tx_t virt_wifi_start_xmit (struct sk_buff *skb, +struct net_device *dev) +{ +struct virt_wifi_netdev_priv *priv =netdev_priv (dev); +struct virt_wifi_priv *w_priv =wiphy_priv (dev->ieee80211_ptr->wiphy); + +if (!w_priv->is_connected) +return NETDEV_TX_BUSY ;
I think you should just drop the frame.
drivers/net/wireless/virt_wifi.c line 368
Quoted 2 lines
drivers/net/wireless/virt_wifi.c new file +2
-0+/* Called under rcu_read_lock() from netif_receive_skb */ +static rx_handler_result_t virt_wifi_rx_handler (struct sk_buff **pskb)
FWIW, the stuff beyond this point, especially the netlink, I'm less familiar with.
Quoted 1 line
+/** Called with rtnl lock held. */
/** is usually used only for kernel-doc
johannes