1 /* 2 * Copyright (C) 2013 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.ipv6.cts; 18 19 import android.test.AndroidTestCase; 20 import android.util.Log; 21 22 import android.system.ErrnoException; 23 import android.system.Os; 24 import android.system.StructTimeval; 25 import static android.system.OsConstants.*; 26 27 import java.io.FileDescriptor; 28 import java.io.IOException; 29 import java.net.InetAddress; 30 import java.net.Inet6Address; 31 import java.net.InetSocketAddress; 32 import java.net.UnknownHostException; 33 import java.nio.ByteBuffer; 34 import java.util.Arrays; 35 import java.util.Random; 36 37 /** 38 * Checks that the device has kernel support for the IPv6 ping socket. This allows ping6 to work 39 * without root privileges. The necessary kernel code is in Linux 3.11 or above, or the 40 * <code>common/android-3.x</code> kernel trees. If you are not running one of these kernels, the 41 * functionality can be obtained by cherry-picking the following patches from David Miller's 42 * <code>net-next</code> tree: 43 * <ul> 44 * <li>6d0bfe2 net: ipv6: Add IPv6 support to the ping socket. 45 * <li>c26d6b4 ping: always initialize ->sin6_scope_id and ->sin6_flowinfo 46 * <li>fbfe80c net: ipv6: fix wrong ping_v6_sendmsg return value 47 * <li>a1bdc45 net: ipv6: add missing lock in ping_v6_sendmsg 48 * <li>cf970c0 ping: prevent NULL pointer dereference on write to msg_name 49 * </ul> 50 * or the equivalent backports to the <code>common/android-3.x</code> trees. 51 */ 52 public class PingTest extends AndroidTestCase { 53 /** Maximum size of the packets we're using to test. */ 54 private static final int MAX_SIZE = 4096; 55 56 /** Size of the ICMPv6 header. */ 57 private static final int ICMP_HEADER_SIZE = 8; 58 59 /** Number of packets to test. */ 60 private static final int NUM_PACKETS = 10; 61 62 /** The beginning of an ICMPv6 echo request: type, code, and uninitialized checksum. */ 63 private static final byte[] PING_HEADER = new byte[] { 64 (byte) ICMP6_ECHO_REQUEST, (byte) 0x00, (byte) 0x00, (byte) 0x00 65 }; 66 67 /** 68 * Returns a byte array containing an ICMPv6 echo request with the specified payload length. 69 */ pingPacket(int payloadLength)70 private byte[] pingPacket(int payloadLength) { 71 byte[] packet = new byte[payloadLength + ICMP_HEADER_SIZE]; 72 new Random().nextBytes(packet); 73 System.arraycopy(PING_HEADER, 0, packet, 0, PING_HEADER.length); 74 return packet; 75 } 76 77 /** 78 * Checks that the first length bytes of two byte arrays are equal. 79 */ assertArrayBytesEqual(byte[] expected, byte[] actual, int length)80 private void assertArrayBytesEqual(byte[] expected, byte[] actual, int length) { 81 for (int i = 0; i < length; i++) { 82 assertEquals("Arrays differ at index " + i + ":", expected[i], actual[i]); 83 } 84 } 85 86 /** 87 * Creates an IPv6 ping socket and sets a receive timeout of 100ms. 88 */ createPingSocket()89 private FileDescriptor createPingSocket() throws ErrnoException { 90 FileDescriptor s = Os.socket(AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6); 91 Os.setsockoptTimeval(s, SOL_SOCKET, SO_RCVTIMEO, StructTimeval.fromMillis(100)); 92 return s; 93 } 94 95 /** 96 * Sends a ping packet to a random port on the specified address on the specified socket. 97 */ sendPing(FileDescriptor s, InetAddress address, byte[] packet)98 private void sendPing(FileDescriptor s, 99 InetAddress address, byte[] packet) throws ErrnoException, IOException { 100 // Pick a random port. Choose a range that gives a reasonable chance of picking a low port. 101 int port = (int) (Math.random() * 2048); 102 103 // Send the packet. 104 int ret = Os.sendto(s, ByteBuffer.wrap(packet), 0, address, port); 105 assertEquals(packet.length, ret); 106 } 107 108 /** 109 * Checks that a socket has received a response appropriate to the specified packet. 110 */ checkResponse(FileDescriptor s, InetAddress dest, byte[] sent, boolean useRecvfrom)111 private void checkResponse(FileDescriptor s, InetAddress dest, 112 byte[] sent, boolean useRecvfrom) throws ErrnoException, IOException { 113 ByteBuffer responseBuffer = ByteBuffer.allocate(MAX_SIZE); 114 int bytesRead; 115 116 // Receive the response. 117 if (useRecvfrom) { 118 InetSocketAddress from = new InetSocketAddress(0); 119 bytesRead = Os.recvfrom(s, responseBuffer, 0, from); 120 121 // Check the source address and scope ID. 122 assertTrue(from.getAddress() instanceof Inet6Address); 123 Inet6Address fromAddress = (Inet6Address) from.getAddress(); 124 assertEquals(0, fromAddress.getScopeId()); 125 assertNull(fromAddress.getScopedInterface()); 126 assertEquals(dest.getHostAddress(), fromAddress.getHostAddress()); 127 } else { 128 bytesRead = Os.read(s, responseBuffer); 129 } 130 131 // Check the packet length. 132 assertEquals(sent.length, bytesRead); 133 134 // Check the response is an echo reply. 135 byte[] response = new byte[bytesRead]; 136 responseBuffer.flip(); 137 responseBuffer.get(response, 0, bytesRead); 138 assertEquals((byte) ICMP6_ECHO_REPLY, response[0]); 139 140 // Find out what ICMP ID was used in the packet that was sent. 141 int id = ((InetSocketAddress) Os.getsockname(s)).getPort(); 142 sent[4] = (byte) (id / 256); 143 sent[5] = (byte) (id % 256); 144 145 // Ensure the response is the same as the packet, except for the type (which is 0x81) 146 // and the ID and checksum, which are set by the kernel. 147 response[0] = (byte) 0x80; // Type. 148 response[2] = response[3] = (byte) 0x00; // Checksum. 149 assertArrayBytesEqual(response, sent, bytesRead); 150 } 151 152 /** 153 * Sends NUM_PACKETS random ping packets to ::1 and checks the replies. 154 */ testLoopbackPing()155 public void testLoopbackPing() throws ErrnoException, IOException { 156 // Generate a random ping packet and send it to localhost. 157 InetAddress ipv6Loopback = InetAddress.getByName(null); 158 assertEquals("::1", ipv6Loopback.getHostAddress()); 159 160 for (int i = 0; i < NUM_PACKETS; i++) { 161 byte[] packet = pingPacket((int) (Math.random() * (MAX_SIZE - ICMP_HEADER_SIZE))); 162 FileDescriptor s = createPingSocket(); 163 // Use both recvfrom and read(). 164 sendPing(s, ipv6Loopback, packet); 165 checkResponse(s, ipv6Loopback, packet, true); 166 sendPing(s, ipv6Loopback, packet); 167 checkResponse(s, ipv6Loopback, packet, false); 168 // Check closing the socket doesn't raise an exception. 169 Os.close(s); 170 } 171 } 172 } 173