IdletimerController.cpp revision 8e188ed5c989ddcc07f0f5e9839493c22d17e7b6
1/*
2 * Copyright (C) 2012 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// #define LOG_NDEBUG 0
18
19/*
20 * MODUS OPERANDI
21 * --------------
22 *
23 * IPTABLES command sequence:
24 *
25 * iptables -F
26 *
27 * iptables -t nat -F idletimer_PREROUTING
28 * iptables -t nat -F idletimer_POSTROUTING
29 *
30 *
31 * iptables -t nat -N idletimer_PREROUTING
32 * iptables -t nat -N idletimer_POSTROUTING
33 *
34 * iptables -t nat -D PREROUTING -j idletimer_PREROUTING
35 * iptables -t nat -D POSTROUTING -j idletimer_POSTROUTING
36 *
37 *
38 * iptables -t nat -I PREROUTING -j idletimer_PREROUTING
39 * iptables -t nat -I POSTROUTING -j idletimer_POSTROUTING
40 *
41 * # For notifications to work the lable name must match the name of a valid interface.
42 * # If the label name does match an interface, the rules will be a no-op.
43 *
44 * iptables -t nat -A idletimer_PREROUTING -i rmnet0 -j IDLETIMER  --timeout 5 --label test-chain --send_nl_msg 1
45 * iptables -t nat -A idletimer_POSTROUTING -o rmnet0 -j IDLETIMER  --timeout 5 --label test-chain --send_nl_msg 1
46 *
47 * iptables -nxvL -t nat
48 *
49 * =================
50 *
51 * ndc command sequence
52 * ------------------
53 * ndc idletimer enable
54 * ndc idletimer add <iface> <timeout> <class label>
55 * ndc idletimer remove <iface> <timeout> <class label>
56 *
57 * Monitor effect on the iptables chains after each step using:
58 *     iptables -nxvL -t nat
59 *
60 * Remember that the timeout value has to be same at the time of the
61 * removal.
62 *
63 * Note that currently if the name of the iface is incorrect, iptables
64 * will setup rules without checking if it is the name of a valid
65 * interface (although no notifications will ever be received).  It is
66 * the responsibility of code in Java land to ensure that the interface name
67 * is correct. The benefit of this, is that idletimers can be setup on
68 * interfaces than come and go.
69 *
70 * A remove should be called for each add command issued during cleanup, as duplicate
71 * entries of the rule may exist and will all have to removed.
72 *
73 */
74
75#include <stdlib.h>
76#include <errno.h>
77#include <sys/socket.h>
78#include <sys/stat.h>
79#include <fcntl.h>
80#include <netinet/in.h>
81#include <arpa/inet.h>
82#include <string.h>
83#include <cutils/properties.h>
84
85#define LOG_TAG "IdletimerController"
86#include <cutils/log.h>
87
88#include "IdletimerController.h"
89#include "NetdConstants.h"
90
91extern "C" int system_nosh(const char *command);
92
93const char* IdletimerController::LOCAL_NAT_PREROUTING = "idletimer_nat_PREROUTING";
94const char* IdletimerController::LOCAL_NAT_POSTROUTING = "idletimer_nat_POSTROUTING";
95
96IdletimerController::IdletimerController() {
97}
98
99IdletimerController::~IdletimerController() {
100}
101/* return 0 or non-zero */
102int IdletimerController::runIpxtablesCmd(const char *cmd) {
103    char *buffer;
104    size_t len = strnlen(cmd, 255);
105    int res;
106
107    if (len == 255) {
108        ALOGE("command too long");
109        return -1;
110    }
111
112    asprintf(&buffer, "%s %s", IPTABLES_PATH, cmd);
113    res = system_nosh(buffer);
114    ALOGV("%s #%d", buffer, res);
115    free(buffer);
116
117    return res;
118}
119
120bool IdletimerController::setupIptablesHooks() {
121    return true;
122}
123
124int IdletimerController::setDefaults() {
125  if (runIpxtablesCmd("-t nat -F idletimer_nat_PREROUTING")
126      || runIpxtablesCmd("-t nat -F idletimer_nat_POSTROUTING") )
127      return -1;
128  return 0;
129}
130
131int IdletimerController::enableIdletimerControl() {
132    int res = setDefaults();
133    return res;
134}
135
136int IdletimerController::disableIdletimerControl() {
137    int res = setDefaults();
138    return res;
139}
140
141int IdletimerController::modifyInterfaceIdletimer(IptOp op, const char *iface,
142                                                  uint32_t timeout,
143                                                  const char *classLabel) {
144  int res;
145  char *buffer;
146  asprintf(&buffer, "-t nat -%c idletimer_nat_PREROUTING -i %s -j IDLETIMER"
147           " --timeout %u --label %s --send_nl_msg 1",
148           (op == IptOpAdd) ? 'A' : 'D', iface, timeout, classLabel);
149  res = runIpxtablesCmd(buffer);
150  free(buffer);
151
152  asprintf(&buffer, "-t nat -%c idletimer_nat_POSTROUTING -o %s -j IDLETIMER"
153           " --timeout %u --label %s --send_nl_msg 1",
154           (op == IptOpAdd) ? 'A' : 'D', iface, timeout, classLabel);
155  res |= runIpxtablesCmd(buffer);
156  free(buffer);
157
158  return res;
159}
160
161int IdletimerController::addInterfaceIdletimer(const char *iface,
162                                               uint32_t timeout,
163                                               const char *classLabel) {
164  return modifyInterfaceIdletimer(IptOpAdd, iface, timeout, classLabel);
165}
166
167int IdletimerController::removeInterfaceIdletimer(const char *iface,
168                                                  uint32_t timeout,
169                                                  const char *classLabel) {
170  return modifyInterfaceIdletimer(IptOpDelete, iface, timeout, classLabel);
171}
172