Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 20 | #include <map> |
| 21 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 22 | #include <sysutils/FrameworkListener.h> |
| 23 | |
Elliott Hughes | 970274a | 2012-09-11 18:56:36 -0700 | [diff] [blame] | 24 | #include <net/if.h> |
Chad Brubaker | 2251c0f | 2013-06-27 17:20:39 -0700 | [diff] [blame] | 25 | #include "NetdConstants.h" |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 26 | #include "NetworkController.h" |
Jaime A Lopez-Sollano | d14fd4f | 2012-01-11 16:29:28 -0800 | [diff] [blame] | 27 | |
| 28 | #ifndef IFNAMSIZ |
| 29 | #define IFNAMSIZ 16 |
| 30 | #endif |
| 31 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 32 | static const int BASE_TABLE_NUMBER = 60; |
Chad Brubaker | 2349aa6 | 2013-07-15 15:28:59 -0700 | [diff] [blame] | 33 | static const char *EXEMPT_PRIO = "99"; |
| 34 | static const char *RULE_PRIO = "100"; |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 35 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 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. |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 38 | class SecondaryTableController { |
| 39 | |
| 40 | public: |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 41 | SecondaryTableController(NetworkController* controller); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 42 | virtual ~SecondaryTableController(); |
| 43 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 44 | // Add/remove a particular route in a particular interface's table. |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 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); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 47 | |
| 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 Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 51 | // Add/remove rules and chains so packets intended for a particular interface use that |
| 52 | // interface. |
Chad Brubaker | 7a6ce4b | 2013-06-06 21:42:53 -0700 | [diff] [blame] | 53 | int addFwmarkRule(const char *iface); |
| 54 | int removeFwmarkRule(const char *iface); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 55 | |
| 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 Brubaker | 2251c0f | 2013-06-27 17:20:39 -0700 | [diff] [blame] | 59 | int addFwmarkRoute(const char* iface, const char *dest, int prefix); |
| 60 | int removeFwmarkRoute(const char* iface, const char *dest, int prefix); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 61 | |
| 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 Brubaker | 4a94609 | 2013-07-10 12:08:08 -0700 | [diff] [blame] | 67 | int addHostExemption(const char *host); |
| 68 | int removeHostExemption(const char *host); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 69 | |
Chad Brubaker | da7df7c | 2013-07-11 12:05:39 -0700 | [diff] [blame] | 70 | void getUidMark(SocketClient *cli, int uid); |
| 71 | void getProtectMark(SocketClient *cli); |
Chad Brubaker | 2251c0f | 2013-06-27 17:20:39 -0700 | [diff] [blame] | 72 | |
| 73 | int setupIptablesHooks(); |
Chad Brubaker | 9a50889 | 2013-05-31 20:51:46 -0700 | [diff] [blame] | 74 | |
| 75 | static const char* LOCAL_MANGLE_OUTPUT; |
JP Abgrall | 9440e7f | 2013-11-20 17:27:01 -0800 | [diff] [blame] | 76 | static const char* LOCAL_MANGLE_POSTROUTING; |
Chad Brubaker | 7a6ce4b | 2013-06-06 21:42:53 -0700 | [diff] [blame] | 77 | static const char* LOCAL_NAT_POSTROUTING; |
Chad Brubaker | 9a50889 | 2013-05-31 20:51:46 -0700 | [diff] [blame] | 78 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 79 | |
| 80 | private: |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 81 | NetworkController *mNetCtrl; |
Chad Brubaker | d261793 | 2013-06-21 15:26:35 -0700 | [diff] [blame] | 82 | |
Chad Brubaker | 7a6ce4b | 2013-06-06 21:42:53 -0700 | [diff] [blame] | 83 | int setFwmarkRule(const char *iface, bool add); |
Chad Brubaker | 2251c0f | 2013-06-27 17:20:39 -0700 | [diff] [blame] | 84 | int setFwmarkRoute(const char* iface, const char *dest, int prefix, bool add); |
Chad Brubaker | 4a94609 | 2013-07-10 12:08:08 -0700 | [diff] [blame] | 85 | int setHostExemption(const char *host, bool add); |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 86 | int modifyRoute(SocketClient *cli, const char *action, char *iface, char *dest, int prefix, |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 87 | char *gateway, unsigned netId); |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 88 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame] | 89 | std::map<unsigned, int> mNetIdRuleCount; |
| 90 | void modifyRuleCount(unsigned netId, const char *action); |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 91 | const char *getVersion(const char *addr); |
Chad Brubaker | 2251c0f | 2013-06-27 17:20:39 -0700 | [diff] [blame] | 92 | IptablesTarget getIptablesTarget(const char *addr); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 93 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 94 | int runCmd(int argc, const char **argv); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | #endif |