blob: 9b83125a221d741881ed71a9de0e85353ab96061 [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
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070017#include <string.h>
Rom Lemarchand838ef642013-01-24 15:14:41 -080018#include <sys/wait.h>
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070019
Jeff Sharkeybec6d042012-09-06 15:45:56 -070020#define LOG_TAG "Netd"
21
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070022#include <cutils/log.h>
Rom Lemarchand838ef642013-01-24 15:14:41 -080023#include <logwrap/logwrap.h>
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070024
Robert Greenwaltc4621772012-01-31 12:46:45 -080025#include "NetdConstants.h"
26
27const char * const OEM_SCRIPT_PATH = "/system/bin/oem-iptables-init.sh";
28const char * const IPTABLES_PATH = "/system/bin/iptables";
JP Abgrall0031cea2012-04-17 16:38:23 -070029const char * const IP6TABLES_PATH = "/system/bin/ip6tables";
Robert Greenwaltc4621772012-01-31 12:46:45 -080030const char * const TC_PATH = "/system/bin/tc";
31const char * const IP_PATH = "/system/bin/ip";
32const char * const ADD = "add";
33const char * const DEL = "del";
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070034
Rom Lemarchand838ef642013-01-24 15:14:41 -080035static void logExecError(const char* argv[], int res, int status) {
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070036 const char** argp = argv;
37 std::string args = "";
38 while (*argp) {
39 args += *argp;
40 args += ' ';
41 argp++;
42 }
Rom Lemarchand838ef642013-01-24 15:14:41 -080043 ALOGE("exec() res=%d, status=%d for %s", res, status, args.c_str());
44}
45
46static int execIptablesCommand(int argc, const char *argv[], bool silent) {
47 int res;
48 int status;
49
50 res = android_fork_execvp(argc, (char **)argv, &status, false,
51 !silent);
52 if (res || !WIFEXITED(status) || WEXITSTATUS(status)) {
53 if (!silent) {
54 logExecError(argv, res, status);
55 }
56 if (res)
57 return res;
58 if (!WIFEXITED(status))
59 return ECHILD;
60 }
61 return WEXITSTATUS(status);
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070062}
63
64static int execIptables(IptablesTarget target, bool silent, va_list args) {
65 /* Read arguments from incoming va_list; we expect the list to be NULL terminated. */
66 std::list<const char*> argsList;
67 argsList.push_back(NULL);
68 const char* arg;
69 do {
70 arg = va_arg(args, const char *);
71 argsList.push_back(arg);
72 } while (arg);
73
74 int i = 0;
75 const char* argv[argsList.size()];
76 std::list<const char*>::iterator it;
77 for (it = argsList.begin(); it != argsList.end(); it++, i++) {
78 argv[i] = *it;
79 }
80
81 int res = 0;
82 if (target == V4 || target == V4V6) {
83 argv[0] = IPTABLES_PATH;
Rom Lemarchand838ef642013-01-24 15:14:41 -080084 res |= execIptablesCommand(argsList.size(), argv, silent);
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070085 }
86 if (target == V6 || target == V4V6) {
87 argv[0] = IP6TABLES_PATH;
Rom Lemarchand838ef642013-01-24 15:14:41 -080088 res |= execIptablesCommand(argsList.size(), argv, silent);
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070089 }
90 return res;
91}
92
93int execIptables(IptablesTarget target, ...) {
94 va_list args;
95 va_start(args, target);
96 int res = execIptables(target, false, args);
97 va_end(args);
98 return res;
99}
100
101int execIptablesSilently(IptablesTarget target, ...) {
102 va_list args;
103 va_start(args, target);
104 int res = execIptables(target, true, args);
105 va_end(args);
106 return res;
107}