blob: 45ae0823ba55d6e15438a115ba3dd06de02c8a4e [file] [log] [blame]
Jeff Sharkeyd8c64022012-07-13 18:04:07 -07001/*
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 Benichi528d3d02018-06-20 13:35:58 +090020#include <sys/types.h>
Luke Huangd1ee4622018-06-29 13:49:58 +080021#include <mutex>
Lorenzo Colitti1411d452017-07-17 22:12:15 +090022#include <set>
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070023#include <string>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090024#include <vector>
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070025
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090026#include "NetdConstants.h"
27
Luke Huange64fa382018-07-24 16:38:22 +080028namespace android {
29namespace net {
30
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070031/*
32 * Simple firewall that drops all packets except those matching explicitly
33 * defined ALLOW rules.
Lorenzo Colittiddf2d5b2016-02-26 11:30:59 +090034 *
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 Sharkeyd8c64022012-07-13 18:04:07 -070038 */
39class FirewallController {
40public:
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070041 FirewallController();
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070042
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070043 int setupIptablesHooks(void);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070044
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070045 int setFirewallType(FirewallType);
46 int resetFirewall(void);
47 int isFirewallEnabled(void);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070048
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070049 /* 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 Chen1cdfa9a2015-06-08 16:28:12 -070053
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070054 static std::string makeCriticalCommands(IptablesTarget target, const char* chainName);
waynema53de1ff2021-12-01 11:35:06 +080055
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070056 static const char* TABLE;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070057
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070058 static const char* LOCAL_INPUT;
59 static const char* LOCAL_OUTPUT;
60 static const char* LOCAL_FORWARD;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070061
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070062 static const char* ICMPV6_TYPES[];
Lorenzo Colittic8683d72015-09-01 16:53:35 +090063
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070064 std::mutex lock;
Lorenzo Colittiddf2d5b2016-02-26 11:30:59 +090065
Lorenzo Colitti89faa342016-02-26 11:38:47 +090066protected:
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070067 friend class FirewallControllerTest;
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070068 static int (*execIptablesRestore)(IptablesTarget target, const std::string& commands);
Lorenzo Colitti89faa342016-02-26 11:38:47 +090069
Amith Yamasani390e4ea2015-04-25 19:08:57 -070070private:
Hugo Benichi528d3d02018-06-20 13:35:58 +090071 FirewallType mFirewallType;
Hugo Benichi528d3d02018-06-20 13:35:58 +090072 std::set<std::string> mIfaceRules;
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070073 int flushRules(void);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070074};
75
Luke Huange64fa382018-07-24 16:38:22 +080076} // namespace net
77} // namespace android
78
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070079#endif