1 /* 2 * Copyright 2010, 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 #include "AsynchronousSocketCloseMonitor.h" 18 #include "JNIHelp.h" 19 #include "JniException.h" 20 #include "JniConstants.h" 21 #include "NetFd.h" 22 #include "NetworkUtilities.h" 23 #include "ScopedUtfChars.h" 24 #include "ScopedPrimitiveArray.h" 25 26 #include "jni.h" 27 28 #include <sys/types.h> 29 #include <sys/socket.h> 30 #include <linux/rtnetlink.h> 31 #include <net/if.h> 32 #include <linux/if_ether.h> 33 #include <linux/if_packet.h> 34 #include <arpa/inet.h> 35 #include <errno.h> 36 #include <fcntl.h> 37 #include <poll.h> 38 #include <netinet/ip.h> 39 #include <linux/udp.h> 40 41 union GCC_HIDDEN sockunion { 42 sockaddr sa; 43 sockaddr_ll sll; 44 } su; 45 46 /* 47 * Creates a socket suitable for raw socket operations. The socket is 48 * bound to the interface specified by the supplied name. The socket 49 * value is placed into the supplied FileDescriptor instance. 50 * 51 * TODO(chesnutt): consider breaking this into pieces: create a 52 * variety of constructors for different socket types, then a generic 53 * setBlocking() method followed by polymorphic bind(). 54 */ 55 static void RawSocket_create(JNIEnv* env, jclass, jobject fileDescriptor, 56 jstring interfaceName) { 57 58 ScopedUtfChars ifname(env, interfaceName); 59 if (ifname.c_str() == NULL) { 60 return; 61 } 62 63 short protocol = ETH_P_IP; 64 sockunion su; 65 memset(&su, 0, sizeof(su)); 66 su.sll.sll_family = PF_PACKET; 67 su.sll.sll_protocol = htons(protocol); 68 su.sll.sll_ifindex = if_nametoindex(ifname.c_str()); 69 70 int sock = socket(PF_PACKET, SOCK_DGRAM, htons(protocol)); 71 if (sock == -1) { 72 LOGE("Can't create socket %s", strerror(errno)); 73 jniThrowSocketException(env, errno); 74 return; 75 } 76 77 jniSetFileDescriptorOfFD(env, fileDescriptor, sock); 78 if (!setBlocking(sock, false)) { 79 LOGE("Can't set non-blocking mode on socket %s", strerror(errno)); 80 jniThrowSocketException(env, errno); 81 return; 82 } 83 84 int err = bind(sock, &su.sa, sizeof(su)); 85 if (err != 0) { 86 LOGE("Socket bind error %s", strerror(errno)); 87 jniThrowSocketException(env, errno); 88 return; 89 } 90 } 91 92 /* 93 * Writes the L3 (IP) packet to the raw socket supplied in the 94 * FileDescriptor instance. 95 * 96 * Assumes that the caller has validated the offset & byteCount values. 97 */ 98 static int RawSocket_sendPacket(JNIEnv* env, jclass, jobject fileDescriptor, 99 jstring interfaceName, jbyteArray destMac, jbyteArray packet, jint offset, 100 jint byteCount) 101 { 102 NetFd fd(env, fileDescriptor); 103 104 if (fd.isClosed()) { 105 return 0; 106 } 107 108 ScopedUtfChars ifname(env, interfaceName); 109 if (ifname.c_str() == NULL) { 110 return 0; 111 } 112 113 ScopedByteArrayRO byteArray(env, packet); 114 if (byteArray.get() == NULL) { 115 return 0; 116 } 117 118 ScopedByteArrayRO mac(env, destMac); 119 if (mac.get() == NULL) { 120 return 0; 121 } 122 123 short protocol = ETH_P_IP; 124 sockunion su; 125 memset(&su, 0, sizeof(su)); 126 su.sll.sll_hatype = htons(1); // ARPHRD_ETHER 127 su.sll.sll_halen = mac.size(); 128 memcpy(&su.sll.sll_addr, mac.get(), mac.size()); 129 su.sll.sll_family = AF_PACKET; 130 su.sll.sll_protocol = htons(protocol); 131 su.sll.sll_ifindex = if_nametoindex(ifname.c_str()); 132 133 int err; 134 { 135 int intFd = fd.get(); 136 AsynchronousSocketCloseMonitor monitor(intFd); 137 err = NET_FAILURE_RETRY(fd, sendto(intFd, byteArray.get() + offset, 138 byteCount, 0, &su.sa, sizeof(su))); 139 } 140 141 return err; 142 } 143 144 /* 145 * Reads a network packet into the user-supplied buffer. Return the 146 * length of the packet, or a 0 if there was a timeout or an 147 * unacceptable packet was acquired. 148 * 149 * Assumes that the caller has validated the offset & byteCount values. 150 */ 151 static jint RawSocket_recvPacket(JNIEnv* env, jclass, jobject fileDescriptor, 152 jbyteArray packet, jint offset, jint byteCount, jint port, 153 jint timeout_millis) 154 { 155 NetFd fd(env, fileDescriptor); 156 if (fd.isClosed()) { 157 return 0; 158 } 159 160 ScopedByteArrayRW body(env, packet); 161 jbyte* packetData = body.get(); 162 if (packetData == NULL) { 163 return 0; 164 } 165 166 packetData += offset; 167 168 pollfd fds[1]; 169 fds[0].fd = fd.get(); 170 fds[0].events = POLLIN; 171 int retval = poll(fds, 1, timeout_millis); 172 if (retval <= 0) { 173 return 0; 174 } 175 176 unsigned int size = 0; 177 { 178 int packetSize = byteCount; 179 int intFd = fd.get(); 180 AsynchronousSocketCloseMonitor monitor(intFd); 181 size = NET_FAILURE_RETRY(fd, read(intFd, packetData, packetSize)); 182 } 183 184 if (env->ExceptionOccurred()) { 185 return 0; 186 } 187 188 // quick check for UDP type & UDP port 189 // the packet is an IP header, UDP header, and UDP payload 190 if ((size < (sizeof(iphdr) + sizeof(udphdr)))) { 191 return 0; // runt packet 192 } 193 194 u_int8_t ip_proto = ((iphdr *) packetData)->protocol; 195 if (ip_proto != IPPROTO_UDP) { 196 return 0; // something other than UDP 197 } 198 199 __be16 destPort = htons((reinterpret_cast<udphdr*>(packetData + sizeof(iphdr)))->dest); 200 if (destPort != port) { 201 return 0; // something other than requested port 202 } 203 204 return size; 205 } 206 207 static JNINativeMethod gRawMethods[] = { 208 NATIVE_METHOD(RawSocket, create, "(Ljava/io/FileDescriptor;Ljava/lang/String;)V"), 209 NATIVE_METHOD(RawSocket, sendPacket, "(Ljava/io/FileDescriptor;Ljava/lang/String;[B[BII)I"), 210 NATIVE_METHOD(RawSocket, recvPacket, "(Ljava/io/FileDescriptor;[BIIII)I"), 211 }; 212 213 int register_libcore_net_RawSocket(JNIEnv* env) { 214 return jniRegisterNativeMethods(env, "libcore/net/RawSocket", gRawMethods, NELEM(gRawMethods)); 215 } 216