2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <netinet/in.h>
27 #define LOG_TAG "Netd"
29 #include <cutils/log.h>
30 #include <logwrap/logwrap.h>
32 #include "NetdConstants.h"
34 const char * const OEM_SCRIPT_PATH = "/system/bin/oem-iptables-init.sh";
35 const char * const IPTABLES_PATH = "/system/bin/iptables";
36 const char * const IP6TABLES_PATH = "/system/bin/ip6tables";
37 const char * const IPTABLES_RESTORE_PATH = "/system/bin/iptables-restore";
38 const char * const IP6TABLES_RESTORE_PATH = "/system/bin/ip6tables-restore";
39 const char * const TC_PATH = "/system/bin/tc";
40 const char * const IP_PATH = "/system/bin/ip";
41 const char * const ADD = "add";
42 const char * const DEL = "del";
44 static void logExecError(const char* argv[], int res, int status) {
45 const char** argp = argv;
46 std::string args = "";
52 ALOGE("exec() res=%d, status=%d for %s", res, status, args.c_str());
55 static int execIptablesCommand(int argc, const char *argv[], bool silent) {
59 res = android_fork_execvp(argc, (char **)argv, &status, false,
61 if (res || !WIFEXITED(status) || WEXITSTATUS(status)) {
63 logExecError(argv, res, status);
67 if (!WIFEXITED(status))
70 return WEXITSTATUS(status);
73 static int execIptables(IptablesTarget target, bool silent, va_list args) {
74 /* Read arguments from incoming va_list; we expect the list to be NULL terminated. */
75 std::list<const char*> argsList;
76 argsList.push_back(NULL);
79 // Wait to avoid failure due to another process holding the lock
80 argsList.push_back("-w");
83 arg = va_arg(args, const char *);
84 argsList.push_back(arg);
88 const char* argv[argsList.size()];
89 std::list<const char*>::iterator it;
90 for (it = argsList.begin(); it != argsList.end(); it++, i++) {
95 if (target == V4 || target == V4V6) {
96 argv[0] = IPTABLES_PATH;
97 res |= execIptablesCommand(argsList.size(), argv, silent);
99 if (target == V6 || target == V4V6) {
100 argv[0] = IP6TABLES_PATH;
101 res |= execIptablesCommand(argsList.size(), argv, silent);
106 int execIptables(IptablesTarget target, ...) {
108 va_start(args, target);
109 int res = execIptables(target, false, args);
114 int execIptablesSilently(IptablesTarget target, ...) {
116 va_start(args, target);
117 int res = execIptables(target, true, args);
122 static int execIptablesRestoreCommand(const char *cmd, const std::string& commands) {
123 const char *argv[] = {
125 "--noflush", // Don't flush the whole table.
126 "-w", // Wait instead of failing if the lock is held.
128 AndroidForkExecvpOption opt[1] = {
130 .opt_type = FORK_EXECVP_OPTION_INPUT,
131 .opt_input.input = reinterpret_cast<const uint8_t*>(commands.c_str()),
132 .opt_input.input_len = commands.size(),
137 int res = android_fork_execvp_ext(
138 ARRAY_SIZE(argv), (char**)argv, &status, false /* ignore_int_quit */, LOG_NONE,
139 false /* abbreviated */, NULL /* file_path */, opt, ARRAY_SIZE(opt));
141 ALOGE("%s failed with res=%d, status=%d", argv[0], res, status);
148 int execIptablesRestore(IptablesTarget target, const std::string& commands) {
150 if (target == V4 || target == V4V6) {
151 res |= execIptablesRestoreCommand(IPTABLES_RESTORE_PATH, commands);
153 if (target == V6 || target == V4V6) {
154 res |= execIptablesRestoreCommand(IP6TABLES_RESTORE_PATH, commands);
160 * Check an interface name for plausibility. This should e.g. help against
161 * directory traversal.
163 bool isIfaceName(const char *name) {
165 size_t name_len = strlen(name);
166 if ((name_len == 0) || (name_len > IFNAMSIZ)) {
170 /* First character must be alphanumeric */
171 if (!isalnum(name[0])) {
175 for (i = 1; i < name_len; i++) {
176 if (!isalnum(name[i]) && (name[i] != '_') && (name[i] != '-') && (name[i] != ':')) {
184 int parsePrefix(const char *prefix, uint8_t *family, void *address, int size, uint8_t *prefixlen) {
185 if (!prefix || !family || !address || !prefixlen) {
189 // Find the '/' separating address from prefix length.
190 const char *slash = strchr(prefix, '/');
191 const char *prefixlenString = slash + 1;
192 if (!slash || !*prefixlenString)
195 // Convert the prefix length to a uint8_t.
198 templen = strtoul(prefixlenString, &endptr, 10);
199 if (*endptr || templen > 255) {
202 *prefixlen = templen;
204 // Copy the address part of the prefix to a local buffer. We have to copy
205 // because inet_pton and getaddrinfo operate on null-terminated address
206 // strings, but prefix is const and has '/' after the address.
207 std::string addressString(prefix, slash - prefix);
209 // Parse the address.
212 .ai_flags = AI_NUMERICHOST,
214 int ret = getaddrinfo(addressString.c_str(), NULL, &hints, &res);
216 return -EINVAL; // getaddrinfo return values are not errno values.
219 // Convert the address string to raw address bytes.
222 switch (res[0].ai_family) {
224 if (*prefixlen > 32) {
227 sockaddr_in *sin = (sockaddr_in *) res[0].ai_addr;
228 rawAddress = &sin->sin_addr;
233 if (*prefixlen > 128) {
236 sockaddr_in6 *sin6 = (sockaddr_in6 *) res[0].ai_addr;
237 rawAddress = &sin6->sin6_addr;
243 return -EAFNOSUPPORT;
247 if (rawLength > size) {
252 *family = res[0].ai_family;
253 memcpy(address, rawAddress, rawLength);