Chia-chi Yeh | 837a1c7 | 2009-06-26 09:40:31 +0800 | [diff] [blame] | 1 | /* |
Chia-chi Yeh | e9fc376 | 2011-07-07 03:20:34 -0700 | [diff] [blame] | 2 | * Copyright (C) 2011 The Android Open Source Project |
Chia-chi Yeh | 837a1c7 | 2009-06-26 09:40:31 +0800 | [diff] [blame] | 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> |
Chia-chi Yeh | bd5fa3c | 2009-07-07 16:24:13 +0800 | [diff] [blame] | 19 | #include <stdarg.h> |
Chia-chi Yeh | 837a1c7 | 2009-06-26 09:40:31 +0800 | [diff] [blame] | 20 | #include <signal.h> |
Chia-chi Yeh | f8a6a76 | 2011-07-04 17:21:23 -0700 | [diff] [blame] | 21 | #include <poll.h> |
Chia-chi Yeh | 66d08f4 | 2012-09-17 00:29:46 -0700 | [diff] [blame] | 22 | #include <unistd.h> |
Chia-chi Yeh | f8a6a76 | 2011-07-04 17:21:23 -0700 | [diff] [blame] | 23 | |
Chia-chi Yeh | 837a1c7 | 2009-06-26 09:40:31 +0800 | [diff] [blame] | 24 | #include "config.h" |
Chia-chi Yeh | 837a1c7 | 2009-06-26 09:40:31 +0800 | [diff] [blame] | 25 | #include "gcmalloc.h" |
Chia-chi Yeh | 837a1c7 | 2009-06-26 09:40:31 +0800 | [diff] [blame] | 26 | #include "schedule.h" |
Chia-chi Yeh | bd5fa3c | 2009-07-07 16:24:13 +0800 | [diff] [blame] | 27 | #include "plog.h" |
Chia-chi Yeh | 837a1c7 | 2009-06-26 09:40:31 +0800 | [diff] [blame] | 28 | |
Chia-chi Yeh | 458fe1e | 2009-06-26 14:36:17 +0800 | [diff] [blame] | 29 | #ifdef ANDROID_CHANGES |
| 30 | |
Chia-chi Yeh | 1070097 | 2011-07-12 18:06:57 -0700 | [diff] [blame] | 31 | #include <string.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/socket.h> |
| 34 | #include <sys/ioctl.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <errno.h> |
| 38 | #include <linux/if.h> |
| 39 | #include <linux/if_tun.h> |
| 40 | |
| 41 | #include <android/log.h> |
| 42 | #include <cutils/sockets.h> |
| 43 | #include <private/android_filesystem_config.h> |
| 44 | |
Chia-chi Yeh | cadace4 | 2011-12-08 16:22:51 -0800 | [diff] [blame] | 45 | static void notify_death() |
| 46 | { |
| 47 | creat("/data/misc/vpn/abort", 0); |
| 48 | } |
| 49 | |
Chia-chi Yeh | 4dd8f6b | 2011-07-13 19:18:58 -0700 | [diff] [blame] | 50 | static int android_get_control_and_arguments(int *argc, char ***argv) |
Chia-chi Yeh | 458fe1e | 2009-06-26 14:36:17 +0800 | [diff] [blame] | 51 | { |
Chia-chi Yeh | c9ac7d2 | 2011-06-28 18:37:45 -0700 | [diff] [blame] | 52 | static char *args[32]; |
Chia-chi Yeh | 458fe1e | 2009-06-26 14:36:17 +0800 | [diff] [blame] | 53 | int control; |
| 54 | int i; |
| 55 | |
Chia-chi Yeh | cadace4 | 2011-12-08 16:22:51 -0800 | [diff] [blame] | 56 | atexit(notify_death); |
| 57 | |
Chia-chi Yeh | 458fe1e | 2009-06-26 14:36:17 +0800 | [diff] [blame] | 58 | if ((i = android_get_control_socket("racoon")) == -1) { |
Chia-chi Yeh | e9fc376 | 2011-07-07 03:20:34 -0700 | [diff] [blame] | 59 | return -1; |
Chia-chi Yeh | 458fe1e | 2009-06-26 14:36:17 +0800 | [diff] [blame] | 60 | } |
| 61 | do_plog(LLV_DEBUG, "Waiting for control socket"); |
| 62 | if (listen(i, 1) == -1 || (control = accept(i, NULL, 0)) == -1) { |
| 63 | do_plog(LLV_ERROR, "Cannot get control socket"); |
Chia-chi Yeh | f8a6a76 | 2011-07-04 17:21:23 -0700 | [diff] [blame] | 64 | exit(1); |
Chia-chi Yeh | 458fe1e | 2009-06-26 14:36:17 +0800 | [diff] [blame] | 65 | } |
| 66 | close(i); |
Chia-chi Yeh | 3724e61 | 2011-08-22 13:05:42 -0700 | [diff] [blame] | 67 | fcntl(control, F_SETFD, FD_CLOEXEC); |
Chia-chi Yeh | 458fe1e | 2009-06-26 14:36:17 +0800 | [diff] [blame] | 68 | |
| 69 | args[0] = (*argv)[0]; |
Chia-chi Yeh | c9ac7d2 | 2011-06-28 18:37:45 -0700 | [diff] [blame] | 70 | for (i = 1; i < 32; ++i) { |
| 71 | unsigned char bytes[2]; |
Chia-chi Yeh | 3724e61 | 2011-08-22 13:05:42 -0700 | [diff] [blame] | 72 | if (recv(control, &bytes[0], 1, 0) != 1 || |
| 73 | recv(control, &bytes[1], 1, 0) != 1) { |
Chia-chi Yeh | 458fe1e | 2009-06-26 14:36:17 +0800 | [diff] [blame] | 74 | do_plog(LLV_ERROR, "Cannot get argument length"); |
Chia-chi Yeh | f8a6a76 | 2011-07-04 17:21:23 -0700 | [diff] [blame] | 75 | exit(1); |
Chia-chi Yeh | 458fe1e | 2009-06-26 14:36:17 +0800 | [diff] [blame] | 76 | } else { |
Chia-chi Yeh | 3724e61 | 2011-08-22 13:05:42 -0700 | [diff] [blame] | 77 | int length = bytes[0] << 8 | bytes[1]; |
Chia-chi Yeh | 458fe1e | 2009-06-26 14:36:17 +0800 | [diff] [blame] | 78 | int offset = 0; |
Chia-chi Yeh | c9ac7d2 | 2011-06-28 18:37:45 -0700 | [diff] [blame] | 79 | |
Chia-chi Yeh | 3724e61 | 2011-08-22 13:05:42 -0700 | [diff] [blame] | 80 | if (length == 0xFFFF) { |
| 81 | break; |
| 82 | } |
Chia-chi Yeh | 458fe1e | 2009-06-26 14:36:17 +0800 | [diff] [blame] | 83 | args[i] = malloc(length + 1); |
| 84 | while (offset < length) { |
| 85 | int n = recv(control, &args[i][offset], length - offset, 0); |
| 86 | if (n > 0) { |
| 87 | offset += n; |
| 88 | } else { |
| 89 | do_plog(LLV_ERROR, "Cannot get argument value"); |
Chia-chi Yeh | f8a6a76 | 2011-07-04 17:21:23 -0700 | [diff] [blame] | 90 | exit(1); |
Chia-chi Yeh | 458fe1e | 2009-06-26 14:36:17 +0800 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | args[i][length] = 0; |
| 94 | } |
| 95 | } |
| 96 | do_plog(LLV_DEBUG, "Received %d arguments", i - 1); |
| 97 | |
| 98 | *argc = i; |
| 99 | *argv = args; |
Chia-chi Yeh | e9fc376 | 2011-07-07 03:20:34 -0700 | [diff] [blame] | 100 | return control; |
Chia-chi Yeh | c454954 | 2009-07-22 06:46:14 +0800 | [diff] [blame] | 101 | } |
| 102 | |
Chia-chi Yeh | a9a07ac | 2011-07-19 18:29:16 -0700 | [diff] [blame] | 103 | const char *android_hook(char **envp) |
Chia-chi Yeh | 1070097 | 2011-07-12 18:06:57 -0700 | [diff] [blame] | 104 | { |
Chia-chi Yeh | 1070097 | 2011-07-12 18:06:57 -0700 | [diff] [blame] | 105 | struct ifreq ifr = {.ifr_flags = IFF_TUN}; |
Chia-chi Yeh | dc6f5b9 | 2011-07-14 15:29:06 -0700 | [diff] [blame] | 106 | int tun = open("/dev/tun", 0); |
Chia-chi Yeh | 1070097 | 2011-07-12 18:06:57 -0700 | [diff] [blame] | 107 | |
Chia-chi Yeh | a9a07ac | 2011-07-19 18:29:16 -0700 | [diff] [blame] | 108 | /* Android does not support INTERNAL_WINS4_LIST, so we just use it. */ |
Chia-chi Yeh | dc6f5b9 | 2011-07-14 15:29:06 -0700 | [diff] [blame] | 109 | while (*envp && strncmp(*envp, "INTERNAL_WINS4_LIST=", 20)) { |
| 110 | ++envp; |
| 111 | } |
| 112 | if (!*envp) { |
| 113 | do_plog(LLV_ERROR, "Cannot find environment variable\n"); |
| 114 | exit(1); |
| 115 | } |
Chia-chi Yeh | 1070097 | 2011-07-12 18:06:57 -0700 | [diff] [blame] | 116 | if (ioctl(tun, TUNSETIFF, &ifr)) { |
| 117 | do_plog(LLV_ERROR, "Cannot allocate TUN: %s\n", strerror(errno)); |
| 118 | exit(1); |
| 119 | } |
Chia-chi Yeh | dc6f5b9 | 2011-07-14 15:29:06 -0700 | [diff] [blame] | 120 | sprintf(*envp, "INTERFACE=%s", ifr.ifr_name); |
Chia-chi Yeh | a9a07ac | 2011-07-19 18:29:16 -0700 | [diff] [blame] | 121 | return "/etc/ppp/ip-up-vpn"; |
Chia-chi Yeh | 1070097 | 2011-07-12 18:06:57 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Chia-chi Yeh | 458fe1e | 2009-06-26 14:36:17 +0800 | [diff] [blame] | 124 | #endif |
| 125 | |
Chia-chi Yeh | c454954 | 2009-07-22 06:46:14 +0800 | [diff] [blame] | 126 | extern void setup(int argc, char **argv); |
tetsuya, kobayashi | cb1e955 | 2013-06-14 23:43:19 +0900 | [diff] [blame] | 127 | extern void shutdown_session(); |
Chia-chi Yeh | f8a6a76 | 2011-07-04 17:21:23 -0700 | [diff] [blame] | 128 | |
Chia-chi Yeh | c91307a | 2012-03-26 14:18:52 -0700 | [diff] [blame] | 129 | static int monitors; |
| 130 | static void (*callbacks[10])(int fd); |
Chia-chi Yeh | f8a6a76 | 2011-07-04 17:21:23 -0700 | [diff] [blame] | 131 | static struct pollfd pollfds[10]; |
Chia-chi Yeh | c454954 | 2009-07-22 06:46:14 +0800 | [diff] [blame] | 132 | |
Chia-chi Yeh | c91307a | 2012-03-26 14:18:52 -0700 | [diff] [blame] | 133 | char *pname; |
| 134 | |
Chia-chi Yeh | c454954 | 2009-07-22 06:46:14 +0800 | [diff] [blame] | 135 | static void terminate(int signal) |
| 136 | { |
| 137 | exit(1); |
| 138 | } |
| 139 | |
| 140 | static void terminated() |
| 141 | { |
| 142 | do_plog(LLV_INFO, "Bye\n"); |
| 143 | } |
| 144 | |
Chia-chi Yeh | c91307a | 2012-03-26 14:18:52 -0700 | [diff] [blame] | 145 | void monitor_fd(int fd, void (*callback)(int)) |
| 146 | { |
| 147 | if (fd < 0 || monitors == 10) { |
| 148 | do_plog(LLV_ERROR, "Cannot monitor fd"); |
| 149 | exit(1); |
| 150 | } |
| 151 | callbacks[monitors] = callback; |
| 152 | pollfds[monitors].fd = fd; |
| 153 | pollfds[monitors].events = callback ? POLLIN : 0; |
| 154 | ++monitors; |
| 155 | } |
| 156 | |
Chia-chi Yeh | 837a1c7 | 2009-06-26 09:40:31 +0800 | [diff] [blame] | 157 | int main(int argc, char **argv) |
| 158 | { |
Chia-chi Yeh | e9fc376 | 2011-07-07 03:20:34 -0700 | [diff] [blame] | 159 | #ifdef ANDROID_CHANGES |
Chia-chi Yeh | 4dd8f6b | 2011-07-13 19:18:58 -0700 | [diff] [blame] | 160 | int control = android_get_control_and_arguments(&argc, &argv); |
Adam Langley | a029281 | 2014-09-30 15:20:01 -0700 | [diff] [blame] | 161 | |
Chia-chi Yeh | e9fc376 | 2011-07-07 03:20:34 -0700 | [diff] [blame] | 162 | if (control != -1) { |
Chia-chi Yeh | 8f3b388 | 2011-07-07 13:43:20 -0700 | [diff] [blame] | 163 | pname = "%p"; |
Chia-chi Yeh | c91307a | 2012-03-26 14:18:52 -0700 | [diff] [blame] | 164 | monitor_fd(control, NULL); |
Chia-chi Yeh | e9fc376 | 2011-07-07 03:20:34 -0700 | [diff] [blame] | 165 | } |
| 166 | #endif |
| 167 | |
Chia-chi Yeh | c91307a | 2012-03-26 14:18:52 -0700 | [diff] [blame] | 168 | do_plog(LLV_INFO, "ipsec-tools 0.7.3 (http://ipsec-tools.sf.net)\n"); |
Chia-chi Yeh | 837a1c7 | 2009-06-26 09:40:31 +0800 | [diff] [blame] | 169 | |
Chia-chi Yeh | 7197eb7 | 2009-07-13 16:43:29 +0800 | [diff] [blame] | 170 | signal(SIGHUP, terminate); |
| 171 | signal(SIGINT, terminate); |
| 172 | signal(SIGTERM, terminate); |
Chia-chi Yeh | 837a1c7 | 2009-06-26 09:40:31 +0800 | [diff] [blame] | 173 | signal(SIGPIPE, SIG_IGN); |
Chia-chi Yeh | 9d271b6 | 2009-07-30 17:23:56 +0800 | [diff] [blame] | 174 | atexit(terminated); |
Chia-chi Yeh | 837a1c7 | 2009-06-26 09:40:31 +0800 | [diff] [blame] | 175 | |
Chia-chi Yeh | 12f4493 | 2011-07-13 16:40:15 -0700 | [diff] [blame] | 176 | setup(argc, argv); |
| 177 | |
| 178 | #ifdef ANDROID_CHANGES |
Chia-chi Yeh | 3724e61 | 2011-08-22 13:05:42 -0700 | [diff] [blame] | 179 | shutdown(control, SHUT_WR); |
Chia-chi Yeh | 12f4493 | 2011-07-13 16:40:15 -0700 | [diff] [blame] | 180 | #endif |
| 181 | |
Chia-chi Yeh | 837a1c7 | 2009-06-26 09:40:31 +0800 | [diff] [blame] | 182 | while (1) { |
Chia-chi Yeh | f8a6a76 | 2011-07-04 17:21:23 -0700 | [diff] [blame] | 183 | struct timeval *tv = schedular(); |
| 184 | int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000 + 1; |
| 185 | |
Chia-chi Yeh | c91307a | 2012-03-26 14:18:52 -0700 | [diff] [blame] | 186 | if (poll(pollfds, monitors, timeout) > 0) { |
Chia-chi Yeh | f8a6a76 | 2011-07-04 17:21:23 -0700 | [diff] [blame] | 187 | int i; |
Chia-chi Yeh | c91307a | 2012-03-26 14:18:52 -0700 | [diff] [blame] | 188 | for (i = 0; i < monitors; ++i) { |
Chia-chi Yeh | f8a6a76 | 2011-07-04 17:21:23 -0700 | [diff] [blame] | 189 | if (pollfds[i].revents & POLLHUP) { |
Chia-chi Yeh | 66d08f4 | 2012-09-17 00:29:46 -0700 | [diff] [blame] | 190 | do_plog(LLV_INFO, "Connection is closed\n", pollfds[i].fd); |
tetsuya, kobayashi | cb1e955 | 2013-06-14 23:43:19 +0900 | [diff] [blame] | 191 | shutdown_session(); |
| 192 | |
Chia-chi Yeh | 66d08f4 | 2012-09-17 00:29:46 -0700 | [diff] [blame] | 193 | /* Wait for few seconds to consume late messages. */ |
| 194 | sleep(5); |
Chia-chi Yeh | f8a6a76 | 2011-07-04 17:21:23 -0700 | [diff] [blame] | 195 | exit(1); |
| 196 | } |
| 197 | if (pollfds[i].revents & POLLIN) { |
Chia-chi Yeh | c91307a | 2012-03-26 14:18:52 -0700 | [diff] [blame] | 198 | callbacks[i](pollfds[i].fd); |
Chia-chi Yeh | f8a6a76 | 2011-07-04 17:21:23 -0700 | [diff] [blame] | 199 | } |
Chia-chi Yeh | 837a1c7 | 2009-06-26 09:40:31 +0800 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | } |
Adam Langley | a029281 | 2014-09-30 15:20:01 -0700 | [diff] [blame] | 203 | |
Chia-chi Yeh | 837a1c7 | 2009-06-26 09:40:31 +0800 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | /* plog.h */ |
| 208 | |
| 209 | void do_plog(int level, char *format, ...) |
| 210 | { |
Chia-chi Yeh | 458fe1e | 2009-06-26 14:36:17 +0800 | [diff] [blame] | 211 | if (level >= 0 && level <= 5) { |
| 212 | #ifdef ANDROID_CHANGES |
| 213 | static int levels[6] = { |
| 214 | ANDROID_LOG_ERROR, ANDROID_LOG_WARN, ANDROID_LOG_INFO, |
| 215 | ANDROID_LOG_INFO, ANDROID_LOG_DEBUG, ANDROID_LOG_VERBOSE |
| 216 | }; |
| 217 | va_list ap; |
| 218 | va_start(ap, format); |
| 219 | __android_log_vprint(levels[level], "racoon", format, ap); |
| 220 | va_end(ap); |
| 221 | #else |
| 222 | static char *levels = "EWNIDV"; |
| 223 | fprintf(stderr, "%c: ", levels[level]); |
| 224 | va_list ap; |
| 225 | va_start(ap, format); |
| 226 | vfprintf(stderr, format, ap); |
| 227 | va_end(ap); |
| 228 | #endif |
| 229 | } |
Chia-chi Yeh | 837a1c7 | 2009-06-26 09:40:31 +0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | char *binsanitize(char *data, size_t length) |
| 233 | { |
| 234 | char *output = racoon_malloc(length + 1); |
| 235 | if (output) { |
| 236 | size_t i; |
| 237 | for (i = 0; i < length; ++i) { |
Chia-chi Yeh | c454954 | 2009-07-22 06:46:14 +0800 | [diff] [blame] | 238 | output[i] = (data[i] < ' ' || data[i] > '~') ? '?' : data[i]; |
Chia-chi Yeh | 837a1c7 | 2009-06-26 09:40:31 +0800 | [diff] [blame] | 239 | } |
| 240 | output[length] = '\0'; |
| 241 | } |
| 242 | return output; |
| 243 | } |