1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_NDEBUG 0
#define LOG_TAG "VpnJni"
#include <cutils/log.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <linux/route.h>
#include <linux/ipv6_route.h>
#include "jni.h"
#include "JNIHelp.h"
namespace android
{
static int inet4 = -1;
static int inet6 = -1;
static inline in_addr_t *as_in_addr(sockaddr *sa) {
return &((sockaddr_in *)sa)->sin_addr.s_addr;
}
//------------------------------------------------------------------------------
#define SYSTEM_ERROR -1
#define BAD_ARGUMENT -2
static int create_interface(int mtu)
{
int tun = open("/dev/tun", O_RDWR | O_NONBLOCK);
ifreq ifr4;
memset(&ifr4, 0, sizeof(ifr4));
// Allocate interface.
ifr4.ifr_flags = IFF_TUN | IFF_NO_PI;
if (ioctl(tun, TUNSETIFF, &ifr4)) {
ALOGE("Cannot allocate TUN: %s", strerror(errno));
goto error;
}
// Activate interface.
ifr4.ifr_flags = IFF_UP;
if (ioctl(inet4, SIOCSIFFLAGS, &ifr4)) {
ALOGE("Cannot activate %s: %s", ifr4.ifr_name, strerror(errno));
goto error;
}
// Set MTU if it is specified.
ifr4.ifr_mtu = mtu;
if (mtu > 0 && ioctl(inet4, SIOCSIFMTU, &ifr4)) {
ALOGE("Cannot set MTU on %s: %s", ifr4.ifr_name, strerror(errno));
goto error;
}
return tun;
error:
close(tun);
return SYSTEM_ERROR;
}
static int get_interface_name(char *name, int tun)
{
ifreq ifr4;
if (ioctl(tun, TUNGETIFF, &ifr4)) {
ALOGE("Cannot get interface name: %s", strerror(errno));
return SYSTEM_ERROR;
}
strncpy(name, ifr4.ifr_name, IFNAMSIZ);
return 0;
}
static int get_interface_index(const char *name)
{
ifreq ifr4;
strncpy(ifr4.ifr_name, name, IFNAMSIZ);
if (ioctl(inet4, SIOGIFINDEX, &ifr4)) {
ALOGE("Cannot get index of %s: %s", name, strerror(errno));
return SYSTEM_ERROR;
}
return ifr4.ifr_ifindex;
}
static int set_addresses(const char *name, const char *addresses)
{
int index = get_interface_index(name);
if (index < 0) {
return index;
}
ifreq ifr4;
memset(&ifr4, 0, sizeof(ifr4));
strncpy(ifr4.ifr_name, name, IFNAMSIZ);
ifr4.ifr_addr.sa_family = AF_INET;
ifr4.ifr_netmask.sa_family = AF_INET;
in6_ifreq ifr6;
memset(&ifr6, 0, sizeof(ifr6));
ifr6.ifr6_ifindex = index;
char address[65];
int prefix;
int chars;
int count = 0;
while (sscanf(addresses, " %64[^/]/%d %n", address, &prefix, &chars) == 2) {
addresses += chars;
if (strchr(address, ':')) {
// Add an IPv6 address.
if (inet_pton(AF_INET6, address, &ifr6.ifr6_addr) != 1 ||
prefix < 0 || prefix > 128) {
count = BAD_ARGUMENT;
break;
}
ifr6.ifr6_prefixlen = prefix;
if (ioctl(inet6, SIOCSIFADDR, &ifr6)) {
count = (errno == EINVAL) ? BAD_ARGUMENT : SYSTEM_ERROR;
break;
}
} else {
// Add an IPv4 address.
if (inet_pton(AF_INET, address, as_in_addr(&ifr4.ifr_addr)) != 1 ||
prefix < 0 || prefix > 32) {
count = BAD_ARGUMENT;
break;
}
if (count) {
sprintf(ifr4.ifr_name, "%s:%d", name, count);
}
if (ioctl(inet4, SIOCSIFADDR, &ifr4)) {
count = (errno == EINVAL) ? BAD_ARGUMENT : SYSTEM_ERROR;
break;
}
in_addr_t mask = prefix ? (~0 << (32 - prefix)) : 0;
*as_in_addr(&ifr4.ifr_netmask) = htonl(mask);
if (ioctl(inet4, SIOCSIFNETMASK, &ifr4)) {
count = (errno == EINVAL) ? BAD_ARGUMENT : SYSTEM_ERROR;
break;
}
}
ALOGD("Address added on %s: %s/%d", name, address, prefix);
++count;
}
if (count == BAD_ARGUMENT) {
ALOGE("Invalid address: %s/%d", address, prefix);
} else if (count == SYSTEM_ERROR) {
ALOGE("Cannot add address: %s/%d: %s", address, prefix, strerror(errno));
} else if (*addresses) {
ALOGE("Invalid address: %s", addresses);
count = BAD_ARGUMENT;
}
return count;
}
static int set_routes(const char *name, const char *routes)
{
int index = get_interface_index(name);
if (index < 0) {
return index;
}
rtentry rt4;
memset(&rt4, 0, sizeof(rt4));
rt4.rt_dev = (char *)name;
rt4.rt_flags = RTF_UP;
rt4.rt_dst.sa_family = AF_INET;
rt4.rt_genmask.sa_family = AF_INET;
in6_rtmsg rt6;
memset(&rt6, 0, sizeof(rt6));
rt6.rtmsg_ifindex = index;
rt6.rtmsg_flags = RTF_UP;
char address[65];
int prefix;
int chars;
int count = 0;
while (sscanf(routes, " %64[^/]/%d %n", address, &prefix, &chars) == 2) {
routes += chars;
if (strchr(address, ':')) {
// Add an IPv6 route.
if (inet_pton(AF_INET6, address, &rt6.rtmsg_dst) != 1 ||
prefix < 0 || prefix > 128) {
count = BAD_ARGUMENT;
break;
}
rt6.rtmsg_dst_len = prefix ? prefix : 1;
if (ioctl(inet6, SIOCADDRT, &rt6) && errno != EEXIST) {
count = (errno == EINVAL) ? BAD_ARGUMENT : SYSTEM_ERROR;
break;
}
if (!prefix) {
// Split the route instead of replacing the default route.
rt6.rtmsg_dst.s6_addr[0] ^= 0x80;
if (ioctl(inet6, SIOCADDRT, &rt6) && errno != EEXIST) {
count = SYSTEM_ERROR;
break;
}
}
} else {
// Add an IPv4 route.
if (inet_pton(AF_INET, address, as_in_addr(&rt4.rt_dst)) != 1 ||
prefix < 0 || prefix > 32) {
count = BAD_ARGUMENT;
break;
}
in_addr_t mask = prefix ? (~0 << (32 - prefix)) : 0x80000000;
*as_in_addr(&rt4.rt_genmask) = htonl(mask);
if (ioctl(inet4, SIOCADDRT, &rt4) && errno != EEXIST) {
count = (errno == EINVAL) ? BAD_ARGUMENT : SYSTEM_ERROR;
break;
}
if (!prefix) {
// Split the route instead of replacing the default route.
*as_in_addr(&rt4.rt_dst) ^= htonl(0x80000000);
if (ioctl(inet4, SIOCADDRT, &rt4) && errno != EEXIST) {
count = SYSTEM_ERROR;
break;
}
}
}
ALOGD("Route added on %s: %s/%d", name, address, prefix);
++count;
}
if (count == BAD_ARGUMENT) {
ALOGE("Invalid route: %s/%d", address, prefix);
} else if (count == SYSTEM_ERROR) {
ALOGE("Cannot add route: %s/%d: %s",
address, prefix, strerror(errno));
} else if (*routes) {
ALOGE("Invalid route: %s", routes);
count = BAD_ARGUMENT;
}
return count;
}
static int reset_interface(const char *name)
{
ifreq ifr4;
strncpy(ifr4.ifr_name, name, IFNAMSIZ);
ifr4.ifr_flags = 0;
if (ioctl(inet4, SIOCSIFFLAGS, &ifr4) && errno != ENODEV) {
ALOGE("Cannot reset %s: %s", name, strerror(errno));
return SYSTEM_ERROR;
}
return 0;
}
static int check_interface(const char *name)
{
ifreq ifr4;
strncpy(ifr4.ifr_name, name, IFNAMSIZ);
ifr4.ifr_flags = 0;
if (ioctl(inet4, SIOCGIFFLAGS, &ifr4) && errno != ENODEV) {
ALOGE("Cannot check %s: %s", name, strerror(errno));
}
return ifr4.ifr_flags;
}
static int bind_to_interface(int socket, const char *name)
{
if (setsockopt(socket, SOL_SOCKET, SO_BINDTODEVICE, name, strlen(name))) {
ALOGE("Cannot bind socket to %s: %s", name, strerror(errno));
return SYSTEM_ERROR;
}
return 0;
}
//------------------------------------------------------------------------------
static void throwException(JNIEnv *env, int error, const char *message)
{
if (error == SYSTEM_ERROR) {
jniThrowException(env, "java/lang/IllegalStateException", message);
} else {
jniThrowException(env, "java/lang/IllegalArgumentException", message);
}
}
static jint create(JNIEnv *env, jobject thiz, jint mtu)
{
int tun = create_interface(mtu);
if (tun < 0) {
throwException(env, tun, "Cannot create interface");
return -1;
}
return tun;
}
static jstring getName(JNIEnv *env, jobject thiz, jint tun)
{
char name[IFNAMSIZ];
if (get_interface_name(name, tun) < 0) {
throwException(env, SYSTEM_ERROR, "Cannot get interface name");
return NULL;
}
return env->NewStringUTF(name);
}
static jint setAddresses(JNIEnv *env, jobject thiz, jstring jName,
jstring jAddresses)
{
const char *name = NULL;
const char *addresses = NULL;
int count = -1;
name = jName ? env->GetStringUTFChars(jName, NULL) : NULL;
if (!name) {
jniThrowNullPointerException(env, "name");
goto error;
}
addresses = jAddresses ? env->GetStringUTFChars(jAddresses, NULL) : NULL;
if (!addresses) {
jniThrowNullPointerException(env, "addresses");
goto error;
}
count = set_addresses(name, addresses);
if (count < 0) {
throwException(env, count, "Cannot set address");
count = -1;
}
error:
if (name) {
env->ReleaseStringUTFChars(jName, name);
}
if (addresses) {
env->ReleaseStringUTFChars(jAddresses, addresses);
}
return count;
}
static jint setRoutes(JNIEnv *env, jobject thiz, jstring jName,
jstring jRoutes)
{
const char *name = NULL;
const char *routes = NULL;
int count = -1;
name = jName ? env->GetStringUTFChars(jName, NULL) : NULL;
if (!name) {
jniThrowNullPointerException(env, "name");
goto error;
}
routes = jRoutes ? env->GetStringUTFChars(jRoutes, NULL) : NULL;
if (!routes) {
jniThrowNullPointerException(env, "routes");
goto error;
}
count = set_routes(name, routes);
if (count < 0) {
throwException(env, count, "Cannot set route");
count = -1;
}
error:
if (name) {
env->ReleaseStringUTFChars(jName, name);
}
if (routes) {
env->ReleaseStringUTFChars(jRoutes, routes);
}
return count;
}
static void reset(JNIEnv *env, jobject thiz, jstring jName)
{
const char *name = jName ? env->GetStringUTFChars(jName, NULL) : NULL;
if (!name) {
jniThrowNullPointerException(env, "name");
return;
}
if (reset_interface(name) < 0) {
throwException(env, SYSTEM_ERROR, "Cannot reset interface");
}
env->ReleaseStringUTFChars(jName, name);
}
static jint check(JNIEnv *env, jobject thiz, jstring jName)
{
const char *name = jName ? env->GetStringUTFChars(jName, NULL) : NULL;
if (!name) {
jniThrowNullPointerException(env, "name");
return 0;
}
int flags = check_interface(name);
env->ReleaseStringUTFChars(jName, name);
return flags;
}
static void protect(JNIEnv *env, jobject thiz, jint socket, jstring jName)
{
const char *name = jName ? env->GetStringUTFChars(jName, NULL) : NULL;
if (!name) {
jniThrowNullPointerException(env, "name");
return;
}
if (bind_to_interface(socket, name) < 0) {
throwException(env, SYSTEM_ERROR, "Cannot protect socket");
}
env->ReleaseStringUTFChars(jName, name);
}
//------------------------------------------------------------------------------
static JNINativeMethod gMethods[] = {
{"jniCreate", "(I)I", (void *)create},
{"jniGetName", "(I)Ljava/lang/String;", (void *)getName},
{"jniSetAddresses", "(Ljava/lang/String;Ljava/lang/String;)I", (void *)setAddresses},
{"jniSetRoutes", "(Ljava/lang/String;Ljava/lang/String;)I", (void *)setRoutes},
{"jniReset", "(Ljava/lang/String;)V", (void *)reset},
{"jniCheck", "(Ljava/lang/String;)I", (void *)check},
{"jniProtect", "(ILjava/lang/String;)V", (void *)protect},
};
int register_android_server_connectivity_Vpn(JNIEnv *env)
{
if (inet4 == -1) {
inet4 = socket(AF_INET, SOCK_DGRAM, 0);
}
if (inet6 == -1) {
inet6 = socket(AF_INET6, SOCK_DGRAM, 0);
}
return jniRegisterNativeMethods(env, "com/android/server/connectivity/Vpn",
gMethods, NELEM(gMethods));
}
};
|