blob: 103e7cd65eb7ba6d615ef26e5fbb10e6174326d8 [file] [log] [blame]
JP Abgrall0031cea2012-04-17 16:38:23 -07001/*
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
JP Abgrall0031cea2012-04-17 16:38:23 -070017/*
18 * MODUS OPERANDI
19 * --------------
20 *
21 * IPTABLES command sequence:
22 *
23 * iptables -F
24 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070025 * iptables -t raw -F idletimer_PREROUTING
26 * iptables -t mangle -F idletimer_POSTROUTING
JP Abgrall0031cea2012-04-17 16:38:23 -070027 *
28 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070029 * iptables -t raw -N idletimer_PREROUTING
30 * iptables -t mangle -N idletimer_POSTROUTING
JP Abgrall0031cea2012-04-17 16:38:23 -070031 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070032 * iptables -t raw -D PREROUTING -j idletimer_PREROUTING
33 * iptables -t mangle -D POSTROUTING -j idletimer_POSTROUTING
JP Abgrall0031cea2012-04-17 16:38:23 -070034 *
35 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070036 * iptables -t raw -I PREROUTING -j idletimer_PREROUTING
37 * iptables -t mangle -I POSTROUTING -j idletimer_POSTROUTING
JP Abgrall0031cea2012-04-17 16:38:23 -070038 *
39 * # For notifications to work the lable name must match the name of a valid interface.
40 * # If the label name does match an interface, the rules will be a no-op.
41 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070042 * iptables -t raw -A idletimer_PREROUTING -i rmnet0 -j IDLETIMER --timeout 5 --label test-chain --send_nl_msg 1
43 * iptables -t mangle -A idletimer_POSTROUTING -o rmnet0 -j IDLETIMER --timeout 5 --label test-chain --send_nl_msg 1
JP Abgrall0031cea2012-04-17 16:38:23 -070044 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070045 * iptables -nxvL -t raw
46 * iptables -nxvL -t mangle
JP Abgrall0031cea2012-04-17 16:38:23 -070047 *
48 * =================
49 *
50 * ndc command sequence
51 * ------------------
52 * ndc idletimer enable
Haoyu Bai98f65d32012-06-28 16:16:51 -070053 * ndc idletimer add <iface> <timeout> <class label>
54 * ndc idletimer remove <iface> <timeout> <class label>
JP Abgrall0031cea2012-04-17 16:38:23 -070055 *
56 * Monitor effect on the iptables chains after each step using:
Haoyu Bai8c54ec52012-07-26 15:36:53 -070057 * iptables -nxvL -t raw
58 * iptables -nxvL -t mangle
JP Abgrall0031cea2012-04-17 16:38:23 -070059 *
60 * Remember that the timeout value has to be same at the time of the
61 * removal.
62 *
Haoyu Bai8c54ec52012-07-26 15:36:53 -070063 * =================
64 *
65 * Verifying the iptables rule
66 * ---------------------------
67 * We want to make sure the iptable rules capture every packet. It can be
68 * verified with tcpdump. First take a note of the pkts count for the two rules:
69 *
70 * adb shell iptables -t mangle -L idletimer_mangle_POSTROUTING -v && adb shell iptables -t raw -L idletimer_raw_PREROUTING -v
71 *
72 * And then, before any network traffics happen on the device, run tcpdump:
73 *
74 * adb shell tcpdump | tee tcpdump.log
75 *
76 * After a while run iptables commands again, you could then count the number
77 * of incoming and outgoing packets captured by tcpdump, and compare that with
78 * the numbers reported by iptables command. There shouldn't be too much
79 * difference on these numbers, i.e., with 2000 packets captured it should
80 * differ by less than 5.
81 *
82 * =================
83 *
JP Abgrall0031cea2012-04-17 16:38:23 -070084 * Note that currently if the name of the iface is incorrect, iptables
85 * will setup rules without checking if it is the name of a valid
86 * interface (although no notifications will ever be received). It is
87 * the responsibility of code in Java land to ensure that the interface name
88 * is correct. The benefit of this, is that idletimers can be setup on
89 * interfaces than come and go.
90 *
91 * A remove should be called for each add command issued during cleanup, as duplicate
92 * entries of the rule may exist and will all have to removed.
93 *
94 */
95
Ashish Sharma1c2c27f2014-02-03 17:28:04 -080096#define LOG_NDEBUG 0
97
Lorenzo Colittiad46e762017-08-10 19:44:34 +090098#include <string>
99#include <vector>
100
Lorenzo Colitti85a21602017-08-10 19:22:45 +0900101#include <stdint.h>
JP Abgrall0031cea2012-04-17 16:38:23 -0700102#include <stdlib.h>
103#include <errno.h>
104#include <sys/socket.h>
105#include <sys/stat.h>
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800106#include <sys/wait.h>
JP Abgrall0031cea2012-04-17 16:38:23 -0700107#include <fcntl.h>
108#include <netinet/in.h>
109#include <arpa/inet.h>
110#include <string.h>
111#include <cutils/properties.h>
112
Lorenzo Colittiad46e762017-08-10 19:44:34 +0900113#include <android-base/strings.h>
114#include <android-base/stringprintf.h>
115
JP Abgrall0031cea2012-04-17 16:38:23 -0700116#define LOG_TAG "IdletimerController"
Logan Chien3f461482018-04-23 14:31:32 +0800117#include <log/log.h>
JP Abgrall0031cea2012-04-17 16:38:23 -0700118
119#include "IdletimerController.h"
120#include "NetdConstants.h"
121
Lorenzo Colittiad46e762017-08-10 19:44:34 +0900122using android::base::Join;
123using android::base::StringPrintf;
124
Haoyu Bai8c54ec52012-07-26 15:36:53 -0700125const char* IdletimerController::LOCAL_RAW_PREROUTING = "idletimer_raw_PREROUTING";
126const char* IdletimerController::LOCAL_MANGLE_POSTROUTING = "idletimer_mangle_POSTROUTING";
Jeff Sharkey8e188ed2012-07-12 18:32:03 -0700127
Lorenzo Colittiad46e762017-08-10 19:44:34 +0900128auto IdletimerController::execIptablesRestore = ::execIptablesRestore;
Lorenzo Colitti85a21602017-08-10 19:22:45 +0900129
JP Abgrall0031cea2012-04-17 16:38:23 -0700130IdletimerController::IdletimerController() {
131}
132
133IdletimerController::~IdletimerController() {
134}
JP Abgrall0031cea2012-04-17 16:38:23 -0700135
136bool IdletimerController::setupIptablesHooks() {
JP Abgrall0031cea2012-04-17 16:38:23 -0700137 return true;
138}
139
JP Abgrall0031cea2012-04-17 16:38:23 -0700140int IdletimerController::modifyInterfaceIdletimer(IptOp op, const char *iface,
Haoyu Bai98f65d32012-06-28 16:16:51 -0700141 uint32_t timeout,
142 const char *classLabel) {
Lorenzo Colittiad46e762017-08-10 19:44:34 +0900143 if (!isIfaceName(iface)) {
144 errno = ENOENT;
145 return -1;
146 }
Rom Lemarchand001f0a42013-01-31 12:41:03 -0800147
Lorenzo Colittiad46e762017-08-10 19:44:34 +0900148 const char *addRemove = (op == IptOpAdd) ? "-A" : "-D";
149 std::vector<std::string> cmds = {
150 "*raw",
151 StringPrintf("%s %s -i %s -j IDLETIMER --timeout %u --label %s --send_nl_msg 1",
152 addRemove, LOCAL_RAW_PREROUTING, iface, timeout, classLabel),
153 "COMMIT",
154 "*mangle",
155 StringPrintf("%s %s -o %s -j IDLETIMER --timeout %u --label %s --send_nl_msg 1",
156 addRemove, LOCAL_MANGLE_POSTROUTING, iface, timeout, classLabel),
157 "COMMIT\n",
158 };
JP Abgrall69261cb2014-06-19 18:35:24 -0700159
Luke Huang0051a622018-07-23 20:30:16 +0800160 return (execIptablesRestore(V4V6, Join(cmds, '\n')) == 0) ? 0 : -EREMOTEIO;
JP Abgrall0031cea2012-04-17 16:38:23 -0700161}
162
Haoyu Bai98f65d32012-06-28 16:16:51 -0700163int IdletimerController::addInterfaceIdletimer(const char *iface,
164 uint32_t timeout,
165 const char *classLabel) {
Lorenzo Colittiad46e762017-08-10 19:44:34 +0900166 return modifyInterfaceIdletimer(IptOpAdd, iface, timeout, classLabel);
JP Abgrall0031cea2012-04-17 16:38:23 -0700167}
168
Haoyu Bai98f65d32012-06-28 16:16:51 -0700169int IdletimerController::removeInterfaceIdletimer(const char *iface,
170 uint32_t timeout,
171 const char *classLabel) {
Lorenzo Colittiad46e762017-08-10 19:44:34 +0900172 return modifyInterfaceIdletimer(IptOpDelete, iface, timeout, classLabel);
JP Abgrall0031cea2012-04-17 16:38:23 -0700173}