TestNetworkService: use IFF_NO_CARRIER to bring up iface without carrier

This will be supported in kernels >= 6.0.

Test: TH
Bug: 249611919
Change-Id: Ie84e2cd4f1b5181bd29b9ccbd96775c6c5572eac
This commit is contained in:
Patrick Rohr
2023-01-11 05:46:57 -08:00
parent b5cfb6426c
commit 1e732f6b17
2 changed files with 17 additions and 6 deletions

View File

@@ -260,7 +260,7 @@ public class TestNetworkManager {
/** /**
* Create a tap interface with or without carrier for testing purposes. * Create a tap interface with or without carrier for testing purposes.
* *
* Note: setting carrierUp = false is not supported until kernel version 5.0. * Note: setting carrierUp = false is not supported until kernel version 6.0.
* *
* @param carrierUp whether the created interface has a carrier or not. * @param carrierUp whether the created interface has a carrier or not.
* @param bringUp whether to bring up the interface before returning it. * @param bringUp whether to bring up the interface before returning it.
@@ -280,6 +280,8 @@ public class TestNetworkManager {
/** /**
* Create a tap interface for testing purposes. * Create a tap interface for testing purposes.
* *
* Note: setting carrierUp = false is not supported until kernel version 6.0.
*
* @param carrierUp whether the created interface has a carrier or not. * @param carrierUp whether the created interface has a carrier or not.
* @param bringUp whether to bring up the interface before returning it. * @param bringUp whether to bring up the interface before returning it.
* @param disableIpv6ProvisioningDelay whether to disable DAD and RS delay. * @param disableIpv6ProvisioningDelay whether to disable DAD and RS delay.

View File

@@ -38,9 +38,14 @@
#include "jni.h" #include "jni.h"
#include <android-base/stringprintf.h> #include <android-base/stringprintf.h>
#include <android-base/unique_fd.h> #include <android-base/unique_fd.h>
#include <bpf/KernelVersion.h>
#include <nativehelper/JNIHelp.h> #include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedUtfChars.h> #include <nativehelper/ScopedUtfChars.h>
#ifndef IFF_NO_CARRIER
#define IFF_NO_CARRIER 0x0040
#endif
namespace android { namespace android {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -66,17 +71,21 @@ static int createTunTapImpl(JNIEnv* env, bool isTun, bool hasCarrier, bool setIf
// Allocate interface. // Allocate interface.
ifr.ifr_flags = (isTun ? IFF_TUN : IFF_TAP) | IFF_NO_PI; ifr.ifr_flags = (isTun ? IFF_TUN : IFF_TAP) | IFF_NO_PI;
if (!hasCarrier) {
// Using IFF_NO_CARRIER is supported starting in kernel version >= 6.0
// Up until then, unsupported flags are ignored.
if (!bpf::isAtLeastKernelVersion(6, 0, 0)) {
throwException(env, EOPNOTSUPP, "IFF_NO_CARRIER not supported", ifr.ifr_name);
return -1;
}
ifr.ifr_flags |= IFF_NO_CARRIER;
}
strlcpy(ifr.ifr_name, iface, IFNAMSIZ); strlcpy(ifr.ifr_name, iface, IFNAMSIZ);
if (ioctl(tun.get(), TUNSETIFF, &ifr)) { if (ioctl(tun.get(), TUNSETIFF, &ifr)) {
throwException(env, errno, "allocating", ifr.ifr_name); throwException(env, errno, "allocating", ifr.ifr_name);
return -1; return -1;
} }
if (!hasCarrier) {
// disable carrier before setting IFF_UP
setTunTapCarrierEnabledImpl(env, iface, tun.get(), hasCarrier);
}
// Mark some TAP interfaces as supporting multicast // Mark some TAP interfaces as supporting multicast
if (setIffMulticast && !isTun) { if (setIffMulticast && !isTun) {
base::unique_fd inet6CtrlSock(socket(AF_INET6, SOCK_DGRAM, 0)); base::unique_fd inet6CtrlSock(socket(AF_INET6, SOCK_DGRAM, 0));