Add enable and disable ip serving common functions

This is a op CL which prepare for refactor.

Bug: 185649441
Bug: 189410000
Bug: 178116595

Test: atest TetheringTests
Change-Id: I270ff43c01736b9d18bd40a8e88e1379e9e4e863
This commit is contained in:
markchien
2021-06-09 15:53:34 +08:00
parent cab01b7ebc
commit 76e2e1c702

View File

@@ -520,24 +520,35 @@ public class Tethering {
} }
} }
// This method needs to exist because TETHERING_BLUETOOTH and TETHERING_WIGIG can't use
// enableIpServing.
private void startOrStopIpServer(final String iface, boolean enabled) {
// TODO: do not listen to USB interface state changes. USB tethering is driven only by
// USB_ACTION broadcasts.
if (enabled) {
ensureIpServerStarted(iface);
} else {
ensureIpServerStopped(iface);
}
}
void interfaceStatusChanged(String iface, boolean up) { void interfaceStatusChanged(String iface, boolean up) {
// Never called directly: only called from interfaceLinkStateChanged. // Never called directly: only called from interfaceLinkStateChanged.
// See NetlinkHandler.cpp: notifyInterfaceChanged. // See NetlinkHandler.cpp: notifyInterfaceChanged.
if (VDBG) Log.d(TAG, "interfaceStatusChanged " + iface + ", " + up); if (VDBG) Log.d(TAG, "interfaceStatusChanged " + iface + ", " + up);
if (up) {
maybeTrackNewInterface(iface); final int type = ifaceNameToType(iface);
} else { if (!up && type != TETHERING_BLUETOOTH && type != TETHERING_WIGIG) {
if (ifaceNameToType(iface) == TETHERING_BLUETOOTH // Ignore usb interface down after enabling RNDIS.
|| ifaceNameToType(iface) == TETHERING_WIGIG) { // We will handle disconnect in interfaceRemoved.
stopTrackingInterface(iface); // Similarly, ignore interface down for WiFi. We monitor WiFi AP status
} else { // through the WifiManager.WIFI_AP_STATE_CHANGED_ACTION intent.
// Ignore usb0 down after enabling RNDIS. if (VDBG) Log.d(TAG, "ignore interface down for " + iface);
// We will handle disconnect in interfaceRemoved. return;
// Similarly, ignore interface down for WiFi. We monitor WiFi AP status
// through the WifiManager.WIFI_AP_STATE_CHANGED_ACTION intent.
if (VDBG) Log.d(TAG, "ignore interface down for " + iface);
}
} }
startOrStopIpServer(iface, up);
} }
void interfaceLinkStateChanged(String iface, boolean up) { void interfaceLinkStateChanged(String iface, boolean up) {
@@ -565,12 +576,12 @@ public class Tethering {
void interfaceAdded(String iface) { void interfaceAdded(String iface) {
if (VDBG) Log.d(TAG, "interfaceAdded " + iface); if (VDBG) Log.d(TAG, "interfaceAdded " + iface);
maybeTrackNewInterface(iface); startOrStopIpServer(iface, true /* enabled */);
} }
void interfaceRemoved(String iface) { void interfaceRemoved(String iface) {
if (VDBG) Log.d(TAG, "interfaceRemoved " + iface); if (VDBG) Log.d(TAG, "interfaceRemoved " + iface);
stopTrackingInterface(iface); startOrStopIpServer(iface, false /* enabled */);
} }
void startTethering(final TetheringRequestParcel request, final IIntResultListener listener) { void startTethering(final TetheringRequestParcel request, final IIntResultListener listener) {
@@ -734,7 +745,7 @@ public class Tethering {
private void stopEthernetTethering() { private void stopEthernetTethering() {
if (mConfiguredEthernetIface != null) { if (mConfiguredEthernetIface != null) {
stopTrackingInterface(mConfiguredEthernetIface); ensureIpServerStopped(mConfiguredEthernetIface);
mConfiguredEthernetIface = null; mConfiguredEthernetIface = null;
} }
if (mEthernetCallback != null) { if (mEthernetCallback != null) {
@@ -751,8 +762,7 @@ public class Tethering {
// Ethernet callback arrived after Ethernet tethering stopped. Ignore. // Ethernet callback arrived after Ethernet tethering stopped. Ignore.
return; return;
} }
maybeTrackNewInterface(iface, TETHERING_ETHERNET); enableIpServing(TETHERING_ETHERNET, iface, getRequestedState(TETHERING_ETHERNET));
changeInterfaceState(iface, getRequestedState(TETHERING_ETHERNET));
mConfiguredEthernetIface = iface; mConfiguredEthernetIface = iface;
} }
@@ -917,12 +927,14 @@ public class Tethering {
} else if (tetherState.lastState == IpServer.STATE_LOCAL_ONLY) { } else if (tetherState.lastState == IpServer.STATE_LOCAL_ONLY) {
localOnly.add(tetheringIface); localOnly.add(tetheringIface);
} else if (tetherState.lastState == IpServer.STATE_TETHERED) { } else if (tetherState.lastState == IpServer.STATE_TETHERED) {
if (cfg.isUsb(iface)) { switch (type) {
downstreamTypesMask |= (1 << TETHERING_USB); case TETHERING_USB:
} else if (cfg.isWifi(iface)) { case TETHERING_WIFI:
downstreamTypesMask |= (1 << TETHERING_WIFI); case TETHERING_BLUETOOTH:
} else if (cfg.isBluetooth(iface)) { downstreamTypesMask |= (1 << type);
downstreamTypesMask |= (1 << TETHERING_BLUETOOTH); break;
default:
// Do nothing.
} }
tethered.add(tetheringIface); tethered.add(tetheringIface);
} }
@@ -1047,17 +1059,13 @@ public class Tethering {
// For more explanation, see b/62552150 . // For more explanation, see b/62552150 .
if (!usbConnected && (mRndisEnabled || mNcmEnabled)) { if (!usbConnected && (mRndisEnabled || mNcmEnabled)) {
// Turn off tethering if it was enabled and there is a disconnect. // Turn off tethering if it was enabled and there is a disconnect.
tetherMatchingInterfaces(IpServer.STATE_AVAILABLE, TETHERING_USB); disableUsbIpServing(TETHERING_USB);
mEntitlementMgr.stopProvisioningIfNeeded(TETHERING_USB); mEntitlementMgr.stopProvisioningIfNeeded(TETHERING_USB);
} else if (usbConfigured && rndisEnabled) { } else if (usbConfigured && rndisEnabled) {
// Tether if rndis is enabled and usb is configured. // Tether if rndis is enabled and usb is configured.
final int type = getRequestedUsbType(false /* isNcm */); enableUsbIpServing(false /* isNcm */);
final int state = getRequestedState(type);
tetherMatchingInterfaces(state, type);
} else if (usbConfigured && ncmEnabled) { } else if (usbConfigured && ncmEnabled) {
final int type = getRequestedUsbType(true /* isNcm */); enableUsbIpServing(true /* isNcm */);
final int state = getRequestedState(type);
tetherMatchingInterfaces(state, type);
} }
mRndisEnabled = usbConfigured && rndisEnabled; mRndisEnabled = usbConfigured && rndisEnabled;
mNcmEnabled = usbConfigured && ncmEnabled; mNcmEnabled = usbConfigured && ncmEnabled;
@@ -1207,6 +1215,11 @@ public class Tethering {
} }
} }
private void enableIpServing(int tetheringType, String ifname, int ipServingMode) {
ensureIpServerStarted(ifname, tetheringType);
changeInterfaceState(ifname, ipServingMode);
}
private void disableWifiIpServingCommon(int tetheringType, String ifname, int apState) { private void disableWifiIpServingCommon(int tetheringType, String ifname, int apState) {
mLog.log("Canceling WiFi tethering request -" mLog.log("Canceling WiFi tethering request -"
+ " type=" + tetheringType + " type=" + tetheringType
@@ -1267,7 +1280,7 @@ public class Tethering {
} }
if (!TextUtils.isEmpty(ifname)) { if (!TextUtils.isEmpty(ifname)) {
maybeTrackNewInterface(ifname); ensureIpServerStarted(ifname);
changeInterfaceState(ifname, ipServingMode); changeInterfaceState(ifname, ipServingMode);
} else { } else {
mLog.e(String.format( mLog.e(String.format(
@@ -1282,18 +1295,17 @@ public class Tethering {
// - handles both enabling and disabling serving states // - handles both enabling and disabling serving states
// - only tethers the first matching interface in listInterfaces() // - only tethers the first matching interface in listInterfaces()
// order of a given type // order of a given type
private void tetherMatchingInterfaces(int requestedState, int interfaceType) { private void enableUsbIpServing(boolean isNcm) {
if (VDBG) { final int interfaceType = getRequestedUsbType(isNcm);
Log.d(TAG, "tetherMatchingInterfaces(" + requestedState + ", " + interfaceType + ")"); final int requestedState = getRequestedState(interfaceType);
}
String[] ifaces = null; String[] ifaces = null;
try { try {
ifaces = mNetd.interfaceGetList(); ifaces = mNetd.interfaceGetList();
} catch (RemoteException | ServiceSpecificException e) { } catch (RemoteException | ServiceSpecificException e) {
Log.e(TAG, "Error listing Interfaces", e); mLog.e("Cannot enableUsbIpServing due to error listing Interfaces" + e);
return; return;
} }
String chosenIface = null; String chosenIface = null;
if (ifaces != null) { if (ifaces != null) {
for (String iface : ifaces) { for (String iface : ifaces) {
@@ -1303,6 +1315,7 @@ public class Tethering {
} }
} }
} }
if (chosenIface == null) { if (chosenIface == null) {
Log.e(TAG, "could not find iface of type " + interfaceType); Log.e(TAG, "could not find iface of type " + interfaceType);
return; return;
@@ -1311,6 +1324,33 @@ public class Tethering {
changeInterfaceState(chosenIface, requestedState); changeInterfaceState(chosenIface, requestedState);
} }
private void disableUsbIpServing(int interfaceType) {
String[] ifaces = null;
try {
ifaces = mNetd.interfaceGetList();
} catch (RemoteException | ServiceSpecificException e) {
mLog.e("Cannot disableUsbIpServing due to error listing Interfaces" + e);
return;
}
String chosenIface = null;
if (ifaces != null) {
for (String iface : ifaces) {
if (ifaceNameToType(iface) == interfaceType) {
chosenIface = iface;
break;
}
}
}
if (chosenIface == null) {
Log.e(TAG, "could not find iface of type " + interfaceType);
return;
}
changeInterfaceState(chosenIface, IpServer.STATE_AVAILABLE);
}
private void changeInterfaceState(String ifname, int requestedState) { private void changeInterfaceState(String ifname, int requestedState) {
final int result; final int result;
switch (requestedState) { switch (requestedState) {
@@ -2488,7 +2528,7 @@ public class Tethering {
mTetherMainSM.sendMessage(which, state, 0, newLp); mTetherMainSM.sendMessage(which, state, 0, newLp);
} }
private void maybeTrackNewInterface(final String iface) { private void ensureIpServerStarted(final String iface) {
// If we don't care about this type of interface, ignore. // If we don't care about this type of interface, ignore.
final int interfaceType = ifaceNameToType(iface); final int interfaceType = ifaceNameToType(iface);
if (interfaceType == TETHERING_INVALID) { if (interfaceType == TETHERING_INVALID) {
@@ -2508,17 +2548,17 @@ public class Tethering {
return; return;
} }
maybeTrackNewInterface(iface, interfaceType); ensureIpServerStarted(iface, interfaceType);
} }
private void maybeTrackNewInterface(final String iface, int interfaceType) { private void ensureIpServerStarted(final String iface, int interfaceType) {
// If we have already started a TISM for this interface, skip. // If we have already started a TISM for this interface, skip.
if (mTetherStates.containsKey(iface)) { if (mTetherStates.containsKey(iface)) {
mLog.log("active iface (" + iface + ") reported as added, ignoring"); mLog.log("active iface (" + iface + ") reported as added, ignoring");
return; return;
} }
mLog.log("adding TetheringInterfaceStateMachine for: " + iface); mLog.log("adding IpServer for: " + iface);
final TetherState tetherState = new TetherState( final TetherState tetherState = new TetherState(
new IpServer(iface, mLooper, interfaceType, mLog, mNetd, mBpfCoordinator, new IpServer(iface, mLooper, interfaceType, mLog, mNetd, mBpfCoordinator,
makeControlCallback(), mConfig.enableLegacyDhcpServer, makeControlCallback(), mConfig.enableLegacyDhcpServer,
@@ -2528,14 +2568,12 @@ public class Tethering {
tetherState.ipServer.start(); tetherState.ipServer.start();
} }
private void stopTrackingInterface(final String iface) { private void ensureIpServerStopped(final String iface) {
final TetherState tetherState = mTetherStates.get(iface); final TetherState tetherState = mTetherStates.get(iface);
if (tetherState == null) { if (tetherState == null) return;
mLog.log("attempting to remove unknown iface (" + iface + "), ignoring");
return;
}
tetherState.ipServer.stop(); tetherState.ipServer.stop();
mLog.log("removing TetheringInterfaceStateMachine for: " + iface); mLog.log("removing IpServer for: " + iface);
mTetherStates.remove(iface); mTetherStates.remove(iface);
} }