blob: 9fcc950c229a4f91ca57706aa603fdf47d60d6a0 [file] [log] [blame]
Chia-chi Yeh6278d5e2011-07-02 16:41:59 -07001/*
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#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Chia-chi Yeh1591aa02011-07-14 14:47:48 -070020#include <errno.h>
Chia-chi Yeh6278d5e2011-07-02 16:41:59 -070021
Chia-chi Yeh1591aa02011-07-14 14:47:48 -070022#include <arpa/inet.h>
23#include <netinet/in.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <sys/socket.h>
27#include <sys/ioctl.h>
28#include <linux/if.h>
29#include <linux/route.h>
Chia-chi Yeh6278d5e2011-07-02 16:41:59 -070030
Chia-chi Yeh1591aa02011-07-14 14:47:48 -070031#define LOG_TAG "ip-up-vpn"
32#include <cutils/log.h>
33
34#define DIR "/data/misc/vpn/"
35
36static const char *env(const char *name) {
37 const char *value = getenv(name);
38 return value ? value : "";
39}
40
41static int set_address(struct sockaddr *sa, const char *address) {
42 sa->sa_family = AF_INET;
Chia-chi Yehf459d322011-09-22 10:53:05 -070043 errno = EINVAL;
Chia-chi Yeh1591aa02011-07-14 14:47:48 -070044 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. Each
50 * parameter occupies one line in the file, and it can be an empty string or
51 * space-separated values. The order and the format must be consistent with
52 * 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 *
60 * The secondary goal is to unify the outcome of VPN. The current baseline
61 * is to have an interface configured with the given address and netmask
62 * and maybe add a host route to protect the tunnel. PPP-based VPN already
63 * does this, but others might not. Routes, DNS servers, and search domains
64 * are handled by the framework since they can be overridden by the users.
65 */
Chia-chi Yeh6278d5e2011-07-02 16:41:59 -070066int main(int argc, char **argv)
67{
Chia-chi Yeh1591aa02011-07-14 14:47:48 -070068 FILE *state = fopen(DIR ".tmp", "wb");
69 if (!state) {
Steve Block3762c312012-01-06 19:20:56 +000070 ALOGE("Cannot create state: %s", strerror(errno));
Chia-chi Yeh1591aa02011-07-14 14:47:48 -070071 return 1;
72 }
Chia-chi Yeh6278d5e2011-07-02 16:41:59 -070073
Chia-chi Yeh1591aa02011-07-14 14:47:48 -070074 if (argc >= 6) {
75 /* Invoked by pppd. */
76 fprintf(state, "%s\n", argv[1]);
77 fprintf(state, "%s/32\n", argv[4]);
78 fprintf(state, "0.0.0.0/0\n");
79 fprintf(state, "%s %s\n", env("DNS1"), env("DNS2"));
80 fprintf(state, "\n");
81 } else if (argc == 2) {
82 /* Invoked by racoon. */
83 const char *interface = env("INTERFACE");
84 const char *address = env("INTERNAL_ADDR4");
85 const char *routes = env("SPLIT_INCLUDE_CIDR");
86
87 int s = socket(AF_INET, SOCK_DGRAM, 0);
88 struct rtentry rt;
89 struct ifreq ifr;
90
91 memset(&rt, 0, sizeof(rt));
92 memset(&ifr, 0, sizeof(ifr));
93
94 /* Remove the old host route. There could be more than one. */
95 rt.rt_flags |= RTF_UP | RTF_HOST;
96 if (set_address(&rt.rt_dst, env("REMOTE_ADDR"))) {
97 while (!ioctl(s, SIOCDELRT, &rt));
98 }
99 if (errno != ESRCH) {
Steve Block3762c312012-01-06 19:20:56 +0000100 ALOGE("Cannot remove host route: %s", strerror(errno));
Chia-chi Yeh1591aa02011-07-14 14:47:48 -0700101 return 1;
102 }
103
104 /* Create a new host route. */
105 rt.rt_flags |= RTF_GATEWAY;
106 if (!set_address(&rt.rt_gateway, argv[1]) ||
107 (ioctl(s, SIOCADDRT, &rt) && errno != EEXIST)) {
Steve Block3762c312012-01-06 19:20:56 +0000108 ALOGE("Cannot create host route: %s", strerror(errno));
Chia-chi Yeh1591aa02011-07-14 14:47:48 -0700109 return 1;
110 }
111
112 /* Bring up the interface. */
113 ifr.ifr_flags = IFF_UP;
114 strncpy(ifr.ifr_name, interface, IFNAMSIZ);
115 if (ioctl(s, SIOCSIFFLAGS, &ifr)) {
Steve Block3762c312012-01-06 19:20:56 +0000116 ALOGE("Cannot bring up %s: %s", interface, strerror(errno));
Chia-chi Yeh1591aa02011-07-14 14:47:48 -0700117 return 1;
118 }
119
120 /* Set the address. */
121 if (!set_address(&ifr.ifr_addr, address) ||
122 ioctl(s, SIOCSIFADDR, &ifr)) {
Steve Block3762c312012-01-06 19:20:56 +0000123 ALOGE("Cannot set address: %s", strerror(errno));
Chia-chi Yeh1591aa02011-07-14 14:47:48 -0700124 return 1;
125 }
126
127 /* Set the netmask. */
Chia-chi Yehf459d322011-09-22 10:53:05 -0700128 if (set_address(&ifr.ifr_netmask, env("INTERNAL_NETMASK4"))) {
129 if (ioctl(s, SIOCSIFNETMASK, &ifr)) {
Steve Block3762c312012-01-06 19:20:56 +0000130 ALOGE("Cannot set netmask: %s", strerror(errno));
Chia-chi Yehf459d322011-09-22 10:53:05 -0700131 return 1;
132 }
Chia-chi Yeh1591aa02011-07-14 14:47:48 -0700133 }
134
135 /* TODO: Send few packets to trigger phase 2? */
136
137 fprintf(state, "%s\n", interface);
138 fprintf(state, "%s/%s\n", address, env("INTERNAL_CIDR4"));
139 fprintf(state, "%s\n", routes[0] ? routes : "0.0.0.0/0");
140 fprintf(state, "%s\n", env("INTERNAL_DNS4_LIST"));
141 fprintf(state, "%s\n", env("DEFAULT_DOMAIN"));
142 } else {
Steve Block3762c312012-01-06 19:20:56 +0000143 ALOGE("Cannot parse parameters");
Chia-chi Yeh1591aa02011-07-14 14:47:48 -0700144 return 1;
145 }
146
147 fclose(state);
148 if (chmod(DIR ".tmp", 0444) || rename(DIR ".tmp", DIR "state")) {
Steve Block3762c312012-01-06 19:20:56 +0000149 ALOGE("Cannot write state: %s", strerror(errno));
Chia-chi Yeh1591aa02011-07-14 14:47:48 -0700150 return 1;
Chia-chi Yeh6278d5e2011-07-02 16:41:59 -0700151 }
152 return 0;
153}