On U+ require config_networkWakeupPacketMark/Mask to be 0x80000000

See system/netd/include/Fwmark.h which reserves the bottom 21 bits
of skb->mark for netid, etc... so out of necessity these have to be
in the top 11 bits.

In practice the only values that really make sense are either:
  mark == mask == 0 (disabled)
or (mark == mask) being one of the top 11 available bits, for example:
  mark == mask == 0x80000000 (enabled, using top-most bit)
(only a single bit makes sense, as this is really just a boolean signal,
 and only the topmost is known to be used on any real devices)

Let's just force the use of 0x80000000 for ecosystem consistency.

Bug: 284334830
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I199e9b00de845b3747940426ea6644426ab72e87
This commit is contained in:
Maciej Żenczykowski
2023-06-02 07:48:11 +00:00
parent 83bd5a5927
commit cf41fc877a
2 changed files with 36 additions and 14 deletions

View File

@@ -317,6 +317,7 @@ import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.lang.IllegalArgumentException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
@@ -441,6 +442,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
private final Context mContext;
private final ConnectivityResources mResources;
private final int mWakeUpMark;
private final int mWakeUpMask;
// The Context is created for UserHandle.ALL.
private final Context mUserAllContext;
private final Dependencies mDeps;
@@ -1610,6 +1613,29 @@ public class ConnectivityService extends IConnectivityManager.Stub
mCellularRadioTimesharingCapable =
mResources.get().getBoolean(R.bool.config_cellular_radio_timesharing_capable);
int mark = mResources.get().getInteger(R.integer.config_networkWakeupPacketMark);
int mask = mResources.get().getInteger(R.integer.config_networkWakeupPacketMask);
if (SdkLevel.isAtLeastU()) {
// U+ default value of both mark & mask, this is the top bit of the skb->mark,
// see //system/netd/include/FwMark.h union Fwmark, field ingress_cpu_wakeup
final int defaultUMarkMask = 0x80000000; // u32
if ((mark == 0) || (mask == 0)) {
// simply treat unset/disabled as the default U value
mark = defaultUMarkMask;
mask = defaultUMarkMask;
}
if ((mark != defaultUMarkMask) || (mask != defaultUMarkMask)) {
// invalid device overlay settings
throw new IllegalArgumentException(
"Bad config_networkWakeupPacketMark/Mask " + mark + "/" + mask);
}
}
mWakeUpMark = mark;
mWakeUpMask = mask;
mNetd = netd;
mBpfNetMaps = mDeps.getBpfNetMaps(mContext, netd);
mHandlerThread = mDeps.makeHandlerThread();
@@ -8088,21 +8114,18 @@ public class ConnectivityService extends IConnectivityManager.Stub
return;
}
int mark = mResources.get().getInteger(R.integer.config_networkWakeupPacketMark);
int mask = mResources.get().getInteger(R.integer.config_networkWakeupPacketMask);
// Mask/mark of zero will not detect anything interesting.
// Don't install rules unless both values are nonzero.
if (mark == 0 || mask == 0) {
if (mWakeUpMark == 0 || mWakeUpMask == 0) {
return;
}
final String prefix = makeNflogPrefix(iface, nai.network.getNetworkHandle());
try {
if (add) {
mNetd.wakeupAddInterface(iface, prefix, mark, mask);
mNetd.wakeupAddInterface(iface, prefix, mWakeUpMark, mWakeUpMask);
} else {
mNetd.wakeupDelInterface(iface, prefix, mark, mask);
mNetd.wakeupDelInterface(iface, prefix, mWakeUpMark, mWakeUpMask);
}
} catch (Exception e) {
loge("Exception modifying wakeup packet monitoring: " + e);

View File

@@ -541,8 +541,7 @@ public class ConnectivityServiceTest {
private static final int TEST_PACKAGE_UID2 = 321;
private static final int TEST_PACKAGE_UID3 = 456;
private static final int PACKET_WAKEUP_MASK = 0xffff0000;
private static final int PACKET_WAKEUP_MARK = 0x88880000;
private static final int PACKET_WAKEUP_MARK_MASK = 0x80000000;
private static final String ALWAYS_ON_PACKAGE = "com.android.test.alwaysonvpn";
@@ -1924,9 +1923,9 @@ public class ConnectivityServiceTest {
doReturn(0).when(mResources).getInteger(R.integer.config_activelyPreferBadWifi);
doReturn(true).when(mResources)
.getBoolean(R.bool.config_cellular_radio_timesharing_capable);
doReturn(PACKET_WAKEUP_MASK).when(mResources).getInteger(
doReturn(PACKET_WAKEUP_MARK_MASK).when(mResources).getInteger(
R.integer.config_networkWakeupPacketMask);
doReturn(PACKET_WAKEUP_MARK).when(mResources).getInteger(
doReturn(PACKET_WAKEUP_MARK_MASK).when(mResources).getInteger(
R.integer.config_networkWakeupPacketMark);
}
@@ -18224,8 +18223,8 @@ public class ConnectivityServiceTest {
final String expectedPrefix = makeNflogPrefix(WIFI_IFNAME,
mWiFiAgent.getNetwork().getNetworkHandle());
verify(mMockNetd).wakeupAddInterface(WIFI_IFNAME, expectedPrefix, PACKET_WAKEUP_MARK,
PACKET_WAKEUP_MASK);
verify(mMockNetd).wakeupAddInterface(WIFI_IFNAME, expectedPrefix, PACKET_WAKEUP_MARK_MASK,
PACKET_WAKEUP_MARK_MASK);
}
@Test
@@ -18238,8 +18237,8 @@ public class ConnectivityServiceTest {
if (mDeps.isAtLeastU()) {
final String expectedPrefix = makeNflogPrefix(MOBILE_IFNAME,
mCellAgent.getNetwork().getNetworkHandle());
verify(mMockNetd).wakeupAddInterface(MOBILE_IFNAME, expectedPrefix, PACKET_WAKEUP_MARK,
PACKET_WAKEUP_MASK);
verify(mMockNetd).wakeupAddInterface(MOBILE_IFNAME, expectedPrefix,
PACKET_WAKEUP_MARK_MASK, PACKET_WAKEUP_MARK_MASK);
} else {
verify(mMockNetd, never()).wakeupAddInterface(eq(MOBILE_IFNAME), anyString(), anyInt(),
anyInt());