blob: b90ff233c1acf01770fa280183f6e61b215e2f2e [file] [log] [blame]
Benedict Wong6c242132018-11-14 17:53:19 -08001/*
2 * Copyright (C) 2018 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#define LOG_NDEBUG 0
18
19#define LOG_TAG "TestNetworkServiceJni"
20
21#include <arpa/inet.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <linux/if.h>
25#include <linux/if_tun.h>
26#include <linux/ipv6_route.h>
27#include <linux/route.h>
28#include <netinet/in.h>
29#include <stdio.h>
30#include <string.h>
31#include <sys/ioctl.h>
32#include <sys/socket.h>
33#include <sys/stat.h>
34#include <sys/types.h>
35
36#include <log/log.h>
37
38#include "netutils/ifc.h"
39
40#include "jni.h"
41#include <android-base/stringprintf.h>
42#include <android-base/unique_fd.h>
43#include <nativehelper/JNIHelp.h>
44#include <nativehelper/ScopedUtfChars.h>
45
46namespace android {
47
48//------------------------------------------------------------------------------
49
50static void throwException(JNIEnv* env, int error, const char* action, const char* iface) {
51 const std::string& msg =
52 android::base::StringPrintf("Error %s %s: %s", action, iface, strerror(error));
53
54 jniThrowException(env, "java/lang/IllegalStateException", msg.c_str());
55}
56
57static int createTunInterface(JNIEnv* env, const char* iface) {
58 base::unique_fd tun(open("/dev/tun", O_RDWR | O_NONBLOCK));
59 ifreq ifr{};
60
61 // Allocate interface.
62 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
63 strlcpy(ifr.ifr_name, iface, IFNAMSIZ);
64 if (ioctl(tun.get(), TUNSETIFF, &ifr)) {
65 throwException(env, errno, "allocating", ifr.ifr_name);
66 return -1;
67 }
68
69 // Activate interface using an unconnected datagram socket.
70 base::unique_fd inet6CtrlSock(socket(AF_INET6, SOCK_DGRAM, 0));
71 ifr.ifr_flags = IFF_UP;
72
73 if (ioctl(inet6CtrlSock.get(), SIOCSIFFLAGS, &ifr)) {
74 throwException(env, errno, "activating", ifr.ifr_name);
75 return -1;
76 }
77
78 return tun.release();
79}
80
81//------------------------------------------------------------------------------
82
83static jint create(JNIEnv* env, jobject /* thiz */, jstring jIface) {
84 ScopedUtfChars iface(env, jIface);
85 if (!iface.c_str()) {
86 jniThrowNullPointerException(env, "iface");
87 return -1;
88 }
89
90 int tun = createTunInterface(env, iface.c_str());
91
92 // Any exceptions will be thrown from the createTunInterface call
93 return tun;
94}
95
96//------------------------------------------------------------------------------
97
98static const JNINativeMethod gMethods[] = {
99 {"jniCreateTun", "(Ljava/lang/String;)I", (void*)create},
100};
101
102int register_android_server_TestNetworkService(JNIEnv* env) {
103 return jniRegisterNativeMethods(env, "com/android/server/TestNetworkService", gMethods,
104 NELEM(gMethods));
105}
106
107}; // namespace android