Skip to content
Snippets Groups Projects
Commit f80d2b74 authored by Patrick Rohr's avatar Patrick Rohr
Browse files

TestNetworkService: fix test network interface bringup

Bring up tun/tap interfaces after disabling dad / setting rs delay to 0,
so it is applied properly.

Test: TH
Bug: 235559605
Change-Id: I6bc209a12f9cbdba7bdde99c2a8817f8e52d34ce
parent 336bb5cf
No related branches found
No related tags found
No related merge requests found
......@@ -76,22 +76,41 @@ static int createTunTapImpl(JNIEnv* env, bool isTun, bool hasCarrier, const char
setTunTapCarrierEnabledImpl(env, iface, tun.get(), hasCarrier);
}
// Mark TAP interfaces as supporting multicast
if (!isTun) {
base::unique_fd inet6CtrlSock(socket(AF_INET6, SOCK_DGRAM, 0));
ifr.ifr_flags = IFF_MULTICAST;
if (ioctl(inet6CtrlSock.get(), SIOCSIFFLAGS, &ifr)) {
throwException(env, errno, "set IFF_MULTICAST", ifr.ifr_name);
return -1;
}
}
return tun.release();
}
static void bringUpInterfaceImpl(JNIEnv* env, const char* iface) {
// Activate interface using an unconnected datagram socket.
base::unique_fd inet6CtrlSock(socket(AF_INET6, SOCK_DGRAM, 0));
ifr.ifr_flags = IFF_UP;
// Mark TAP interfaces as supporting multicast
if (!isTun) ifr.ifr_flags |= IFF_MULTICAST;
ifreq ifr{};
strlcpy(ifr.ifr_name, iface, IFNAMSIZ);
if (ioctl(inet6CtrlSock.get(), SIOCGIFFLAGS, &ifr)) {
throwException(env, errno, "read flags", iface);
return;
}
ifr.ifr_flags |= IFF_UP;
if (ioctl(inet6CtrlSock.get(), SIOCSIFFLAGS, &ifr)) {
throwException(env, errno, "activating", ifr.ifr_name);
return -1;
throwException(env, errno, "set IFF_UP", iface);
return;
}
return tun.release();
}
//------------------------------------------------------------------------------
static void setTunTapCarrierEnabled(JNIEnv* env, jclass /* clazz */, jstring
jIface, jint tunFd, jboolean enabled) {
ScopedUtfChars iface(env, jIface);
......@@ -113,11 +132,21 @@ static jint createTunTap(JNIEnv* env, jclass /* clazz */, jboolean isTun,
return createTunTapImpl(env, isTun, hasCarrier, iface.c_str());
}
static void bringUpInterface(JNIEnv* env, jclass /* clazz */, jstring jIface) {
ScopedUtfChars iface(env, jIface);
if (!iface.c_str()) {
jniThrowNullPointerException(env, "iface");
return;
}
bringUpInterfaceImpl(env, iface.c_str());
}
//------------------------------------------------------------------------------
static const JNINativeMethod gMethods[] = {
{"nativeSetTunTapCarrierEnabled", "(Ljava/lang/String;IZ)V", (void*)setTunTapCarrierEnabled},
{"nativeCreateTunTap", "(ZZLjava/lang/String;)I", (void*)createTunTap},
{"nativeBringUpInterface", "(Ljava/lang/String;)V", (void*)bringUpInterface},
};
int register_com_android_server_TestNetworkService(JNIEnv* env) {
......
......@@ -47,7 +47,6 @@ import android.util.SparseArray;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.net.module.util.NetdUtils;
import com.android.net.module.util.NetworkStackConstants;
import java.io.IOException;
......@@ -83,6 +82,8 @@ class TestNetworkService extends ITestNetworkManager.Stub {
private static native void nativeSetTunTapCarrierEnabled(@NonNull String iface, int tunFd,
boolean enabled);
private static native void nativeBringUpInterface(String iface);
@VisibleForTesting
protected TestNetworkService(@NonNull Context context) {
mHandlerThread = new HandlerThread("TestNetworkServiceThread");
......@@ -153,7 +154,7 @@ class TestNetworkService extends ITestNetworkManager.Stub {
}
if (bringUp) {
NetdUtils.setInterfaceUp(mNetd, interfaceName);
nativeBringUpInterface(interfaceName);
}
return new TestNetworkInterface(tunIntf, interfaceName);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment