NetdConstants.cpp revision bec6d04e1a84823b89e5581ea8ebcf9f08915216
1c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt/* 2c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * Copyright (C) 2012 The Android Open Source Project 3c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * 4c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * Licensed under the Apache License, Version 2.0 (the "License"); 5c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * you may not use this file except in compliance with the License. 6c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * You may obtain a copy of the License at 7c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * 8c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * http://www.apache.org/licenses/LICENSE-2.0 9c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * 10c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * Unless required by applicable law or agreed to in writing, software 11c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * distributed under the License is distributed on an "AS IS" BASIS, 12c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * See the License for the specific language governing permissions and 14c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt * limitations under the License. 15c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt */ 16c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt 178e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey#include <string.h> 188e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey 19bec6d04e1a84823b89e5581ea8ebcf9f08915216Jeff Sharkey#define LOG_TAG "Netd" 20bec6d04e1a84823b89e5581ea8ebcf9f08915216Jeff Sharkey 218e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey#include <cutils/log.h> 228e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey 23c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt#include "NetdConstants.h" 24c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt 25c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const OEM_SCRIPT_PATH = "/system/bin/oem-iptables-init.sh"; 26c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const IPTABLES_PATH = "/system/bin/iptables"; 270031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallconst char * const IP6TABLES_PATH = "/system/bin/ip6tables"; 28c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const TC_PATH = "/system/bin/tc"; 29c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const IP_PATH = "/system/bin/ip"; 30c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const ADD = "add"; 31c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const DEL = "del"; 328e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey 338e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeystatic void logExecError(const char* argv[], int res) { 348e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey const char** argp = argv; 358e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey std::string args = ""; 368e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey while (*argp) { 378e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey args += *argp; 388e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey args += ' '; 398e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey argp++; 408e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey } 418e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey ALOGE("exec() res=%d for %s", res, args.c_str()); 428e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey} 438e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey 448e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeystatic int execIptables(IptablesTarget target, bool silent, va_list args) { 458e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey /* Read arguments from incoming va_list; we expect the list to be NULL terminated. */ 468e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey std::list<const char*> argsList; 478e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey argsList.push_back(NULL); 488e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey const char* arg; 498e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey do { 508e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey arg = va_arg(args, const char *); 518e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey argsList.push_back(arg); 528e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey } while (arg); 538e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey 548e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey int i = 0; 558e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey const char* argv[argsList.size()]; 568e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey std::list<const char*>::iterator it; 578e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey for (it = argsList.begin(); it != argsList.end(); it++, i++) { 588e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey argv[i] = *it; 598e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey } 608e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey 618e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey int res = 0; 628e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey if (target == V4 || target == V4V6) { 638e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey argv[0] = IPTABLES_PATH; 648e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey int localRes = fork_and_execve(argv[0], argv); 658e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey if (localRes) { 668e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey if (!silent) { 678e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey logExecError(argv, localRes); 688e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey } 698e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey res |= localRes; 708e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey } 718e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey } 728e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey if (target == V6 || target == V4V6) { 738e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey argv[0] = IP6TABLES_PATH; 748e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey int localRes = fork_and_execve(argv[0], argv); 758e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey if (localRes) { 768e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey if (!silent) { 778e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey logExecError(argv, localRes); 788e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey } 798e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey res |= localRes; 808e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey } 818e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey } 828e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey return res; 838e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey} 848e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey 858e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyint execIptables(IptablesTarget target, ...) { 868e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey va_list args; 878e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey va_start(args, target); 888e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey int res = execIptables(target, false, args); 898e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey va_end(args); 908e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey return res; 918e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey} 928e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey 938e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyint execIptablesSilently(IptablesTarget target, ...) { 948e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey va_list args; 958e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey va_start(args, target); 968e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey int res = execIptables(target, true, args); 978e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey va_end(args); 988e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey return res; 998e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey} 100