Merge "Fix endian error when pass upstream prefixes to bpf" into main
This commit is contained in:
commit
7e3344889e
3 changed files with 63 additions and 18 deletions
|
|
@ -90,7 +90,6 @@ import java.net.InetAddress;
|
|||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
|
@ -1258,12 +1257,18 @@ public class BpfCoordinator {
|
|||
pw.decreaseIndent();
|
||||
}
|
||||
|
||||
private IpPrefix longToPrefix(long ip64) {
|
||||
final ByteBuffer prefixBuffer = ByteBuffer.allocate(IPV6_ADDR_LEN);
|
||||
prefixBuffer.putLong(ip64);
|
||||
/**
|
||||
* Returns a /64 IpPrefix corresponding to the passed in byte array
|
||||
*
|
||||
* @param ip64 byte array to convert format
|
||||
* @return the converted IpPrefix
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public static IpPrefix bytesToPrefix(final byte[] ip64) {
|
||||
IpPrefix sourcePrefix;
|
||||
byte[] prefixBytes = Arrays.copyOf(ip64, IPV6_ADDR_LEN);
|
||||
try {
|
||||
sourcePrefix = new IpPrefix(InetAddress.getByAddress(prefixBuffer.array()), 64);
|
||||
sourcePrefix = new IpPrefix(InetAddress.getByAddress(prefixBytes), 64);
|
||||
} catch (UnknownHostException e) {
|
||||
// Cannot happen. InetAddress.getByAddress can only throw an exception if the byte array
|
||||
// is the wrong length, but we allocate it with fixed length IPV6_ADDR_LEN.
|
||||
|
|
@ -1274,7 +1279,7 @@ public class BpfCoordinator {
|
|||
|
||||
private String ipv6UpstreamRuleToString(TetherUpstream6Key key, Tether6Value value) {
|
||||
return String.format("%d(%s) [%s] [%s] -> %d(%s) %04x [%s] [%s]",
|
||||
key.iif, getIfName(key.iif), key.dstMac, longToPrefix(key.src64), value.oif,
|
||||
key.iif, getIfName(key.iif), key.dstMac, bytesToPrefix(key.src64), value.oif,
|
||||
getIfName(value.oif), value.ethProto, value.ethSrcMac, value.ethDstMac);
|
||||
}
|
||||
|
||||
|
|
@ -1570,7 +1575,7 @@ public class BpfCoordinator {
|
|||
*/
|
||||
@NonNull
|
||||
public TetherUpstream6Key makeTetherUpstream6Key() {
|
||||
long prefix64 = ByteBuffer.wrap(sourcePrefix.getRawAddress()).getLong();
|
||||
final byte[] prefix64 = Arrays.copyOf(sourcePrefix.getRawAddress(), 8);
|
||||
return new TetherUpstream6Key(downstreamIfindex, inDstMac, prefix64);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,10 +32,10 @@ public class TetherUpstream6Key extends Struct {
|
|||
@Field(order = 1, type = Type.EUI48, padding = 6)
|
||||
public final MacAddress dstMac; // Destination ethernet mac address (zeroed iff rawip ingress).
|
||||
|
||||
@Field(order = 2, type = Type.S64)
|
||||
public final long src64; // The top 64-bits of the source ip.
|
||||
@Field(order = 2, type = Type.ByteArray, arraysize = 8)
|
||||
public final byte[] src64; // The top 64-bits of the source ip.
|
||||
|
||||
public TetherUpstream6Key(int iif, @NonNull final MacAddress dstMac, long src64) {
|
||||
public TetherUpstream6Key(int iif, @NonNull final MacAddress dstMac, final byte[] src64) {
|
||||
Objects.requireNonNull(dstMac);
|
||||
|
||||
this.iif = iif;
|
||||
|
|
|
|||
|
|
@ -64,6 +64,7 @@ import static com.android.networkstack.tethering.BpfUtils.UPSTREAM;
|
|||
import static com.android.networkstack.tethering.TetheringConfiguration.DEFAULT_TETHER_OFFLOAD_POLL_INTERVAL_MS;
|
||||
import static com.android.testutils.MiscAsserts.assertSameElements;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
|
@ -166,7 +167,6 @@ import java.io.StringWriter;
|
|||
import java.net.Inet4Address;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
|
|
@ -794,7 +794,7 @@ public class BpfCoordinatorTest {
|
|||
if (!mDeps.isAtLeastS()) return;
|
||||
ArrayMap<TetherUpstream6Key, Tether6Value> expected = new ArrayMap<>();
|
||||
for (IpPrefix upstreamPrefix : upstreamPrefixes) {
|
||||
long prefix64 = prefixToLong(upstreamPrefix);
|
||||
final byte[] prefix64 = prefixToIp64(upstreamPrefix);
|
||||
final TetherUpstream6Key key = new TetherUpstream6Key(DOWNSTREAM_IFACE_PARAMS.index,
|
||||
DOWNSTREAM_IFACE_PARAMS.macAddr, prefix64);
|
||||
final Tether6Value value = new Tether6Value(upstreamIfindex,
|
||||
|
|
@ -822,7 +822,7 @@ public class BpfCoordinatorTest {
|
|||
if (!mDeps.isAtLeastS()) return;
|
||||
Set<TetherUpstream6Key> expected = new ArraySet<>();
|
||||
for (IpPrefix upstreamPrefix : upstreamPrefixes) {
|
||||
long prefix64 = prefixToLong(upstreamPrefix);
|
||||
final byte[] prefix64 = prefixToIp64(upstreamPrefix);
|
||||
final TetherUpstream6Key key = new TetherUpstream6Key(DOWNSTREAM_IFACE_PARAMS.index,
|
||||
DOWNSTREAM_IFACE_PARAMS.macAddr, prefix64);
|
||||
expected.add(key);
|
||||
|
|
@ -1275,7 +1275,7 @@ public class BpfCoordinatorTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testRuleMakeTetherDownstream6Key() throws Exception {
|
||||
public void testIpv6DownstreamRuleMakeTetherDownstream6Key() throws Exception {
|
||||
final int mobileIfIndex = 100;
|
||||
final Ipv6DownstreamRule rule = buildTestDownstreamRule(mobileIfIndex, NEIGH_A, MAC_A);
|
||||
|
||||
|
|
@ -1288,7 +1288,7 @@ public class BpfCoordinatorTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testRuleMakeTether6Value() throws Exception {
|
||||
public void testIpv6DownstreamRuleMakeTether6Value() throws Exception {
|
||||
final int mobileIfIndex = 100;
|
||||
final Ipv6DownstreamRule rule = buildTestDownstreamRule(mobileIfIndex, NEIGH_A, MAC_A);
|
||||
|
||||
|
|
@ -1302,6 +1302,46 @@ public class BpfCoordinatorTest {
|
|||
assertEquals(20, value.writeToBytes().length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIpv6UpstreamRuleMakeTetherUpstream6Key() {
|
||||
final byte[] bytes = new byte[]{(byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8,
|
||||
(byte) 0xab, (byte) 0xcd, (byte) 0xfe, (byte) 0x00};
|
||||
final IpPrefix prefix = new IpPrefix("2001:db8:abcd:fe00::/64");
|
||||
final Ipv6UpstreamRule rule = buildTestUpstreamRule(UPSTREAM_IFINDEX,
|
||||
DOWNSTREAM_IFINDEX, prefix, DOWNSTREAM_MAC);
|
||||
|
||||
final TetherUpstream6Key key = rule.makeTetherUpstream6Key();
|
||||
assertEquals(DOWNSTREAM_IFINDEX, key.iif);
|
||||
assertEquals(DOWNSTREAM_MAC, key.dstMac);
|
||||
assertArrayEquals(bytes, key.src64);
|
||||
// iif (4) + dstMac (6) + padding (6) + src64 (8) = 24
|
||||
assertEquals(24, key.writeToBytes().length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIpv6UpstreamRuleMakeTether6Value() {
|
||||
final IpPrefix prefix = new IpPrefix("2001:db8:abcd:fe00::/64");
|
||||
final Ipv6UpstreamRule rule = buildTestUpstreamRule(UPSTREAM_IFINDEX,
|
||||
DOWNSTREAM_IFINDEX, prefix, DOWNSTREAM_MAC);
|
||||
|
||||
final Tether6Value value = rule.makeTether6Value();
|
||||
assertEquals(UPSTREAM_IFINDEX, value.oif);
|
||||
assertEquals(MAC_NULL, value.ethDstMac);
|
||||
assertEquals(MAC_NULL, value.ethSrcMac);
|
||||
assertEquals(ETH_P_IPV6, value.ethProto);
|
||||
assertEquals(NetworkStackConstants.ETHER_MTU, value.pmtu);
|
||||
// oif (4) + ethDstMac (6) + ethSrcMac (6) + ethProto (2) + pmtu (2) = 20
|
||||
assertEquals(20, value.writeToBytes().length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBytesToPrefix() {
|
||||
final byte[] bytes = new byte[]{(byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8,
|
||||
(byte) 0x00, (byte) 0x00, (byte) 0x12, (byte) 0x34};
|
||||
final IpPrefix prefix = new IpPrefix("2001:db8:0:1234::/64");
|
||||
assertEquals(prefix, BpfCoordinator.bytesToPrefix(bytes));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetDataLimit() throws Exception {
|
||||
setupFunctioningNetdInterface();
|
||||
|
|
@ -2269,8 +2309,8 @@ public class BpfCoordinatorTest {
|
|||
100 /* nonzero, CT_NEW */);
|
||||
}
|
||||
|
||||
private static long prefixToLong(IpPrefix prefix) {
|
||||
return ByteBuffer.wrap(prefix.getRawAddress()).getLong();
|
||||
private static byte[] prefixToIp64(IpPrefix prefix) {
|
||||
return Arrays.copyOf(prefix.getRawAddress(), 8);
|
||||
}
|
||||
|
||||
void checkRule4ExistInUpstreamDownstreamMap() throws Exception {
|
||||
|
|
@ -2515,7 +2555,7 @@ public class BpfCoordinatorTest {
|
|||
final Ipv6DownstreamRule rule = buildTestDownstreamRule(UPSTREAM_IFINDEX, NEIGH_A, MAC_A);
|
||||
mBpfDownstream6Map.insertEntry(rule.makeTetherDownstream6Key(), rule.makeTether6Value());
|
||||
|
||||
final long prefix64 = prefixToLong(UPSTREAM_PREFIX);
|
||||
final byte[] prefix64 = prefixToIp64(UPSTREAM_PREFIX);
|
||||
final TetherUpstream6Key upstream6Key = new TetherUpstream6Key(DOWNSTREAM_IFINDEX,
|
||||
DOWNSTREAM_MAC, prefix64);
|
||||
final Tether6Value upstream6Value = new Tether6Value(UPSTREAM_IFINDEX,
|
||||
|
|
|
|||
Loading…
Reference in a new issue