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 int PROTECT_MARK = 0x1;
34static const char *EXEMPT_PRIO = "99";
35static const char *RULE_PRIO = "100";
36
37class SecondaryTableController {
38
39public:
40    SecondaryTableController(UidMarkMap *map);
41    virtual ~SecondaryTableController();
42
43    int addRoute(SocketClient *cli, char *iface, char *dest, int prefixLen, char *gateway);
44    int removeRoute(SocketClient *cli, char *iface, char *dest, int prefixLen, char *gateway);
45    int findTableNumber(const char *iface);
46    int modifyFromRule(int tableIndex, const char *action, const char *addr);
47    int modifyLocalRoute(int tableIndex, const char *action, const char *iface, const char *addr);
48    int addUidRule(const char *iface, int uid_start, int uid_end);
49    int removeUidRule(const char *iface, int uid_start, int uid_end);
50    int addFwmarkRule(const char *iface);
51    int removeFwmarkRule(const char *iface);
52    int addFwmarkRoute(const char* iface, const char *dest, int prefix);
53    int removeFwmarkRoute(const char* iface, const char *dest, int prefix);
54    int addHostExemption(const char *host);
55    int removeHostExemption(const char *host);
56    void getUidMark(SocketClient *cli, int uid);
57    void getProtectMark(SocketClient *cli);
58
59    int setupIptablesHooks();
60
61    static const char* LOCAL_MANGLE_OUTPUT;
62    static const char* LOCAL_MANGLE_POSTROUTING;
63    static const char* LOCAL_MANGLE_EXEMPT;
64    static const char* LOCAL_MANGLE_IFACE_FORMAT;
65    static const char* LOCAL_NAT_POSTROUTING;
66    static const char* LOCAL_FILTER_OUTPUT;
67
68
69private:
70    UidMarkMap *mUidMarkMap;
71
72    int setUidRule(const char* iface, int uid_start, int uid_end, bool add);
73    int setFwmarkRule(const char *iface, bool add);
74    int setFwmarkRoute(const char* iface, const char *dest, int prefix, bool add);
75    int setHostExemption(const char *host, bool add);
76    int modifyRoute(SocketClient *cli, const char *action, char *iface, char *dest, int prefix,
77            char *gateway, int tableIndex);
78
79    char mInterfaceTable[INTERFACES_TRACKED][IFNAMSIZ + 1];
80    int mInterfaceRuleCount[INTERFACES_TRACKED];
81    void modifyRuleCount(int tableIndex, const char *action);
82    int verifyTableIndex(int tableIndex);
83    const char *getVersion(const char *addr);
84    IptablesTarget getIptablesTarget(const char *addr);
85
86    int runCmd(int argc, const char **argv);
87};
88
89#endif
90