Skip to content

Commit 731026c

Browse files
author
Robert Greenwalt
committed
Ignore errors when untethering
Sometimes the interface is removed before we can untether leading to errors when cleanup up various rules (iptables). Do as much as we can and then let a re-tether result in error if needed. bug:5536516 Change-Id: Ib1d064ecc8e9022566f9b0e4678b33144906971c
1 parent bcf05a6 commit 731026c

File tree

1 file changed

+27
-52
lines changed

1 file changed

+27
-52
lines changed

services/java/com/android/server/connectivity/Tethering.java

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
7373
private Context mContext;
7474
private final static String TAG = "Tethering";
7575
private final static boolean DBG = true;
76-
private final static boolean VDBG = false;
76+
private final static boolean VDBG = true;
7777

7878
// TODO - remove both of these - should be part of interface inspection/selection stuff
7979
private String[] mTetherableUsbRegexs;
@@ -920,6 +920,29 @@ public void enter() {
920920
setTethered(true);
921921
sendTetherStateChangedBroadcast();
922922
}
923+
924+
void cleanupUpstream() {
925+
if (mMyUpstreamIfaceName != null) {
926+
// note that we don't care about errors here.
927+
// sometimes interfaces are gone before we get
928+
// to remove their rules, which generates errors.
929+
// just do the best we can.
930+
try {
931+
// about to tear down NAT; gather remaining statistics
932+
mStatsService.forceUpdate();
933+
} catch (Exception e) {
934+
if (VDBG) Log.e(TAG, "Exception in forceUpdate: " + e.toString());
935+
}
936+
try {
937+
mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName);
938+
} catch (Exception e) {
939+
if (VDBG) Log.e(TAG, "Exception in disableNat: " + e.toString());
940+
}
941+
mMyUpstreamIfaceName = null;
942+
}
943+
return;
944+
}
945+
923946
@Override
924947
public boolean processMessage(Message message) {
925948
if (VDBG) Log.d(TAG, "TetheredState.processMessage what=" + message.what);
@@ -928,23 +951,7 @@ public boolean processMessage(Message message) {
928951
switch (message.what) {
929952
case CMD_TETHER_UNREQUESTED:
930953
case CMD_INTERFACE_DOWN:
931-
if (mMyUpstreamIfaceName != null) {
932-
try {
933-
// about to tear down NAT; gather remaining statistics
934-
mStatsService.forceUpdate();
935-
936-
mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName);
937-
mMyUpstreamIfaceName = null;
938-
} catch (Exception e) {
939-
try {
940-
mNMService.untetherInterface(mIfaceName);
941-
} catch (Exception ee) {}
942-
943-
setLastErrorAndTransitionToInitialState(
944-
ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
945-
break;
946-
}
947-
}
954+
cleanupUpstream();
948955
try {
949956
mNMService.untetherInterface(mIfaceName);
950957
} catch (Exception e) {
@@ -975,23 +982,7 @@ public boolean processMessage(Message message) {
975982
if (VDBG) Log.d(TAG, "Connection changed noop - dropping");
976983
break;
977984
}
978-
if (mMyUpstreamIfaceName != null) {
979-
try {
980-
// about to tear down NAT; gather remaining statistics
981-
mStatsService.forceUpdate();
982-
983-
mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName);
984-
mMyUpstreamIfaceName = null;
985-
} catch (Exception e) {
986-
try {
987-
mNMService.untetherInterface(mIfaceName);
988-
} catch (Exception ee) {}
989-
990-
setLastErrorAndTransitionToInitialState(
991-
ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
992-
break;
993-
}
994-
}
985+
cleanupUpstream();
995986
if (newUpstreamIfaceName != null) {
996987
try {
997988
mNMService.enableNat(mIfaceName, newUpstreamIfaceName);
@@ -1016,23 +1007,7 @@ public boolean processMessage(Message message) {
10161007
error = true;
10171008
// fall through
10181009
case CMD_TETHER_MODE_DEAD:
1019-
if (mMyUpstreamIfaceName != null) {
1020-
try {
1021-
// about to tear down NAT; gather remaining statistics
1022-
mStatsService.forceUpdate();
1023-
1024-
mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName);
1025-
mMyUpstreamIfaceName = null;
1026-
} catch (Exception e) {
1027-
try {
1028-
mNMService.untetherInterface(mIfaceName);
1029-
} catch (Exception ee) {}
1030-
1031-
setLastErrorAndTransitionToInitialState(
1032-
ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
1033-
break;
1034-
}
1035-
}
1010+
cleanupUpstream();
10361011
try {
10371012
mNMService.untetherInterface(mIfaceName);
10381013
} catch (Exception e) {

0 commit comments

Comments
 (0)