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 | |
| 17 | #include <errno.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #define LOG_TAG "FirewallController" |
| 23 | #define LOG_NDEBUG 0 |
| 24 | |
| 25 | #include <cutils/log.h> |
| 26 | |
| 27 | #include "NetdConstants.h" |
| 28 | #include "FirewallController.h" |
| 29 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame^] | 30 | const char* FirewallController::TABLE = "filter"; |
| 31 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 32 | const char* FirewallController::LOCAL_INPUT = "fw_INPUT"; |
| 33 | const char* FirewallController::LOCAL_OUTPUT = "fw_OUTPUT"; |
| 34 | const char* FirewallController::LOCAL_FORWARD = "fw_FORWARD"; |
| 35 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame^] | 36 | const char* FirewallController::LOCAL_DOZABLE = "fw_dozable"; |
| 37 | const char* FirewallController::LOCAL_STANDBY = "fw_standby"; |
| 38 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 39 | FirewallController::FirewallController(void) { |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 40 | // If no rules are set, it's in BLACKLIST mode |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame^] | 41 | mFirewallType = BLACKLIST; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | int FirewallController::setupIptablesHooks(void) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame^] | 45 | int res = 0; |
| 46 | // child chains are created but not attached, they will be attached explicitly. |
| 47 | FirewallType firewallType = getFirewallType(DOZABLE); |
| 48 | res |= createChain(LOCAL_DOZABLE, LOCAL_INPUT, firewallType); |
| 49 | |
| 50 | firewallType = getFirewallType(STANDBY); |
| 51 | res |= createChain(LOCAL_STANDBY, LOCAL_INPUT, firewallType); |
| 52 | |
| 53 | return res; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 56 | int FirewallController::enableFirewall(FirewallType ftype) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 57 | int res = 0; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame^] | 58 | if (mFirewallType != ftype) { |
| 59 | // flush any existing rules |
| 60 | disableFirewall(); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 61 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame^] | 62 | if (ftype == WHITELIST) { |
| 63 | // create default rule to drop all traffic |
| 64 | res |= execIptables(V4V6, "-A", LOCAL_INPUT, "-j", "DROP", NULL); |
| 65 | res |= execIptables(V4V6, "-A", LOCAL_OUTPUT, "-j", "REJECT", NULL); |
| 66 | res |= execIptables(V4V6, "-A", LOCAL_FORWARD, "-j", "REJECT", NULL); |
| 67 | } |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 68 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame^] | 69 | // Set this after calling disableFirewall(), since it defaults to WHITELIST there |
| 70 | mFirewallType = ftype; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 71 | } |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 72 | return res; |
| 73 | } |
| 74 | |
| 75 | int FirewallController::disableFirewall(void) { |
| 76 | int res = 0; |
| 77 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame^] | 78 | mFirewallType = WHITELIST; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 79 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 80 | // flush any existing rules |
| 81 | res |= execIptables(V4V6, "-F", LOCAL_INPUT, NULL); |
| 82 | res |= execIptables(V4V6, "-F", LOCAL_OUTPUT, NULL); |
| 83 | res |= execIptables(V4V6, "-F", LOCAL_FORWARD, NULL); |
| 84 | |
| 85 | return res; |
| 86 | } |
| 87 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame^] | 88 | int FirewallController::enableChildChains(ChildChain chain, bool enable) { |
| 89 | int res = 0; |
| 90 | const char* name; |
| 91 | switch(chain) { |
| 92 | case DOZABLE: |
| 93 | name = LOCAL_DOZABLE; |
| 94 | break; |
| 95 | case STANDBY: |
| 96 | name = LOCAL_STANDBY; |
| 97 | break; |
| 98 | default: |
| 99 | return res; |
| 100 | } |
| 101 | |
| 102 | if (enable) { |
| 103 | res |= attachChain(name, LOCAL_INPUT); |
| 104 | res |= attachChain(name, LOCAL_OUTPUT); |
| 105 | } else { |
| 106 | res |= detachChain(name, LOCAL_INPUT); |
| 107 | res |= detachChain(name, LOCAL_OUTPUT); |
| 108 | } |
| 109 | return res; |
| 110 | } |
| 111 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 112 | int FirewallController::isFirewallEnabled(void) { |
| 113 | // TODO: verify that rules are still in place near top |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | int FirewallController::setInterfaceRule(const char* iface, FirewallRule rule) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame^] | 118 | if (mFirewallType == BLACKLIST) { |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 119 | // Unsupported in BLACKLIST mode |
| 120 | return -1; |
| 121 | } |
| 122 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 123 | if (!isIfaceName(iface)) { |
| 124 | errno = ENOENT; |
| 125 | return -1; |
| 126 | } |
| 127 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 128 | const char* op; |
| 129 | if (rule == ALLOW) { |
| 130 | op = "-I"; |
| 131 | } else { |
| 132 | op = "-D"; |
| 133 | } |
| 134 | |
| 135 | int res = 0; |
| 136 | res |= execIptables(V4V6, op, LOCAL_INPUT, "-i", iface, "-j", "RETURN", NULL); |
| 137 | res |= execIptables(V4V6, op, LOCAL_OUTPUT, "-o", iface, "-j", "RETURN", NULL); |
| 138 | return res; |
| 139 | } |
| 140 | |
| 141 | int FirewallController::setEgressSourceRule(const char* addr, FirewallRule rule) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame^] | 142 | if (mFirewallType == BLACKLIST) { |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 143 | // Unsupported in BLACKLIST mode |
| 144 | return -1; |
| 145 | } |
| 146 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 147 | IptablesTarget target = V4; |
| 148 | if (strchr(addr, ':')) { |
| 149 | target = V6; |
| 150 | } |
| 151 | |
| 152 | const char* op; |
| 153 | if (rule == ALLOW) { |
| 154 | op = "-I"; |
| 155 | } else { |
| 156 | op = "-D"; |
| 157 | } |
| 158 | |
| 159 | int res = 0; |
| 160 | res |= execIptables(target, op, LOCAL_INPUT, "-d", addr, "-j", "RETURN", NULL); |
| 161 | res |= execIptables(target, op, LOCAL_OUTPUT, "-s", addr, "-j", "RETURN", NULL); |
| 162 | return res; |
| 163 | } |
| 164 | |
| 165 | int FirewallController::setEgressDestRule(const char* addr, int protocol, int port, |
| 166 | FirewallRule rule) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame^] | 167 | if (mFirewallType == BLACKLIST) { |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 168 | // Unsupported in BLACKLIST mode |
| 169 | return -1; |
| 170 | } |
| 171 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 172 | IptablesTarget target = V4; |
| 173 | if (strchr(addr, ':')) { |
| 174 | target = V6; |
| 175 | } |
| 176 | |
| 177 | char protocolStr[16]; |
| 178 | sprintf(protocolStr, "%d", protocol); |
| 179 | |
| 180 | char portStr[16]; |
| 181 | sprintf(portStr, "%d", port); |
| 182 | |
| 183 | const char* op; |
| 184 | if (rule == ALLOW) { |
| 185 | op = "-I"; |
| 186 | } else { |
| 187 | op = "-D"; |
| 188 | } |
| 189 | |
| 190 | int res = 0; |
| 191 | res |= execIptables(target, op, LOCAL_INPUT, "-s", addr, "-p", protocolStr, |
| 192 | "--sport", portStr, "-j", "RETURN", NULL); |
| 193 | res |= execIptables(target, op, LOCAL_OUTPUT, "-d", addr, "-p", protocolStr, |
| 194 | "--dport", portStr, "-j", "RETURN", NULL); |
| 195 | return res; |
| 196 | } |
| 197 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame^] | 198 | FirewallType FirewallController::getFirewallType(ChildChain chain) { |
| 199 | switch(chain) { |
| 200 | case DOZABLE: |
| 201 | return WHITELIST; |
| 202 | case STANDBY: |
| 203 | return BLACKLIST; |
| 204 | case NONE: |
| 205 | return mFirewallType; |
| 206 | default: |
| 207 | return BLACKLIST; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | int FirewallController::setUidRule(ChildChain chain, int uid, FirewallRule rule) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 212 | char uidStr[16]; |
| 213 | sprintf(uidStr, "%d", uid); |
| 214 | |
| 215 | const char* op; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 216 | const char* target; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame^] | 217 | FirewallType firewallType = getFirewallType(chain); |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 218 | if (firewallType == WHITELIST) { |
| 219 | target = "RETURN"; |
| 220 | op = (rule == ALLOW)? "-I" : "-D"; |
| 221 | } else { // BLACKLIST mode |
| 222 | target = "DROP"; |
| 223 | op = (rule == DENY)? "-I" : "-D"; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | int res = 0; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame^] | 227 | switch(chain) { |
| 228 | case DOZABLE: |
| 229 | res |= execIptables(V4V6, op, LOCAL_DOZABLE, "-m", "owner", "--uid-owner", |
| 230 | uidStr, "-j", target, NULL); |
| 231 | break; |
| 232 | case STANDBY: |
| 233 | res |= execIptables(V4V6, op, LOCAL_STANDBY, "-m", "owner", "--uid-owner", |
| 234 | uidStr, "-j", target, NULL); |
| 235 | break; |
| 236 | case NONE: |
| 237 | res |= execIptables(V4V6, op, LOCAL_INPUT, "-m", "owner", "--uid-owner", uidStr, |
| 238 | "-j", target, NULL); |
| 239 | res |= execIptables(V4V6, op, LOCAL_OUTPUT, "-m", "owner", "--uid-owner", uidStr, |
| 240 | "-j", target, NULL); |
| 241 | break; |
| 242 | default: |
| 243 | ALOGW("Unknown child chain: %d", chain); |
| 244 | break; |
| 245 | } |
| 246 | return res; |
| 247 | } |
| 248 | |
| 249 | int FirewallController::attachChain(const char* childChain, const char* parentChain) { |
| 250 | return execIptables(V4V6, "-t", TABLE, "-A", parentChain, "-j", childChain, NULL); |
| 251 | } |
| 252 | |
| 253 | int FirewallController::detachChain(const char* childChain, const char* parentChain) { |
| 254 | return execIptables(V4V6, "-t", TABLE, "-D", parentChain, "-j", childChain, NULL); |
| 255 | } |
| 256 | |
| 257 | int FirewallController::createChain(const char* childChain, |
| 258 | const char* parentChain, FirewallType type) { |
| 259 | // Order is important, otherwise later steps may fail. |
| 260 | execIptablesSilently(V4V6, "-t", TABLE, "-D", parentChain, "-j", childChain, NULL); |
| 261 | execIptablesSilently(V4V6, "-t", TABLE, "-F", childChain, NULL); |
| 262 | execIptablesSilently(V4V6, "-t", TABLE, "-X", childChain, NULL); |
| 263 | int res = 0; |
| 264 | res |= execIptables(V4V6, "-t", TABLE, "-N", childChain, NULL); |
| 265 | if (type == WHITELIST) { |
| 266 | // create default rule to drop all traffic |
| 267 | res |= execIptables(V4V6, "-A", childChain, "-j", "DROP", NULL); |
| 268 | } |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 269 | return res; |
| 270 | } |