NetdConstants.cpp revision 8e188ed5c989ddcc07f0f5e9839493c22d17e7b6
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
198e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey#include <cutils/log.h>
208e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
21c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt#include "NetdConstants.h"
22c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt
23c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const OEM_SCRIPT_PATH = "/system/bin/oem-iptables-init.sh";
24c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const IPTABLES_PATH = "/system/bin/iptables";
250031cead820149e2fe3ccb3cc2fe05758a3cb5c2JP Abgrallconst char * const IP6TABLES_PATH = "/system/bin/ip6tables";
26c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const TC_PATH = "/system/bin/tc";
27c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const IP_PATH = "/system/bin/ip";
28c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const ADD = "add";
29c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwaltconst char * const DEL = "del";
308e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
318e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeystatic void logExecError(const char* argv[], int res) {
328e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    const char** argp = argv;
338e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    std::string args = "";
348e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    while (*argp) {
358e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        args += *argp;
368e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        args += ' ';
378e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        argp++;
388e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    }
398e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    ALOGE("exec() res=%d for %s", res, args.c_str());
408e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey}
418e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
428e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeystatic int execIptables(IptablesTarget target, bool silent, va_list args) {
438e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    /* Read arguments from incoming va_list; we expect the list to be NULL terminated. */
448e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    std::list<const char*> argsList;
458e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    argsList.push_back(NULL);
468e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    const char* arg;
478e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    do {
488e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        arg = va_arg(args, const char *);
498e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        argsList.push_back(arg);
508e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    } while (arg);
518e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
528e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    int i = 0;
538e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    const char* argv[argsList.size()];
548e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    std::list<const char*>::iterator it;
558e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    for (it = argsList.begin(); it != argsList.end(); it++, i++) {
568e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        argv[i] = *it;
578e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    }
588e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
598e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    int res = 0;
608e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    if (target == V4 || target == V4V6) {
618e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        argv[0] = IPTABLES_PATH;
628e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        int localRes = fork_and_execve(argv[0], argv);
638e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        if (localRes) {
648e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey            if (!silent) {
658e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey                logExecError(argv, localRes);
668e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey            }
678e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey            res |= localRes;
688e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        }
698e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    }
708e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    if (target == V6 || target == V4V6) {
718e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        argv[0] = IP6TABLES_PATH;
728e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        int localRes = fork_and_execve(argv[0], argv);
738e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        if (localRes) {
748e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey            if (!silent) {
758e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey                logExecError(argv, localRes);
768e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey            }
778e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey            res |= localRes;
788e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey        }
798e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    }
808e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    return res;
818e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey}
828e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
838e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyint execIptables(IptablesTarget target, ...) {
848e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    va_list args;
858e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    va_start(args, target);
868e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    int res = execIptables(target, false, args);
878e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    va_end(args);
888e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    return res;
898e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey}
908e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey
918e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkeyint execIptablesSilently(IptablesTarget target, ...) {
928e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    va_list args;
938e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    va_start(args, target);
948e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    int res = execIptables(target, true, args);
958e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    va_end(args);
968e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey    return res;
978e188ed5c989ddcc07f0f5e9839493c22d17e7b6Jeff Sharkey}
98