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 | 2251c0f | 2013-06-27 17:20:39 -0700 | [diff] [blame] | 33 | static const int PROTECT_MARK = 0x1; |
Chad Brubaker | 2349aa6 | 2013-07-15 15:28:59 -0700 | [diff] [blame] | 34 | static const char *EXEMPT_PRIO = "99"; |
| 35 | static const char *RULE_PRIO = "100"; |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 36 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame^] | 37 | // 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 Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 39 | class SecondaryTableController { |
| 40 | |
| 41 | public: |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame^] | 42 | SecondaryTableController(NetworkController* controller); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 43 | virtual ~SecondaryTableController(); |
| 44 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame^] | 45 | // Add/remove a particular route in a particular interface's table. |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 46 | 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 Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame^] | 48 | |
| 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 Brubaker | 8830b94 | 2013-06-12 10:51:55 -0700 | [diff] [blame] | 54 | int addUidRule(const char *iface, int uid_start, int uid_end); |
| 55 | int removeUidRule(const char *iface, int uid_start, int uid_end); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame^] | 56 | |
| 57 | // Add/remove rules and chains so packets intended for a particular interface use that |
| 58 | // interface. |
Chad Brubaker | 7a6ce4b | 2013-06-06 21:42:53 -0700 | [diff] [blame] | 59 | int addFwmarkRule(const char *iface); |
| 60 | int removeFwmarkRule(const char *iface); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame^] | 61 | |
| 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 Brubaker | 2251c0f | 2013-06-27 17:20:39 -0700 | [diff] [blame] | 65 | int addFwmarkRoute(const char* iface, const char *dest, int prefix); |
| 66 | int removeFwmarkRoute(const char* iface, const char *dest, int prefix); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame^] | 67 | |
| 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 Brubaker | 4a94609 | 2013-07-10 12:08:08 -0700 | [diff] [blame] | 73 | int addHostExemption(const char *host); |
| 74 | int removeHostExemption(const char *host); |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame^] | 75 | |
Chad Brubaker | da7df7c | 2013-07-11 12:05:39 -0700 | [diff] [blame] | 76 | void getUidMark(SocketClient *cli, int uid); |
| 77 | void getProtectMark(SocketClient *cli); |
Chad Brubaker | 2251c0f | 2013-06-27 17:20:39 -0700 | [diff] [blame] | 78 | |
| 79 | int setupIptablesHooks(); |
Chad Brubaker | 9a50889 | 2013-05-31 20:51:46 -0700 | [diff] [blame] | 80 | |
| 81 | static const char* LOCAL_MANGLE_OUTPUT; |
Chad Brubaker | 4a94609 | 2013-07-10 12:08:08 -0700 | [diff] [blame] | 82 | static const char* LOCAL_MANGLE_EXEMPT; |
Chad Brubaker | 2251c0f | 2013-06-27 17:20:39 -0700 | [diff] [blame] | 83 | static const char* LOCAL_MANGLE_IFACE_FORMAT; |
Chad Brubaker | 7a6ce4b | 2013-06-06 21:42:53 -0700 | [diff] [blame] | 84 | static const char* LOCAL_NAT_POSTROUTING; |
Chad Brubaker | 2251c0f | 2013-06-27 17:20:39 -0700 | [diff] [blame] | 85 | static const char* LOCAL_FILTER_OUTPUT; |
Chad Brubaker | 9a50889 | 2013-05-31 20:51:46 -0700 | [diff] [blame] | 86 | |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 87 | |
| 88 | private: |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame^] | 89 | NetworkController *mNetCtrl; |
Chad Brubaker | d261793 | 2013-06-21 15:26:35 -0700 | [diff] [blame] | 90 | |
Chad Brubaker | 8830b94 | 2013-06-12 10:51:55 -0700 | [diff] [blame] | 91 | int setUidRule(const char* iface, int uid_start, int uid_end, bool add); |
Chad Brubaker | 7a6ce4b | 2013-06-06 21:42:53 -0700 | [diff] [blame] | 92 | int setFwmarkRule(const char *iface, bool add); |
Chad Brubaker | 2251c0f | 2013-06-27 17:20:39 -0700 | [diff] [blame] | 93 | int setFwmarkRoute(const char* iface, const char *dest, int prefix, bool add); |
Chad Brubaker | 4a94609 | 2013-07-10 12:08:08 -0700 | [diff] [blame] | 94 | int setHostExemption(const char *host, bool add); |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 95 | 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^] | 96 | char *gateway, unsigned netId); |
Robert Greenwalt | 063af32 | 2011-11-18 15:32:13 -0800 | [diff] [blame] | 97 | |
Szymon Jakubczak | a0efaec | 2014-02-14 17:09:43 -0500 | [diff] [blame^] | 98 | std::map<unsigned, int> mNetIdRuleCount; |
| 99 | void modifyRuleCount(unsigned netId, const char *action); |
Robert Greenwalt | c462177 | 2012-01-31 12:46:45 -0800 | [diff] [blame] | 100 | const char *getVersion(const char *addr); |
Chad Brubaker | 2251c0f | 2013-06-27 17:20:39 -0700 | [diff] [blame] | 101 | IptablesTarget getIptablesTarget(const char *addr); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 102 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 103 | int runCmd(int argc, const char **argv); |
Robert Greenwalt | fc97b82 | 2011-11-02 16:48:36 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | #endif |