Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [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 | |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 17 | #include <set> |
| 18 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 19 | #include <errno.h> |
Hugo Benichi | 528d3d0 | 2018-06-20 13:35:58 +0900 | [diff] [blame] | 20 | #include <limits.h> |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Hugo Benichi | 528d3d0 | 2018-06-20 13:35:58 +0900 | [diff] [blame] | 24 | #include <cstdint> |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 25 | |
| 26 | #define LOG_TAG "FirewallController" |
| 27 | #define LOG_NDEBUG 0 |
| 28 | |
Hugo Benichi | 528d3d0 | 2018-06-20 13:35:58 +0900 | [diff] [blame] | 29 | #include <android-base/file.h> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 30 | #include <android-base/stringprintf.h> |
Hugo Benichi | 528d3d0 | 2018-06-20 13:35:58 +0900 | [diff] [blame] | 31 | #include <android-base/strings.h> |
Logan Chien | 3f46148 | 2018-04-23 14:31:32 +0800 | [diff] [blame] | 32 | #include <log/log.h> |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 33 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 34 | #include "Controllers.h" |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 35 | #include "FirewallController.h" |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 36 | #include "NetdConstants.h" |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 37 | |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 38 | using android::base::Join; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 39 | using android::base::StringAppendF; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 40 | using android::base::StringPrintf; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 41 | |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 42 | namespace android { |
| 43 | namespace net { |
| 44 | |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 45 | auto FirewallController::execIptablesRestore = ::execIptablesRestore; |
| 46 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 47 | const char* FirewallController::TABLE = "filter"; |
| 48 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 49 | const char* FirewallController::LOCAL_INPUT = "fw_INPUT"; |
| 50 | const char* FirewallController::LOCAL_OUTPUT = "fw_OUTPUT"; |
| 51 | const char* FirewallController::LOCAL_FORWARD = "fw_FORWARD"; |
| 52 | |
Lorenzo Colitti | c8683d7 | 2015-09-01 16:53:35 +0900 | [diff] [blame] | 53 | // ICMPv6 types that are required for any form of IPv6 connectivity to work. Note that because the |
| 54 | // fw_dozable chain is called from both INPUT and OUTPUT, this includes both packets that we need |
| 55 | // to be able to send (e.g., RS, NS), and packets that we need to receive (e.g., RA, NA). |
| 56 | const char* FirewallController::ICMPV6_TYPES[] = { |
| 57 | "packet-too-big", |
| 58 | "router-solicitation", |
| 59 | "router-advertisement", |
| 60 | "neighbour-solicitation", |
| 61 | "neighbour-advertisement", |
| 62 | "redirect", |
| 63 | }; |
| 64 | |
waynema | 53de1ff | 2021-12-01 11:35:06 +0800 | [diff] [blame] | 65 | FirewallController::FirewallController(void) { |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 66 | // If no rules are set, it's in DENYLIST mode |
| 67 | mFirewallType = DENYLIST; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 68 | mIfaceRules = {}; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | int FirewallController::setupIptablesHooks(void) { |
waynema | 53de1ff | 2021-12-01 11:35:06 +0800 | [diff] [blame] | 72 | return flushRules(); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 75 | int FirewallController::setFirewallType(FirewallType ftype) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 76 | int res = 0; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 77 | if (mFirewallType != ftype) { |
| 78 | // flush any existing rules |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 79 | resetFirewall(); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 80 | |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 81 | if (ftype == ALLOWLIST) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 82 | // create default rule to drop all traffic |
Lorenzo Colitti | d351bea | 2017-07-16 22:52:30 +0900 | [diff] [blame] | 83 | std::string command = |
| 84 | "*filter\n" |
| 85 | "-A fw_INPUT -j DROP\n" |
| 86 | "-A fw_OUTPUT -j REJECT\n" |
| 87 | "-A fw_FORWARD -j REJECT\n" |
| 88 | "COMMIT\n"; |
| 89 | res = execIptablesRestore(V4V6, command.c_str()); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 90 | } |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 91 | |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 92 | // Set this after calling disableFirewall(), since it defaults to ALLOWLIST there |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 93 | mFirewallType = ftype; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 94 | } |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 95 | return res ? -EREMOTEIO : 0; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Maciej Żenczykowski | 344bb89 | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 98 | int FirewallController::flushRules() { |
| 99 | std::string command = |
| 100 | "*filter\n" |
| 101 | ":fw_INPUT -\n" |
| 102 | ":fw_OUTPUT -\n" |
| 103 | ":fw_FORWARD -\n" |
| 104 | "-6 -A fw_OUTPUT ! -o lo -s ::1 -j DROP\n" |
| 105 | "COMMIT\n"; |
| 106 | |
| 107 | return (execIptablesRestore(V4V6, command.c_str()) == 0) ? 0 : -EREMOTEIO; |
| 108 | } |
| 109 | |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 110 | int FirewallController::resetFirewall(void) { |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 111 | mFirewallType = ALLOWLIST; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 112 | mIfaceRules.clear(); |
Maciej Żenczykowski | 344bb89 | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 113 | return flushRules(); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | int FirewallController::isFirewallEnabled(void) { |
| 117 | // TODO: verify that rules are still in place near top |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | int FirewallController::setInterfaceRule(const char* iface, FirewallRule rule) { |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 122 | if (mFirewallType == DENYLIST) { |
| 123 | // Unsupported in DENYLIST mode |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 124 | return -EINVAL; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 125 | } |
| 126 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 127 | if (!isIfaceName(iface)) { |
| 128 | errno = ENOENT; |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 129 | return -ENOENT; |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 132 | // Only delete rules if we actually added them, because otherwise our iptables-restore |
| 133 | // processes will terminate with "no such rule" errors and cause latency penalties while we |
| 134 | // spin up new ones. |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 135 | const char* op; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 136 | if (rule == ALLOW && mIfaceRules.find(iface) == mIfaceRules.end()) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 137 | op = "-I"; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 138 | mIfaceRules.insert(iface); |
| 139 | } else if (rule == DENY && mIfaceRules.find(iface) != mIfaceRules.end()) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 140 | op = "-D"; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 141 | mIfaceRules.erase(iface); |
| 142 | } else { |
| 143 | return 0; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 146 | std::string command = Join(std::vector<std::string> { |
| 147 | "*filter", |
| 148 | StringPrintf("%s fw_INPUT -i %s -j RETURN", op, iface), |
| 149 | StringPrintf("%s fw_OUTPUT -o %s -j RETURN", op, iface), |
| 150 | "COMMIT\n" |
| 151 | }, "\n"); |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 152 | return (execIptablesRestore(V4V6, command) == 0) ? 0 : -EREMOTEIO; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 155 | /* static */ |
| 156 | std::string FirewallController::makeCriticalCommands(IptablesTarget target, const char* chainName) { |
| 157 | // Allow ICMPv6 packets necessary to make IPv6 connectivity work. http://b/23158230 . |
| 158 | std::string commands; |
| 159 | if (target == V6) { |
| 160 | for (size_t i = 0; i < ARRAY_SIZE(ICMPV6_TYPES); i++) { |
| 161 | StringAppendF(&commands, "-A %s -p icmpv6 --icmpv6-type %s -j RETURN\n", |
| 162 | chainName, ICMPV6_TYPES[i]); |
| 163 | } |
| 164 | } |
| 165 | return commands; |
| 166 | } |
| 167 | |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 168 | } // namespace net |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 169 | } // namespace android |