• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 #include <string>
26 
27 #define LOG_TAG "OemIptablesHook"
28 #include <log/log.h>
29 #include <logwrap/logwrap.h>
30 #include "NetdConstants.h"
31 
32 namespace {
33 
34 const char OEM_SCRIPT_PATH[] = "/system/bin/oem-iptables-init.sh";
35 
oemCleanupHooks()36 bool oemCleanupHooks() {
37     static const std::string cmd4 =
38             "*filter\n"
39             ":oem_out -\n"
40             ":oem_fwd -\n"
41             "COMMIT\n"
42             "*nat\n"
43             ":oem_nat_pre -\n"
44             "COMMIT\n";
45 
46     static const std::string cmd6 =
47             "*filter\n"
48             ":oem_out -\n"
49             ":oem_fwd -\n"
50             "COMMIT\n";
51 
52     return (execIptablesRestore(V4, cmd4) == 0 && execIptablesRestore(V6, cmd6) == 0);
53 }
54 
oemInitChains()55 bool oemInitChains() {
56     int ret = system(OEM_SCRIPT_PATH);  // NOLINT(cert-env33-c)
57     if ((-1 == ret) || (0 != WEXITSTATUS(ret))) {
58         ALOGE("%s failed: %s", OEM_SCRIPT_PATH, strerror(errno));
59         oemCleanupHooks();
60         return false;
61     }
62     return true;
63 }
64 
65 }  // namespace
66 
setupOemIptablesHook()67 void setupOemIptablesHook() {
68     if (0 == access(OEM_SCRIPT_PATH, R_OK | X_OK)) {
69         // The call to oemCleanupHooks() is superfluous when done on bootup,
70         // but is needed for the case where netd has crashed/stopped and is
71         // restarted.
72         if (oemCleanupHooks() && oemInitChains()) {
73             ALOGI("OEM iptable hook installed.");
74         }
75     }
76 }
77