blob: 60dacd4f74edd75192c779125c33c2be8528acaf [file] [log] [blame]
markchienf87ebdc2019-12-07 22:02:28 +08001/*
2 * Copyright (C) 2017 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 <errno.h>
18#include <error.h>
markchienf87ebdc2019-12-07 22:02:28 +080019#include <jni.h>
markchien016ee972020-11-04 21:57:43 +080020#include <linux/filter.h>
markchienf87ebdc2019-12-07 22:02:28 +080021#include <nativehelper/JNIHelp.h>
22#include <nativehelper/ScopedUtfChars.h>
markchienf87ebdc2019-12-07 22:02:28 +080023#include <net/if.h>
markchien016ee972020-11-04 21:57:43 +080024#include <netinet/ether.h>
25#include <netinet/ip6.h>
markchienf87ebdc2019-12-07 22:02:28 +080026#include <netinet/icmp6.h>
27#include <sys/socket.h>
markchien016ee972020-11-04 21:57:43 +080028#include <stdio.h>
markchienf87ebdc2019-12-07 22:02:28 +080029
30#define LOG_TAG "TetheringUtils"
markchien547e1682020-02-05 12:42:25 +080031#include <android/log.h>
markchienf87ebdc2019-12-07 22:02:28 +080032
33namespace android {
34
markchien016ee972020-11-04 21:57:43 +080035static const uint32_t kIPv6NextHeaderOffset = offsetof(ip6_hdr, ip6_nxt);
36static const uint32_t kIPv6PayloadStart = sizeof(ip6_hdr);
37static const uint32_t kICMPv6TypeOffset = kIPv6PayloadStart + offsetof(icmp6_hdr, icmp6_type);
38
39static void android_net_util_setupIcmpFilter(JNIEnv *env, jobject javaFd, uint32_t type) {
40 sock_filter filter_code[] = {
41 // Check header is ICMPv6.
42 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, kIPv6NextHeaderOffset),
43 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_ICMPV6, 0, 3),
44
45 // Check ICMPv6 type.
46 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, kICMPv6TypeOffset),
47 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, type, 0, 1),
48
49 // Accept or reject.
50 BPF_STMT(BPF_RET | BPF_K, 0xffff),
51 BPF_STMT(BPF_RET | BPF_K, 0)
52 };
53
54 const sock_fprog filter = {
55 sizeof(filter_code) / sizeof(filter_code[0]),
56 filter_code,
57 };
58
59 int fd = jniGetFDFromFileDescriptor(env, javaFd);
60 if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)) != 0) {
61 jniThrowExceptionFmt(env, "java/net/SocketException",
62 "setsockopt(SO_ATTACH_FILTER): %s", strerror(errno));
63 }
64}
65
66static void android_net_util_setupNaSocket(JNIEnv *env, jobject clazz, jobject javaFd)
67{
68 android_net_util_setupIcmpFilter(env, javaFd, ND_NEIGHBOR_ADVERT);
69}
70
71static void android_net_util_setupNsSocket(JNIEnv *env, jobject clazz, jobject javaFd)
72{
73 android_net_util_setupIcmpFilter(env, javaFd, ND_NEIGHBOR_SOLICIT);
74}
75
markchienf87ebdc2019-12-07 22:02:28 +080076static void android_net_util_setupRaSocket(JNIEnv *env, jobject clazz, jobject javaFd,
77 jint ifIndex)
78{
79 static const int kLinkLocalHopLimit = 255;
80
81 int fd = jniGetFDFromFileDescriptor(env, javaFd);
82
83 // Set an ICMPv6 filter that only passes Router Solicitations.
84 struct icmp6_filter rs_only;
85 ICMP6_FILTER_SETBLOCKALL(&rs_only);
86 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &rs_only);
87 socklen_t len = sizeof(rs_only);
88 if (setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &rs_only, len) != 0) {
89 jniThrowExceptionFmt(env, "java/net/SocketException",
90 "setsockopt(ICMP6_FILTER): %s", strerror(errno));
91 return;
92 }
93
94 // Most/all of the rest of these options can be set via Java code, but
95 // because we're here on account of setting an icmp6_filter go ahead
96 // and do it all natively for now.
97
98 // Set the multicast hoplimit to 255 (link-local only).
99 int hops = kLinkLocalHopLimit;
100 len = sizeof(hops);
101 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops, len) != 0) {
102 jniThrowExceptionFmt(env, "java/net/SocketException",
103 "setsockopt(IPV6_MULTICAST_HOPS): %s", strerror(errno));
104 return;
105 }
106
107 // Set the unicast hoplimit to 255 (link-local only).
108 hops = kLinkLocalHopLimit;
109 len = sizeof(hops);
110 if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hops, len) != 0) {
111 jniThrowExceptionFmt(env, "java/net/SocketException",
112 "setsockopt(IPV6_UNICAST_HOPS): %s", strerror(errno));
113 return;
114 }
115
116 // Explicitly disable multicast loopback.
117 int off = 0;
118 len = sizeof(off);
119 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &off, len) != 0) {
120 jniThrowExceptionFmt(env, "java/net/SocketException",
121 "setsockopt(IPV6_MULTICAST_LOOP): %s", strerror(errno));
122 return;
123 }
124
125 // Specify the IPv6 interface to use for outbound multicast.
126 len = sizeof(ifIndex);
127 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifIndex, len) != 0) {
128 jniThrowExceptionFmt(env, "java/net/SocketException",
129 "setsockopt(IPV6_MULTICAST_IF): %s", strerror(errno));
130 return;
131 }
132
133 // Additional options to be considered:
134 // - IPV6_TCLASS
135 // - IPV6_RECVPKTINFO
136 // - IPV6_RECVHOPLIMIT
137
138 // Bind to [::].
139 const struct sockaddr_in6 sin6 = {
140 .sin6_family = AF_INET6,
141 .sin6_port = 0,
142 .sin6_flowinfo = 0,
143 .sin6_addr = IN6ADDR_ANY_INIT,
144 .sin6_scope_id = 0,
145 };
146 auto sa = reinterpret_cast<const struct sockaddr *>(&sin6);
147 len = sizeof(sin6);
148 if (bind(fd, sa, len) != 0) {
149 jniThrowExceptionFmt(env, "java/net/SocketException",
150 "bind(IN6ADDR_ANY): %s", strerror(errno));
151 return;
152 }
153
154 // Join the all-routers multicast group, ff02::2%index.
155 struct ipv6_mreq all_rtrs = {
156 .ipv6mr_multiaddr = {{{0xff,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2}}},
157 .ipv6mr_interface = ifIndex,
158 };
159 len = sizeof(all_rtrs);
160 if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &all_rtrs, len) != 0) {
161 jniThrowExceptionFmt(env, "java/net/SocketException",
162 "setsockopt(IPV6_JOIN_GROUP): %s", strerror(errno));
163 return;
164 }
165}
166
167/*
168 * JNI registration.
169 */
170static const JNINativeMethod gMethods[] = {
171 /* name, signature, funcPtr */
markchien016ee972020-11-04 21:57:43 +0800172 { "setupNaSocket", "(Ljava/io/FileDescriptor;)V",
173 (void*) android_net_util_setupNaSocket },
174 { "setupNsSocket", "(Ljava/io/FileDescriptor;)V",
175 (void*) android_net_util_setupNsSocket },
176 { "setupRaSocket", "(Ljava/io/FileDescriptor;I)V",
177 (void*) android_net_util_setupRaSocket },
markchienf87ebdc2019-12-07 22:02:28 +0800178};
179
180int register_android_net_util_TetheringUtils(JNIEnv* env) {
181 return jniRegisterNativeMethods(env,
182 "android/net/util/TetheringUtils",
183 gMethods, NELEM(gMethods));
184}
185
186extern "C" jint JNI_OnLoad(JavaVM* vm, void*) {
187 JNIEnv *env;
188 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
markchien547e1682020-02-05 12:42:25 +0800189 __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "ERROR: GetEnv failed");
markchienf87ebdc2019-12-07 22:02:28 +0800190 return JNI_ERR;
191 }
192
193 if (register_android_net_util_TetheringUtils(env) < 0) {
194 return JNI_ERR;
195 }
196
197 return JNI_VERSION_1_6;
198}
199
200}; // namespace android