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