2 * Copyright 2008, The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "NetUtils"
21 #include "NetdClient.h"
22 #include <utils/misc.h>
23 #include <android_runtime/AndroidRuntime.h>
24 #include <utils/Log.h>
25 #include <arpa/inet.h>
27 #include <linux/filter.h>
29 #include <linux/if_arp.h>
30 #include <linux/if_ether.h>
31 #include <linux/if_packet.h>
32 #include <net/if_ether.h>
33 #include <netinet/icmp6.h>
34 #include <netinet/ip.h>
35 #include <netinet/ip6.h>
36 #include <netinet/udp.h>
37 #include <cutils/properties.h>
39 #include "core_jni_helpers.h"
42 int ifc_enable(const char *ifname);
43 int ifc_disable(const char *ifname);
46 #define NETUTILS_PKG_NAME "android/net/NetworkUtils"
50 static const uint16_t kDhcpClientPort = 68;
52 static void android_net_utils_attachDhcpFilter(JNIEnv *env, jobject clazz, jobject javaFd)
54 uint32_t ip_offset = sizeof(ether_header);
55 uint32_t proto_offset = ip_offset + offsetof(iphdr, protocol);
56 uint32_t flags_offset = ip_offset + offsetof(iphdr, frag_off);
57 uint32_t dport_indirect_offset = ip_offset + offsetof(udphdr, dest);
58 struct sock_filter filter_code[] = {
59 // Check the protocol is UDP.
60 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, proto_offset),
61 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_UDP, 0, 6),
63 // Check this is not a fragment.
64 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, flags_offset),
65 BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 0x1fff, 4, 0),
67 // Get the IP header length.
68 BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, ip_offset),
70 // Check the destination port.
71 BPF_STMT(BPF_LD | BPF_H | BPF_IND, dport_indirect_offset),
72 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, kDhcpClientPort, 0, 1),
75 BPF_STMT(BPF_RET | BPF_K, 0xffff),
76 BPF_STMT(BPF_RET | BPF_K, 0)
78 struct sock_fprog filter = {
79 sizeof(filter_code) / sizeof(filter_code[0]),
83 int fd = jniGetFDFromFileDescriptor(env, javaFd);
84 if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)) != 0) {
85 jniThrowExceptionFmt(env, "java/net/SocketException",
86 "setsockopt(SO_ATTACH_FILTER): %s", strerror(errno));
90 static void android_net_utils_attachRaFilter(JNIEnv *env, jobject clazz, jobject javaFd,
91 jint hardwareAddressType)
93 if (hardwareAddressType != ARPHRD_ETHER) {
94 jniThrowExceptionFmt(env, "java/net/SocketException",
95 "attachRaFilter only supports ARPHRD_ETHER");
99 uint32_t ipv6_offset = sizeof(ether_header);
100 uint32_t ipv6_next_header_offset = ipv6_offset + offsetof(ip6_hdr, ip6_nxt);
101 uint32_t icmp6_offset = ipv6_offset + sizeof(ip6_hdr);
102 uint32_t icmp6_type_offset = icmp6_offset + offsetof(icmp6_hdr, icmp6_type);
103 struct sock_filter filter_code[] = {
104 // Check IPv6 Next Header is ICMPv6.
105 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, ipv6_next_header_offset),
106 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_ICMPV6, 0, 3),
108 // Check ICMPv6 type is Router Advertisement.
109 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, icmp6_type_offset),
110 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ND_ROUTER_ADVERT, 0, 1),
113 BPF_STMT(BPF_RET | BPF_K, 0xffff),
114 BPF_STMT(BPF_RET | BPF_K, 0)
116 struct sock_fprog filter = {
117 sizeof(filter_code) / sizeof(filter_code[0]),
121 int fd = jniGetFDFromFileDescriptor(env, javaFd);
122 if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)) != 0) {
123 jniThrowExceptionFmt(env, "java/net/SocketException",
124 "setsockopt(SO_ATTACH_FILTER): %s", strerror(errno));
128 static jboolean android_net_utils_bindProcessToNetwork(JNIEnv *env, jobject thiz, jint netId)
130 return (jboolean) !setNetworkForProcess(netId);
133 static jint android_net_utils_getBoundNetworkForProcess(JNIEnv *env, jobject thiz)
135 return getNetworkForProcess();
138 static jboolean android_net_utils_bindProcessToNetworkForHostResolution(JNIEnv *env, jobject thiz,
141 return (jboolean) !setNetworkForResolv(netId);
144 static jint android_net_utils_bindSocketToNetwork(JNIEnv *env, jobject thiz, jint socket,
147 return setNetworkForSocket(netId, socket);
150 static jboolean android_net_utils_protectFromVpn(JNIEnv *env, jobject thiz, jint socket)
152 return (jboolean) !protectFromVpn(socket);
155 static jboolean android_net_utils_queryUserAccess(JNIEnv *env, jobject thiz, jint uid, jint netId)
157 return (jboolean) !queryUserAccess(uid, netId);
161 // ----------------------------------------------------------------------------
166 static const JNINativeMethod gNetworkUtilMethods[] = {
167 /* name, signature, funcPtr */
168 { "bindProcessToNetwork", "(I)Z", (void*) android_net_utils_bindProcessToNetwork },
169 { "getBoundNetworkForProcess", "()I", (void*) android_net_utils_getBoundNetworkForProcess },
170 { "bindProcessToNetworkForHostResolution", "(I)Z", (void*) android_net_utils_bindProcessToNetworkForHostResolution },
171 { "bindSocketToNetwork", "(II)I", (void*) android_net_utils_bindSocketToNetwork },
172 { "protectFromVpn", "(I)Z", (void*)android_net_utils_protectFromVpn },
173 { "queryUserAccess", "(II)Z", (void*)android_net_utils_queryUserAccess },
174 { "attachDhcpFilter", "(Ljava/io/FileDescriptor;)V", (void*) android_net_utils_attachDhcpFilter },
175 { "attachRaFilter", "(Ljava/io/FileDescriptor;I)V", (void*) android_net_utils_attachRaFilter },
178 int register_android_net_NetworkUtils(JNIEnv* env)
180 return RegisterMethodsOrDie(env, NETUTILS_PKG_NAME, gNetworkUtilMethods,
181 NELEM(gNetworkUtilMethods));
184 }; // namespace android