blob: a7082da91b3871c5fce5e585156750347a233bc0 [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
Luke Huange64fa382018-07-24 16:38:22 +080026#include "android/net/INetd.h"
27
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090028#include "NetdConstants.h"
29
Luke Huange64fa382018-07-24 16:38:22 +080030namespace android {
31namespace net {
32
33enum FirewallRule { ALLOW = INetd::FIREWALL_RULE_ALLOW, DENY = INetd::FIREWALL_RULE_DENY };
Amith Yamasani390e4ea2015-04-25 19:08:57 -070034
35// WHITELIST means the firewall denies all by default, uids must be explicitly ALLOWed
36// BLACKLIST means the firewall allows all by default, uids must be explicitly DENYed
37
Luke Huange64fa382018-07-24 16:38:22 +080038enum FirewallType { WHITELIST = INetd::FIREWALL_WHITELIST, BLACKLIST = INetd::FIREWALL_BLACKLIST };
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070039
Luke Huange64fa382018-07-24 16:38:22 +080040enum ChildChain {
41 NONE = INetd::FIREWALL_CHAIN_NONE,
42 DOZABLE = INetd::FIREWALL_CHAIN_DOZABLE,
43 STANDBY = INetd::FIREWALL_CHAIN_STANDBY,
44 POWERSAVE = INetd::FIREWALL_CHAIN_POWERSAVE,
45 INVALID_CHAIN
46};
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070047
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070048/*
49 * Simple firewall that drops all packets except those matching explicitly
50 * defined ALLOW rules.
Lorenzo Colittiddf2d5b2016-02-26 11:30:59 +090051 *
52 * Methods in this class must be called when holding a write lock on |lock|, and may not call
53 * any other controller without explicitly managing that controller's lock. There are currently
54 * no such methods.
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070055 */
56class FirewallController {
57public:
58 FirewallController();
59
60 int setupIptablesHooks(void);
61
Luke Huange64fa382018-07-24 16:38:22 +080062 int setFirewallType(FirewallType);
63 int resetFirewall(void);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070064 int isFirewallEnabled(void);
65
66 /* Match traffic going in/out over the given iface. */
67 int setInterfaceRule(const char*, FirewallRule);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070068 /* Match traffic owned by given UID. This is specific to a particular chain. */
69 int setUidRule(ChildChain, int, FirewallRule);
70
71 int enableChildChains(ChildChain, bool);
72
Erik Klinef52d4522018-03-14 15:01:46 +090073 int replaceUidChain(const std::string&, bool, const std::vector<int32_t>&);
Lorenzo Colitti89faa342016-02-26 11:38:47 +090074
Lorenzo Colittiaff28792017-09-26 17:46:18 +090075 static std::string makeCriticalCommands(IptablesTarget target, const char* chainName);
Hugo Benichi528d3d02018-06-20 13:35:58 +090076 static uid_t discoverMaximumValidUid(const std::string& fileName);
Lorenzo Colittiaff28792017-09-26 17:46:18 +090077
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070078 static const char* TABLE;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070079
80 static const char* LOCAL_INPUT;
81 static const char* LOCAL_OUTPUT;
82 static const char* LOCAL_FORWARD;
83
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070084 static const char* LOCAL_DOZABLE;
85 static const char* LOCAL_STANDBY;
Felipe Leme3f624342016-02-10 18:12:39 -080086 static const char* LOCAL_POWERSAVE;
Lorenzo Colittic8683d72015-09-01 16:53:35 +090087
88 static const char* ICMPV6_TYPES[];
89
Luke Huangd1ee4622018-06-29 13:49:58 +080090 std::mutex lock;
Lorenzo Colittiddf2d5b2016-02-26 11:30:59 +090091
Lorenzo Colitti89faa342016-02-26 11:38:47 +090092protected:
93 friend class FirewallControllerTest;
Lorenzo Colittif157caf2016-05-13 11:25:54 +090094 std::string makeUidRules(IptablesTarget target, const char *name, bool isWhitelist,
95 const std::vector<int32_t>& uids);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090096 static int (*execIptablesRestore)(IptablesTarget target, const std::string& commands);
Lorenzo Colitti89faa342016-02-26 11:38:47 +090097
Amith Yamasani390e4ea2015-04-25 19:08:57 -070098private:
Hugo Benichi528d3d02018-06-20 13:35:58 +090099 // Netd supports two cases, in both of which mMaxUid that derives from the uid mapping is const:
100 // - netd runs in a root namespace which contains all UIDs.
101 // - netd runs in a user namespace where the uid mapping is written once before netd starts.
102 // In that case, an attempt to write more than once to a uid_map file in a user namespace
103 // fails with EPERM. Netd can therefore assumes the max valid uid to be const.
104 const uid_t mMaxUid;
105 FirewallType mFirewallType;
106 bool mUseBpfOwnerMatch;
107 std::set<std::string> mIfaceRules;
108 int attachChain(const char*, const char*);
109 int detachChain(const char*, const char*);
110 int createChain(const char*, FirewallType);
111 FirewallType getFirewallType(ChildChain);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700112};
113
Luke Huange64fa382018-07-24 16:38:22 +0800114} // namespace net
115} // namespace android
116
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700117#endif