1 /*
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 "jni.h"
39 #include <android-base/stringprintf.h>
40 #include <android-base/unique_fd.h>
41 #include <nativehelper/JNIHelp.h>
42 #include <nativehelper/ScopedUtfChars.h>
43 
44 namespace android {
45 
46 //------------------------------------------------------------------------------
47 
throwException(JNIEnv * env,int error,const char * action,const char * iface)48 static void throwException(JNIEnv* env, int error, const char* action, const char* iface) {
49     const std::string& msg = "Error: " + std::string(action) + " " + std::string(iface) +  ": "
50                 + std::string(strerror(error));
51     jniThrowException(env, "java/lang/IllegalStateException", msg.c_str());
52 }
53 
createTunTapInterface(JNIEnv * env,bool isTun,const char * iface)54 static int createTunTapInterface(JNIEnv* env, bool isTun, const char* iface) {
55     base::unique_fd tun(open("/dev/tun", O_RDWR | O_NONBLOCK));
56     ifreq ifr{};
57 
58     // Allocate interface.
59     ifr.ifr_flags = (isTun ? IFF_TUN : IFF_TAP) | IFF_NO_PI;
60     strlcpy(ifr.ifr_name, iface, IFNAMSIZ);
61     if (ioctl(tun.get(), TUNSETIFF, &ifr)) {
62         throwException(env, errno, "allocating", ifr.ifr_name);
63         return -1;
64     }
65 
66     // Activate interface using an unconnected datagram socket.
67     base::unique_fd inet6CtrlSock(socket(AF_INET6, SOCK_DGRAM, 0));
68     ifr.ifr_flags = IFF_UP;
69 
70     if (ioctl(inet6CtrlSock.get(), SIOCSIFFLAGS, &ifr)) {
71         throwException(env, errno, "activating", ifr.ifr_name);
72         return -1;
73     }
74 
75     return tun.release();
76 }
77 
78 //------------------------------------------------------------------------------
79 
create(JNIEnv * env,jobject,jboolean isTun,jstring jIface)80 static jint create(JNIEnv* env, jobject /* thiz */, jboolean isTun, jstring jIface) {
81     ScopedUtfChars iface(env, jIface);
82     if (!iface.c_str()) {
83         jniThrowNullPointerException(env, "iface");
84         return -1;
85     }
86 
87     int tun = createTunTapInterface(env, isTun, iface.c_str());
88 
89     // Any exceptions will be thrown from the createTunTapInterface call
90     return tun;
91 }
92 
93 //------------------------------------------------------------------------------
94 
95 static const JNINativeMethod gMethods[] = {
96     {"jniCreateTunTap", "(ZLjava/lang/String;)I", (void*)create},
97 };
98 
register_android_server_TestNetworkService(JNIEnv * env)99 int register_android_server_TestNetworkService(JNIEnv* env) {
100     return jniRegisterNativeMethods(env, "com/android/server/TestNetworkService", gMethods,
101                                     NELEM(gMethods));
102 }
103 
104 }; // namespace android
105