Issue #1792

WiFi connection established via RepWifi not correctly reported by the Android Framework to user-space apps

Added by Fil Bergamo about 1 month ago. Updated 6 days ago.

Status:In Progress Start date:04/12/2017
Priority:Normal Due date:05/19/2017
Assignee:Fil Bergamo % Done:

90%

Category:Wi-Fi
Target version:Replicant 6.0
Resolution: Device:

Description

It is a suggested best practice for Android apps to check for the connectivity status of the device before performing any network-related task.
To do so, the Android Framework exposes some methods that apps can use to retrieve the available network connections, and their status.

When a WiFi connection is established via RepWifi app, although perfectly working (ping, dns resolution, web browsing), the framework still presents the WiFi connection as disabled or disconnected to the user-space apps.
This misleads apps that rely on this mechanism, so that they prevent the user from doing network-related tasks.

E.g. F-Droid prevents the user from updating the repos while connected to WiFi via RepWifi app, because it detects the WiFi connection as inactive.

A possible solution is to modify the designated system service in the Android Framework that reports the connection status to client apps.
Some research has already been done in this field.
A promising approach seems to properly modify the class "com.android.ConnectivityService.java" to read the ip routing tables, and detect the route established by RepWifi, and if present, modify the status of the corresponding "NetworkInfo" instance returned to calling applications.

History

Updated by Wolfgang Wiedmeyer about 1 month ago

Thanks for summarizing the issue!

The same issue affects USB networking (reverse tethering) and it's likely that a fix for this can be used to fix the issue for USB networking as well.

Updated by Fil Bergamo 28 days ago

  • % Done changed from 0 to 10

Got everything set up to do the job.
I'm expecting to start working on it in one week. This doesn't guarantee any successful or quick resolution, though.

Updated by Fil Bergamo 18 days ago

  • % Done changed from 10 to 20

Debugging Android Java framework in Eclipse works well enough, following this tutorial and this one
Thanks to that I'm now working on the issue fast and comfortably enough, and I'm now able to say that workarounds are definitely possible.
What I plan to do is create a subclass of NetworkInfo, designing it to specifically report the connection created via RepWifi, and override the default behaviour of the "proxy" methods in ConnectivityManager so that if a wifi connection on interface wlan0 is active, it gets added to the ones returned by the underlying system service.
This workaround should be enough to intercept the vast majority of Apps querying the system for connectivity status.

A deeper (and smarter) fix would need investigating the inner workings of the underlying system services, which can be done, but requires time and concentration, and I currently lack both, unfortunately.
So I will limit myself in putting up the aforementioned workaround, for now.

To summarize, I'm very optimistic about overcoming this issue.
Updates are coming.

Updated by Fil Bergamo 18 days ago

  • Status changed from New to In Progress

Updated by Fil Bergamo 14 days ago

  • % Done changed from 20 to 50

Just a quick update.
It works.
Changes to the "ConnectivityService" do compile, and they do work as expected.
I was able to have F-Droid update the repos via the wifi connection established by RepWifi.
So, yes. It can be done.
In the upcoming days, I'll be working on cleaning up my test code, to make it suitable for production.

Updated by Fil Bergamo 10 days ago

  • Due date changed from 05/12/2017 to 05/19/2017
  • % Done changed from 50 to 80

Good GNUs, everybody!
Everything works here!

I did the following:
  • Added an internal class RepWifiConnection to the class NetworkInfo, under frameworks/base/core/java/android/net.
  • RepWifiConnection has a static method getNetworkInfo that returns an instance of NetworkInfo, whose internal state is set up according to the state of the connection created via RepWifi.
  • The connection state is determined inside RepWifiConnection by issuing the following shell commands:
    ip link
    determines whether the dongle is plugged in or not, which translates to the NetworkInfo instance being "avalable" or not.
    pidof wpa_supplicant
    determines if wpa_supplicant is running. If it's not, then the NetworkInfo instance is presented as DISCONNECTED.
    ip route
    determines whether a route for the interface wlan0 exists. If a route exists, and it has a gateway, then the NetworkInfo instance will be presented as CONNECTED.
  • Added a public static method getRepWifiNetworkInfo to the NetworkInfo class, which is a proxy to expose the internal method RepWifiConnection.getNetworkInfo.
And finally:
  • Modified the ConnectivityService class under frameworks/base/services/core/java/com/android/server, updating the method getActiveNetworkInfo which is the one that gets called in most cases when apps query for the connection state. I added a call to NetworkInfo.getRepWifiNetworkInfo(), performed only if the default behaviour is returning no connection, otherwise the default behaviour is preserved.

ConnectivityService is the system service that answers queries about the network state coming from the Apps.
In most cases the method that gets called under the hood is getActiveNetworkInfo which I modified as reported above.

The resulting behaviour of the system service is the following (tested with F-Droid, clicking on the "update repos" button):
- when the wifi dongle is unplugged, ConnectivityService reports no connections, so F-droid shows "Cannot update. Are you connected to the internet?"
- the same happens when the dongle is plugged, but no connection has been established via RepWifi.
- when the dongle is plugged AND RepWifi has successfully put up a connection, then ConnectivityService returns to F-Droid a valid NetworkInfo instance whose state is set as CONNECTED. As such, F-Droid doesn't complain any more, and succeeds updating the repos, happily using the RepWifi connection.
- F-Droid is even able to download and install apps using the RepWifi connection (but this used to work even before).

I tried to keep modifications as compact as possible, which makes it easy to remove them and restore the "original" behaviour.

Now things must be tidied up a bit and get packed nicely.
Then some more test cases need to be brought up.
In theory the vast majority of the apps out there should be covered by these modifications, but I only tested them against F-droid, because I don't know of any other app that refuses to work well with RepWifi.

If anybody knows anything about other apps presenting the same issue, please let me know, so that I can test them to make sure they work fine with my methods.

I'm then going to submit a patch immediately after a few successful tests.

Updated by jc gargma 10 days ago

If anybody knows anything about other apps presenting the same issue, please let me know, so that I can test them to make sure they work fine with my methods.

Antox and OpenVPN for Android are the other apps I know of with this issue.
Antox states "Internet not found" regardless of whether "Use only WiFi" is enabled.
OpenVPN for Android perpetually delays connection attempts stating "waiting for active internet connection"

Updated by Fil Bergamo 6 days ago

  • % Done changed from 80 to 90

I did introduce some other adaptations to NetworkInfo, in order to include support for reverse-tethering.
Basically the same things explained for the connection established by RepWifi apply for the connection established via the reverse-tether scripts.

Now I have everything ready for a patch.
I'm struggling with git to make it spit out the needed diff file, but for the rest, everything is ready.

Updated by Fil Bergamo 6 days ago

jc gargma wrote:

If anybody knows anything about other apps presenting the same issue, please let me know, so that I can test them to make sure they work fine with my methods.

Antox and OpenVPN for Android are the other apps I know of with this issue.
Antox states "Internet not found" regardless of whether "Use only WiFi" is enabled.
OpenVPN for Android perpetually delays connection attempts stating "waiting for active internet connection"

Thanks for suggesting those apps!
I downloaded them and tried to verify the patch against them.
I dind't go very far, as I don't have any VPN service to use with "OpenVPN for Android".
Also, I'm new to Tox, so I still have to understand how it works, make a test account and proceed with testing..
I'll post any update here.

Thanks again.

Also available in: Atom PDF