blob: 091f95e45fe13aea760f5c0e15975127a974d031 [file] [log] [blame]
Robert Greenwaltfc97b822011-11-02 16:48:36 -07001/*
2 * Copyright (C) 2008 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 _SECONDARY_TABLE_CONTROLLER_H
18#define _SECONDARY_TABLE_CONTROLLER_H
19
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050020#include <map>
21
Robert Greenwaltfc97b822011-11-02 16:48:36 -070022#include <sysutils/FrameworkListener.h>
23
Elliott Hughes970274a2012-09-11 18:56:36 -070024#include <net/if.h>
Chad Brubaker2251c0f2013-06-27 17:20:39 -070025#include "NetdConstants.h"
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050026#include "NetworkController.h"
Jaime A Lopez-Sollanod14fd4f2012-01-11 16:29:28 -080027
28#ifndef IFNAMSIZ
29#define IFNAMSIZ 16
30#endif
31
Robert Greenwaltfc97b822011-11-02 16:48:36 -070032static const int BASE_TABLE_NUMBER = 60;
Chad Brubaker2251c0f2013-06-27 17:20:39 -070033static const int PROTECT_MARK = 0x1;
Chad Brubaker2349aa62013-07-15 15:28:59 -070034static const char *EXEMPT_PRIO = "99";
35static const char *RULE_PRIO = "100";
Robert Greenwaltfc97b822011-11-02 16:48:36 -070036
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050037// SecondaryTableController is responsible for maintaining the "secondary" routing tables, where
38// "secondary" means not the main table. The "secondary" tables are used for VPNs.
Robert Greenwaltfc97b822011-11-02 16:48:36 -070039class SecondaryTableController {
40
41public:
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050042 SecondaryTableController(NetworkController* controller);
Robert Greenwaltfc97b822011-11-02 16:48:36 -070043 virtual ~SecondaryTableController();
44
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050045 // Add/remove a particular route in a particular interface's table.
Robert Greenwaltfc97b822011-11-02 16:48:36 -070046 int addRoute(SocketClient *cli, char *iface, char *dest, int prefixLen, char *gateway);
47 int removeRoute(SocketClient *cli, char *iface, char *dest, int prefixLen, char *gateway);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050048
49 int modifyFromRule(unsigned netId, const char *action, const char *addr);
50 int modifyLocalRoute(unsigned netId, const char *action, const char *iface, const char *addr);
51
52 // Add/remove rules to force packets in a particular range of UIDs over a particular interface.
53 // This is accomplished with a rule specifying these UIDs use the interface's routing chain.
Chad Brubaker8830b942013-06-12 10:51:55 -070054 int addUidRule(const char *iface, int uid_start, int uid_end);
55 int removeUidRule(const char *iface, int uid_start, int uid_end);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050056
57 // Add/remove rules and chains so packets intended for a particular interface use that
58 // interface.
Chad Brubaker7a6ce4b2013-06-06 21:42:53 -070059 int addFwmarkRule(const char *iface);
60 int removeFwmarkRule(const char *iface);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050061
62 // Add/remove rules so packets going to a particular range of IPs use a particular interface.
63 // This is accomplished by adding/removeing a rule to/from an interface’s chain to mark packets
64 // destined for the IP address range with the mark for the interface’s table.
Chad Brubaker2251c0f2013-06-27 17:20:39 -070065 int addFwmarkRoute(const char* iface, const char *dest, int prefix);
66 int removeFwmarkRoute(const char* iface, const char *dest, int prefix);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050067
68 // Add/remove rules so packets going to a particular IP address use the main table (i.e. not
69 // the VPN tables). This is used in conjunction with adding a specific route to the main
70 // table. This is to support requestRouteToHost().
71 // This is accomplished by marking these packets with the protect mark and adding a rule to
72 // use the main table.
Chad Brubaker4a946092013-07-10 12:08:08 -070073 int addHostExemption(const char *host);
74 int removeHostExemption(const char *host);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050075
Chad Brubakerda7df7c2013-07-11 12:05:39 -070076 void getUidMark(SocketClient *cli, int uid);
77 void getProtectMark(SocketClient *cli);
Chad Brubaker2251c0f2013-06-27 17:20:39 -070078
79 int setupIptablesHooks();
Chad Brubaker9a508892013-05-31 20:51:46 -070080
81 static const char* LOCAL_MANGLE_OUTPUT;
Chad Brubaker4a946092013-07-10 12:08:08 -070082 static const char* LOCAL_MANGLE_EXEMPT;
Chad Brubaker2251c0f2013-06-27 17:20:39 -070083 static const char* LOCAL_MANGLE_IFACE_FORMAT;
Chad Brubaker7a6ce4b2013-06-06 21:42:53 -070084 static const char* LOCAL_NAT_POSTROUTING;
Chad Brubaker2251c0f2013-06-27 17:20:39 -070085 static const char* LOCAL_FILTER_OUTPUT;
Chad Brubaker9a508892013-05-31 20:51:46 -070086
Robert Greenwaltfc97b822011-11-02 16:48:36 -070087
88private:
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050089 NetworkController *mNetCtrl;
Chad Brubakerd2617932013-06-21 15:26:35 -070090
Chad Brubaker8830b942013-06-12 10:51:55 -070091 int setUidRule(const char* iface, int uid_start, int uid_end, bool add);
Chad Brubaker7a6ce4b2013-06-06 21:42:53 -070092 int setFwmarkRule(const char *iface, bool add);
Chad Brubaker2251c0f2013-06-27 17:20:39 -070093 int setFwmarkRoute(const char* iface, const char *dest, int prefix, bool add);
Chad Brubaker4a946092013-07-10 12:08:08 -070094 int setHostExemption(const char *host, bool add);
Robert Greenwaltc4621772012-01-31 12:46:45 -080095 int modifyRoute(SocketClient *cli, const char *action, char *iface, char *dest, int prefix,
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050096 char *gateway, unsigned netId);
Robert Greenwalt063af322011-11-18 15:32:13 -080097
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050098 std::map<unsigned, int> mNetIdRuleCount;
99 void modifyRuleCount(unsigned netId, const char *action);
Robert Greenwaltc4621772012-01-31 12:46:45 -0800100 const char *getVersion(const char *addr);
Chad Brubaker2251c0f2013-06-27 17:20:39 -0700101 IptablesTarget getIptablesTarget(const char *addr);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700102
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800103 int runCmd(int argc, const char **argv);
Robert Greenwaltfc97b822011-11-02 16:48:36 -0700104};
105
106#endif