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
20#include <sysutils/FrameworkListener.h>
21
22#include <net/if.h>
23#include "UidMarkMap.h"
24#include "NetdConstants.h"
25
26#ifndef IFNAMSIZ
27#define IFNAMSIZ 16
28#endif
29
30static const int INTERFACES_TRACKED = 10;
31static const int BASE_TABLE_NUMBER = 60;
32static int MAX_TABLE_NUMBER = BASE_TABLE_NUMBER + INTERFACES_TRACKED;
33static const char *EXEMPT_PRIO = "99";
34static const char *RULE_PRIO = "100";
35
36class SecondaryTableController {
37
38public:
39    SecondaryTableController(UidMarkMap *map);
40    virtual ~SecondaryTableController();
41
42    int addRoute(SocketClient *cli, char *iface, char *dest, int prefixLen, char *gateway);
43    int removeRoute(SocketClient *cli, char *iface, char *dest, int prefixLen, char *gateway);
44    int findTableNumber(const char *iface);
45    int modifyFromRule(int tableIndex, const char *action, const char *addr);
46    int modifyLocalRoute(int tableIndex, const char *action, const char *iface, const char *addr);
47    int addUidRule(const char *iface, int uid_start, int uid_end);
48    int removeUidRule(const char *iface, int uid_start, int uid_end);
49    int addFwmarkRule(const char *iface);
50    int removeFwmarkRule(const char *iface);
51    int addFwmarkRoute(const char* iface, const char *dest, int prefix);
52    int removeFwmarkRoute(const char* iface, const char *dest, int prefix);
53    int addHostExemption(const char *host);
54    int removeHostExemption(const char *host);
55    void getUidMark(SocketClient *cli, int uid);
56    void getProtectMark(SocketClient *cli);
57
58    int setupIptablesHooks();
59
60    static const char* LOCAL_MANGLE_OUTPUT;
61    static const char* LOCAL_MANGLE_POSTROUTING;
62    static const char* LOCAL_NAT_POSTROUTING;
63
64
65private:
66    UidMarkMap *mUidMarkMap;
67
68    int setUidRule(const char* iface, int uid_start, int uid_end, bool add);
69    int setFwmarkRule(const char *iface, bool add);
70    int setFwmarkRoute(const char* iface, const char *dest, int prefix, bool add);
71    int setHostExemption(const char *host, bool add);
72    int modifyRoute(SocketClient *cli, const char *action, char *iface, char *dest, int prefix,
73            char *gateway, int tableIndex);
74
75    char mInterfaceTable[INTERFACES_TRACKED][IFNAMSIZ + 1];
76    int mInterfaceRuleCount[INTERFACES_TRACKED];
77    void modifyRuleCount(int tableIndex, const char *action);
78    int verifyTableIndex(int tableIndex);
79    const char *getVersion(const char *addr);
80    IptablesTarget getIptablesTarget(const char *addr);
81
82    int runCmd(int argc, const char **argv);
83};
84
85#endif
86