Remi NGUYEN VAN | bd1de20 | 2020-09-18 18:11:42 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net.util |
| 18 | |
| 19 | import android.Manifest.permission.MANAGE_TEST_NETWORKS |
| 20 | import android.content.Context |
| 21 | import android.net.InetAddresses.parseNumericAddress |
Remi NGUYEN VAN | 3df5c23 | 2020-09-28 18:48:46 +0900 | [diff] [blame] | 22 | import android.net.IpPrefix |
Remi NGUYEN VAN | bd1de20 | 2020-09-18 18:11:42 +0900 | [diff] [blame] | 23 | import android.net.MacAddress |
| 24 | import android.net.TestNetworkInterface |
| 25 | import android.net.TestNetworkManager |
| 26 | import android.net.dhcp.DhcpPacket |
| 27 | import android.os.HandlerThread |
| 28 | import android.system.Os |
| 29 | import android.system.OsConstants.AF_INET |
Remi NGUYEN VAN | 3df5c23 | 2020-09-28 18:48:46 +0900 | [diff] [blame] | 30 | import android.system.OsConstants.AF_PACKET |
| 31 | import android.system.OsConstants.ARPHRD_ETHER |
| 32 | import android.system.OsConstants.ETH_P_IPV6 |
Remi NGUYEN VAN | bd1de20 | 2020-09-18 18:11:42 +0900 | [diff] [blame] | 33 | import android.system.OsConstants.IPPROTO_UDP |
| 34 | import android.system.OsConstants.SOCK_DGRAM |
| 35 | import android.system.OsConstants.SOCK_NONBLOCK |
Remi NGUYEN VAN | 3df5c23 | 2020-09-28 18:48:46 +0900 | [diff] [blame] | 36 | import android.system.OsConstants.SOCK_RAW |
| 37 | import android.system.OsConstants.SOL_SOCKET |
| 38 | import android.system.OsConstants.SO_RCVTIMEO |
| 39 | import android.system.StructTimeval |
Remi NGUYEN VAN | f475a7e | 2022-07-21 19:14:31 +0900 | [diff] [blame^] | 40 | import androidx.test.platform.app.InstrumentationRegistry |
Patrick Rohr | 9622786 | 2022-03-04 16:13:34 +0100 | [diff] [blame] | 41 | import com.android.net.module.util.InterfaceParams |
Remi NGUYEN VAN | 3df5c23 | 2020-09-28 18:48:46 +0900 | [diff] [blame] | 42 | import com.android.net.module.util.Ipv6Utils |
Remi NGUYEN VAN | e3cd6c4 | 2020-09-28 19:03:05 +0900 | [diff] [blame] | 43 | import com.android.net.module.util.NetworkStackConstants.ETHER_ADDR_LEN |
| 44 | import com.android.net.module.util.NetworkStackConstants.IPV4_ADDR_ANY |
Remi NGUYEN VAN | 3df5c23 | 2020-09-28 18:48:46 +0900 | [diff] [blame] | 45 | import com.android.net.module.util.NetworkStackConstants.IPV6_ADDR_ALL_NODES_MULTICAST |
| 46 | import com.android.net.module.util.structs.PrefixInformationOption |
Remi NGUYEN VAN | f475a7e | 2022-07-21 19:14:31 +0900 | [diff] [blame^] | 47 | import com.android.networkstack.util.NetworkStackUtils |
Remi NGUYEN VAN | bd1de20 | 2020-09-18 18:11:42 +0900 | [diff] [blame] | 48 | import com.android.testutils.ArpRequestFilter |
| 49 | import com.android.testutils.ETHER_HEADER_LENGTH |
| 50 | import com.android.testutils.IPV4_HEADER_LENGTH |
| 51 | import com.android.testutils.IPv4UdpFilter |
| 52 | import com.android.testutils.TapPacketReader |
| 53 | import com.android.testutils.UDP_HEADER_LENGTH |
| 54 | import org.junit.After |
| 55 | import org.junit.Assert.assertArrayEquals |
| 56 | import org.junit.Before |
| 57 | import org.junit.Test |
Remi NGUYEN VAN | 3df5c23 | 2020-09-28 18:48:46 +0900 | [diff] [blame] | 58 | import java.io.FileDescriptor |
Remi NGUYEN VAN | bd1de20 | 2020-09-18 18:11:42 +0900 | [diff] [blame] | 59 | import java.net.Inet4Address |
Remi NGUYEN VAN | 3df5c23 | 2020-09-28 18:48:46 +0900 | [diff] [blame] | 60 | import java.net.Inet6Address |
| 61 | import java.nio.ByteBuffer |
Remi NGUYEN VAN | f475a7e | 2022-07-21 19:14:31 +0900 | [diff] [blame^] | 62 | import kotlin.reflect.KClass |
Remi NGUYEN VAN | bd1de20 | 2020-09-18 18:11:42 +0900 | [diff] [blame] | 63 | import kotlin.test.assertEquals |
Remi NGUYEN VAN | 3df5c23 | 2020-09-28 18:48:46 +0900 | [diff] [blame] | 64 | import kotlin.test.assertTrue |
Remi NGUYEN VAN | bd1de20 | 2020-09-18 18:11:42 +0900 | [diff] [blame] | 65 | import kotlin.test.fail |
| 66 | |
| 67 | class NetworkStackUtilsIntegrationTest { |
| 68 | private val inst by lazy { InstrumentationRegistry.getInstrumentation() } |
| 69 | private val context by lazy { inst.context } |
| 70 | |
| 71 | private val TEST_TIMEOUT_MS = 10_000L |
Remi NGUYEN VAN | 3df5c23 | 2020-09-28 18:48:46 +0900 | [diff] [blame] | 72 | private val TEST_MTU = 1500 |
Remi NGUYEN VAN | bd1de20 | 2020-09-18 18:11:42 +0900 | [diff] [blame] | 73 | private val TEST_TARGET_IPV4_ADDR = parseNumericAddress("192.0.2.42") as Inet4Address |
Remi NGUYEN VAN | 3df5c23 | 2020-09-28 18:48:46 +0900 | [diff] [blame] | 74 | private val TEST_SRC_MAC = MacAddress.fromString("BA:98:76:54:32:10") |
Remi NGUYEN VAN | bd1de20 | 2020-09-18 18:11:42 +0900 | [diff] [blame] | 75 | private val TEST_TARGET_MAC = MacAddress.fromString("01:23:45:67:89:0A") |
Remi NGUYEN VAN | 3df5c23 | 2020-09-28 18:48:46 +0900 | [diff] [blame] | 76 | private val TEST_INET6ADDR_1 = parseNumericAddress("2001:db8::1") as Inet6Address |
| 77 | private val TEST_INET6ADDR_2 = parseNumericAddress("2001:db8::2") as Inet6Address |
Remi NGUYEN VAN | bd1de20 | 2020-09-18 18:11:42 +0900 | [diff] [blame] | 78 | |
| 79 | private val readerHandler = HandlerThread( |
| 80 | NetworkStackUtilsIntegrationTest::class.java.simpleName) |
| 81 | private lateinit var iface: TestNetworkInterface |
| 82 | private lateinit var reader: TapPacketReader |
| 83 | |
| 84 | @Before |
| 85 | fun setUp() { |
| 86 | inst.uiAutomation.adoptShellPermissionIdentity(MANAGE_TEST_NETWORKS) |
| 87 | try { |
| 88 | val tnm = context.assertHasService(TestNetworkManager::class) |
| 89 | iface = tnm.createTapInterface() |
| 90 | } finally { |
| 91 | inst.uiAutomation.dropShellPermissionIdentity() |
| 92 | } |
| 93 | readerHandler.start() |
| 94 | reader = TapPacketReader(readerHandler.threadHandler, iface.fileDescriptor.fileDescriptor, |
| 95 | 1500 /* maxPacketSize */) |
| 96 | readerHandler.threadHandler.post { reader.start() } |
| 97 | } |
| 98 | |
| 99 | @After |
| 100 | fun tearDown() { |
| 101 | readerHandler.quitSafely() |
| 102 | if (this::iface.isInitialized) iface.fileDescriptor.close() |
| 103 | } |
| 104 | |
| 105 | @Test |
| 106 | fun testAddArpEntry() { |
| 107 | val socket = Os.socket(AF_INET, SOCK_DGRAM or SOCK_NONBLOCK, IPPROTO_UDP) |
| 108 | SocketUtils.bindSocketToInterface(socket, iface.interfaceName) |
| 109 | |
| 110 | NetworkStackUtils.addArpEntry(TEST_TARGET_IPV4_ADDR, TEST_TARGET_MAC, iface.interfaceName, |
| 111 | socket) |
| 112 | |
| 113 | // Fake DHCP packet: would not be usable as a DHCP offer (most IPv4 addresses are all-zero, |
| 114 | // no gateway or DNS servers, etc). |
| 115 | // Using a DHCP packet to replicate actual usage of the API: it is used in DhcpServer to |
| 116 | // send packets to clients before their IP address has been assigned. |
| 117 | val buffer = DhcpPacket.buildOfferPacket(DhcpPacket.ENCAP_BOOTP, 123 /* transactionId */, |
| 118 | false /* broadcast */, IPV4_ADDR_ANY /* serverIpAddr */, |
| 119 | IPV4_ADDR_ANY /* relayIp */, IPV4_ADDR_ANY /* yourIp */, |
| 120 | TEST_TARGET_MAC.toByteArray(), 3600 /* timeout */, IPV4_ADDR_ANY /* netMask */, |
| 121 | IPV4_ADDR_ANY /* bcAddr */, emptyList<Inet4Address>() /* gateways */, |
| 122 | emptyList<Inet4Address>() /* dnsServers */, |
| 123 | IPV4_ADDR_ANY /* dhcpServerIdentifier */, null /* domainName */, |
| 124 | null /* hostname */, false /* metered */, 1500 /* mtu */, |
| 125 | null /* captivePortalUrl */) |
| 126 | // Not using .array as per errorprone "ByteBufferBackingArray" recommendation |
Remi NGUYEN VAN | 3df5c23 | 2020-09-28 18:48:46 +0900 | [diff] [blame] | 127 | val originalPacket = buffer.readAsArray() |
Remi NGUYEN VAN | bd1de20 | 2020-09-18 18:11:42 +0900 | [diff] [blame] | 128 | |
| 129 | Os.sendto(socket, originalPacket, 0 /* bytesOffset */, originalPacket.size /* bytesCount */, |
| 130 | 0 /* flags */, TEST_TARGET_IPV4_ADDR, DhcpPacket.DHCP_CLIENT.toInt() /* port */) |
| 131 | |
| 132 | // Verify the packet was sent to the mac address specified in the ARP entry |
| 133 | // Also accept ARP requests, but expect that none is sent before the UDP packet |
| 134 | // IPv6 NS may be sent on the interface but will be filtered out |
Remi NGUYEN VAN | 3df5c23 | 2020-09-28 18:48:46 +0900 | [diff] [blame] | 135 | val sentPacket = reader.poll(TEST_TIMEOUT_MS, IPv4UdpFilter().or(ArpRequestFilter())) |
Remi NGUYEN VAN | bd1de20 | 2020-09-18 18:11:42 +0900 | [diff] [blame] | 136 | ?: fail("Packet was not sent on the interface") |
| 137 | |
| 138 | val sentTargetAddr = MacAddress.fromBytes(sentPacket.copyOfRange(0, ETHER_ADDR_LEN)) |
| 139 | assertEquals(TEST_TARGET_MAC, sentTargetAddr, "Destination ethernet address does not match") |
| 140 | |
| 141 | val sentDhcpPacket = sentPacket.copyOfRange( |
| 142 | ETHER_HEADER_LENGTH + IPV4_HEADER_LENGTH + UDP_HEADER_LENGTH, sentPacket.size) |
| 143 | |
| 144 | assertArrayEquals("Sent packet != original packet", originalPacket, sentDhcpPacket) |
| 145 | } |
Remi NGUYEN VAN | 3df5c23 | 2020-09-28 18:48:46 +0900 | [diff] [blame] | 146 | |
| 147 | @Test |
| 148 | fun testAttachRaFilter() { |
| 149 | val socket = Os.socket(AF_PACKET, SOCK_RAW, ETH_P_IPV6) |
| 150 | val ifParams = InterfaceParams.getByName(iface.interfaceName) |
| 151 | ?: fail("Could not obtain interface params for ${iface.interfaceName}") |
| 152 | val socketAddr = SocketUtils.makePacketSocketAddress(ETH_P_IPV6, ifParams.index) |
| 153 | Os.bind(socket, socketAddr) |
| 154 | Os.setsockoptTimeval(socket, SOL_SOCKET, SO_RCVTIMEO, |
| 155 | StructTimeval.fromMillis(TEST_TIMEOUT_MS)) |
| 156 | |
| 157 | // Verify that before setting any filter, the socket receives pings |
| 158 | val echo = Ipv6Utils.buildEchoRequestPacket(TEST_SRC_MAC, TEST_TARGET_MAC, TEST_INET6ADDR_1, |
| 159 | TEST_INET6ADDR_2) |
| 160 | reader.sendResponse(echo) |
| 161 | echo.rewind() |
| 162 | assertNextPacketEquals(socket, echo.readAsArray(), "ICMPv6 echo") |
| 163 | |
| 164 | NetworkStackUtils.attachRaFilter(socket, ARPHRD_ETHER) |
| 165 | // Send another echo, then an RA. After setting the filter expect only the RA. |
| 166 | echo.rewind() |
| 167 | reader.sendResponse(echo) |
| 168 | val pio = PrefixInformationOption.build(IpPrefix("2001:db8:1::/64"), |
| 169 | 0.toByte() /* flags */, 3600 /* validLifetime */, 1800 /* preferredLifetime */) |
| 170 | val ra = Ipv6Utils.buildRaPacket(TEST_SRC_MAC, TEST_TARGET_MAC, |
| 171 | TEST_INET6ADDR_1 /* routerAddr */, IPV6_ADDR_ALL_NODES_MULTICAST, |
| 172 | 0.toByte() /* flags */, 1800 /* lifetime */, 0 /* reachableTime */, |
| 173 | 0 /* retransTimer */, pio) |
| 174 | reader.sendResponse(ra) |
| 175 | ra.rewind() |
| 176 | |
| 177 | assertNextPacketEquals(socket, ra.readAsArray(), "ICMPv6 RA") |
| 178 | } |
| 179 | |
| 180 | private fun assertNextPacketEquals(socket: FileDescriptor, expected: ByteArray, descr: String) { |
| 181 | val buffer = ByteArray(TEST_MTU) |
| 182 | val readPacket = Os.read(socket, buffer, 0 /* byteOffset */, buffer.size) |
| 183 | assertTrue(readPacket > 0, "$descr not received") |
| 184 | assertEquals(expected.size, readPacket, "Received packet size does not match for $descr") |
| 185 | assertArrayEquals("Received packet != expected $descr", |
| 186 | expected, buffer.copyOfRange(0, readPacket)) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | private fun ByteBuffer.readAsArray(): ByteArray { |
| 191 | val out = ByteArray(remaining()) |
| 192 | get(out) |
| 193 | return out |
Remi NGUYEN VAN | bd1de20 | 2020-09-18 18:11:42 +0900 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | private fun <T : Any> Context.assertHasService(manager: KClass<T>) = getSystemService(manager.java) |
Remi NGUYEN VAN | 3df5c23 | 2020-09-28 18:48:46 +0900 | [diff] [blame] | 197 | ?: fail("Could not find service $manager") |