blob: d3ef777a6742075f2d2eec7b3995c5b836ae3727 [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
20#include <string>
21
Lorenzo Colittiddf2d5b2016-02-26 11:30:59 +090022#include <utils/RWLock.h>
23
Amith Yamasani390e4ea2015-04-25 19:08:57 -070024enum FirewallRule { DENY, ALLOW };
25
26// WHITELIST means the firewall denies all by default, uids must be explicitly ALLOWed
27// BLACKLIST means the firewall allows all by default, uids must be explicitly DENYed
28
29enum FirewallType { WHITELIST, BLACKLIST };
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070030
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070031enum ChildChain { NONE, DOZABLE, STANDBY, INVALID_CHAIN };
32
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070033#define PROTOCOL_TCP 6
34#define PROTOCOL_UDP 17
35
36/*
37 * Simple firewall that drops all packets except those matching explicitly
38 * defined ALLOW rules.
Lorenzo Colittiddf2d5b2016-02-26 11:30:59 +090039 *
40 * Methods in this class must be called when holding a write lock on |lock|, and may not call
41 * any other controller without explicitly managing that controller's lock. There are currently
42 * no such methods.
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070043 */
44class FirewallController {
45public:
46 FirewallController();
47
48 int setupIptablesHooks(void);
49
Amith Yamasani390e4ea2015-04-25 19:08:57 -070050 int enableFirewall(FirewallType);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070051 int disableFirewall(void);
52 int isFirewallEnabled(void);
53
54 /* Match traffic going in/out over the given iface. */
55 int setInterfaceRule(const char*, FirewallRule);
56 /* Match traffic coming-in-to or going-out-from given address. */
57 int setEgressSourceRule(const char*, FirewallRule);
58 /* Match traffic coming-in-from or going-out-to given address, port, and protocol. */
59 int setEgressDestRule(const char*, int, int, 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
65 static const char* TABLE;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070066
67 static const char* LOCAL_INPUT;
68 static const char* LOCAL_OUTPUT;
69 static const char* LOCAL_FORWARD;
70
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070071 static const char* LOCAL_DOZABLE;
72 static const char* LOCAL_STANDBY;
Lorenzo Colittic8683d72015-09-01 16:53:35 +090073
74 static const char* ICMPV6_TYPES[];
75
Lorenzo Colittiddf2d5b2016-02-26 11:30:59 +090076 android::RWLock lock;
77
Amith Yamasani390e4ea2015-04-25 19:08:57 -070078private:
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070079 FirewallType mFirewallType;
80 int attachChain(const char*, const char*);
81 int detachChain(const char*, const char*);
82 int createChain(const char*, const char*, FirewallType);
83 FirewallType getFirewallType(ChildChain);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070084};
85
86#endif