Support LocalNetworkAgent for Tethering

The feature will be gradually enabled via flag rollout.

Sample ip rule output:
0:	from all lookup local
10000:	from all fwmark 0xc0000/0xd0000 lookup legacy_system
11000:	from all iif lo oif dummy0 uidrange 0-0 lookup dummy0
11000:	from all iif lo oif rmnet1 uidrange 0-0 lookup rmnet1
11000:	from all iif lo oif wlan0 uidrange 0-0 lookup wlan0
16000:	from all fwmark 0x10063/0x1ffff iif lo lookup local_network
16000:	from all fwmark 0x10064/0x1ffff iif lo lookup rmnet1
16000:	from all fwmark 0x10066/0x1ffff iif lo lookup wlan0
16000:	from all fwmark 0x10063/0x1ffff iif lo lookup wlan0
17000:	from all iif lo oif dummy0 lookup dummy0
17000:	from all iif lo oif rmnet1 lookup rmnet1
17000:	from all iif lo oif wlan0 lookup wlan0
18000:	from all fwmark 0x0/0x10000 lookup legacy_system
19000:	from all fwmark 0x0/0x10000 lookup legacy_network
20000:	from all fwmark 0x0/0x10000 lookup local_network
20000:	from all fwmark 0x0/0x10000 lookup wlan0
21000:	from all iif wlan0 lookup rmnet1
23000:	from all fwmark 0x64/0x1ffff iif lo lookup rmnet1
23000:	from all fwmark 0x66/0x1ffff iif lo lookup wlan0
31000:	from all fwmark 0x0/0xffff iif lo lookup rmnet1
32000:	from all unreachable

Test: adb shell device_config put tethering tethering_local_network_agent 1
Test: atest TetheringTests:android.net.ip.IpServerTest TetheringManagerTest
Bug: 349487600
Change-Id: Iaa88dde645da917149fec0d44fff796c551bcc7e
This commit is contained in:
Junyu Lai 2024-12-13 15:47:44 +08:00
commit d55bd89bd6

View file

@ -16,11 +16,25 @@
package android.net.connectivity;
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_CONGESTED;
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING;
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.LocalNetworkConfig;
import android.net.NetworkAgent;
import android.net.NetworkAgentConfig;
import android.net.NetworkCapabilities;
import android.net.NetworkScore;
import android.os.Build;
import android.os.IBinder;
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
/**
@ -57,4 +71,36 @@ public class ConnectivityInternalApiUtil {
final ConnectivityManager cm = ctx.getSystemService(ConnectivityManager.class);
return cm.getRoutingCoordinatorService();
}
/**
* Create a NetworkAgent instance to be used by Tethering.
* @param ctx the context
* @return an instance of the {@code NetworkAgent}
*/
// TODO: Expose LocalNetworkConfig related APIs and delete this method. This method is
// only here because on R Tethering is installed and not Connectivity, requiring all
// shared classes to be public API. LocalNetworkConfig is not public yet, but it will
// only be used by Tethering on V+ so it's fine.
@SuppressLint("WrongConstant")
@NonNull
public static NetworkAgent buildTetheringNetworkAgent(@NonNull Context ctx,
@NonNull Looper looper, @NonNull String logTag, int transportType,
@NonNull LinkProperties lp) {
final NetworkCapabilities.Builder builder = new NetworkCapabilities.Builder()
.addCapability(NET_CAPABILITY_NOT_METERED)
.addCapability(NET_CAPABILITY_NOT_ROAMING)
.addCapability(NET_CAPABILITY_NOT_CONGESTED)
.addCapability(NET_CAPABILITY_NOT_SUSPENDED)
.addTransportType(transportType);
// TODO: Change to use the constant definition. Flags.netCapabilityLocalNetwork() was not
// fully rolled out but the service will still process this capability, set it anyway.
builder.addCapability(36 /* NET_CAPABILITY_LOCAL_NETWORK */);
final NetworkCapabilities caps = builder.build();
final NetworkAgentConfig nac = new NetworkAgentConfig.Builder().build();
return new NetworkAgent(ctx, looper, logTag, caps, lp,
new LocalNetworkConfig.Builder().build(), new NetworkScore.Builder()
.setKeepConnectedReason(NetworkScore.KEEP_CONNECTED_LOCAL_NETWORK)
.build(), nac, null /* provider */) {
};
}
}