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