blob: 1b8a6e44aaa1d130340c18c2e22331b18a93f857 [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"
Logan Chien3f461482018-04-23 14:31:32 +080026#include <log/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
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -060030static bool oemCleanupHooks() {
Lorenzo Colitti99338102017-08-11 01:45:04 +090031 std::string cmd =
32 "*filter\n"
33 ":oem_out -\n"
34 ":oem_fwd -\n"
35 "COMMIT\n"
36 "*nat\n"
37 ":oem_nat_pre -\n"
38 "COMMIT\n";
Rom Lemarchand001f0a42013-01-31 12:41:03 -080039
Lorenzo Colitti99338102017-08-11 01:45:04 +090040 return (execIptablesRestore(V4V6, cmd) == 0);
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -060041}
42
43static bool oemInitChains() {
44 int ret = system(OEM_SCRIPT_PATH);
45 if ((-1 == ret) || (0 != WEXITSTATUS(ret))) {
JP Abgrall7c73e892012-01-19 11:44:24 -080046 ALOGE("%s failed: %s", OEM_SCRIPT_PATH, strerror(errno));
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -060047 oemCleanupHooks();
48 return false;
49 }
50 return true;
51}
52
53
54void setupOemIptablesHook() {
55 if (0 == access(OEM_SCRIPT_PATH, R_OK | X_OK)) {
56 // The call to oemCleanupHooks() is superfluous when done on bootup,
57 // but is needed for the case where netd has crashed/stopped and is
58 // restarted.
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070059 if (oemCleanupHooks() && oemInitChains()) {
JP Abgrall7c73e892012-01-19 11:44:24 -080060 ALOGI("OEM iptable hook installed.");
Kazuhiro Ondo4ab46852012-01-12 16:15:06 -060061 }
62 }
63}