[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:
@@ -257,6 +257,7 @@ public class ClatCoordinator {
|
||||
try {
|
||||
mNetd.interfaceSetEnableIPv6(tunIface, false /* enabled */);
|
||||
} catch (RemoteException | ServiceSpecificException e) {
|
||||
tunFd.close();
|
||||
Log.e(TAG, "Disable IPv6 on " + tunIface + " failed: " + e);
|
||||
}
|
||||
|
||||
@@ -273,6 +274,7 @@ public class ClatCoordinator {
|
||||
try {
|
||||
mNetd.interfaceSetMtu(tunIface, mtu);
|
||||
} catch (RemoteException | ServiceSpecificException e) {
|
||||
tunFd.close();
|
||||
throw new IOException("Set MTU " + mtu + " on " + tunIface + " failed: " + e);
|
||||
}
|
||||
final InterfaceConfigurationParcel ifConfig = new InterfaceConfigurationParcel();
|
||||
@@ -284,6 +286,7 @@ public class ClatCoordinator {
|
||||
try {
|
||||
mNetd.interfaceSetCfg(ifConfig);
|
||||
} catch (RemoteException | ServiceSpecificException e) {
|
||||
tunFd.close();
|
||||
throw new IOException("Setting IPv4 address to " + ifConfig.ipv4Addr + "/"
|
||||
+ ifConfig.prefixLength + " failed on " + ifConfig.ifName + ": " + e);
|
||||
}
|
||||
@@ -293,11 +296,12 @@ public class ClatCoordinator {
|
||||
final ParcelFileDescriptor readSock6;
|
||||
try {
|
||||
// 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
|
||||
// descriptor to initialize ParcelFileDescriptor object instead.
|
||||
readSock6 = mDeps.adoptFd(mDeps.openPacketSocket());
|
||||
} catch (IOException e) {
|
||||
tunFd.close();
|
||||
throw new IOException("Open packet socket failed: " + e);
|
||||
}
|
||||
|
||||
@@ -308,11 +312,16 @@ public class ClatCoordinator {
|
||||
// reason why we use jniOpenPacketSocket6().
|
||||
writeSock6 = mDeps.adoptFd(mDeps.openRawSocket6(fwmark));
|
||||
} catch (IOException e) {
|
||||
tunFd.close();
|
||||
readSock6.close();
|
||||
throw new IOException("Open raw socket failed: " + e);
|
||||
}
|
||||
|
||||
final int ifaceIndex = mDeps.getInterfaceIndex(iface);
|
||||
if (ifaceIndex == INVALID_IFINDEX) {
|
||||
tunFd.close();
|
||||
readSock6.close();
|
||||
writeSock6.close();
|
||||
throw new IOException("Fail to get interface index for interface " + iface);
|
||||
}
|
||||
|
||||
@@ -320,6 +329,9 @@ public class ClatCoordinator {
|
||||
try {
|
||||
mDeps.addAnycastSetsockopt(writeSock6.getFileDescriptor(), v6, ifaceIndex);
|
||||
} catch (IOException e) {
|
||||
tunFd.close();
|
||||
readSock6.close();
|
||||
writeSock6.close();
|
||||
throw new IOException("add anycast sockopt failed: " + e);
|
||||
}
|
||||
|
||||
@@ -327,6 +339,9 @@ public class ClatCoordinator {
|
||||
try {
|
||||
mDeps.configurePacketSocket(readSock6.getFileDescriptor(), v6, ifaceIndex);
|
||||
} catch (IOException e) {
|
||||
tunFd.close();
|
||||
readSock6.close();
|
||||
writeSock6.close();
|
||||
throw new IOException("configure packet socket failed: " + e);
|
||||
}
|
||||
|
||||
@@ -340,6 +355,10 @@ public class ClatCoordinator {
|
||||
mXlatLocalAddress6 = v6;
|
||||
} catch (IOException e) {
|
||||
throw new IOException("Error start clatd on " + iface + ": " + e);
|
||||
} finally {
|
||||
tunFd.close();
|
||||
readSock6.close();
|
||||
writeSock6.close();
|
||||
}
|
||||
|
||||
return v6;
|
||||
|
||||
Reference in New Issue
Block a user