San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 <stdlib.h> |
| 18 | #include <errno.h> |
San Mehat | 1873784 | 2010-01-21 09:22:43 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
Olivier Bailly | ff2c0d8 | 2010-11-17 11:45:07 -0800 | [diff] [blame] | 20 | #include <string.h> |
San Mehat | 1873784 | 2010-01-21 09:22:43 -0800 | [diff] [blame] | 21 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
San Mehat | 1873784 | 2010-01-21 09:22:43 -0800 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #include <sys/wait.h> |
Subash Abhinov Kasiviswanathan | 0e217b5 | 2014-07-15 00:08:40 -0600 | [diff] [blame] | 26 | #include <linux/capability.h> |
San Mehat | 1873784 | 2010-01-21 09:22:43 -0800 | [diff] [blame] | 27 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 28 | #include <netinet/in.h> |
| 29 | #include <arpa/inet.h> |
| 30 | |
| 31 | #define LOG_TAG "TetherController" |
Subash Abhinov Kasiviswanathan | 0e217b5 | 2014-07-15 00:08:40 -0600 | [diff] [blame] | 32 | #define LOG_NDEBUG 0 |
| 33 | #define LOG_NDDEBUG 0 |
| 34 | #define LOG_NIDEBUG 0 |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 35 | #include <cutils/log.h> |
Kazuhiro Ondo | 6b858eb | 2011-06-24 20:31:03 -0500 | [diff] [blame] | 36 | #include <cutils/properties.h> |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 37 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 38 | #include "Fwmark.h" |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 39 | #include "NetdConstants.h" |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 40 | #include "Permission.h" |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 41 | #include "TetherController.h" |
| 42 | |
Subash Abhinov Kasiviswanathan | 0e217b5 | 2014-07-15 00:08:40 -0600 | [diff] [blame] | 43 | #include <private/android_filesystem_config.h> |
| 44 | #include <unistd.h> |
| 45 | |
| 46 | #define RTRADVDAEMON "/system/bin/radish" |
| 47 | #define IP4_CFG_IP_FORWARD "/proc/sys/net/ipv4/ip_forward" |
| 48 | #define IP6_CFG_ALL_PROXY_NDP "/proc/sys/net/ipv6/conf/all/proxy_ndp" |
| 49 | #define IP6_CFG_ALL_FORWARDING "/proc/sys/net/ipv6/conf/all/forwarding" |
| 50 | #define IP6_IFACE_CFG_ACCEPT_RA "/proc/sys/net/ipv6/conf/%s/accept_ra" |
| 51 | #define PROC_PATH_SIZE 255 |
| 52 | |
| 53 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 54 | TetherController::TetherController() { |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 55 | mInterfaces = new InterfaceCollection(); |
Subash Abhinov Kasiviswanathan | 0e217b5 | 2014-07-15 00:08:40 -0600 | [diff] [blame] | 56 | mUpstreamInterfaces = new InterfaceCollection(); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 57 | mDnsForwarders = new NetAddressCollection(); |
| 58 | mDaemonFd = -1; |
| 59 | mDaemonPid = 0; |
| 60 | } |
| 61 | |
| 62 | TetherController::~TetherController() { |
| 63 | InterfaceCollection::iterator it; |
| 64 | |
| 65 | for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) { |
| 66 | free(*it); |
| 67 | } |
| 68 | mInterfaces->clear(); |
| 69 | |
Subash Abhinov Kasiviswanathan | 0e217b5 | 2014-07-15 00:08:40 -0600 | [diff] [blame] | 70 | for (it = mUpstreamInterfaces->begin(); it != mUpstreamInterfaces->end(); ++it) { |
| 71 | free(*it); |
| 72 | } |
| 73 | mUpstreamInterfaces->clear(); |
| 74 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 75 | mDnsForwarders->clear(); |
| 76 | } |
| 77 | |
Subash Abhinov Kasiviswanathan | 0e217b5 | 2014-07-15 00:08:40 -0600 | [diff] [blame] | 78 | static int config_write_setting(const char *path, const char *value) |
| 79 | { |
| 80 | int fd = open(path, O_WRONLY); |
| 81 | |
| 82 | ALOGD("config_write_setting(%s, %s)", path, value); |
| 83 | if (fd < 0) { |
| 84 | ALOGE("Failed to open %s (%s)", path, strerror(errno)); |
| 85 | return -1; |
| 86 | } |
| 87 | if (write(fd, value, strlen(value)) != (int)strlen(value)) { |
| 88 | ALOGE("Failed to write to %s (%s)", path, strerror(errno)); |
| 89 | close(fd); |
| 90 | return -1; |
| 91 | } |
| 92 | close(fd); |
| 93 | return 0; |
| 94 | } |
| 95 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 96 | int TetherController::setIpFwdEnabled(bool enable) { |
| 97 | |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 98 | ALOGD("Setting IP forward enable = %d", enable); |
Kazuhiro Ondo | 6b858eb | 2011-06-24 20:31:03 -0500 | [diff] [blame] | 99 | |
| 100 | // In BP tools mode, do not disable IP forwarding |
| 101 | char bootmode[PROPERTY_VALUE_MAX] = {0}; |
| 102 | property_get("ro.bootmode", bootmode, "unknown"); |
| 103 | if ((enable == false) && (0 == strcmp("bp-tools", bootmode))) { |
| 104 | return 0; |
| 105 | } |
| 106 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 107 | int fd = open("/proc/sys/net/ipv4/ip_forward", O_WRONLY); |
| 108 | if (fd < 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 109 | ALOGE("Failed to open ip_forward (%s)", strerror(errno)); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | if (write(fd, (enable ? "1" : "0"), 1) != 1) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 114 | ALOGE("Failed to write ip_forward (%s)", strerror(errno)); |
Robert Greenwalt | 37dc4a5 | 2010-04-28 16:05:04 -0700 | [diff] [blame] | 115 | close(fd); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 116 | return -1; |
| 117 | } |
| 118 | close(fd); |
Subash Abhinov Kasiviswanathan | 0e217b5 | 2014-07-15 00:08:40 -0600 | [diff] [blame] | 119 | if (config_write_setting( |
| 120 | IP6_CFG_ALL_PROXY_NDP, enable ? "2" : "0")) { |
| 121 | ALOGE("Failed to write proxy_ndp (%s)", strerror(errno)); |
| 122 | return -1; |
| 123 | } |
| 124 | if (config_write_setting( |
| 125 | IP6_CFG_ALL_FORWARDING, enable ? "2" : "0")) { |
| 126 | ALOGE("Failed to write ip6 forwarding (%s)", strerror(errno)); |
| 127 | return -1; |
| 128 | } |
| 129 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | bool TetherController::getIpFwdEnabled() { |
| 134 | int fd = open("/proc/sys/net/ipv4/ip_forward", O_RDONLY); |
| 135 | |
| 136 | if (fd < 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 137 | ALOGE("Failed to open ip_forward (%s)", strerror(errno)); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 138 | return false; |
| 139 | } |
| 140 | |
| 141 | char enabled; |
| 142 | if (read(fd, &enabled, 1) != 1) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 143 | ALOGE("Failed to read ip_forward (%s)", strerror(errno)); |
Robert Greenwalt | 37dc4a5 | 2010-04-28 16:05:04 -0700 | [diff] [blame] | 144 | close(fd); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | close(fd); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 149 | return (enabled == '1' ? true : false); |
| 150 | } |
| 151 | |
Dmitry Shmidt | bc775ed | 2013-12-12 16:41:16 -0800 | [diff] [blame] | 152 | #define TETHER_START_CONST_ARG 8 |
| 153 | |
Robert Greenwalt | 3208ea0 | 2010-03-24 16:32:55 -0700 | [diff] [blame] | 154 | int TetherController::startTethering(int num_addrs, struct in_addr* addrs) { |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 155 | if (mDaemonPid != 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 156 | ALOGE("Tethering already started"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 157 | errno = EBUSY; |
| 158 | return -1; |
| 159 | } |
| 160 | |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 161 | ALOGD("Starting tethering services"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 162 | |
| 163 | pid_t pid; |
| 164 | int pipefd[2]; |
| 165 | |
| 166 | if (pipe(pipefd) < 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 167 | ALOGE("pipe failed (%s)", strerror(errno)); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * TODO: Create a monitoring thread to handle and restart |
| 173 | * the daemon if it exits prematurely |
| 174 | */ |
| 175 | if ((pid = fork()) < 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 176 | ALOGE("fork failed (%s)", strerror(errno)); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 177 | close(pipefd[0]); |
| 178 | close(pipefd[1]); |
| 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | if (!pid) { |
| 183 | close(pipefd[1]); |
| 184 | if (pipefd[0] != STDIN_FILENO) { |
| 185 | if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 186 | ALOGE("dup2 failed (%s)", strerror(errno)); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 187 | return -1; |
| 188 | } |
| 189 | close(pipefd[0]); |
| 190 | } |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 191 | |
Dmitry Shmidt | bc775ed | 2013-12-12 16:41:16 -0800 | [diff] [blame] | 192 | int num_processed_args = TETHER_START_CONST_ARG + (num_addrs/2) + 1; |
Robert Greenwalt | 3208ea0 | 2010-03-24 16:32:55 -0700 | [diff] [blame] | 193 | char **args = (char **)malloc(sizeof(char *) * num_processed_args); |
| 194 | args[num_processed_args - 1] = NULL; |
| 195 | args[0] = (char *)"/system/bin/dnsmasq"; |
Peter Nilsson | b756f69 | 2011-09-08 09:48:31 -0700 | [diff] [blame] | 196 | args[1] = (char *)"--keep-in-foreground"; |
Robert Greenwalt | 3208ea0 | 2010-03-24 16:32:55 -0700 | [diff] [blame] | 197 | args[2] = (char *)"--no-resolv"; |
| 198 | args[3] = (char *)"--no-poll"; |
Dmitry Shmidt | bc775ed | 2013-12-12 16:41:16 -0800 | [diff] [blame] | 199 | args[4] = (char *)"--dhcp-authoritative"; |
Jeff Sharkey | 6df79da | 2012-04-18 21:53:35 -0700 | [diff] [blame] | 200 | // TODO: pipe through metered status from ConnService |
Dmitry Shmidt | bc775ed | 2013-12-12 16:41:16 -0800 | [diff] [blame] | 201 | args[5] = (char *)"--dhcp-option-force=43,ANDROID_METERED"; |
| 202 | args[6] = (char *)"--pid-file"; |
| 203 | args[7] = (char *)""; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 204 | |
Dmitry Shmidt | bc775ed | 2013-12-12 16:41:16 -0800 | [diff] [blame] | 205 | int nextArg = TETHER_START_CONST_ARG; |
Robert Greenwalt | 3208ea0 | 2010-03-24 16:32:55 -0700 | [diff] [blame] | 206 | for (int addrIndex=0; addrIndex < num_addrs;) { |
| 207 | char *start = strdup(inet_ntoa(addrs[addrIndex++])); |
| 208 | char *end = strdup(inet_ntoa(addrs[addrIndex++])); |
| 209 | asprintf(&(args[nextArg++]),"--dhcp-range=%s,%s,1h", start, end); |
| 210 | } |
| 211 | |
| 212 | if (execv(args[0], args)) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 213 | ALOGE("execl failed (%s)", strerror(errno)); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 214 | } |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 215 | ALOGE("Should never get here!"); |
JP Abgrall | ce4f379 | 2012-08-06 13:44:44 -0700 | [diff] [blame] | 216 | _exit(-1); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 217 | } else { |
| 218 | close(pipefd[0]); |
| 219 | mDaemonPid = pid; |
| 220 | mDaemonFd = pipefd[1]; |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 221 | applyDnsInterfaces(); |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 222 | ALOGD("Tethering services running"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | int TetherController::stopTethering() { |
| 229 | |
| 230 | if (mDaemonPid == 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 231 | ALOGE("Tethering already stopped"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 235 | ALOGD("Stopping tethering services"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 236 | |
| 237 | kill(mDaemonPid, SIGTERM); |
San Mehat | 1873784 | 2010-01-21 09:22:43 -0800 | [diff] [blame] | 238 | waitpid(mDaemonPid, NULL, 0); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 239 | mDaemonPid = 0; |
| 240 | close(mDaemonFd); |
| 241 | mDaemonFd = -1; |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 242 | ALOGD("Tethering services stopped"); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 243 | return 0; |
| 244 | } |
Matthew Xie | 1994410 | 2012-07-12 16:42:07 -0700 | [diff] [blame] | 245 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 246 | bool TetherController::isTetheringStarted() { |
| 247 | return (mDaemonPid == 0 ? false : true); |
| 248 | } |
| 249 | |
Subash Abhinov Kasiviswanathan | 0e217b5 | 2014-07-15 00:08:40 -0600 | [diff] [blame] | 250 | int TetherController::startV6RtrAdv(int num_ifaces, char **ifaces) { |
| 251 | int pid; |
| 252 | int num_processed_args = 1; |
| 253 | gid_t groups [] = { AID_NET_ADMIN, AID_NET_RAW, AID_INET }; |
| 254 | |
| 255 | if (num_ifaces < 2) { |
| 256 | ALOGD("Need atleast two interfaces to start Router advertisement daemon"); |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | if ((pid = fork()) < 0) { |
| 261 | ALOGE("%s: fork failed (%s)", __func__, strerror(errno)); |
| 262 | return -1; |
| 263 | } |
| 264 | if (!pid) { |
| 265 | char **args; |
| 266 | const char *cmd = RTRADVDAEMON; |
| 267 | |
| 268 | args = (char **)calloc(num_ifaces * 3 + 2, sizeof(char *)); |
| 269 | |
| 270 | args[0] = strdup(RTRADVDAEMON); |
| 271 | for (int i=0; i < num_ifaces; i++) { |
| 272 | int aidx = 3 * i + num_processed_args; |
| 273 | args[aidx] = (char *)"-i"; |
| 274 | args[aidx + 1] = ifaces[i]; |
| 275 | args[aidx + 2] = (char *)"-x"; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | setgroups(sizeof(groups)/sizeof(groups[0]), groups); |
| 280 | setresgid(AID_RADIO, AID_RADIO, AID_RADIO); |
| 281 | setresuid(AID_RADIO, AID_RADIO, AID_RADIO); |
| 282 | |
| 283 | if (execv(cmd, args)) { |
| 284 | ALOGE("Unable to exec %s: (%s)" , cmd, strerror(errno)); |
| 285 | } |
| 286 | free(args[0]); |
| 287 | free(args); |
| 288 | exit(0); |
| 289 | } else { |
| 290 | mRtrAdvPid = pid; |
| 291 | ALOGD("Router advertisement daemon running"); |
| 292 | } |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | int TetherController::stopV6RtrAdv() { |
| 297 | if (!mRtrAdvPid) { |
| 298 | ALOGD("Router advertisement daemon already stopped"); |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | kill(mRtrAdvPid, SIGTERM); |
| 303 | waitpid(mRtrAdvPid, NULL, 0); |
| 304 | mRtrAdvPid = 0; |
| 305 | ALOGD("Router advertisement daemon stopped"); |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | int TetherController::addV6RtrAdvIface(const char *iface) { |
| 310 | char **args; |
| 311 | int i; |
| 312 | int len; |
| 313 | InterfaceCollection::iterator it; |
| 314 | /* For now, just stop and start the daemon with the new interface list */ |
| 315 | |
| 316 | len = mInterfaces->size() + mUpstreamInterfaces->size(); |
| 317 | ALOGD("addV6RtrAdvIface: len = %d. Iface: %s\n", len, iface); |
| 318 | args = (char **)calloc(len, sizeof(char *)); |
| 319 | |
| 320 | if (!args) { |
| 321 | errno = ENOMEM; |
| 322 | return -1; |
| 323 | } |
| 324 | |
| 325 | for (i = 0, it = mInterfaces->begin(); it != mInterfaces->end(); it++, i++) { |
| 326 | args[i] = *it; |
| 327 | } |
| 328 | |
| 329 | for (it = mUpstreamInterfaces->begin(); i < len && it != mUpstreamInterfaces->end(); it++, i++) { |
| 330 | args[i] = *it; |
| 331 | } |
| 332 | |
| 333 | stopV6RtrAdv(); |
| 334 | startV6RtrAdv(i, args); |
| 335 | |
| 336 | free(args); |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | int TetherController::removeV6RtrAdvIface(const char *iface) { |
| 342 | /* For now, just call addV6RtrAdvIface, since that will stop and |
| 343 | * start the daemon with the updated interfaces |
| 344 | */ |
| 345 | return addV6RtrAdvIface(iface); |
| 346 | } |
| 347 | bool TetherController::isV6RtrAdvStarted() { |
| 348 | return (mRtrAdvPid == 0 ? false : true); |
| 349 | } |
| 350 | |
Kenny Root | cf52faf | 2010-02-18 09:59:55 -0800 | [diff] [blame] | 351 | #define MAX_CMD_SIZE 1024 |
| 352 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 353 | int TetherController::setDnsForwarders(unsigned netId, char **servers, int numServers) { |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 354 | int i; |
Kenny Root | cf52faf | 2010-02-18 09:59:55 -0800 | [diff] [blame] | 355 | char daemonCmd[MAX_CMD_SIZE]; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 356 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 357 | Fwmark fwmark; |
| 358 | fwmark.netId = netId; |
| 359 | fwmark.explicitlySelected = true; |
| 360 | fwmark.protectedFromVpn = true; |
| 361 | fwmark.permission = PERMISSION_SYSTEM; |
| 362 | |
| 363 | snprintf(daemonCmd, sizeof(daemonCmd), "update_dns:0x%x", fwmark.intValue); |
Kenny Root | cf52faf | 2010-02-18 09:59:55 -0800 | [diff] [blame] | 364 | int cmdLen = strlen(daemonCmd); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 365 | |
| 366 | mDnsForwarders->clear(); |
| 367 | for (i = 0; i < numServers; i++) { |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 368 | ALOGD("setDnsForwarders(0x%x %d = '%s')", fwmark.intValue, i, servers[i]); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 369 | |
| 370 | struct in_addr a; |
| 371 | |
| 372 | if (!inet_aton(servers[i], &a)) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 373 | ALOGE("Failed to parse DNS server '%s'", servers[i]); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 374 | mDnsForwarders->clear(); |
| 375 | return -1; |
| 376 | } |
Kenny Root | cf52faf | 2010-02-18 09:59:55 -0800 | [diff] [blame] | 377 | |
Nick Kralevich | ad5b41f | 2012-07-19 18:48:05 -0700 | [diff] [blame] | 378 | cmdLen += (strlen(servers[i]) + 1); |
| 379 | if (cmdLen + 1 >= MAX_CMD_SIZE) { |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 380 | ALOGD("Too many DNS servers listed"); |
Kenny Root | cf52faf | 2010-02-18 09:59:55 -0800 | [diff] [blame] | 381 | break; |
| 382 | } |
| 383 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 384 | strcat(daemonCmd, ":"); |
| 385 | strcat(daemonCmd, servers[i]); |
| 386 | mDnsForwarders->push_back(a); |
| 387 | } |
| 388 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 389 | mDnsNetId = netId; |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 390 | if (mDaemonFd != -1) { |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 391 | ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 392 | if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 393 | ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno)); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 394 | mDnsForwarders->clear(); |
| 395 | return -1; |
| 396 | } |
| 397 | } |
| 398 | return 0; |
| 399 | } |
| 400 | |
Lorenzo Colitti | 667c477 | 2014-08-26 14:13:07 -0700 | [diff] [blame] | 401 | unsigned TetherController::getDnsNetId() { |
| 402 | return mDnsNetId; |
| 403 | } |
| 404 | |
Subash Abhinov Kasiviswanathan | 0e217b5 | 2014-07-15 00:08:40 -0600 | [diff] [blame] | 405 | int TetherController::addUpstreamInterface(char *iface) |
| 406 | { |
| 407 | InterfaceCollection::iterator it; |
| 408 | |
| 409 | ALOGD("addUpstreamInterface(%s)\n", iface); |
| 410 | |
| 411 | if (!iface) { |
| 412 | ALOGE("addUpstreamInterface: received null interface"); |
| 413 | return 0; |
| 414 | } |
| 415 | for (it = mUpstreamInterfaces->begin(); it != mUpstreamInterfaces->end(); ++it) { |
| 416 | ALOGD("."); |
| 417 | if (*it && !strcmp(iface, *it)) { |
| 418 | ALOGD("addUpstreamInterface: interface %s already present", iface); |
| 419 | return 0; |
| 420 | } |
| 421 | } |
| 422 | mUpstreamInterfaces->push_back(strdup(iface)); |
| 423 | |
| 424 | return addV6RtrAdvIface(iface); |
| 425 | } |
| 426 | |
| 427 | int TetherController::removeUpstreamInterface(char *iface) |
| 428 | { |
| 429 | InterfaceCollection::iterator it; |
| 430 | |
| 431 | if (!iface) { |
| 432 | ALOGE("removeUpstreamInterface: Null interface name received"); |
| 433 | return 0; |
| 434 | } |
| 435 | for (it = mUpstreamInterfaces->begin(); it != mUpstreamInterfaces->end(); ++it) { |
| 436 | if (*it && !strcmp(iface, *it)) { |
| 437 | free(*it); |
| 438 | mUpstreamInterfaces->erase(it); |
| 439 | return removeV6RtrAdvIface(iface); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | ALOGW("Couldn't find interface %s to remove", iface); |
| 444 | return 0; |
| 445 | } |
| 446 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 447 | NetAddressCollection *TetherController::getDnsForwarders() { |
| 448 | return mDnsForwarders; |
| 449 | } |
| 450 | |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 451 | int TetherController::applyDnsInterfaces() { |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 452 | char daemonCmd[MAX_CMD_SIZE]; |
| 453 | |
| 454 | strcpy(daemonCmd, "update_ifaces"); |
| 455 | int cmdLen = strlen(daemonCmd); |
| 456 | InterfaceCollection::iterator it; |
| 457 | bool haveInterfaces = false; |
| 458 | |
| 459 | for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) { |
| 460 | cmdLen += (strlen(*it) + 1); |
| 461 | if (cmdLen + 1 >= MAX_CMD_SIZE) { |
| 462 | ALOGD("Too many DNS ifaces listed"); |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | strcat(daemonCmd, ":"); |
| 467 | strcat(daemonCmd, *it); |
| 468 | haveInterfaces = true; |
| 469 | } |
| 470 | |
| 471 | if ((mDaemonFd != -1) && haveInterfaces) { |
| 472 | ALOGD("Sending update msg to dnsmasq [%s]", daemonCmd); |
| 473 | if (write(mDaemonFd, daemonCmd, strlen(daemonCmd) +1) < 0) { |
| 474 | ALOGE("Failed to send update command to dnsmasq (%s)", strerror(errno)); |
| 475 | return -1; |
| 476 | } |
| 477 | } |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 481 | int TetherController::tetherInterface(const char *interface) { |
| 482 | ALOGD("tetherInterface(%s)", interface); |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 483 | if (!isIfaceName(interface)) { |
| 484 | errno = ENOENT; |
| 485 | return -1; |
| 486 | } |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 487 | mInterfaces->push_back(strdup(interface)); |
| 488 | |
Subash Abhinov Kasiviswanathan | 0e217b5 | 2014-07-15 00:08:40 -0600 | [diff] [blame] | 489 | addV6RtrAdvIface(interface); |
| 490 | |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 491 | if (applyDnsInterfaces()) { |
| 492 | InterfaceCollection::iterator it; |
| 493 | for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) { |
| 494 | if (!strcmp(interface, *it)) { |
| 495 | free(*it); |
| 496 | mInterfaces->erase(it); |
| 497 | break; |
| 498 | } |
| 499 | } |
| 500 | return -1; |
| 501 | } else { |
| 502 | return 0; |
| 503 | } |
| 504 | } |
| 505 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 506 | int TetherController::untetherInterface(const char *interface) { |
| 507 | InterfaceCollection::iterator it; |
| 508 | |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 509 | ALOGD("untetherInterface(%s)", interface); |
| 510 | |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 511 | for (it = mInterfaces->begin(); it != mInterfaces->end(); ++it) { |
| 512 | if (!strcmp(interface, *it)) { |
| 513 | free(*it); |
| 514 | mInterfaces->erase(it); |
Subash Abhinov Kasiviswanathan | 0e217b5 | 2014-07-15 00:08:40 -0600 | [diff] [blame] | 515 | removeV6RtrAdvIface(NULL); |
Robert Greenwalt | 3d4c758 | 2012-12-11 12:33:37 -0800 | [diff] [blame] | 516 | return applyDnsInterfaces(); |
San Mehat | 9d10b34 | 2010-01-18 09:51:02 -0800 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | errno = ENOENT; |
| 520 | return -1; |
| 521 | } |
| 522 | |
| 523 | InterfaceCollection *TetherController::getTetheredInterfaceList() { |
| 524 | return mInterfaces; |
| 525 | } |