Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 <stdarg.h> |
| 20 | #include <string.h> |
| 21 | #include <errno.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <sys/poll.h> |
| 25 | #include <sys/wait.h> |
| 26 | #include <netdb.h> |
| 27 | #include <signal.h> |
| 28 | #include <unistd.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <time.h> |
Sam Protsenko | 28cfaab | 2017-10-30 17:02:26 +0200 | [diff] [blame] | 31 | #include <limits.h> |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 32 | |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 33 | #ifdef ANDROID_CHANGES |
| 34 | #include <android/log.h> |
| 35 | #include <cutils/sockets.h> |
| 36 | #endif |
| 37 | |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 38 | #include "mtpd.h" |
Robin Lee | b8e401e | 2017-03-14 14:13:58 +0000 | [diff] [blame] | 39 | #include "NetdClient.h" |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 40 | |
Sam Protsenko | 28cfaab | 2017-10-30 17:02:26 +0200 | [diff] [blame] | 41 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 42 | /* Characters count in string with max value of unsigned type t */ |
| 43 | #define TYPE_STRLEN_U(t) ((((sizeof(t) * CHAR_BIT) * 1233) >> 12) + 1) |
| 44 | /* Length of string with max file descriptor value */ |
| 45 | #define FD_MAX_LEN TYPE_STRLEN_U(int) |
| 46 | |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 47 | int the_socket = -1; |
| 48 | |
| 49 | extern struct protocol l2tp; |
Chia-chi Yeh | 063bb92 | 2009-06-15 14:43:38 +0800 | [diff] [blame] | 50 | extern struct protocol pptp; |
| 51 | static struct protocol *protocols[] = {&l2tp, &pptp, NULL}; |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 52 | static struct protocol *the_protocol; |
| 53 | |
Chia-chi Yeh | 3afd73f | 2011-07-13 18:40:09 -0700 | [diff] [blame] | 54 | static char *interface; |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 55 | static int pppd_argc; |
| 56 | static char **pppd_argv; |
| 57 | static pid_t pppd_pid; |
| 58 | |
| 59 | /* We redirect signals to a pipe in order to prevent race conditions. */ |
| 60 | static int signals[2]; |
| 61 | |
| 62 | static void interrupt(int signal) |
| 63 | { |
| 64 | write(signals[1], &signal, sizeof(int)); |
| 65 | } |
| 66 | |
| 67 | static int initialize(int argc, char **argv) |
| 68 | { |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 69 | int i; |
| 70 | |
Chia-chi Yeh | 7b66d20 | 2011-06-28 16:39:23 -0700 | [diff] [blame] | 71 | for (i = 0; protocols[i]; ++i) { |
| 72 | struct protocol *p = protocols[i]; |
Chia-chi Yeh | 3afd73f | 2011-07-13 18:40:09 -0700 | [diff] [blame] | 73 | if (argc - 3 >= p->arguments && !strcmp(argv[2], p->name)) { |
Chia-chi Yeh | 7b66d20 | 2011-06-28 16:39:23 -0700 | [diff] [blame] | 74 | log_print(INFO, "Using protocol %s", p->name); |
| 75 | the_protocol = p; |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
Chia-chi Yeh | 7b66d20 | 2011-06-28 16:39:23 -0700 | [diff] [blame] | 80 | if (!the_protocol) { |
| 81 | printf("Usages:\n"); |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 82 | for (i = 0; protocols[i]; ++i) { |
Chia-chi Yeh | 7b66d20 | 2011-06-28 16:39:23 -0700 | [diff] [blame] | 83 | struct protocol *p = protocols[i]; |
Chia-chi Yeh | 3afd73f | 2011-07-13 18:40:09 -0700 | [diff] [blame] | 84 | printf(" %s interface %s %s pppd-arguments\n", |
| 85 | argv[0], p->name, p->usage); |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 86 | } |
Chia-chi Yeh | 7b66d20 | 2011-06-28 16:39:23 -0700 | [diff] [blame] | 87 | exit(0); |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 88 | } |
| 89 | |
Chia-chi Yeh | 3afd73f | 2011-07-13 18:40:09 -0700 | [diff] [blame] | 90 | interface = argv[1]; |
| 91 | pppd_argc = argc - 3 - the_protocol->arguments; |
| 92 | pppd_argv = &argv[3 + the_protocol->arguments]; |
| 93 | return the_protocol->connect(&argv[3]); |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static void stop_pppd() |
| 97 | { |
| 98 | if (pppd_pid) { |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 99 | int status; |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 100 | log_print(INFO, "Sending signal to pppd (pid = %d)", pppd_pid); |
| 101 | kill(pppd_pid, SIGTERM); |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 102 | waitpid(pppd_pid, &status, 0); |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 103 | pppd_pid = 0; |
| 104 | } |
| 105 | } |
| 106 | |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 107 | #ifdef ANDROID_CHANGES |
| 108 | |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 109 | static int android_get_control_and_arguments(int *argc, char ***argv) |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 110 | { |
Chia-chi Yeh | 7b66d20 | 2011-06-28 16:39:23 -0700 | [diff] [blame] | 111 | static char *args[32]; |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 112 | int control; |
| 113 | int i; |
| 114 | |
Chia-chi Yeh | 063bb92 | 2009-06-15 14:43:38 +0800 | [diff] [blame] | 115 | if ((i = android_get_control_socket("mtpd")) == -1) { |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 116 | return -1; |
Chia-chi Yeh | 063bb92 | 2009-06-15 14:43:38 +0800 | [diff] [blame] | 117 | } |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 118 | log_print(DEBUG, "Waiting for control socket"); |
Chia-chi Yeh | 063bb92 | 2009-06-15 14:43:38 +0800 | [diff] [blame] | 119 | if (listen(i, 1) == -1 || (control = accept(i, NULL, 0)) == -1) { |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 120 | log_print(FATAL, "Cannot get control socket"); |
| 121 | exit(SYSTEM_ERROR); |
| 122 | } |
| 123 | close(i); |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 124 | fcntl(control, F_SETFD, FD_CLOEXEC); |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 125 | |
| 126 | args[0] = (*argv)[0]; |
Chia-chi Yeh | 7b66d20 | 2011-06-28 16:39:23 -0700 | [diff] [blame] | 127 | for (i = 1; i < 32; ++i) { |
Chia-chi Yeh | 41ab620 | 2011-06-28 16:52:02 -0700 | [diff] [blame] | 128 | unsigned char bytes[2]; |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 129 | if (recv(control, &bytes[0], 1, 0) != 1 || |
| 130 | recv(control, &bytes[1], 1, 0) != 1) { |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 131 | log_print(FATAL, "Cannot get argument length"); |
| 132 | exit(SYSTEM_ERROR); |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 133 | } else { |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 134 | int length = bytes[0] << 8 | bytes[1]; |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 135 | int offset = 0; |
Chia-chi Yeh | 41ab620 | 2011-06-28 16:52:02 -0700 | [diff] [blame] | 136 | |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 137 | if (length == 0xFFFF) { |
| 138 | break; |
| 139 | } |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 140 | args[i] = malloc(length + 1); |
| 141 | while (offset < length) { |
| 142 | int n = recv(control, &args[i][offset], length - offset, 0); |
| 143 | if (n > 0) { |
| 144 | offset += n; |
| 145 | } else { |
| 146 | log_print(FATAL, "Cannot get argument value"); |
| 147 | exit(SYSTEM_ERROR); |
| 148 | } |
| 149 | } |
| 150 | args[i][length] = 0; |
| 151 | } |
| 152 | } |
| 153 | log_print(DEBUG, "Received %d arguments", i - 1); |
| 154 | |
| 155 | *argc = i; |
| 156 | *argv = args; |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 157 | return control; |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | #endif |
| 161 | |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 162 | int main(int argc, char **argv) |
| 163 | { |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 164 | struct pollfd pollfds[3]; |
| 165 | int control = -1; |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 166 | int timeout; |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 167 | int status; |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 168 | |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 169 | #ifdef ANDROID_CHANGES |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 170 | control = android_get_control_and_arguments(&argc, &argv); |
| 171 | shutdown(control, SHUT_WR); |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 172 | #endif |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 173 | |
| 174 | srandom(time(NULL)); |
| 175 | |
| 176 | if (pipe(signals) == -1) { |
| 177 | log_print(FATAL, "Pipe() %s", strerror(errno)); |
| 178 | exit(SYSTEM_ERROR); |
| 179 | } |
Chia-chi Yeh | 2d24765 | 2009-06-23 09:13:45 +0800 | [diff] [blame] | 180 | fcntl(signals[0], F_SETFD, FD_CLOEXEC); |
| 181 | fcntl(signals[1], F_SETFD, FD_CLOEXEC); |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 182 | |
Chia-chi Yeh | a777654 | 2009-06-19 16:36:32 +0800 | [diff] [blame] | 183 | timeout = initialize(argc, argv); |
| 184 | |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 185 | signal(SIGHUP, interrupt); |
| 186 | signal(SIGINT, interrupt); |
| 187 | signal(SIGTERM, interrupt); |
| 188 | signal(SIGCHLD, interrupt); |
| 189 | signal(SIGPIPE, SIG_IGN); |
| 190 | atexit(stop_pppd); |
| 191 | |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 192 | pollfds[0].fd = the_socket; |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 193 | pollfds[0].events = POLLIN; |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 194 | pollfds[1].fd = signals[0]; |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 195 | pollfds[1].events = POLLIN; |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 196 | pollfds[2].fd = control; |
| 197 | pollfds[2].events = 0; |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 198 | |
| 199 | while (timeout >= 0) { |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 200 | if (poll(pollfds, 3, timeout ? timeout : -1) == -1 && errno != EINTR) { |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 201 | log_print(FATAL, "Poll() %s", strerror(errno)); |
| 202 | exit(SYSTEM_ERROR); |
| 203 | } |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 204 | if (pollfds[1].revents) { |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 205 | break; |
| 206 | } |
Chia-chi Yeh | 0269612 | 2011-08-22 12:59:46 -0700 | [diff] [blame] | 207 | if (pollfds[2].revents) { |
| 208 | interrupt(SIGTERM); |
| 209 | } |
Chia-chi Yeh | fba8fdc | 2011-12-27 13:39:15 -0800 | [diff] [blame] | 210 | timeout = pollfds[0].revents ? |
| 211 | the_protocol->process() : the_protocol->timeout(); |
Chia-chi Yeh | 35c3150 | 2011-12-08 16:35:09 -0800 | [diff] [blame] | 212 | #ifdef ANDROID_CHANGES |
| 213 | if (!access("/data/misc/vpn/abort", F_OK)) { |
| 214 | interrupt(SIGTERM); |
| 215 | } |
Chia-chi Yeh | fba8fdc | 2011-12-27 13:39:15 -0800 | [diff] [blame] | 216 | if (!timeout) { |
| 217 | timeout = 5000; |
| 218 | } |
Chia-chi Yeh | 35c3150 | 2011-12-08 16:35:09 -0800 | [diff] [blame] | 219 | #endif |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | if (timeout < 0) { |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 223 | status = -timeout; |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 224 | } else { |
| 225 | int signal; |
| 226 | read(signals[0], &signal, sizeof(int)); |
| 227 | log_print(INFO, "Received signal %d", signal); |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 228 | if (signal == SIGCHLD && waitpid(pppd_pid, &status, WNOHANG) == pppd_pid |
Chia-chi Yeh | ec2b039 | 2011-07-13 19:38:14 -0700 | [diff] [blame] | 229 | && WIFEXITED(status)) { |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 230 | status = WEXITSTATUS(status); |
| 231 | log_print(INFO, "Pppd is terminated (status = %d)", status); |
| 232 | status += PPPD_EXITED; |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 233 | pppd_pid = 0; |
| 234 | } else { |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 235 | status = USER_REQUESTED; |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | stop_pppd(); |
| 240 | the_protocol->shutdown(); |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 241 | log_print(INFO, "Mtpd is terminated (status = %d)", status); |
| 242 | return status; |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | void log_print(int level, char *format, ...) |
| 246 | { |
| 247 | if (level >= 0 && level <= LOG_MAX) { |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 248 | #ifdef ANDROID_CHANGES |
| 249 | static int levels[5] = { |
| 250 | ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN, |
| 251 | ANDROID_LOG_ERROR, ANDROID_LOG_FATAL |
| 252 | }; |
| 253 | va_list ap; |
| 254 | va_start(ap, format); |
| 255 | __android_log_vprint(levels[level], "mtpd", format, ap); |
| 256 | va_end(ap); |
| 257 | #else |
| 258 | static char *levels = "DIWEF"; |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 259 | va_list ap; |
| 260 | fprintf(stderr, "%c: ", levels[level]); |
| 261 | va_start(ap, format); |
| 262 | vfprintf(stderr, format, ap); |
| 263 | va_end(ap); |
| 264 | fputc('\n', stderr); |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 265 | #endif |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | void create_socket(int family, int type, char *server, char *port) |
| 270 | { |
| 271 | struct addrinfo hints = { |
| 272 | .ai_flags = AI_NUMERICSERV, |
| 273 | .ai_family = family, |
| 274 | .ai_socktype = type, |
| 275 | }; |
| 276 | struct addrinfo *records; |
| 277 | struct addrinfo *r; |
| 278 | int error; |
| 279 | |
Chia-chi Yeh | 3afd73f | 2011-07-13 18:40:09 -0700 | [diff] [blame] | 280 | log_print(INFO, "Connecting to %s port %s via %s", server, port, interface); |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 281 | |
| 282 | error = getaddrinfo(server, port, &hints, &records); |
| 283 | if (error) { |
| 284 | log_print(FATAL, "Getaddrinfo() %s", (error == EAI_SYSTEM) ? |
Chia-chi Yeh | ec2b039 | 2011-07-13 19:38:14 -0700 | [diff] [blame] | 285 | strerror(errno) : gai_strerror(error)); |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 286 | exit(NETWORK_ERROR); |
| 287 | } |
| 288 | |
| 289 | for (r = records; r; r = r->ai_next) { |
Chia-chi Yeh | 3afd73f | 2011-07-13 18:40:09 -0700 | [diff] [blame] | 290 | int s = socket(r->ai_family, r->ai_socktype, r->ai_protocol); |
| 291 | if (!setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, interface, |
| 292 | strlen(interface)) && !connect(s, r->ai_addr, r->ai_addrlen)) { |
| 293 | the_socket = s; |
| 294 | break; |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 295 | } |
Chia-chi Yeh | 3afd73f | 2011-07-13 18:40:09 -0700 | [diff] [blame] | 296 | close(s); |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | freeaddrinfo(records); |
| 300 | |
| 301 | if (the_socket == -1) { |
| 302 | log_print(FATAL, "Connect() %s", strerror(errno)); |
| 303 | exit(NETWORK_ERROR); |
| 304 | } |
| 305 | |
Robin Lee | b8e401e | 2017-03-14 14:13:58 +0000 | [diff] [blame] | 306 | #ifdef ANDROID_CHANGES |
| 307 | protectFromVpn(the_socket); |
| 308 | #endif |
| 309 | |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 310 | fcntl(the_socket, F_SETFD, FD_CLOEXEC); |
| 311 | log_print(INFO, "Connection established (socket = %d)", the_socket); |
| 312 | } |
| 313 | |
| 314 | void start_pppd(int pppox) |
| 315 | { |
| 316 | if (pppd_pid) { |
| 317 | log_print(WARNING, "Pppd is already started (pid = %d)", pppd_pid); |
| 318 | close(pppox); |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | log_print(INFO, "Starting pppd (pppox = %d)", pppox); |
| 323 | |
| 324 | pppd_pid = fork(); |
| 325 | if (pppd_pid < 0) { |
| 326 | log_print(FATAL, "Fork() %s", strerror(errno)); |
| 327 | exit(SYSTEM_ERROR); |
| 328 | } |
| 329 | |
| 330 | if (!pppd_pid) { |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 331 | char *args[pppd_argc + 5]; |
Sam Protsenko | 28cfaab | 2017-10-30 17:02:26 +0200 | [diff] [blame] | 332 | char number[FD_MAX_LEN + 1]; |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 333 | |
Sam Protsenko | 28cfaab | 2017-10-30 17:02:26 +0200 | [diff] [blame] | 334 | snprintf(number, FD_MAX_LEN + 1, "%d", pppox); |
Chia-chi Yeh | 84e3195 | 2009-06-08 14:11:34 +0800 | [diff] [blame] | 335 | args[0] = "pppd"; |
| 336 | args[1] = "nodetach"; |
| 337 | args[2] = "pppox"; |
| 338 | args[3] = number; |
| 339 | memcpy(&args[4], pppd_argv, sizeof(char *) * pppd_argc); |
| 340 | args[4 + pppd_argc] = NULL; |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 341 | |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 342 | execvp("pppd", args); |
| 343 | log_print(FATAL, "Exec() %s", strerror(errno)); |
Sam Protsenko | 28cfaab | 2017-10-30 17:02:26 +0200 | [diff] [blame] | 344 | exit(SYSTEM_ERROR); /* Pretending a fatal error in pppd. */ |
Chia-chi Yeh | 79e6232 | 2009-06-02 08:49:55 +0800 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | log_print(INFO, "Pppd started (pid = %d)", pppd_pid); |
| 348 | close(pppox); |
| 349 | } |
Sam Protsenko | 28cfaab | 2017-10-30 17:02:26 +0200 | [diff] [blame] | 350 | |
| 351 | /** |
| 352 | * Start pppd daemon with pppol2tp-android plugin. |
| 353 | * |
| 354 | * @param tunnel_fd Tunnel socket file descriptor |
| 355 | * @param session_fd Session socket file descriptor |
| 356 | * @param tunnel_id Tunnel ID; must be in host byte order |
| 357 | * @param session_id Session ID; must be in host byte order |
| 358 | */ |
| 359 | void start_pppd_ol2tp(int tunnel_fd, int session_fd, int tunnel_id, |
| 360 | int session_id) |
| 361 | { |
| 362 | if (pppd_pid) { |
| 363 | log_print(WARNING, "Pppd is already started (pid = %d)", pppd_pid); |
| 364 | goto ret; |
| 365 | } |
| 366 | |
| 367 | log_print(INFO, "Starting pppd (tunnel_fd = %d, session_fd = %d)", |
| 368 | tunnel_fd, session_fd); |
| 369 | |
| 370 | pppd_pid = fork(); |
| 371 | if (pppd_pid < 0) { |
| 372 | log_print(FATAL, "Fork() %s", strerror(errno)); |
| 373 | exit(SYSTEM_ERROR); |
| 374 | } |
| 375 | |
| 376 | if (!pppd_pid) { |
| 377 | char tunnel_fd_str[FD_MAX_LEN + 1]; |
| 378 | char session_fd_str[FD_MAX_LEN + 1]; |
| 379 | char tunnel_id_str[FD_MAX_LEN + 1]; |
| 380 | char session_id_str[FD_MAX_LEN + 1]; |
| 381 | |
| 382 | snprintf(tunnel_fd_str, FD_MAX_LEN + 1, "%d", tunnel_fd); |
| 383 | snprintf(session_fd_str, FD_MAX_LEN + 1, "%d", session_fd); |
| 384 | snprintf(tunnel_id_str, FD_MAX_LEN + 1, "%d", tunnel_id); |
| 385 | snprintf(session_id_str, FD_MAX_LEN + 1, "%d", session_id); |
| 386 | |
| 387 | const char *l2tp_args[] = { |
| 388 | "pppd", |
| 389 | "nodetach", |
| 390 | "plugin", |
| 391 | "pppol2tp-android.so", |
| 392 | "session_fd", |
| 393 | session_fd_str, |
| 394 | "tunnel_fd", |
| 395 | tunnel_fd_str, |
| 396 | "session_id", |
| 397 | session_id_str, |
| 398 | "tunnel_id", |
| 399 | tunnel_id_str, |
| 400 | }; |
| 401 | const size_t args_len = ARRAY_SIZE(l2tp_args) + pppd_argc + 1; |
| 402 | char *args[args_len]; |
| 403 | |
| 404 | /* Populate args[] from l2tp_args[] and pppd_argv[] */ |
| 405 | memcpy(args, l2tp_args, sizeof(l2tp_args)); |
| 406 | memcpy(args + ARRAY_SIZE(l2tp_args), pppd_argv, |
| 407 | sizeof(char *) * pppd_argc); |
| 408 | args[args_len - 1] = NULL; |
| 409 | |
| 410 | execvp("pppd", args); |
| 411 | log_print(FATAL, "Exec() %s", strerror(errno)); |
| 412 | exit(SYSTEM_ERROR); /* Pretending a fatal error in pppd. */ |
| 413 | } |
| 414 | |
| 415 | log_print(INFO, "Pppd started (pid = %d)", pppd_pid); |
| 416 | |
| 417 | ret: |
| 418 | close(session_fd); |
| 419 | close(tunnel_fd); |
| 420 | } |
Sam Protsenko | 0368bbd | 2018-06-08 01:11:16 +0300 | [diff] [blame] | 421 | |
| 422 | /** |
| 423 | * Start pppd daemon with pppopptp-android plugin. |
| 424 | * |
| 425 | * @param pptp_fd PPTP socket file descriptor |
| 426 | */ |
| 427 | void start_pppd_pptp(int pptp_fd) |
| 428 | { |
| 429 | if (pppd_pid) { |
| 430 | log_print(WARNING, "Pppd is already started (pid = %d)", pppd_pid); |
| 431 | goto ret; |
| 432 | } |
| 433 | |
| 434 | log_print(INFO, "Starting pppd (pptp_fd = %d)", pptp_fd); |
| 435 | |
| 436 | pppd_pid = fork(); |
| 437 | if (pppd_pid < 0) { |
| 438 | log_print(FATAL, "Fork() %s", strerror(errno)); |
| 439 | exit(SYSTEM_ERROR); |
| 440 | } |
| 441 | |
| 442 | if (!pppd_pid) { |
| 443 | char pptp_fd_str[FD_MAX_LEN + 1]; |
| 444 | |
| 445 | snprintf(pptp_fd_str, FD_MAX_LEN + 1, "%d", pptp_fd); |
| 446 | |
| 447 | const char *pptp_args[] = { |
| 448 | "pppd", |
| 449 | "nodetach", |
| 450 | "plugin", |
| 451 | "pppopptp-android.so", |
| 452 | "pptp_socket", |
| 453 | pptp_fd_str, |
| 454 | }; |
| 455 | const size_t args_len = ARRAY_SIZE(pptp_args) + pppd_argc + 1; |
| 456 | char *args[args_len]; |
| 457 | |
| 458 | /* Populate args[] from pptp_args[] and pppd_argv[] */ |
| 459 | memcpy(args, pptp_args, sizeof(pptp_args)); |
| 460 | memcpy(args + ARRAY_SIZE(pptp_args), pppd_argv, |
| 461 | sizeof(char *) * pppd_argc); |
| 462 | args[args_len - 1] = NULL; |
| 463 | |
| 464 | execvp("pppd", args); |
| 465 | log_print(FATAL, "Exec() %s", strerror(errno)); |
| 466 | exit(SYSTEM_ERROR); /* Pretending a fatal error in pppd. */ |
| 467 | } |
| 468 | |
| 469 | log_print(INFO, "Pppd started (pid = %d)", pppd_pid); |
| 470 | |
| 471 | ret: |
| 472 | close(pptp_fd); |
| 473 | } |