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 | |
Hugo Benichi | 528d3d0 | 2018-06-20 13:35:58 +0900 | [diff] [blame] | 20 | #include <sys/types.h> |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 21 | #include <mutex> |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 22 | #include <set> |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 23 | #include <string> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 24 | #include <vector> |
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 | |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace net { |
| 30 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 31 | /* |
| 32 | * Simple firewall that drops all packets except those matching explicitly |
| 33 | * defined ALLOW rules. |
Lorenzo Colitti | ddf2d5b | 2016-02-26 11:30:59 +0900 | [diff] [blame] | 34 | * |
| 35 | * Methods in this class must be called when holding a write lock on |lock|, and may not call |
| 36 | * any other controller without explicitly managing that controller's lock. There are currently |
| 37 | * no such methods. |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 38 | */ |
| 39 | class FirewallController { |
| 40 | public: |
Maciej Żenczykowski | 344bb89 | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 41 | FirewallController(); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 42 | |
Maciej Żenczykowski | 344bb89 | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 43 | int setupIptablesHooks(void); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 44 | |
Maciej Żenczykowski | 344bb89 | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 45 | int setFirewallType(FirewallType); |
| 46 | int resetFirewall(void); |
| 47 | int isFirewallEnabled(void); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 48 | |
Maciej Żenczykowski | 344bb89 | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 49 | /* Match traffic going in/out over the given iface. */ |
| 50 | int setInterfaceRule(const char*, FirewallRule); |
| 51 | /* Match traffic owned by given UID. This is specific to a particular chain. */ |
| 52 | int setUidRule(ChildChain, int, FirewallRule); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 53 | |
Maciej Żenczykowski | 344bb89 | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 54 | static std::string makeCriticalCommands(IptablesTarget target, const char* chainName); |
waynema | 53de1ff | 2021-12-01 11:35:06 +0800 | [diff] [blame] | 55 | |
Maciej Żenczykowski | 344bb89 | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 56 | static const char* TABLE; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 57 | |
Maciej Żenczykowski | 344bb89 | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 58 | static const char* LOCAL_INPUT; |
| 59 | static const char* LOCAL_OUTPUT; |
| 60 | static const char* LOCAL_FORWARD; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 61 | |
Maciej Żenczykowski | 344bb89 | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 62 | static const char* ICMPV6_TYPES[]; |
Lorenzo Colitti | c8683d7 | 2015-09-01 16:53:35 +0900 | [diff] [blame] | 63 | |
Maciej Żenczykowski | 344bb89 | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 64 | std::mutex lock; |
Lorenzo Colitti | ddf2d5b | 2016-02-26 11:30:59 +0900 | [diff] [blame] | 65 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 66 | protected: |
Maciej Żenczykowski | 344bb89 | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 67 | friend class FirewallControllerTest; |
Maciej Żenczykowski | 344bb89 | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 68 | static int (*execIptablesRestore)(IptablesTarget target, const std::string& commands); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 69 | |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 70 | private: |
Hugo Benichi | 528d3d0 | 2018-06-20 13:35:58 +0900 | [diff] [blame] | 71 | FirewallType mFirewallType; |
Hugo Benichi | 528d3d0 | 2018-06-20 13:35:58 +0900 | [diff] [blame] | 72 | std::set<std::string> mIfaceRules; |
Maciej Żenczykowski | 344bb89 | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 73 | int flushRules(void); |
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 | } // namespace net |
| 77 | } // namespace android |
| 78 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 79 | #endif |