xref: /aosp_15_r20/system/netd/server/oem_iptables_hook.cpp (revision 8542734a0dd1db395a4d42aae09c37f3c3c3e7a1)
1*8542734aSAndroid Build Coastguard Worker /*
2*8542734aSAndroid Build Coastguard Worker  * Copyright (C) 2012 The Android Open Source Project
3*8542734aSAndroid Build Coastguard Worker  *
4*8542734aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8542734aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8542734aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8542734aSAndroid Build Coastguard Worker  *
8*8542734aSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8542734aSAndroid Build Coastguard Worker  *
10*8542734aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8542734aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8542734aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8542734aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8542734aSAndroid Build Coastguard Worker  * limitations under the License.
15*8542734aSAndroid Build Coastguard Worker  */
16*8542734aSAndroid Build Coastguard Worker 
17*8542734aSAndroid Build Coastguard Worker #include <stdio.h>
18*8542734aSAndroid Build Coastguard Worker #include <stdlib.h>
19*8542734aSAndroid Build Coastguard Worker #include <sys/types.h>
20*8542734aSAndroid Build Coastguard Worker #include <sys/wait.h>
21*8542734aSAndroid Build Coastguard Worker #include <errno.h>
22*8542734aSAndroid Build Coastguard Worker #include <string.h>
23*8542734aSAndroid Build Coastguard Worker #include <unistd.h>
24*8542734aSAndroid Build Coastguard Worker 
25*8542734aSAndroid Build Coastguard Worker #include <string>
26*8542734aSAndroid Build Coastguard Worker 
27*8542734aSAndroid Build Coastguard Worker #define LOG_TAG "OemIptablesHook"
28*8542734aSAndroid Build Coastguard Worker #include <log/log.h>
29*8542734aSAndroid Build Coastguard Worker #include "NetdConstants.h"
30*8542734aSAndroid Build Coastguard Worker 
31*8542734aSAndroid Build Coastguard Worker namespace {
32*8542734aSAndroid Build Coastguard Worker 
33*8542734aSAndroid Build Coastguard Worker const char OEM_SCRIPT_PATH[] = "/system/bin/oem-iptables-init.sh";
34*8542734aSAndroid Build Coastguard Worker 
oemCleanupHooks()35*8542734aSAndroid Build Coastguard Worker bool oemCleanupHooks() {
36*8542734aSAndroid Build Coastguard Worker     static const std::string cmd4 =
37*8542734aSAndroid Build Coastguard Worker             "*filter\n"
38*8542734aSAndroid Build Coastguard Worker             ":oem_out -\n"
39*8542734aSAndroid Build Coastguard Worker             ":oem_fwd -\n"
40*8542734aSAndroid Build Coastguard Worker             "COMMIT\n"
41*8542734aSAndroid Build Coastguard Worker             "*nat\n"
42*8542734aSAndroid Build Coastguard Worker             ":oem_nat_pre -\n"
43*8542734aSAndroid Build Coastguard Worker             "COMMIT\n";
44*8542734aSAndroid Build Coastguard Worker 
45*8542734aSAndroid Build Coastguard Worker     static const std::string cmd6 =
46*8542734aSAndroid Build Coastguard Worker             "*filter\n"
47*8542734aSAndroid Build Coastguard Worker             ":oem_out -\n"
48*8542734aSAndroid Build Coastguard Worker             ":oem_fwd -\n"
49*8542734aSAndroid Build Coastguard Worker             "COMMIT\n";
50*8542734aSAndroid Build Coastguard Worker 
51*8542734aSAndroid Build Coastguard Worker     return (execIptablesRestore(V4, cmd4) == 0 && execIptablesRestore(V6, cmd6) == 0);
52*8542734aSAndroid Build Coastguard Worker }
53*8542734aSAndroid Build Coastguard Worker 
oemInitChains()54*8542734aSAndroid Build Coastguard Worker bool oemInitChains() {
55*8542734aSAndroid Build Coastguard Worker     int ret = system(OEM_SCRIPT_PATH);  // NOLINT(cert-env33-c)
56*8542734aSAndroid Build Coastguard Worker     if ((-1 == ret) || (0 != WEXITSTATUS(ret))) {
57*8542734aSAndroid Build Coastguard Worker         ALOGE("%s failed: %s", OEM_SCRIPT_PATH, strerror(errno));
58*8542734aSAndroid Build Coastguard Worker         oemCleanupHooks();
59*8542734aSAndroid Build Coastguard Worker         return false;
60*8542734aSAndroid Build Coastguard Worker     }
61*8542734aSAndroid Build Coastguard Worker     return true;
62*8542734aSAndroid Build Coastguard Worker }
63*8542734aSAndroid Build Coastguard Worker 
64*8542734aSAndroid Build Coastguard Worker }  // namespace
65*8542734aSAndroid Build Coastguard Worker 
setupOemIptablesHook()66*8542734aSAndroid Build Coastguard Worker void setupOemIptablesHook() {
67*8542734aSAndroid Build Coastguard Worker     if (0 == access(OEM_SCRIPT_PATH, R_OK | X_OK)) {
68*8542734aSAndroid Build Coastguard Worker         // The call to oemCleanupHooks() is superfluous when done on bootup,
69*8542734aSAndroid Build Coastguard Worker         // but is needed for the case where netd has crashed/stopped and is
70*8542734aSAndroid Build Coastguard Worker         // restarted.
71*8542734aSAndroid Build Coastguard Worker         if (oemCleanupHooks() && oemInitChains()) {
72*8542734aSAndroid Build Coastguard Worker             ALOGI("OEM iptable hook installed.");
73*8542734aSAndroid Build Coastguard Worker         }
74*8542734aSAndroid Build Coastguard Worker     }
75*8542734aSAndroid Build Coastguard Worker }
76