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