blob: eb105d292a434b38b8bbf45966364f78a8a44b7e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright 2008, The Android Open Source Project
3 *
Elliott Hughesdd66bcb2011-04-12 11:28:59 -07004 * 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 *
Elliott Hughesdd66bcb2011-04-12 11:28:59 -07008 * http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 *
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070010 * 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014 * limitations under the License.
15 */
16
17#define LOG_TAG "NetUtils"
18
19#include "jni.h"
Chad Brubaker12324b42013-07-11 13:30:36 -070020#include "JNIHelp.h"
Paul Jensen38764952014-05-20 11:25:35 -040021#include "NetdClient.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022#include <utils/misc.h>
23#include <android_runtime/AndroidRuntime.h>
24#include <utils/Log.h>
25#include <arpa/inet.h>
Lorenzo Colitticbe4f7c2015-03-06 19:57:39 +090026#include <net/if.h>
27#include <linux/filter.h>
28#include <linux/if.h>
Paul Jensen578a76e2016-01-14 14:54:39 -050029#include <linux/if_arp.h>
Lorenzo Colitticbe4f7c2015-03-06 19:57:39 +090030#include <linux/if_ether.h>
31#include <linux/if_packet.h>
32#include <net/if_ether.h>
Paul Jensen578a76e2016-01-14 14:54:39 -050033#include <netinet/icmp6.h>
Lorenzo Colitticbe4f7c2015-03-06 19:57:39 +090034#include <netinet/ip.h>
Paul Jensen578a76e2016-01-14 14:54:39 -050035#include <netinet/ip6.h>
Lorenzo Colitticbe4f7c2015-03-06 19:57:39 +090036#include <netinet/udp.h>
Robert Greenwalt0216e612011-01-14 16:29:58 -080037#include <cutils/properties.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
Andreas Gampe987f79f2014-11-18 17:29:46 -080039#include "core_jni_helpers.h"
40
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041extern "C" {
Mike Lockwood0900f362009-07-10 17:24:07 -040042int ifc_enable(const char *ifname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043int ifc_disable(const char *ifname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044}
45
46#define NETUTILS_PKG_NAME "android/net/NetworkUtils"
47
48namespace android {
49
Erik Kline473355f2016-10-19 17:42:01 +090050static const uint32_t kEtherTypeOffset = offsetof(ether_header, ether_type);
51static const uint32_t kEtherHeaderLen = sizeof(ether_header);
52static const uint32_t kIPv4Protocol = kEtherHeaderLen + offsetof(iphdr, protocol);
53static const uint32_t kIPv4FlagsOffset = kEtherHeaderLen + offsetof(iphdr, frag_off);
54static const uint32_t kIPv6NextHeader = kEtherHeaderLen + offsetof(ip6_hdr, ip6_nxt);
55static const uint32_t kIPv6PayloadStart = kEtherHeaderLen + sizeof(ip6_hdr);
56static const uint32_t kICMPv6TypeOffset = kIPv6PayloadStart + offsetof(icmp6_hdr, icmp6_type);
57static const uint32_t kUDPSrcPortIndirectOffset = kEtherHeaderLen + offsetof(udphdr, source);
58static const uint32_t kUDPDstPortIndirectOffset = kEtherHeaderLen + offsetof(udphdr, dest);
Lorenzo Colitticbe4f7c2015-03-06 19:57:39 +090059static const uint16_t kDhcpClientPort = 68;
60
Lorenzo Colitticbe4f7c2015-03-06 19:57:39 +090061static void android_net_utils_attachDhcpFilter(JNIEnv *env, jobject clazz, jobject javaFd)
62{
Lorenzo Colitticbe4f7c2015-03-06 19:57:39 +090063 struct sock_filter filter_code[] = {
64 // Check the protocol is UDP.
Erik Kline473355f2016-10-19 17:42:01 +090065 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, kIPv4Protocol),
Lorenzo Colitticbe4f7c2015-03-06 19:57:39 +090066 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_UDP, 0, 6),
67
68 // Check this is not a fragment.
Erik Kline473355f2016-10-19 17:42:01 +090069 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, kIPv4FlagsOffset),
70 BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, IP_OFFMASK, 4, 0),
Lorenzo Colitticbe4f7c2015-03-06 19:57:39 +090071
72 // Get the IP header length.
Erik Kline473355f2016-10-19 17:42:01 +090073 BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, kEtherHeaderLen),
Lorenzo Colitticbe4f7c2015-03-06 19:57:39 +090074
75 // Check the destination port.
Erik Kline473355f2016-10-19 17:42:01 +090076 BPF_STMT(BPF_LD | BPF_H | BPF_IND, kUDPDstPortIndirectOffset),
Lorenzo Colitticbe4f7c2015-03-06 19:57:39 +090077 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, kDhcpClientPort, 0, 1),
78
79 // Accept or reject.
80 BPF_STMT(BPF_RET | BPF_K, 0xffff),
81 BPF_STMT(BPF_RET | BPF_K, 0)
82 };
83 struct sock_fprog filter = {
84 sizeof(filter_code) / sizeof(filter_code[0]),
85 filter_code,
86 };
87
Paul Jensen578a76e2016-01-14 14:54:39 -050088 int fd = jniGetFDFromFileDescriptor(env, javaFd);
89 if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)) != 0) {
90 jniThrowExceptionFmt(env, "java/net/SocketException",
91 "setsockopt(SO_ATTACH_FILTER): %s", strerror(errno));
92 }
93}
94
95static void android_net_utils_attachRaFilter(JNIEnv *env, jobject clazz, jobject javaFd,
96 jint hardwareAddressType)
97{
98 if (hardwareAddressType != ARPHRD_ETHER) {
99 jniThrowExceptionFmt(env, "java/net/SocketException",
100 "attachRaFilter only supports ARPHRD_ETHER");
101 return;
102 }
103
Paul Jensen578a76e2016-01-14 14:54:39 -0500104 struct sock_filter filter_code[] = {
105 // Check IPv6 Next Header is ICMPv6.
Erik Kline473355f2016-10-19 17:42:01 +0900106 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, kIPv6NextHeader),
Paul Jensen578a76e2016-01-14 14:54:39 -0500107 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_ICMPV6, 0, 3),
108
109 // Check ICMPv6 type is Router Advertisement.
Erik Kline473355f2016-10-19 17:42:01 +0900110 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, kICMPv6TypeOffset),
Paul Jensen578a76e2016-01-14 14:54:39 -0500111 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ND_ROUTER_ADVERT, 0, 1),
112
113 // Accept or reject.
114 BPF_STMT(BPF_RET | BPF_K, 0xffff),
115 BPF_STMT(BPF_RET | BPF_K, 0)
116 };
117 struct sock_fprog filter = {
118 sizeof(filter_code) / sizeof(filter_code[0]),
119 filter_code,
120 };
121
122 int fd = jniGetFDFromFileDescriptor(env, javaFd);
Lorenzo Colitticbe4f7c2015-03-06 19:57:39 +0900123 if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)) != 0) {
124 jniThrowExceptionFmt(env, "java/net/SocketException",
125 "setsockopt(SO_ATTACH_FILTER): %s", strerror(errno));
126 }
127}
128
Erik Kline473355f2016-10-19 17:42:01 +0900129// TODO: Move all this filter code into libnetutils.
130static void android_net_utils_attachControlPacketFilter(
131 JNIEnv *env, jobject clazz, jobject javaFd, jint hardwareAddressType) {
132 if (hardwareAddressType != ARPHRD_ETHER) {
133 jniThrowExceptionFmt(env, "java/net/SocketException",
134 "attachControlPacketFilter only supports ARPHRD_ETHER");
135 return;
136 }
137
138 // Capture all:
139 // - ARPs
140 // - DHCPv4 packets
141 // - Router Advertisements & Solicitations
142 // - Neighbor Advertisements & Solicitations
143 //
144 // tcpdump:
145 // arp or
146 // '(ip and udp port 68)' or
147 // '(icmp6 and ip6[40] >= 133 and ip6[40] <= 136)'
148 struct sock_filter filter_code[] = {
149 // Load the link layer next payload field.
150 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, kEtherTypeOffset),
151
152 // Accept all ARP.
153 // TODO: Figure out how to better filter ARPs on noisy networks.
154 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ETHERTYPE_ARP, 16, 0),
155
156 // If IPv4:
157 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ETHERTYPE_IP, 0, 9),
158
159 // Check the protocol is UDP.
160 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, kIPv4Protocol),
161 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_UDP, 0, 14),
162
163 // Check this is not a fragment.
164 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, kIPv4FlagsOffset),
165 BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, IP_OFFMASK, 12, 0),
166
167 // Get the IP header length.
168 BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, kEtherHeaderLen),
169
170 // Check the source port.
171 BPF_STMT(BPF_LD | BPF_H | BPF_IND, kUDPSrcPortIndirectOffset),
172 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, kDhcpClientPort, 8, 0),
173
174 // Check the destination port.
175 BPF_STMT(BPF_LD | BPF_H | BPF_IND, kUDPDstPortIndirectOffset),
176 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, kDhcpClientPort, 6, 7),
177
178 // IPv6 ...
179 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ETHERTYPE_IPV6, 0, 6),
180 // ... check IPv6 Next Header is ICMPv6 (ignore fragments), ...
181 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, kIPv6NextHeader),
182 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_ICMPV6, 0, 4),
183 // ... and check the ICMPv6 type is one of RS/RA/NS/NA.
184 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, kICMPv6TypeOffset),
185 BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, ND_ROUTER_SOLICIT, 0, 2),
186 BPF_JUMP(BPF_JMP | BPF_JGT | BPF_K, ND_NEIGHBOR_ADVERT, 1, 0),
187
188 // Accept or reject.
189 BPF_STMT(BPF_RET | BPF_K, 0xffff),
190 BPF_STMT(BPF_RET | BPF_K, 0)
191 };
192 struct sock_fprog filter = {
193 sizeof(filter_code) / sizeof(filter_code[0]),
194 filter_code,
195 };
196
197 int fd = jniGetFDFromFileDescriptor(env, javaFd);
198 if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)) != 0) {
199 jniThrowExceptionFmt(env, "java/net/SocketException",
200 "setsockopt(SO_ATTACH_FILTER): %s", strerror(errno));
201 }
202}
203
Erik Klinea3ca6bd2016-05-24 20:12:08 +0900204static void android_net_utils_setupRaSocket(JNIEnv *env, jobject clazz, jobject javaFd,
205 jint ifIndex)
206{
207 static const int kLinkLocalHopLimit = 255;
208
209 int fd = jniGetFDFromFileDescriptor(env, javaFd);
210
211 // Set an ICMPv6 filter that only passes Router Solicitations.
212 struct icmp6_filter rs_only;
213 ICMP6_FILTER_SETBLOCKALL(&rs_only);
214 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &rs_only);
215 socklen_t len = sizeof(rs_only);
216 if (setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &rs_only, len) != 0) {
217 jniThrowExceptionFmt(env, "java/net/SocketException",
218 "setsockopt(ICMP6_FILTER): %s", strerror(errno));
219 return;
220 }
221
222 // Most/all of the rest of these options can be set via Java code, but
223 // because we're here on account of setting an icmp6_filter go ahead
224 // and do it all natively for now.
225 //
226 // TODO: Consider moving these out to Java.
227
228 // Set the multicast hoplimit to 255 (link-local only).
229 int hops = kLinkLocalHopLimit;
230 len = sizeof(hops);
231 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops, len) != 0) {
232 jniThrowExceptionFmt(env, "java/net/SocketException",
233 "setsockopt(IPV6_MULTICAST_HOPS): %s", strerror(errno));
234 return;
235 }
236
237 // Set the unicast hoplimit to 255 (link-local only).
238 hops = kLinkLocalHopLimit;
239 len = sizeof(hops);
240 if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hops, len) != 0) {
241 jniThrowExceptionFmt(env, "java/net/SocketException",
242 "setsockopt(IPV6_UNICAST_HOPS): %s", strerror(errno));
243 return;
244 }
245
246 // Explicitly disable multicast loopback.
247 int off = 0;
248 len = sizeof(off);
249 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &off, len) != 0) {
250 jniThrowExceptionFmt(env, "java/net/SocketException",
251 "setsockopt(IPV6_MULTICAST_LOOP): %s", strerror(errno));
252 return;
253 }
254
255 // Specify the IPv6 interface to use for outbound multicast.
256 len = sizeof(ifIndex);
257 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifIndex, len) != 0) {
258 jniThrowExceptionFmt(env, "java/net/SocketException",
259 "setsockopt(IPV6_MULTICAST_IF): %s", strerror(errno));
260 return;
261 }
262
263 // Additional options to be considered:
264 // - IPV6_TCLASS
265 // - IPV6_RECVPKTINFO
266 // - IPV6_RECVHOPLIMIT
267
268 // Bind to [::].
269 const struct sockaddr_in6 sin6 = {
270 .sin6_family = AF_INET6,
271 .sin6_port = 0,
272 .sin6_flowinfo = 0,
273 .sin6_addr = IN6ADDR_ANY_INIT,
274 .sin6_scope_id = 0,
275 };
276 auto sa = reinterpret_cast<const struct sockaddr *>(&sin6);
277 len = sizeof(sin6);
278 if (bind(fd, sa, len) != 0) {
279 jniThrowExceptionFmt(env, "java/net/SocketException",
280 "bind(IN6ADDR_ANY): %s", strerror(errno));
281 return;
282 }
283
284 // Join the all-routers multicast group, ff02::2%index.
285 struct ipv6_mreq all_rtrs = {
286 .ipv6mr_multiaddr = {{{0xff,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2}}},
287 .ipv6mr_interface = ifIndex,
288 };
289 len = sizeof(all_rtrs);
290 if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &all_rtrs, len) != 0) {
291 jniThrowExceptionFmt(env, "java/net/SocketException",
292 "setsockopt(IPV6_JOIN_GROUP): %s", strerror(errno));
293 return;
294 }
295}
296
Paul Jensen32a58f02014-06-20 13:58:14 -0400297static jboolean android_net_utils_bindProcessToNetwork(JNIEnv *env, jobject thiz, jint netId)
Paul Jensen38764952014-05-20 11:25:35 -0400298{
Paul Jensen32a58f02014-06-20 13:58:14 -0400299 return (jboolean) !setNetworkForProcess(netId);
Paul Jensen38764952014-05-20 11:25:35 -0400300}
301
Paul Jensen72db88e2015-03-10 10:54:12 -0400302static jint android_net_utils_getBoundNetworkForProcess(JNIEnv *env, jobject thiz)
Paul Jensen38764952014-05-20 11:25:35 -0400303{
304 return getNetworkForProcess();
305}
306
Paul Jensen32a58f02014-06-20 13:58:14 -0400307static jboolean android_net_utils_bindProcessToNetworkForHostResolution(JNIEnv *env, jobject thiz,
308 jint netId)
Paul Jensen38764952014-05-20 11:25:35 -0400309{
Paul Jensen32a58f02014-06-20 13:58:14 -0400310 return (jboolean) !setNetworkForResolv(netId);
Paul Jensen38764952014-05-20 11:25:35 -0400311}
312
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -0700313static jint android_net_utils_bindSocketToNetwork(JNIEnv *env, jobject thiz, jint socket,
Paul Jensen32a58f02014-06-20 13:58:14 -0400314 jint netId)
Paul Jensen38764952014-05-20 11:25:35 -0400315{
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -0700316 return setNetworkForSocket(netId, socket);
Paul Jensen38764952014-05-20 11:25:35 -0400317}
318
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400319static jboolean android_net_utils_protectFromVpn(JNIEnv *env, jobject thiz, jint socket)
320{
321 return (jboolean) !protectFromVpn(socket);
322}
323
Paul Jensencee9b512015-05-06 07:32:40 -0400324static jboolean android_net_utils_queryUserAccess(JNIEnv *env, jobject thiz, jint uid, jint netId)
325{
326 return (jboolean) !queryUserAccess(uid, netId);
327}
328
Lorenzo Colitticbe4f7c2015-03-06 19:57:39 +0900329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330// ----------------------------------------------------------------------------
331
332/*
333 * JNI registration.
334 */
Daniel Micay76f6a862015-09-19 17:31:01 -0400335static const JNINativeMethod gNetworkUtilMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 /* name, signature, funcPtr */
Paul Jensen32a58f02014-06-20 13:58:14 -0400337 { "bindProcessToNetwork", "(I)Z", (void*) android_net_utils_bindProcessToNetwork },
Paul Jensen72db88e2015-03-10 10:54:12 -0400338 { "getBoundNetworkForProcess", "()I", (void*) android_net_utils_getBoundNetworkForProcess },
Paul Jensen32a58f02014-06-20 13:58:14 -0400339 { "bindProcessToNetworkForHostResolution", "(I)Z", (void*) android_net_utils_bindProcessToNetworkForHostResolution },
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -0700340 { "bindSocketToNetwork", "(II)I", (void*) android_net_utils_bindSocketToNetwork },
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400341 { "protectFromVpn", "(I)Z", (void*)android_net_utils_protectFromVpn },
Paul Jensencee9b512015-05-06 07:32:40 -0400342 { "queryUserAccess", "(II)Z", (void*)android_net_utils_queryUserAccess },
Lorenzo Colitticbe4f7c2015-03-06 19:57:39 +0900343 { "attachDhcpFilter", "(Ljava/io/FileDescriptor;)V", (void*) android_net_utils_attachDhcpFilter },
Paul Jensen578a76e2016-01-14 14:54:39 -0500344 { "attachRaFilter", "(Ljava/io/FileDescriptor;I)V", (void*) android_net_utils_attachRaFilter },
Erik Kline473355f2016-10-19 17:42:01 +0900345 { "attachControlPacketFilter", "(Ljava/io/FileDescriptor;I)V", (void*) android_net_utils_attachControlPacketFilter },
Erik Klinea3ca6bd2016-05-24 20:12:08 +0900346 { "setupRaSocket", "(Ljava/io/FileDescriptor;I)V", (void*) android_net_utils_setupRaSocket },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347};
348
349int register_android_net_NetworkUtils(JNIEnv* env)
350{
Andreas Gampe987f79f2014-11-18 17:29:46 -0800351 return RegisterMethodsOrDie(env, NETUTILS_PKG_NAME, gNetworkUtilMethods,
352 NELEM(gNetworkUtilMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353}
354
355}; // namespace android