blob: 7ad90c88f4e027f31123953e2d3b8e06278f60c7 [file] [log] [blame]
Robert Greenwaltc4621772012-01-31 12:46:45 -08001/*
2 * Copyright (C) 2012 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
Dan Albertaa1be2b2015-01-06 09:36:17 -080017#include <ctype.h>
18#include <errno.h>
Lorenzo Colitti70afde62013-03-04 17:58:40 +090019#include <fcntl.h>
Lorenzo Colittiba25df92014-06-18 00:22:17 +090020#include <netdb.h>
Dan Albertaa1be2b2015-01-06 09:36:17 -080021#include <net/if.h>
Lorenzo Colittiba25df92014-06-18 00:22:17 +090022#include <netinet/in.h>
23#include <stdlib.h>
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070024#include <string.h>
Rom Lemarchand838ef642013-01-24 15:14:41 -080025#include <sys/wait.h>
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070026
Jeff Sharkeybec6d042012-09-06 15:45:56 -070027#define LOG_TAG "Netd"
28
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070029#include <cutils/log.h>
Rom Lemarchand838ef642013-01-24 15:14:41 -080030#include <logwrap/logwrap.h>
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070031
Robert Greenwaltc4621772012-01-31 12:46:45 -080032#include "NetdConstants.h"
33
34const char * const OEM_SCRIPT_PATH = "/system/bin/oem-iptables-init.sh";
35const char * const IPTABLES_PATH = "/system/bin/iptables";
JP Abgrall0031cea2012-04-17 16:38:23 -070036const char * const IP6TABLES_PATH = "/system/bin/ip6tables";
Lorenzo Colitti89faa342016-02-26 11:38:47 +090037const char * const IPTABLES_RESTORE_PATH = "/system/bin/iptables-restore";
38const char * const IP6TABLES_RESTORE_PATH = "/system/bin/ip6tables-restore";
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +053039const char * const IPTABLES_RETRY_INTERVAL = "10000";
Robert Greenwaltc4621772012-01-31 12:46:45 -080040const char * const TC_PATH = "/system/bin/tc";
41const char * const IP_PATH = "/system/bin/ip";
42const char * const ADD = "add";
43const char * const DEL = "del";
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070044
Rom Lemarchand838ef642013-01-24 15:14:41 -080045static void logExecError(const char* argv[], int res, int status) {
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070046 const char** argp = argv;
47 std::string args = "";
48 while (*argp) {
49 args += *argp;
50 args += ' ';
51 argp++;
52 }
Rom Lemarchand838ef642013-01-24 15:14:41 -080053 ALOGE("exec() res=%d, status=%d for %s", res, status, args.c_str());
54}
55
56static int execIptablesCommand(int argc, const char *argv[], bool silent) {
57 int res;
58 int status;
59
60 res = android_fork_execvp(argc, (char **)argv, &status, false,
61 !silent);
62 if (res || !WIFEXITED(status) || WEXITSTATUS(status)) {
63 if (!silent) {
64 logExecError(argv, res, status);
65 }
66 if (res)
67 return res;
68 if (!WIFEXITED(status))
69 return ECHILD;
70 }
71 return WEXITSTATUS(status);
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070072}
73
74static int execIptables(IptablesTarget target, bool silent, va_list args) {
75 /* Read arguments from incoming va_list; we expect the list to be NULL terminated. */
76 std::list<const char*> argsList;
77 argsList.push_back(NULL);
78 const char* arg;
Amith Yamasani390e4ea2015-04-25 19:08:57 -070079
80 // Wait to avoid failure due to another process holding the lock
81 argsList.push_back("-w");
Devi Sandeep Endluri V V9afc4f92016-10-13 13:15:17 +053082 argsList.push_back("-W");
83 argsList.push_back(IPTABLES_RETRY_INTERVAL);
Amith Yamasani390e4ea2015-04-25 19:08:57 -070084
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070085 do {
86 arg = va_arg(args, const char *);
87 argsList.push_back(arg);
88 } while (arg);
89
90 int i = 0;
91 const char* argv[argsList.size()];
92 std::list<const char*>::iterator it;
93 for (it = argsList.begin(); it != argsList.end(); it++, i++) {
94 argv[i] = *it;
95 }
96
97 int res = 0;
98 if (target == V4 || target == V4V6) {
99 argv[0] = IPTABLES_PATH;
Rom Lemarchand838ef642013-01-24 15:14:41 -0800100 res |= execIptablesCommand(argsList.size(), argv, silent);
Jeff Sharkey8e188ed2012-07-12 18:32:03 -0700101 }
102 if (target == V6 || target == V4V6) {
103 argv[0] = IP6TABLES_PATH;
Rom Lemarchand838ef642013-01-24 15:14:41 -0800104 res |= execIptablesCommand(argsList.size(), argv, silent);
Jeff Sharkey8e188ed2012-07-12 18:32:03 -0700105 }
106 return res;
107}
108
109int execIptables(IptablesTarget target, ...) {
110 va_list args;
111 va_start(args, target);
112 int res = execIptables(target, false, args);
113 va_end(args);
114 return res;
115}
116
117int execIptablesSilently(IptablesTarget target, ...) {
118 va_list args;
119 va_start(args, target);
120 int res = execIptables(target, true, args);
121 va_end(args);
122 return res;
123}
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900124
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900125static int execIptablesRestoreCommand(const char *cmd, const std::string& commands) {
126 const char *argv[] = {
127 cmd,
128 "--noflush", // Don't flush the whole table.
129 "-w", // Wait instead of failing if the lock is held.
130 };
131 AndroidForkExecvpOption opt[1] = {
132 {
133 .opt_type = FORK_EXECVP_OPTION_INPUT,
134 .opt_input.input = reinterpret_cast<const uint8_t*>(commands.c_str()),
135 .opt_input.input_len = commands.size(),
136 }
137 };
138
139 int status = 0;
140 int res = android_fork_execvp_ext(
141 ARRAY_SIZE(argv), (char**)argv, &status, false /* ignore_int_quit */, LOG_NONE,
142 false /* abbreviated */, NULL /* file_path */, opt, ARRAY_SIZE(opt));
143 if (res || status) {
144 ALOGE("%s failed with res=%d, status=%d", argv[0], res, status);
145 return -1;
146 }
147
148 return 0;
149}
150
151int execIptablesRestore(IptablesTarget target, const std::string& commands) {
152 int res = 0;
153 if (target == V4 || target == V4V6) {
154 res |= execIptablesRestoreCommand(IPTABLES_RESTORE_PATH, commands);
155 }
156 if (target == V6 || target == V4V6) {
157 res |= execIptablesRestoreCommand(IP6TABLES_RESTORE_PATH, commands);
158 }
159 return res;
160}
161
JP Abgrall69261cb2014-06-19 18:35:24 -0700162/*
163 * Check an interface name for plausibility. This should e.g. help against
164 * directory traversal.
165 */
166bool isIfaceName(const char *name) {
167 size_t i;
168 size_t name_len = strlen(name);
169 if ((name_len == 0) || (name_len > IFNAMSIZ)) {
170 return false;
171 }
172
173 /* First character must be alphanumeric */
174 if (!isalnum(name[0])) {
175 return false;
176 }
177
178 for (i = 1; i < name_len; i++) {
179 if (!isalnum(name[i]) && (name[i] != '_') && (name[i] != '-') && (name[i] != ':')) {
180 return false;
181 }
182 }
183
184 return true;
185}
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900186
187int parsePrefix(const char *prefix, uint8_t *family, void *address, int size, uint8_t *prefixlen) {
188 if (!prefix || !family || !address || !prefixlen) {
189 return -EFAULT;
190 }
191
192 // Find the '/' separating address from prefix length.
193 const char *slash = strchr(prefix, '/');
194 const char *prefixlenString = slash + 1;
195 if (!slash || !*prefixlenString)
196 return -EINVAL;
197
198 // Convert the prefix length to a uint8_t.
199 char *endptr;
200 unsigned templen;
201 templen = strtoul(prefixlenString, &endptr, 10);
202 if (*endptr || templen > 255) {
203 return -EINVAL;
204 }
205 *prefixlen = templen;
206
207 // Copy the address part of the prefix to a local buffer. We have to copy
208 // because inet_pton and getaddrinfo operate on null-terminated address
209 // strings, but prefix is const and has '/' after the address.
210 std::string addressString(prefix, slash - prefix);
211
212 // Parse the address.
213 addrinfo *res;
214 addrinfo hints = {
215 .ai_flags = AI_NUMERICHOST,
216 };
217 int ret = getaddrinfo(addressString.c_str(), NULL, &hints, &res);
218 if (ret || !res) {
219 return -EINVAL; // getaddrinfo return values are not errno values.
220 }
221
222 // Convert the address string to raw address bytes.
223 void *rawAddress;
224 int rawLength;
225 switch (res[0].ai_family) {
226 case AF_INET: {
227 if (*prefixlen > 32) {
228 return -EINVAL;
229 }
230 sockaddr_in *sin = (sockaddr_in *) res[0].ai_addr;
231 rawAddress = &sin->sin_addr;
232 rawLength = 4;
233 break;
234 }
235 case AF_INET6: {
236 if (*prefixlen > 128) {
237 return -EINVAL;
238 }
239 sockaddr_in6 *sin6 = (sockaddr_in6 *) res[0].ai_addr;
240 rawAddress = &sin6->sin6_addr;
241 rawLength = 16;
242 break;
243 }
244 default: {
245 freeaddrinfo(res);
246 return -EAFNOSUPPORT;
247 }
248 }
249
250 if (rawLength > size) {
251 freeaddrinfo(res);
252 return -ENOSPC;
253 }
254
255 *family = res[0].ai_family;
256 memcpy(address, rawAddress, rawLength);
257 freeaddrinfo(res);
258
259 return rawLength;
260}