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