blob: 84995fa3d9e1edd15cc509d976c5870eaf384fcf [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
20#include <sysutils/FrameworkListener.h>
21
Elliott Hughes970274a2012-09-11 18:56:36 -070022#include <net/if.h>
Chad Brubakerd2617932013-06-21 15:26:35 -070023#include "UidMarkMap.h"
Chad Brubaker2251c0f2013-06-27 17:20:39 -070024#include "NetdConstants.h"
Jaime A Lopez-Sollanod14fd4f2012-01-11 16:29:28 -080025
26#ifndef IFNAMSIZ
27#define IFNAMSIZ 16
28#endif
29
Robert Greenwaltfc97b822011-11-02 16:48:36 -070030static const int INTERFACES_TRACKED = 10;
31static const int BASE_TABLE_NUMBER = 60;
32static int MAX_TABLE_NUMBER = BASE_TABLE_NUMBER + INTERFACES_TRACKED;
Chad Brubaker2251c0f2013-06-27 17:20:39 -070033static const int PROTECT_MARK = 0x1;
Robert Greenwaltfc97b822011-11-02 16:48:36 -070034
35class SecondaryTableController {
36
37public:
Chad Brubakerd2617932013-06-21 15:26:35 -070038 SecondaryTableController(UidMarkMap *map);
Robert Greenwaltfc97b822011-11-02 16:48:36 -070039 virtual ~SecondaryTableController();
40
41 int addRoute(SocketClient *cli, char *iface, char *dest, int prefixLen, char *gateway);
42 int removeRoute(SocketClient *cli, char *iface, char *dest, int prefixLen, char *gateway);
43 int findTableNumber(const char *iface);
Robert Greenwaltc4621772012-01-31 12:46:45 -080044 int modifyFromRule(int tableIndex, const char *action, const char *addr);
45 int modifyLocalRoute(int tableIndex, const char *action, const char *iface, const char *addr);
Chad Brubaker8830b942013-06-12 10:51:55 -070046 int addUidRule(const char *iface, int uid_start, int uid_end);
47 int removeUidRule(const char *iface, int uid_start, int uid_end);
Chad Brubaker7a6ce4b2013-06-06 21:42:53 -070048 int addFwmarkRule(const char *iface);
49 int removeFwmarkRule(const char *iface);
Chad Brubaker2251c0f2013-06-27 17:20:39 -070050 int addFwmarkRoute(const char* iface, const char *dest, int prefix);
51 int removeFwmarkRoute(const char* iface, const char *dest, int prefix);
52
53 int setupIptablesHooks();
Chad Brubaker9a508892013-05-31 20:51:46 -070054
55 static const char* LOCAL_MANGLE_OUTPUT;
Chad Brubaker2251c0f2013-06-27 17:20:39 -070056 static const char* LOCAL_MANGLE_IFACE_FORMAT;
Chad Brubaker7a6ce4b2013-06-06 21:42:53 -070057 static const char* LOCAL_NAT_POSTROUTING;
Chad Brubaker2251c0f2013-06-27 17:20:39 -070058 static const char* LOCAL_FILTER_OUTPUT;
Chad Brubaker9a508892013-05-31 20:51:46 -070059
Robert Greenwaltfc97b822011-11-02 16:48:36 -070060
61private:
Chad Brubakerd2617932013-06-21 15:26:35 -070062 UidMarkMap *mUidMarkMap;
63
Chad Brubaker8830b942013-06-12 10:51:55 -070064 int setUidRule(const char* iface, int uid_start, int uid_end, bool add);
Chad Brubaker7a6ce4b2013-06-06 21:42:53 -070065 int setFwmarkRule(const char *iface, bool add);
Chad Brubaker2251c0f2013-06-27 17:20:39 -070066 int setFwmarkRoute(const char* iface, const char *dest, int prefix, bool add);
Robert Greenwaltc4621772012-01-31 12:46:45 -080067 int modifyRoute(SocketClient *cli, const char *action, char *iface, char *dest, int prefix,
Robert Greenwalt063af322011-11-18 15:32:13 -080068 char *gateway, int tableIndex);
69
Jaime A Lopez-Sollanod14fd4f2012-01-11 16:29:28 -080070 char mInterfaceTable[INTERFACES_TRACKED][IFNAMSIZ + 1];
Robert Greenwaltfc97b822011-11-02 16:48:36 -070071 int mInterfaceRuleCount[INTERFACES_TRACKED];
Robert Greenwaltc4621772012-01-31 12:46:45 -080072 void modifyRuleCount(int tableIndex, const char *action);
73 int verifyTableIndex(int tableIndex);
74 const char *getVersion(const char *addr);
Chad Brubaker2251c0f2013-06-27 17:20:39 -070075 IptablesTarget getIptablesTarget(const char *addr);
Robert Greenwaltfc97b822011-11-02 16:48:36 -070076
Rom Lemarchand001f0a42013-01-31 12:41:03 -080077 int runCmd(int argc, const char **argv);
Robert Greenwaltfc97b822011-11-02 16:48:36 -070078};
79
80#endif