2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef _SECONDARY_TABLE_CONTROLLER_H
18 #define _SECONDARY_TABLE_CONTROLLER_H
22 #include <sysutils/FrameworkListener.h>
25 #include "NetdConstants.h"
26 #include "NetworkController.h"
32 static const int BASE_TABLE_NUMBER = 60;
33 static const char *EXEMPT_PRIO = "99";
34 static const char *RULE_PRIO = "100";
36 // SecondaryTableController is responsible for maintaining the "secondary" routing tables, where
37 // "secondary" means not the main table. The "secondary" tables are used for VPNs.
38 class SecondaryTableController {
41 SecondaryTableController(NetworkController* controller);
42 virtual ~SecondaryTableController();
44 // Add/remove a particular route in a particular interface's table.
45 int addRoute(SocketClient *cli, char *iface, char *dest, int prefixLen, char *gateway);
46 int removeRoute(SocketClient *cli, char *iface, char *dest, int prefixLen, char *gateway);
48 int modifyFromRule(unsigned netId, const char *action, const char *addr);
49 int modifyLocalRoute(unsigned netId, const char *action, const char *iface, const char *addr);
51 // Add/remove rules to force packets in a particular range of UIDs over a particular interface.
52 // This is accomplished with a rule specifying these UIDs use the interface's routing chain.
53 int addUidRule(const char *iface, int uid_start, int uid_end);
54 int removeUidRule(const char *iface, int uid_start, int uid_end);
56 // Add/remove rules and chains so packets intended for a particular interface use that
58 int addFwmarkRule(const char *iface);
59 int removeFwmarkRule(const char *iface);
61 // Add/remove rules so packets going to a particular range of IPs use a particular interface.
62 // This is accomplished by adding/removeing a rule to/from an interface’s chain to mark packets
63 // destined for the IP address range with the mark for the interface’s table.
64 int addFwmarkRoute(const char* iface, const char *dest, int prefix);
65 int removeFwmarkRoute(const char* iface, const char *dest, int prefix);
67 // Add/remove rules so packets going to a particular IP address use the main table (i.e. not
68 // the VPN tables). This is used in conjunction with adding a specific route to the main
69 // table. This is to support requestRouteToHost().
70 // This is accomplished by marking these packets with the protect mark and adding a rule to
71 // use the main table.
72 int addHostExemption(const char *host);
73 int removeHostExemption(const char *host);
75 void getUidMark(SocketClient *cli, int uid);
76 void getProtectMark(SocketClient *cli);
78 int setupIptablesHooks();
80 static const char* LOCAL_MANGLE_OUTPUT;
81 static const char* LOCAL_MANGLE_POSTROUTING;
82 static const char* LOCAL_NAT_POSTROUTING;
86 NetworkController *mNetCtrl;
88 int setUidRule(const char* iface, int uid_start, int uid_end, bool add);
89 int setFwmarkRule(const char *iface, bool add);
90 int setFwmarkRoute(const char* iface, const char *dest, int prefix, bool add);
91 int setHostExemption(const char *host, bool add);
92 int modifyRoute(SocketClient *cli, const char *action, char *iface, char *dest, int prefix,
93 char *gateway, unsigned netId);
95 std::map<unsigned, int> mNetIdRuleCount;
96 void modifyRuleCount(unsigned netId, const char *action);
97 const char *getVersion(const char *addr);
98 IptablesTarget getIptablesTarget(const char *addr);
100 int runCmd(int argc, const char **argv);