blob: 6d6f48fad8dd106026b3265b8f7377c655083e18 [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"
Chenbo Feng47dd0732018-12-11 12:23:24 -080027#include "bpf/BpfUtils.h"
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090028
Luke Huange64fa382018-07-24 16:38:22 +080029namespace android {
30namespace net {
31
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070032/*
33 * Simple firewall that drops all packets except those matching explicitly
34 * defined ALLOW rules.
Lorenzo Colittiddf2d5b2016-02-26 11:30:59 +090035 *
36 * Methods in this class must be called when holding a write lock on |lock|, and may not call
37 * any other controller without explicitly managing that controller's lock. There are currently
38 * no such methods.
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070039 */
40class FirewallController {
41public:
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070042 FirewallController();
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070043
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070044 int setupIptablesHooks(void);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070045
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070046 int setFirewallType(FirewallType);
47 int resetFirewall(void);
48 int isFirewallEnabled(void);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070049
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070050 /* Match traffic going in/out over the given iface. */
51 int setInterfaceRule(const char*, FirewallRule);
52 /* Match traffic owned by given UID. This is specific to a particular chain. */
53 int setUidRule(ChildChain, int, FirewallRule);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070054
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070055 int enableChildChains(ChildChain, bool);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070056
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070057 static std::string makeCriticalCommands(IptablesTarget target, const char* chainName);
waynema53de1ff2021-12-01 11:35:06 +080058
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070059 static const char* TABLE;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070060
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070061 static const char* LOCAL_INPUT;
62 static const char* LOCAL_OUTPUT;
63 static const char* LOCAL_FORWARD;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070064
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070065 static const char* ICMPV6_TYPES[];
Lorenzo Colittic8683d72015-09-01 16:53:35 +090066
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070067 std::mutex lock;
Lorenzo Colittiddf2d5b2016-02-26 11:30:59 +090068
Lorenzo Colitti89faa342016-02-26 11:38:47 +090069protected:
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070070 friend class FirewallControllerTest;
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070071 static int (*execIptablesRestore)(IptablesTarget target, const std::string& commands);
Lorenzo Colitti89faa342016-02-26 11:38:47 +090072
Amith Yamasani390e4ea2015-04-25 19:08:57 -070073private:
Hugo Benichi528d3d02018-06-20 13:35:58 +090074 FirewallType mFirewallType;
Hugo Benichi528d3d02018-06-20 13:35:58 +090075 std::set<std::string> mIfaceRules;
Maciej Żenczykowski344bb892021-10-14 20:22:23 -070076 int flushRules(void);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070077};
78
Luke Huange64fa382018-07-24 16:38:22 +080079} // namespace net
80} // namespace android
81
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070082#endif