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.
23 #define LOG_TAG "Netd"
25 #include <cutils/log.h>
26 #include <logwrap/logwrap.h>
28 #include "NetdConstants.h"
30 const char * const OEM_SCRIPT_PATH = "/system/bin/oem-iptables-init.sh";
31 const char * const IPTABLES_PATH = "/system/bin/iptables";
32 const char * const IP6TABLES_PATH = "/system/bin/ip6tables";
33 const char * const TC_PATH = "/system/bin/tc";
34 const char * const IP_PATH = "/system/bin/ip";
35 const char * const ADD = "add";
36 const char * const DEL = "del";
38 static void logExecError(const char* argv[], int res, int status) {
39 const char** argp = argv;
40 std::string args = "";
46 ALOGE("exec() res=%d, status=%d for %s", res, status, args.c_str());
49 static int execIptablesCommand(int argc, const char *argv[], bool silent) {
53 res = android_fork_execvp(argc, (char **)argv, &status, false,
55 if (res || !WIFEXITED(status) || WEXITSTATUS(status)) {
57 logExecError(argv, res, status);
61 if (!WIFEXITED(status))
64 return WEXITSTATUS(status);
67 static int execIptables(IptablesTarget target, bool silent, va_list args) {
68 /* Read arguments from incoming va_list; we expect the list to be NULL terminated. */
69 std::list<const char*> argsList;
70 argsList.push_back(NULL);
73 arg = va_arg(args, const char *);
74 argsList.push_back(arg);
78 const char* argv[argsList.size()];
79 std::list<const char*>::iterator it;
80 for (it = argsList.begin(); it != argsList.end(); it++, i++) {
85 if (target == V4 || target == V4V6) {
86 argv[0] = IPTABLES_PATH;
87 res |= execIptablesCommand(argsList.size(), argv, silent);
89 if (target == V6 || target == V4V6) {
90 argv[0] = IP6TABLES_PATH;
91 res |= execIptablesCommand(argsList.size(), argv, silent);
96 int execIptables(IptablesTarget target, ...) {
98 va_start(args, target);
99 int res = execIptables(target, false, args);
104 int execIptablesSilently(IptablesTarget target, ...) {
106 va_start(args, target);
107 int res = execIptables(target, true, args);
112 int writeFile(const char *path, const char *value, int size) {
113 int fd = open(path, O_WRONLY);
115 ALOGE("Failed to open %s: %s", path, strerror(errno));
119 if (write(fd, value, size) != size) {
120 ALOGE("Failed to write %s: %s", path, strerror(errno));
128 int readFile(const char *path, char *buf, int *sizep)
130 int fd = open(path, O_RDONLY);
134 ALOGE("Failed to open %s: %s", path, strerror(errno));
138 size = read(fd, buf, *sizep);
140 ALOGE("Failed to write %s: %s", path, strerror(errno));
150 * Check an interface name for plausibility. This should e.g. help against
151 * directory traversal.
153 bool isIfaceName(const char *name) {
155 size_t name_len = strlen(name);
156 if ((name_len == 0) || (name_len > IFNAMSIZ)) {
160 /* First character must be alphanumeric */
161 if (!isalnum(name[0])) {
165 for (i = 1; i < name_len; i++) {
166 if (!isalnum(name[i]) && (name[i] != '_') && (name[i] != '-') && (name[i] != ':')) {