• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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_TAG "ip-up-vpn"
18 
19 #include <arpa/inet.h>
20 #include <errno.h>
21 #include <linux/if.h>
22 #include <linux/route.h>
23 #include <netinet/in.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 
32 #include <log/log.h>
33 
34 #define DIR "/data/misc/vpn/"
35 
env(const char * name)36 static const char *env(const char *name) {
37     const char *value = getenv(name);
38     return value ? value : "";
39 }
40 
set_address(struct sockaddr * sa,const char * address)41 static int set_address(struct sockaddr *sa, const char *address) {
42     sa->sa_family = AF_INET;
43     errno = EINVAL;
44     return inet_pton(AF_INET, address, &((struct sockaddr_in *)sa)->sin_addr);
45 }
46 
47 /*
48  * The primary goal is to create a file with VPN parameters. Currently they
49  * are interface, addresses, routes, DNS servers, and search domains and VPN
50  * server address. Each parameter occupies one line in the file, and it can be
51  * an empty string or space-separated values. The order and the format must be
52  * consistent with com.android.server.connectivity.Vpn. Here is an example.
53  *
54  *   ppp0
55  *   192.168.1.100/24
56  *   0.0.0.0/0
57  *   192.168.1.1 192.168.1.2
58  *   example.org
59  *   192.0.2.1
60  *
61  * The secondary goal is to unify the outcome of VPN. The current baseline
62  * is to have an interface configured with the given address and netmask
63  * and maybe add a host route to protect the tunnel. PPP-based VPN already
64  * does this, but others might not. Routes, DNS servers, and search domains
65  * are handled by the framework since they can be overridden by the users.
66  */
main(int argc,char ** argv)67 int main(int argc, char **argv)
68 {
69     FILE *state = fopen(DIR ".tmp", "wb");
70     if (!state) {
71         ALOGE("Cannot create state: %s", strerror(errno));
72         return 1;
73     }
74 
75     if (argc >= 6) {
76         /* Invoked by pppd. */
77         fprintf(state, "%s\n", argv[1]);
78         fprintf(state, "%s/32\n", argv[4]);
79         fprintf(state, "0.0.0.0/0\n");
80         fprintf(state, "%s %s\n", env("DNS1"), env("DNS2"));
81         fprintf(state, "\n");
82         fprintf(state, "\n");
83     } else if (argc == 2) {
84         /* Invoked by racoon. */
85         const char *interface = env("INTERFACE");
86         const char *address = env("INTERNAL_ADDR4");
87         const char *routes = env("SPLIT_INCLUDE_CIDR");
88 
89         int s = socket(AF_INET, SOCK_DGRAM, 0);
90         struct ifreq ifr;
91         memset(&ifr, 0, sizeof(ifr));
92 
93         /* Bring up the interface. */
94         ifr.ifr_flags = IFF_UP;
95         strncpy(ifr.ifr_name, interface, IFNAMSIZ);
96         if (ioctl(s, SIOCSIFFLAGS, &ifr)) {
97             ALOGE("Cannot bring up %s: %s", interface, strerror(errno));
98             fclose(state);
99             return 1;
100         }
101 
102         /* Set the address. */
103         if (!set_address(&ifr.ifr_addr, address) ||
104                 ioctl(s, SIOCSIFADDR, &ifr)) {
105             ALOGE("Cannot set address: %s", strerror(errno));
106             fclose(state);
107             return 1;
108         }
109 
110         /* Set the netmask. */
111         if (set_address(&ifr.ifr_netmask, env("INTERNAL_NETMASK4"))) {
112             if (ioctl(s, SIOCSIFNETMASK, &ifr)) {
113                 ALOGE("Cannot set netmask: %s", strerror(errno));
114                 fclose(state);
115                 return 1;
116             }
117         }
118 
119         /* TODO: Send few packets to trigger phase 2? */
120 
121         fprintf(state, "%s\n", interface);
122         fprintf(state, "%s/%s\n", address, env("INTERNAL_CIDR4"));
123         fprintf(state, "%s\n", routes[0] ? routes : "0.0.0.0/0");
124         fprintf(state, "%s\n", env("INTERNAL_DNS4_LIST"));
125         fprintf(state, "%s\n", env("DEFAULT_DOMAIN"));
126         fprintf(state, "%s\n", env("REMOTE_ADDR"));
127     } else {
128         ALOGE("Cannot parse parameters");
129         fclose(state);
130         return 1;
131     }
132 
133     fclose(state);
134     if (chmod(DIR ".tmp", 0444) || rename(DIR ".tmp", DIR "state")) {
135         ALOGE("Cannot write state: %s", strerror(errno));
136         return 1;
137     }
138     return 0;
139 }
140