Kazuhiro Ondo | 4ab4685 | 2012-01-12 16:15:06 -0600 | [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 | |
| 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 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 25 | #include <string> |
| 26 | |
Kazuhiro Ondo | 4ab4685 | 2012-01-12 16:15:06 -0600 | [diff] [blame] | 27 | #define LOG_TAG "OemIptablesHook" |
Logan Chien | 3f46148 | 2018-04-23 14:31:32 +0800 | [diff] [blame] | 28 | #include <log/log.h> |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 29 | #include "NetdConstants.h" |
Kazuhiro Ondo | 4ab4685 | 2012-01-12 16:15:06 -0600 | [diff] [blame] | 30 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | const char OEM_SCRIPT_PATH[] = "/system/bin/oem-iptables-init.sh"; |
| 34 | |
| 35 | bool oemCleanupHooks() { |
mtk13799 | 1d02910 | 2018-11-30 17:43:03 +0800 | [diff] [blame] | 36 | static const std::string cmd4 = |
| 37 | "*filter\n" |
| 38 | ":oem_out -\n" |
| 39 | ":oem_fwd -\n" |
| 40 | "COMMIT\n" |
| 41 | "*nat\n" |
| 42 | ":oem_nat_pre -\n" |
| 43 | "COMMIT\n"; |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 44 | |
mtk13799 | 1d02910 | 2018-11-30 17:43:03 +0800 | [diff] [blame] | 45 | static const std::string cmd6 = |
| 46 | "*filter\n" |
| 47 | ":oem_out -\n" |
| 48 | ":oem_fwd -\n" |
| 49 | "COMMIT\n"; |
| 50 | |
| 51 | return (execIptablesRestore(V4, cmd4) == 0 && execIptablesRestore(V6, cmd6) == 0); |
Kazuhiro Ondo | 4ab4685 | 2012-01-12 16:15:06 -0600 | [diff] [blame] | 52 | } |
| 53 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 54 | bool oemInitChains() { |
| 55 | int ret = system(OEM_SCRIPT_PATH); // NOLINT(cert-env33-c) |
Kazuhiro Ondo | 4ab4685 | 2012-01-12 16:15:06 -0600 | [diff] [blame] | 56 | if ((-1 == ret) || (0 != WEXITSTATUS(ret))) { |
JP Abgrall | 7c73e89 | 2012-01-19 11:44:24 -0800 | [diff] [blame] | 57 | ALOGE("%s failed: %s", OEM_SCRIPT_PATH, strerror(errno)); |
Kazuhiro Ondo | 4ab4685 | 2012-01-12 16:15:06 -0600 | [diff] [blame] | 58 | oemCleanupHooks(); |
| 59 | return false; |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 64 | } // namespace |
Kazuhiro Ondo | 4ab4685 | 2012-01-12 16:15:06 -0600 | [diff] [blame] | 65 | |
| 66 | void setupOemIptablesHook() { |
| 67 | if (0 == access(OEM_SCRIPT_PATH, R_OK | X_OK)) { |
| 68 | // The call to oemCleanupHooks() is superfluous when done on bootup, |
| 69 | // but is needed for the case where netd has crashed/stopped and is |
| 70 | // restarted. |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 71 | if (oemCleanupHooks() && oemInitChains()) { |
JP Abgrall | 7c73e89 | 2012-01-19 11:44:24 -0800 | [diff] [blame] | 72 | ALOGI("OEM iptable hook installed."); |
Kazuhiro Ondo | 4ab4685 | 2012-01-12 16:15:06 -0600 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | } |