blob: db1d4cd9bfe9325e6e228828c12fcb4fc6487479 [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>
18
Jeff Sharkeybec6d042012-09-06 15:45:56 -070019#define LOG_TAG "Netd"
20
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070021#include <cutils/log.h>
22
Robert Greenwaltc4621772012-01-31 12:46:45 -080023#include "NetdConstants.h"
24
25const char * const OEM_SCRIPT_PATH = "/system/bin/oem-iptables-init.sh";
26const char * const IPTABLES_PATH = "/system/bin/iptables";
JP Abgrall0031cea2012-04-17 16:38:23 -070027const char * const IP6TABLES_PATH = "/system/bin/ip6tables";
Robert Greenwaltc4621772012-01-31 12:46:45 -080028const char * const TC_PATH = "/system/bin/tc";
29const char * const IP_PATH = "/system/bin/ip";
30const char * const ADD = "add";
31const char * const DEL = "del";
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070032
33static void logExecError(const char* argv[], int res) {
34 const char** argp = argv;
35 std::string args = "";
36 while (*argp) {
37 args += *argp;
38 args += ' ';
39 argp++;
40 }
41 ALOGE("exec() res=%d for %s", res, args.c_str());
42}
43
44static int execIptables(IptablesTarget target, bool silent, va_list args) {
45 /* Read arguments from incoming va_list; we expect the list to be NULL terminated. */
46 std::list<const char*> argsList;
47 argsList.push_back(NULL);
48 const char* arg;
49 do {
50 arg = va_arg(args, const char *);
51 argsList.push_back(arg);
52 } while (arg);
53
54 int i = 0;
55 const char* argv[argsList.size()];
56 std::list<const char*>::iterator it;
57 for (it = argsList.begin(); it != argsList.end(); it++, i++) {
58 argv[i] = *it;
59 }
60
61 int res = 0;
62 if (target == V4 || target == V4V6) {
63 argv[0] = IPTABLES_PATH;
64 int localRes = fork_and_execve(argv[0], argv);
65 if (localRes) {
66 if (!silent) {
67 logExecError(argv, localRes);
68 }
69 res |= localRes;
70 }
71 }
72 if (target == V6 || target == V4V6) {
73 argv[0] = IP6TABLES_PATH;
74 int localRes = fork_and_execve(argv[0], argv);
75 if (localRes) {
76 if (!silent) {
77 logExecError(argv, localRes);
78 }
79 res |= localRes;
80 }
81 }
82 return res;
83}
84
85int execIptables(IptablesTarget target, ...) {
86 va_list args;
87 va_start(args, target);
88 int res = execIptables(target, false, args);
89 va_end(args);
90 return res;
91}
92
93int execIptablesSilently(IptablesTarget target, ...) {
94 va_list args;
95 va_start(args, target);
96 int res = execIptables(target, true, args);
97 va_end(args);
98 return res;
99}