blob: 041aa40c72287443867ea09d862f289738d7f44e [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
Lorenzo Colitti1411d452017-07-17 22:12:15 +090020#include <set>
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070021#include <string>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090022#include <vector>
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070023
Lorenzo Colittiddf2d5b2016-02-26 11:30:59 +090024#include <utils/RWLock.h>
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070025
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090026#include "NetdConstants.h"
27
Amith Yamasani390e4ea2015-04-25 19:08:57 -070028enum FirewallRule { DENY, ALLOW };
29
30// WHITELIST means the firewall denies all by default, uids must be explicitly ALLOWed
31// BLACKLIST means the firewall allows all by default, uids must be explicitly DENYed
32
33enum FirewallType { WHITELIST, BLACKLIST };
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070034
Felipe Leme3f624342016-02-10 18:12:39 -080035enum ChildChain { NONE, DOZABLE, STANDBY, POWERSAVE, INVALID_CHAIN };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070036
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070037#define PROTOCOL_TCP 6
38#define PROTOCOL_UDP 17
39
40/*
41 * Simple firewall that drops all packets except those matching explicitly
42 * defined ALLOW rules.
Lorenzo Colittiddf2d5b2016-02-26 11:30:59 +090043 *
44 * Methods in this class must be called when holding a write lock on |lock|, and may not call
45 * any other controller without explicitly managing that controller's lock. There are currently
46 * no such methods.
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070047 */
48class FirewallController {
49public:
50 FirewallController();
51
52 int setupIptablesHooks(void);
53
Amith Yamasani390e4ea2015-04-25 19:08:57 -070054 int enableFirewall(FirewallType);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070055 int disableFirewall(void);
56 int isFirewallEnabled(void);
57
58 /* Match traffic going in/out over the given iface. */
59 int setInterfaceRule(const char*, FirewallRule);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070060 /* 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
Lorenzo Colitti89faa342016-02-26 11:38:47 +090065 int replaceUidChain(const char*, bool, const std::vector<int32_t>&);
66
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070067 static const char* TABLE;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070068
69 static const char* LOCAL_INPUT;
70 static const char* LOCAL_OUTPUT;
71 static const char* LOCAL_FORWARD;
72
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070073 static const char* LOCAL_DOZABLE;
74 static const char* LOCAL_STANDBY;
Felipe Leme3f624342016-02-10 18:12:39 -080075 static const char* LOCAL_POWERSAVE;
Lorenzo Colittic8683d72015-09-01 16:53:35 +090076
77 static const char* ICMPV6_TYPES[];
78
Lorenzo Colittiddf2d5b2016-02-26 11:30:59 +090079 android::RWLock lock;
80
Lorenzo Colitti89faa342016-02-26 11:38:47 +090081protected:
82 friend class FirewallControllerTest;
Lorenzo Colittif157caf2016-05-13 11:25:54 +090083 std::string makeUidRules(IptablesTarget target, const char *name, bool isWhitelist,
84 const std::vector<int32_t>& uids);
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090085 static int (*execIptablesRestore)(IptablesTarget target, const std::string& commands);
Lorenzo Colitti89faa342016-02-26 11:38:47 +090086
Amith Yamasani390e4ea2015-04-25 19:08:57 -070087private:
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070088 FirewallType mFirewallType;
Lorenzo Colitti1411d452017-07-17 22:12:15 +090089 std::set<std::string> mIfaceRules;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070090 int attachChain(const char*, const char*);
91 int detachChain(const char*, const char*);
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090092 int createChain(const char*, FirewallType);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070093 FirewallType getFirewallType(ChildChain);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070094};
95
96#endif