blob: 0a0ca5dabe6f520623f0b39384b9ba8bc081da2b [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
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090029#include <android-base/stringprintf.h>
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070030#include <cutils/log.h>
Rom Lemarchand838ef642013-01-24 15:14:41 -080031#include <logwrap/logwrap.h>
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070032
Narayan Kamatha5ace892017-01-06 15:10:02 +000033#include "Controllers.h"
Robert Greenwaltc4621772012-01-31 12:46:45 -080034#include "NetdConstants.h"
Narayan Kamatha5ace892017-01-06 15:10:02 +000035#include "IptablesRestoreController.h"
Robert Greenwaltc4621772012-01-31 12:46:45 -080036
37const char * const OEM_SCRIPT_PATH = "/system/bin/oem-iptables-init.sh";
38const char * const IPTABLES_PATH = "/system/bin/iptables";
JP Abgrall0031cea2012-04-17 16:38:23 -070039const char * const IP6TABLES_PATH = "/system/bin/ip6tables";
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");
82
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070083 do {
84 arg = va_arg(args, const char *);
85 argsList.push_back(arg);
86 } while (arg);
87
88 int i = 0;
89 const char* argv[argsList.size()];
90 std::list<const char*>::iterator it;
91 for (it = argsList.begin(); it != argsList.end(); it++, i++) {
92 argv[i] = *it;
93 }
94
95 int res = 0;
96 if (target == V4 || target == V4V6) {
97 argv[0] = IPTABLES_PATH;
Rom Lemarchand838ef642013-01-24 15:14:41 -080098 res |= execIptablesCommand(argsList.size(), argv, silent);
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070099 }
100 if (target == V6 || target == V4V6) {
101 argv[0] = IP6TABLES_PATH;
Rom Lemarchand838ef642013-01-24 15:14:41 -0800102 res |= execIptablesCommand(argsList.size(), argv, silent);
Jeff Sharkey8e188ed2012-07-12 18:32:03 -0700103 }
104 return res;
105}
106
107int execIptables(IptablesTarget target, ...) {
108 va_list args;
109 va_start(args, target);
110 int res = execIptables(target, false, args);
111 va_end(args);
112 return res;
113}
114
115int execIptablesSilently(IptablesTarget target, ...) {
116 va_list args;
117 va_start(args, target);
118 int res = execIptables(target, true, args);
119 va_end(args);
120 return res;
121}
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900122
Lorenzo Colitticd283772017-01-31 19:00:49 +0900123int execIptablesRestoreWithOutput(IptablesTarget target, const std::string& commands,
124 std::string *output) {
125 return android::net::gCtls->iptablesRestoreCtrl.execute(target, commands, output);
126}
127
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900128int execIptablesRestore(IptablesTarget target, const std::string& commands) {
Lorenzo Colitticd283772017-01-31 19:00:49 +0900129 return execIptablesRestoreWithOutput(target, commands, nullptr);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900130}
131
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900132int execIptablesRestoreCommand(IptablesTarget target, const std::string& table,
133 const std::string& command, std::string *output) {
134 std::string fullCmd = android::base::StringPrintf("*%s\n%s\nCOMMIT\n", table.c_str(),
135 command.c_str());
136 return execIptablesRestoreWithOutput(target, fullCmd, output);
137}
138
JP Abgrall69261cb2014-06-19 18:35:24 -0700139/*
140 * Check an interface name for plausibility. This should e.g. help against
141 * directory traversal.
142 */
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900143bool isIfaceName(const std::string& name) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700144 size_t i;
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900145 if ((name.empty()) || (name.size() > IFNAMSIZ)) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700146 return false;
147 }
148
149 /* First character must be alphanumeric */
150 if (!isalnum(name[0])) {
151 return false;
152 }
153
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900154 for (i = 1; i < name.size(); i++) {
JP Abgrall69261cb2014-06-19 18:35:24 -0700155 if (!isalnum(name[i]) && (name[i] != '_') && (name[i] != '-') && (name[i] != ':')) {
156 return false;
157 }
158 }
159
160 return true;
161}
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900162
163int parsePrefix(const char *prefix, uint8_t *family, void *address, int size, uint8_t *prefixlen) {
164 if (!prefix || !family || !address || !prefixlen) {
165 return -EFAULT;
166 }
167
168 // Find the '/' separating address from prefix length.
169 const char *slash = strchr(prefix, '/');
170 const char *prefixlenString = slash + 1;
171 if (!slash || !*prefixlenString)
172 return -EINVAL;
173
174 // Convert the prefix length to a uint8_t.
175 char *endptr;
176 unsigned templen;
177 templen = strtoul(prefixlenString, &endptr, 10);
178 if (*endptr || templen > 255) {
179 return -EINVAL;
180 }
181 *prefixlen = templen;
182
183 // Copy the address part of the prefix to a local buffer. We have to copy
184 // because inet_pton and getaddrinfo operate on null-terminated address
185 // strings, but prefix is const and has '/' after the address.
186 std::string addressString(prefix, slash - prefix);
187
188 // Parse the address.
189 addrinfo *res;
190 addrinfo hints = {
191 .ai_flags = AI_NUMERICHOST,
192 };
193 int ret = getaddrinfo(addressString.c_str(), NULL, &hints, &res);
194 if (ret || !res) {
195 return -EINVAL; // getaddrinfo return values are not errno values.
196 }
197
198 // Convert the address string to raw address bytes.
199 void *rawAddress;
200 int rawLength;
201 switch (res[0].ai_family) {
202 case AF_INET: {
203 if (*prefixlen > 32) {
204 return -EINVAL;
205 }
206 sockaddr_in *sin = (sockaddr_in *) res[0].ai_addr;
207 rawAddress = &sin->sin_addr;
208 rawLength = 4;
209 break;
210 }
211 case AF_INET6: {
212 if (*prefixlen > 128) {
213 return -EINVAL;
214 }
215 sockaddr_in6 *sin6 = (sockaddr_in6 *) res[0].ai_addr;
216 rawAddress = &sin6->sin6_addr;
217 rawLength = 16;
218 break;
219 }
220 default: {
221 freeaddrinfo(res);
222 return -EAFNOSUPPORT;
223 }
224 }
225
226 if (rawLength > size) {
227 freeaddrinfo(res);
228 return -ENOSPC;
229 }
230
231 *family = res[0].ai_family;
232 memcpy(address, rawAddress, rawLength);
233 freeaddrinfo(res);
234
235 return rawLength;
236}
Lorenzo Colitti839d7d62017-04-03 15:37:19 +0900237
238void blockSigpipe() {
239 sigset_t mask;
240
241 sigemptyset(&mask);
242 sigaddset(&mask, SIGPIPE);
243 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
244 ALOGW("WARNING: SIGPIPE not blocked\n");
245}