blob: b2cc36a564a5d3143daf1638f24f1db04f83e528 [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 Brubaker2349aa62013-07-15 15:28:59 -070033static const char *EXEMPT_PRIO = "99";
34static const char *RULE_PRIO = "100";
Robert Greenwaltfc97b822011-11-02 16:48:36 -070035
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050036// SecondaryTableController is responsible for maintaining the "secondary" routing tables, where
37// "secondary" means not the main table. The "secondary" tables are used for VPNs.
Robert Greenwaltfc97b822011-11-02 16:48:36 -070038class SecondaryTableController {
39
40public:
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050041 SecondaryTableController(NetworkController* controller);
Robert Greenwaltfc97b822011-11-02 16:48:36 -070042 virtual ~SecondaryTableController();
43
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050044 // Add/remove a particular route in a particular interface's table.
Robert Greenwaltfc97b822011-11-02 16:48:36 -070045 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);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050047
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);
50
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050051 // Add/remove rules and chains so packets intended for a particular interface use that
52 // interface.
Chad Brubaker7a6ce4b2013-06-06 21:42:53 -070053 int addFwmarkRule(const char *iface);
54 int removeFwmarkRule(const char *iface);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050055
56 // Add/remove rules so packets going to a particular range of IPs use a particular interface.
57 // This is accomplished by adding/removeing a rule to/from an interface’s chain to mark packets
58 // destined for the IP address range with the mark for the interface’s table.
Chad Brubaker2251c0f2013-06-27 17:20:39 -070059 int addFwmarkRoute(const char* iface, const char *dest, int prefix);
60 int removeFwmarkRoute(const char* iface, const char *dest, int prefix);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050061
62 // Add/remove rules so packets going to a particular IP address use the main table (i.e. not
63 // the VPN tables). This is used in conjunction with adding a specific route to the main
64 // table. This is to support requestRouteToHost().
65 // This is accomplished by marking these packets with the protect mark and adding a rule to
66 // use the main table.
Chad Brubaker4a946092013-07-10 12:08:08 -070067 int addHostExemption(const char *host);
68 int removeHostExemption(const char *host);
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050069
Chad Brubakerda7df7c2013-07-11 12:05:39 -070070 void getUidMark(SocketClient *cli, int uid);
71 void getProtectMark(SocketClient *cli);
Chad Brubaker2251c0f2013-06-27 17:20:39 -070072
73 int setupIptablesHooks();
Chad Brubaker9a508892013-05-31 20:51:46 -070074
75 static const char* LOCAL_MANGLE_OUTPUT;
JP Abgrall9440e7f2013-11-20 17:27:01 -080076 static const char* LOCAL_MANGLE_POSTROUTING;
Chad Brubaker7a6ce4b2013-06-06 21:42:53 -070077 static const char* LOCAL_NAT_POSTROUTING;
Chad Brubaker9a508892013-05-31 20:51:46 -070078
Robert Greenwaltfc97b822011-11-02 16:48:36 -070079
80private:
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050081 NetworkController *mNetCtrl;
Chad Brubakerd2617932013-06-21 15:26:35 -070082
Chad Brubaker7a6ce4b2013-06-06 21:42:53 -070083 int setFwmarkRule(const char *iface, bool add);
Chad Brubaker2251c0f2013-06-27 17:20:39 -070084 int setFwmarkRoute(const char* iface, const char *dest, int prefix, bool add);
Chad Brubaker4a946092013-07-10 12:08:08 -070085 int setHostExemption(const char *host, bool add);
Robert Greenwaltc4621772012-01-31 12:46:45 -080086 int modifyRoute(SocketClient *cli, const char *action, char *iface, char *dest, int prefix,
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050087 char *gateway, unsigned netId);
Robert Greenwalt063af322011-11-18 15:32:13 -080088
Szymon Jakubczaka0efaec2014-02-14 17:09:43 -050089 std::map<unsigned, int> mNetIdRuleCount;
90 void modifyRuleCount(unsigned netId, const char *action);
Robert Greenwaltc4621772012-01-31 12:46:45 -080091 const char *getVersion(const char *addr);
Chad Brubaker2251c0f2013-06-27 17:20:39 -070092 IptablesTarget getIptablesTarget(const char *addr);
Robert Greenwaltfc97b822011-11-02 16:48:36 -070093
Rom Lemarchand001f0a42013-01-31 12:41:03 -080094 int runCmd(int argc, const char **argv);
Robert Greenwaltfc97b822011-11-02 16:48:36 -070095};
96
97#endif