Add support for Ethernet tethering

Ethernet tethering can be started via
startTethering(TETHERING_ETHERNET).

Test: flashed, enabled ethernet tethering, verified internet access on
      downstream.
Bug: 130840861

Merged-In: I34842acd94b972e440c3622f7617df10c18acf65
Change-Id: I34842acd94b972e440c3622f7617df10c18acf65
(cherry-pick with conflicts in test-current.txt)
This commit is contained in:
Remi NGUYEN VAN
2020-01-24 22:57:09 +09:00
parent 58527ba974
commit f2f3f3e345
9 changed files with 129 additions and 4 deletions

View File

@@ -1577,6 +1577,7 @@ package android.content {
field public static final String BUGREPORT_SERVICE = "bugreport";
field public static final String CONTENT_SUGGESTIONS_SERVICE = "content_suggestions";
field public static final String CONTEXTHUB_SERVICE = "contexthub";
field public static final String ETHERNET_SERVICE = "ethernet";
field public static final String EUICC_CARD_SERVICE = "euicc_card";
field public static final String HDMI_CONTROL_SERVICE = "hdmi_control";
field public static final String NETD_SERVICE = "netd";
@@ -4382,6 +4383,19 @@ package android.net {
method @Deprecated public void onUpstreamChanged(@Nullable android.net.Network);
}
public class EthernetManager {
method @NonNull public android.net.EthernetManager.TetheredInterfaceRequest requestTetheredInterface(@NonNull android.net.EthernetManager.TetheredInterfaceCallback);
}
public static interface EthernetManager.TetheredInterfaceCallback {
method public void onAvailable(@NonNull String);
method public void onUnavailable();
}
public static class EthernetManager.TetheredInterfaceRequest {
method public void release();
}
public class InvalidPacketException extends java.lang.Exception {
ctor public InvalidPacketException(int);
field public static final int ERROR_INVALID_IP_ADDRESS = -21; // 0xffffffeb
@@ -4755,6 +4769,7 @@ package android.net {
field public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
field public static final String EXTRA_ERRORED_TETHER = "erroredArray";
field public static final int TETHERING_BLUETOOTH = 2; // 0x2
field public static final int TETHERING_ETHERNET = 5; // 0x5
field public static final int TETHERING_INVALID = -1; // 0xffffffff
field public static final int TETHERING_NCM = 4; // 0x4
field public static final int TETHERING_USB = 1; // 0x1

View File

@@ -653,6 +653,7 @@ package android.content {
method public void setContentCaptureOptions(@Nullable android.content.ContentCaptureOptions);
field public static final String BUGREPORT_SERVICE = "bugreport";
field public static final String CONTENT_CAPTURE_MANAGER_SERVICE = "content_capture";
field public static final String ETHERNET_SERVICE = "ethernet";
field public static final String NETWORK_STACK_SERVICE = "network_stack";
field public static final String PERMISSION_SERVICE = "permission";
field public static final String ROLLBACK_SERVICE = "rollback";
@@ -1392,6 +1393,19 @@ package android.net {
field public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT = "android.net.extra.CAPTIVE_PORTAL_USER_AGENT";
}
public class EthernetManager {
method @NonNull public android.net.EthernetManager.TetheredInterfaceRequest requestTetheredInterface(@NonNull android.net.EthernetManager.TetheredInterfaceCallback);
}
public static interface EthernetManager.TetheredInterfaceCallback {
method public void onAvailable(@NonNull String);
method public void onUnavailable();
}
public static class EthernetManager.TetheredInterfaceRequest {
method public void release();
}
public final class IpPrefix implements android.os.Parcelable {
ctor public IpPrefix(@NonNull java.net.InetAddress, @IntRange(from=0, to=128) int);
ctor public IpPrefix(@NonNull String);
@@ -1540,6 +1554,7 @@ package android.net {
field public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
field public static final String EXTRA_ERRORED_TETHER = "erroredArray";
field public static final int TETHERING_BLUETOOTH = 2; // 0x2
field public static final int TETHERING_ETHERNET = 5; // 0x5
field public static final int TETHERING_INVALID = -1; // 0xffffffff
field public static final int TETHERING_NCM = 4; // 0x4
field public static final int TETHERING_USB = 1; // 0x1

View File

@@ -4022,16 +4022,16 @@ public abstract class Context {
public static final String LOWPAN_SERVICE = "lowpan";
/**
* Use with {@link #getSystemService(String)} to retrieve a {@link
* android.net.EthernetManager} for handling management of
* Ethernet access.
* Use with {@link #getSystemService(String)} to retrieve a {@link android.net.EthernetManager}
* for handling management of Ethernet access.
*
* @see #getSystemService(String)
* @see android.net.EthernetManager
*
* @hide
*/
@UnsupportedAppUsage
@SystemApi
@TestApi
public static final String ETHERNET_SERVICE = "ethernet";
/**

View File

@@ -17,7 +17,9 @@
package android.net;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.annotation.TestApi;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.os.Handler;
@@ -32,6 +34,8 @@ import java.util.Objects;
*
* @hide
*/
@SystemApi
@TestApi
@SystemService(Context.ETHERNET_SERVICE)
public class EthernetManager {
private static final String TAG = "EthernetManager";
@@ -62,12 +66,14 @@ public class EthernetManager {
/**
* A listener interface to receive notification on changes in Ethernet.
* @hide
*/
public interface Listener {
/**
* Called when Ethernet port's availability is changed.
* @param iface Ethernet interface name
* @param isAvailable {@code true} if Ethernet port exists.
* @hide
*/
@UnsupportedAppUsage
void onAvailabilityChanged(String iface, boolean isAvailable);
@@ -78,6 +84,7 @@ public class EthernetManager {
* Applications will almost always want to use
* {@link android.content.Context#getSystemService Context.getSystemService()} to retrieve
* the standard {@link android.content.Context#ETHERNET_SERVICE Context.ETHERNET_SERVICE}.
* @hide
*/
public EthernetManager(Context context, IEthernetManager service) {
mContext = context;
@@ -87,6 +94,7 @@ public class EthernetManager {
/**
* Get Ethernet configuration.
* @return the Ethernet Configuration, contained in {@link IpConfiguration}.
* @hide
*/
@UnsupportedAppUsage
public IpConfiguration getConfiguration(String iface) {
@@ -99,6 +107,7 @@ public class EthernetManager {
/**
* Set Ethernet configuration.
* @hide
*/
@UnsupportedAppUsage
public void setConfiguration(String iface, IpConfiguration config) {
@@ -111,6 +120,7 @@ public class EthernetManager {
/**
* Indicates whether the system currently has one or more Ethernet interfaces.
* @hide
*/
@UnsupportedAppUsage
public boolean isAvailable() {
@@ -121,6 +131,7 @@ public class EthernetManager {
* Indicates whether the system has given interface.
*
* @param iface Ethernet interface name
* @hide
*/
@UnsupportedAppUsage
public boolean isAvailable(String iface) {
@@ -135,6 +146,7 @@ public class EthernetManager {
* Adds a listener.
* @param listener A {@link Listener} to add.
* @throws IllegalArgumentException If the listener is null.
* @hide
*/
@UnsupportedAppUsage
public void addListener(Listener listener) {
@@ -153,6 +165,7 @@ public class EthernetManager {
/**
* Returns an array of available Ethernet interface names.
* @hide
*/
@UnsupportedAppUsage
public String[] getAvailableInterfaces() {
@@ -167,6 +180,7 @@ public class EthernetManager {
* Removes a listener.
* @param listener A {@link Listener} to remove.
* @throws IllegalArgumentException If the listener is null.
* @hide
*/
@UnsupportedAppUsage
public void removeListener(Listener listener) {

View File

@@ -136,6 +136,12 @@ public class TetheringManager {
*/
public static final int TETHERING_NCM = 4;
/**
* Ethernet tethering type.
* @see #startTethering(TetheringRequest, Executor, StartTetheringCallback)
*/
public static final int TETHERING_ETHERNET = 5;
public static final int TETHER_ERROR_NO_ERROR = 0;
public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;

View File

@@ -93,6 +93,8 @@ public class IpServer extends StateMachine {
private static final int WIFI_HOST_IFACE_PREFIX_LENGTH = 24;
private static final String WIFI_P2P_IFACE_ADDR = "192.168.49.1";
private static final int WIFI_P2P_IFACE_PREFIX_LENGTH = 24;
private static final String ETHERNET_IFACE_ADDR = "192.168.50.1";
private static final int ETHERNET_IFACE_PREFIX_LENGTH = 24;
// TODO: have PanService use some visible version of this constant
private static final String BLUETOOTH_IFACE_ADDR = "192.168.44.1";
@@ -426,6 +428,10 @@ public class IpServer extends StateMachine {
} else if (mInterfaceType == TetheringManager.TETHERING_WIFI_P2P) {
srvAddr = (Inet4Address) parseNumericAddress(WIFI_P2P_IFACE_ADDR);
prefixLen = WIFI_P2P_IFACE_PREFIX_LENGTH;
} else if (mInterfaceType == TetheringManager.TETHERING_ETHERNET) {
// TODO: randomize address for tethering too, similarly to wifi
srvAddr = (Inet4Address) parseNumericAddress(ETHERNET_IFACE_ADDR);
prefixLen = ETHERNET_IFACE_PREFIX_LENGTH;
} else {
// BT configures the interface elsewhere: only start DHCP.
// TODO: make all tethering types behave the same way, and delete the bluetooth

View File

@@ -30,6 +30,7 @@ import static android.net.TetheringManager.EXTRA_ACTIVE_TETHER;
import static android.net.TetheringManager.EXTRA_AVAILABLE_TETHER;
import static android.net.TetheringManager.EXTRA_ERRORED_TETHER;
import static android.net.TetheringManager.TETHERING_BLUETOOTH;
import static android.net.TetheringManager.TETHERING_ETHERNET;
import static android.net.TetheringManager.TETHERING_INVALID;
import static android.net.TetheringManager.TETHERING_NCM;
import static android.net.TetheringManager.TETHERING_USB;
@@ -68,6 +69,7 @@ import android.content.IntentFilter;
import android.content.res.Resources;
import android.hardware.usb.UsbManager;
import android.net.ConnectivityManager;
import android.net.EthernetManager;
import android.net.IIntResultListener;
import android.net.INetd;
import android.net.ITetheringEventCallback;
@@ -112,6 +114,7 @@ import android.util.SparseArray;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.IndentingPrintWriter;
import com.android.internal.util.MessageUtils;
@@ -214,6 +217,13 @@ public class Tethering {
private boolean mDataSaverEnabled = false;
private String mWifiP2pTetherInterface = null;
@GuardedBy("mPublicSync")
private EthernetManager.TetheredInterfaceRequest mEthernetIfaceRequest;
@GuardedBy("mPublicSync")
private String mConfiguredEthernetIface;
@GuardedBy("mPublicSync")
private EthernetCallback mEthernetCallback;
public Tethering(TetheringDependencies deps) {
mLog.mark("Tethering.constructed");
mDeps = deps;
@@ -464,6 +474,10 @@ public class Tethering {
result = setNcmTethering(enable);
sendTetherResult(listener, result);
break;
case TETHERING_ETHERNET:
result = setEthernetTethering(enable);
sendTetherResult(listener, result);
break;
default:
Log.w(TAG, "Invalid tether type.");
sendTetherResult(listener, TETHER_ERROR_UNKNOWN_IFACE);
@@ -540,6 +554,57 @@ public class Tethering {
}, BluetoothProfile.PAN);
}
private int setEthernetTethering(final boolean enable) {
final EthernetManager em = (EthernetManager) mContext.getSystemService(
Context.ETHERNET_SERVICE);
synchronized (mPublicSync) {
if (enable) {
mEthernetCallback = new EthernetCallback();
mEthernetIfaceRequest = em.requestTetheredInterface(mEthernetCallback);
} else {
if (mConfiguredEthernetIface != null) {
stopEthernetTetheringLocked();
mEthernetIfaceRequest.release();
}
mEthernetCallback = null;
}
}
return TETHER_ERROR_NO_ERROR;
}
private void stopEthernetTetheringLocked() {
if (mConfiguredEthernetIface == null) return;
changeInterfaceState(mConfiguredEthernetIface, IpServer.STATE_AVAILABLE);
stopTrackingInterfaceLocked(mConfiguredEthernetIface);
mConfiguredEthernetIface = null;
}
private class EthernetCallback implements EthernetManager.TetheredInterfaceCallback {
@Override
public void onAvailable(String iface) {
synchronized (mPublicSync) {
if (this != mEthernetCallback) {
// Ethernet callback arrived after Ethernet tethering stopped. Ignore.
return;
}
maybeTrackNewInterfaceLocked(iface, TETHERING_ETHERNET);
changeInterfaceState(iface, IpServer.STATE_TETHERED);
mConfiguredEthernetIface = iface;
}
}
@Override
public void onUnavailable() {
synchronized (mPublicSync) {
if (this != mEthernetCallback) {
// onAvailable called after stopping Ethernet tethering.
return;
}
stopEthernetTetheringLocked();
}
}
}
int tether(String iface) {
return tether(iface, IpServer.STATE_TETHERED);
}
@@ -590,6 +655,7 @@ public class Tethering {
stopTethering(TETHERING_WIFI_P2P);
stopTethering(TETHERING_USB);
stopTethering(TETHERING_BLUETOOTH);
stopTethering(TETHERING_ETHERNET);
}
int getLastTetherError(String iface) {

View File

@@ -19,6 +19,7 @@ android_test {
certificate: "platform",
srcs: [
"src/**/*.java",
"src/**/*.kt",
],
test_suites: [
"device-tests",

View File

@@ -16,6 +16,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.networkstack.tethering.tests.unit">
<uses-permission android:name="android.permission.TETHER_PRIVILEGED"/>
<application android:debuggable="true">
<uses-library android:name="android.test.runner" />
</application>