[CLATJ#23] Close the file descriptor manually

ParcelFileDescriptor rely on garbage collection to close handler.
When there is any error during starting clat or quick on/off
IPv6 only network, the file descriptor may not be able to be closed
before next clatStart is called. This may be problematic. For
example, the same v4- tun interface has not closed yet and clatStart
has been called again.

Test: connect/disconnect to IPv6 only network and ping 8.8.8.8
      repeat 10 times
Change-Id: I8e1c66206dc221827a039213ecc86d5cbd777dff
This commit is contained in:
Hungming Chen
2022-01-22 22:15:56 +08:00
parent 9d11ddfd5c
commit d7b63f8f93

View File

@@ -257,6 +257,7 @@ public class ClatCoordinator {
try { try {
mNetd.interfaceSetEnableIPv6(tunIface, false /* enabled */); mNetd.interfaceSetEnableIPv6(tunIface, false /* enabled */);
} catch (RemoteException | ServiceSpecificException e) { } catch (RemoteException | ServiceSpecificException e) {
tunFd.close();
Log.e(TAG, "Disable IPv6 on " + tunIface + " failed: " + e); Log.e(TAG, "Disable IPv6 on " + tunIface + " failed: " + e);
} }
@@ -273,6 +274,7 @@ public class ClatCoordinator {
try { try {
mNetd.interfaceSetMtu(tunIface, mtu); mNetd.interfaceSetMtu(tunIface, mtu);
} catch (RemoteException | ServiceSpecificException e) { } catch (RemoteException | ServiceSpecificException e) {
tunFd.close();
throw new IOException("Set MTU " + mtu + " on " + tunIface + " failed: " + e); throw new IOException("Set MTU " + mtu + " on " + tunIface + " failed: " + e);
} }
final InterfaceConfigurationParcel ifConfig = new InterfaceConfigurationParcel(); final InterfaceConfigurationParcel ifConfig = new InterfaceConfigurationParcel();
@@ -284,6 +286,7 @@ public class ClatCoordinator {
try { try {
mNetd.interfaceSetCfg(ifConfig); mNetd.interfaceSetCfg(ifConfig);
} catch (RemoteException | ServiceSpecificException e) { } catch (RemoteException | ServiceSpecificException e) {
tunFd.close();
throw new IOException("Setting IPv4 address to " + ifConfig.ipv4Addr + "/" throw new IOException("Setting IPv4 address to " + ifConfig.ipv4Addr + "/"
+ ifConfig.prefixLength + " failed on " + ifConfig.ifName + ": " + e); + ifConfig.prefixLength + " failed on " + ifConfig.ifName + ": " + e);
} }
@@ -293,11 +296,12 @@ public class ClatCoordinator {
final ParcelFileDescriptor readSock6; final ParcelFileDescriptor readSock6;
try { try {
// Use a JNI call to get native file descriptor instead of Os.socket() because we would // Use a JNI call to get native file descriptor instead of Os.socket() because we would
// like to use ParcelFileDescriptor to close file descriptor automatically. But ctor // like to use ParcelFileDescriptor to manage file descriptor. But ctor
// ParcelFileDescriptor(FileDescriptor fd) is a @hide function. Need to use native file // ParcelFileDescriptor(FileDescriptor fd) is a @hide function. Need to use native file
// descriptor to initialize ParcelFileDescriptor object instead. // descriptor to initialize ParcelFileDescriptor object instead.
readSock6 = mDeps.adoptFd(mDeps.openPacketSocket()); readSock6 = mDeps.adoptFd(mDeps.openPacketSocket());
} catch (IOException e) { } catch (IOException e) {
tunFd.close();
throw new IOException("Open packet socket failed: " + e); throw new IOException("Open packet socket failed: " + e);
} }
@@ -308,11 +312,16 @@ public class ClatCoordinator {
// reason why we use jniOpenPacketSocket6(). // reason why we use jniOpenPacketSocket6().
writeSock6 = mDeps.adoptFd(mDeps.openRawSocket6(fwmark)); writeSock6 = mDeps.adoptFd(mDeps.openRawSocket6(fwmark));
} catch (IOException e) { } catch (IOException e) {
tunFd.close();
readSock6.close();
throw new IOException("Open raw socket failed: " + e); throw new IOException("Open raw socket failed: " + e);
} }
final int ifaceIndex = mDeps.getInterfaceIndex(iface); final int ifaceIndex = mDeps.getInterfaceIndex(iface);
if (ifaceIndex == INVALID_IFINDEX) { if (ifaceIndex == INVALID_IFINDEX) {
tunFd.close();
readSock6.close();
writeSock6.close();
throw new IOException("Fail to get interface index for interface " + iface); throw new IOException("Fail to get interface index for interface " + iface);
} }
@@ -320,6 +329,9 @@ public class ClatCoordinator {
try { try {
mDeps.addAnycastSetsockopt(writeSock6.getFileDescriptor(), v6, ifaceIndex); mDeps.addAnycastSetsockopt(writeSock6.getFileDescriptor(), v6, ifaceIndex);
} catch (IOException e) { } catch (IOException e) {
tunFd.close();
readSock6.close();
writeSock6.close();
throw new IOException("add anycast sockopt failed: " + e); throw new IOException("add anycast sockopt failed: " + e);
} }
@@ -327,6 +339,9 @@ public class ClatCoordinator {
try { try {
mDeps.configurePacketSocket(readSock6.getFileDescriptor(), v6, ifaceIndex); mDeps.configurePacketSocket(readSock6.getFileDescriptor(), v6, ifaceIndex);
} catch (IOException e) { } catch (IOException e) {
tunFd.close();
readSock6.close();
writeSock6.close();
throw new IOException("configure packet socket failed: " + e); throw new IOException("configure packet socket failed: " + e);
} }
@@ -340,6 +355,10 @@ public class ClatCoordinator {
mXlatLocalAddress6 = v6; mXlatLocalAddress6 = v6;
} catch (IOException e) { } catch (IOException e) {
throw new IOException("Error start clatd on " + iface + ": " + e); throw new IOException("Error start clatd on " + iface + ": " + e);
} finally {
tunFd.close();
readSock6.close();
writeSock6.close();
} }
return v6; return v6;