Repository navigation
[Android] NetworkInterface.GetAllNetworkInterfaces() returns strange values on Android 11 + IPv6 #6973
Description
Activity
Issue moved from dotnet/maui#6649
- Please respond to @v-longmin.
From @v-longmin on Friday, April 29, 2022 7:51:12 AM
Verified repro on Android 12.0 with VS 17.3.0 Preview 1.0 [32427.455.main]. Repro with above project.
Issue moved from dotnet/maui#6649
- Please respond to @Eilon.
From @Eilon on Friday, April 29, 2022 5:41:03 PM
@jonathanpeppers - should this go to Xamarin Android? Or dotnet/runtime?
Issue moved from dotnet/maui#6649
- Please respond to @jonathanpeppers.
From @jonathanpeppers on Friday, April 29, 2022 6:26:18 PM
I would send this to xamarin/xamarin-android, thanks.
We have some code there that passes these values to the BCL.
- addedArea: App RuntimeIssues in `libmonodroid.so`.Issues in `libmonodroid.so`.
on Apr 29, 2022 @jonathanpeppers @grendello It seems that this works normally on some older phones with Android 8.0 but returns bad values on newer versions of Android. I hope that helps.
Any idea when this might be fixed?
@philipag I'm afraid it might be hard to fix. Newer Android versions clamped down on the information available to "untrusted" apps and some information might be available only via the relevant Java API (you can access it via our bindings, of course).
GetAllNetworkInterfaces()eventually uses Linux netlink protocol which may be severely restricted for some versions of Android :(Please follow the steps below and attach the resulting
logcat.txt, it might help us see system errors, if any:$ adb shell setprop debug.mono.log default,netlink $ adb logcat -G 16M $ adb logcat -c # Run the app here $ adb logcat -d > logcat.txt
@grendello Here is the resulting log. The app name is "testapp".
@philipag thanks! Unfortunately, it seems our netlink code isn't used in your app. The only netlink messages I see are:
07-05 14:01:29.315 957 2594 W NetlinkSocket: maximum read 07-05 14:01:29.315 957 2594 W NetlinkSocket: maximum read 07-05 14:01:29.328 957 2594 W NetlinkSocket: maximum read 07-05 14:01:29.328 957 2594 W NetlinkSocket: maximum read 07-05 14:01:29.378 957 2594 W NetlinkSocket: maximum read 07-05 14:01:29.379 957 2594 W NetlinkSocket: maximum read 07-05 14:01:29.416 957 2594 W NetlinkSocket: maximum read 07-05 14:01:29.691 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.400 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.411 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.411 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.418 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.419 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.420 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.420 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.424 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.425 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.425 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.425 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.426 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.426 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.426 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.426 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.426 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.426 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.426 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.445 957 2594 W NetlinkSocket: maximum read 07-05 14:01:31.446 957 2594 W NetlinkSocket: maximum read 07-05 14:01:34.496 957 2594 W NetlinkSocket: maximum read 07-05 14:01:34.498 957 2594 W NetlinkSocket: maximum read 07-05 14:01:34.498 957 2594 W NetlinkSocket: maximum read 07-05 14:01:34.498 957 2594 W NetlinkSocket: maximum readWhich looks like some kind of throttling (?), possibly triggered by the new network interface enumeration code in dotnet runtime.
@simonrozsival would you mind taking a look?
Reacted by Šimon Rozsíval- addedArea: Mono RuntimeMono-related issues: BCL bugs, AOT issues, etc.Mono-related issues: BCL bugs, AOT issues, etc.and removedArea: App RuntimeIssues in `libmonodroid.so`.Issues in `libmonodroid.so`.
on Jul 5, 2022 I can confirm that
GetAllNetworkInterfaces()returns invalid data on new Android versions. As a workaround, it's possible to use platform specific APIs to enumerate the interfaces:var ifaces = Java.Net.NetworkInterface.NetworkInterfaces; while (ifaces.HasMoreElements) { var iface = ifaces.NextElement().JavaCast<Java.Net.NetworkInterface>(); // ... }
.NET internally uses the libc
getifaddrsfunction to get the network interfaces information. We should consider rewriting this functionality with JNI and get the infromation from the Android APIs.@simonrozsival XA has an implementation of
getifaddrsbased on the netlink protocol, would you be able to test if it works? I wonder if it's the bionic'sgetifaddrsthat's limited and not netlink itself (it's likely that both are limited and one has to go through Java interfaces which have correct privileges to obtain the information)@grendello I'll give it a try.
Reacted by Marek HabersackThe network interfaces' information is accessible through netlink so I ported the XA implementation into dotnet/runtime.
- added a commit that references this issue
on Jul 12, 2022 - ghost locked as resolved and limited conversation to collaborators
on Aug 12, 2022
Issue moved from dotnet/maui#6649
From @MartinRothschink on Friday, April 29, 2022 7:05:05 AM
Description
All tests have been done on a Xaomi Pad 5 (physical device, Android 11).
If target sdk is set to < 30, the values returned by NetworkInterface.GetAllNetworkInterfaces() seem valid:

If target sdk is set to 31, the values returned by NetworkInterface.GetAllNetworkInterfaces() are outside of the enum ranges and name/description are empty. This screen shot shows the situation if IPv6 is not enabled in the router:

If IPv6 is enabled in the router, NetworkInterfaceType and OperationalStatus values are even worse.

Steps to Reproduce
MainPage.xaml.cs
MainPage.xaml
Version with bug
Release Candidate 2 (current)
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android 10/11 (target sdk >= 30)
Did you find any workaround?
Currently I have to ignore the values for OperationalStatus and use the interface with valid UnicastAddresses.
Relevant log output
No response