• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #include <string.h>
18 
19 #define LOG_TAG "Netd"
20 
21 #include <cutils/log.h>
22 
23 #include "NetdConstants.h"
24 
25 const char * const OEM_SCRIPT_PATH = "/system/bin/oem-iptables-init.sh";
26 const char * const IPTABLES_PATH = "/system/bin/iptables";
27 const char * const IP6TABLES_PATH = "/system/bin/ip6tables";
28 const char * const TC_PATH = "/system/bin/tc";
29 const char * const IP_PATH = "/system/bin/ip";
30 const char * const ADD = "add";
31 const char * const DEL = "del";
32 
logExecError(const char * argv[],int res)33 static 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 
execIptables(IptablesTarget target,bool silent,va_list args)44 static 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 
execIptables(IptablesTarget target,...)85 int 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 
execIptablesSilently(IptablesTarget target,...)93 int 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 }
100