blob: 7e4b3cb1423d7c8c8419494f0ce1449af8356fb8 [file] [log] [blame]
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -06001/*
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 <stdio.h>
18#include <stdlib.h>
19#include <sys/types.h>
20#include <sys/wait.h>
21#include <errno.h>
22#include <string.h>
23#include <unistd.h>
24
25#define LOG_TAG "OemIptablesHook"
26#include <cutils/log.h>
Rom Lemarchand001f0a42013-01-31 12:41:03 -080027#include <logwrap/logwrap.h>
Robert Greenwaltc4621772012-01-31 12:46:45 -080028#include "NetdConstants.h"
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -060029
Rom Lemarchand001f0a42013-01-31 12:41:03 -080030static int runIptablesCmd(int argc, const char **argv) {
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -060031 int res;
32
Rom Lemarchand001f0a42013-01-31 12:41:03 -080033 res = android_fork_execvp(argc, (char **)argv, NULL, false, false);
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -060034 return res;
35}
36
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -060037static bool oemCleanupHooks() {
Rom Lemarchand001f0a42013-01-31 12:41:03 -080038 const char *cmd1[] = {
39 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -040040 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -080041 "-F",
42 "oem_out"
43 };
44 runIptablesCmd(ARRAY_SIZE(cmd1), cmd1);
45
46 const char *cmd2[] = {
47 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -040048 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -080049 "-F",
50 "oem_fwd"
51 };
52 runIptablesCmd(ARRAY_SIZE(cmd2), cmd2);
53
54 const char *cmd3[] = {
55 IPTABLES_PATH,
Paul Jensen94b2ab92015-08-04 10:35:05 -040056 "-w",
Rom Lemarchand001f0a42013-01-31 12:41:03 -080057 "-t",
58 "nat",
59 "-F",
60 "oem_nat_pre"
61 };
62 runIptablesCmd(ARRAY_SIZE(cmd3), cmd3);
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -060063 return true;
64}
65
66static bool oemInitChains() {
67 int ret = system(OEM_SCRIPT_PATH);
68 if ((-1 == ret) || (0 != WEXITSTATUS(ret))) {
JP Abgrall7c73e892012-01-19 11:44:24 -080069 ALOGE("%s failed: %s", OEM_SCRIPT_PATH, strerror(errno));
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -060070 oemCleanupHooks();
71 return false;
72 }
73 return true;
74}
75
76
77void setupOemIptablesHook() {
78 if (0 == access(OEM_SCRIPT_PATH, R_OK | X_OK)) {
79 // The call to oemCleanupHooks() is superfluous when done on bootup,
80 // but is needed for the case where netd has crashed/stopped and is
81 // restarted.
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070082 if (oemCleanupHooks() && oemInitChains()) {
JP Abgrall7c73e892012-01-19 11:44:24 -080083 ALOGI("OEM iptable hook installed.");
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -060084 }
85 }
86}