Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 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 | |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 17 | #include <string.h> |
Rom Lemarchand | 838ef64 | 2013-01-24 15:14:41 -0800 | [diff] [blame] | 18 | #include <sys/wait.h> |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 19 | |
Jeff Sharkey | bec6d04 | 2012-09-06 15:45:56 -0700 | [diff] [blame] | 20 | #define LOG_TAG "Netd" |
| 21 | |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 22 | #include <cutils/log.h> |
Rom Lemarchand | 838ef64 | 2013-01-24 15:14:41 -0800 | [diff] [blame] | 23 | #include <logwrap/logwrap.h> |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 24 | |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 25 | #include "NetdConstants.h" |
| 26 | |
| 27 | const char * const OEM_SCRIPT_PATH = "/system/bin/oem-iptables-init.sh"; |
| 28 | const char * const IPTABLES_PATH = "/system/bin/iptables"; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 29 | const char * const IP6TABLES_PATH = "/system/bin/ip6tables"; |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 30 | const char * const TC_PATH = "/system/bin/tc"; |
| 31 | const char * const IP_PATH = "/system/bin/ip"; |
| 32 | const char * const ADD = "add"; |
| 33 | const char * const DEL = "del"; |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 34 | |
Rom Lemarchand | 838ef64 | 2013-01-24 15:14:41 -0800 | [diff] [blame] | 35 | static void logExecError(const char* argv[], int res, int status) { |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 36 | const char** argp = argv; |
| 37 | std::string args = ""; |
| 38 | while (*argp) { |
| 39 | args += *argp; |
| 40 | args += ' '; |
| 41 | argp++; |
| 42 | } |
Rom Lemarchand | 838ef64 | 2013-01-24 15:14:41 -0800 | [diff] [blame] | 43 | ALOGE("exec() res=%d, status=%d for %s", res, status, args.c_str()); |
| 44 | } |
| 45 | |
| 46 | static 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 Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static 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 Lemarchand | 838ef64 | 2013-01-24 15:14:41 -0800 | [diff] [blame] | 84 | res |= execIptablesCommand(argsList.size(), argv, silent); |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 85 | } |
| 86 | if (target == V6 || target == V4V6) { |
| 87 | argv[0] = IP6TABLES_PATH; |
Rom Lemarchand | 838ef64 | 2013-01-24 15:14:41 -0800 | [diff] [blame] | 88 | res |= execIptablesCommand(argsList.size(), argv, silent); |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 89 | } |
| 90 | return res; |
| 91 | } |
| 92 | |
| 93 | int 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 | |
| 101 | int 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 | } |