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 | #ifndef _FIREWALL_CONTROLLER_H |
| 18 | #define _FIREWALL_CONTROLLER_H |
| 19 | |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 20 | #include <set> |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 21 | #include <string> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 22 | #include <vector> |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 23 | |
Lorenzo Colitti | ddf2d5b | 2016-02-26 11:30:59 +0900 | [diff] [blame] | 24 | #include <utils/RWLock.h> |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 25 | |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 26 | #include "NetdConstants.h" |
| 27 | |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 28 | enum FirewallRule { DENY, ALLOW }; |
| 29 | |
| 30 | // WHITELIST means the firewall denies all by default, uids must be explicitly ALLOWed |
| 31 | // BLACKLIST means the firewall allows all by default, uids must be explicitly DENYed |
| 32 | |
| 33 | enum FirewallType { WHITELIST, BLACKLIST }; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 34 | |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 35 | enum ChildChain { NONE, DOZABLE, STANDBY, POWERSAVE, INVALID_CHAIN }; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 36 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 37 | #define PROTOCOL_TCP 6 |
| 38 | #define PROTOCOL_UDP 17 |
| 39 | |
| 40 | /* |
| 41 | * Simple firewall that drops all packets except those matching explicitly |
| 42 | * defined ALLOW rules. |
Lorenzo Colitti | ddf2d5b | 2016-02-26 11:30:59 +0900 | [diff] [blame] | 43 | * |
| 44 | * Methods in this class must be called when holding a write lock on |lock|, and may not call |
| 45 | * any other controller without explicitly managing that controller's lock. There are currently |
| 46 | * no such methods. |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 47 | */ |
| 48 | class FirewallController { |
| 49 | public: |
| 50 | FirewallController(); |
| 51 | |
| 52 | int setupIptablesHooks(void); |
| 53 | |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 54 | int enableFirewall(FirewallType); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 55 | int disableFirewall(void); |
| 56 | int isFirewallEnabled(void); |
| 57 | |
| 58 | /* Match traffic going in/out over the given iface. */ |
| 59 | int setInterfaceRule(const char*, FirewallRule); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 60 | /* Match traffic owned by given UID. This is specific to a particular chain. */ |
| 61 | int setUidRule(ChildChain, int, FirewallRule); |
| 62 | |
| 63 | int enableChildChains(ChildChain, bool); |
| 64 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 65 | int replaceUidChain(const char*, bool, const std::vector<int32_t>&); |
| 66 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 67 | static const char* TABLE; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 68 | |
| 69 | static const char* LOCAL_INPUT; |
| 70 | static const char* LOCAL_OUTPUT; |
| 71 | static const char* LOCAL_FORWARD; |
| 72 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 73 | static const char* LOCAL_DOZABLE; |
| 74 | static const char* LOCAL_STANDBY; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 75 | static const char* LOCAL_POWERSAVE; |
Lorenzo Colitti | c8683d7 | 2015-09-01 16:53:35 +0900 | [diff] [blame] | 76 | |
| 77 | static const char* ICMPV6_TYPES[]; |
| 78 | |
Lorenzo Colitti | ddf2d5b | 2016-02-26 11:30:59 +0900 | [diff] [blame] | 79 | android::RWLock lock; |
| 80 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 81 | protected: |
| 82 | friend class FirewallControllerTest; |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 83 | std::string makeUidRules(IptablesTarget target, const char *name, bool isWhitelist, |
| 84 | const std::vector<int32_t>& uids); |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 85 | static int (*execIptablesRestore)(IptablesTarget target, const std::string& commands); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 86 | |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 87 | private: |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 88 | FirewallType mFirewallType; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 89 | std::set<std::string> mIfaceRules; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 90 | int attachChain(const char*, const char*); |
| 91 | int detachChain(const char*, const char*); |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 92 | int createChain(const char*, FirewallType); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 93 | FirewallType getFirewallType(ChildChain); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | #endif |