JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 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 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 17 | /* |
| 18 | * MODUS OPERANDI |
| 19 | * -------------- |
| 20 | * |
| 21 | * IPTABLES command sequence: |
| 22 | * |
| 23 | * iptables -F |
| 24 | * |
Haoyu Bai | 8c54ec5 | 2012-07-26 15:36:53 -0700 | [diff] [blame] | 25 | * iptables -t raw -F idletimer_PREROUTING |
| 26 | * iptables -t mangle -F idletimer_POSTROUTING |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 27 | * |
| 28 | * |
Haoyu Bai | 8c54ec5 | 2012-07-26 15:36:53 -0700 | [diff] [blame] | 29 | * iptables -t raw -N idletimer_PREROUTING |
| 30 | * iptables -t mangle -N idletimer_POSTROUTING |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 31 | * |
Haoyu Bai | 8c54ec5 | 2012-07-26 15:36:53 -0700 | [diff] [blame] | 32 | * iptables -t raw -D PREROUTING -j idletimer_PREROUTING |
| 33 | * iptables -t mangle -D POSTROUTING -j idletimer_POSTROUTING |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 34 | * |
| 35 | * |
Haoyu Bai | 8c54ec5 | 2012-07-26 15:36:53 -0700 | [diff] [blame] | 36 | * iptables -t raw -I PREROUTING -j idletimer_PREROUTING |
| 37 | * iptables -t mangle -I POSTROUTING -j idletimer_POSTROUTING |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 38 | * |
| 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 Bai | 8c54ec5 | 2012-07-26 15:36:53 -0700 | [diff] [blame] | 42 | * 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 Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 44 | * |
Haoyu Bai | 8c54ec5 | 2012-07-26 15:36:53 -0700 | [diff] [blame] | 45 | * iptables -nxvL -t raw |
| 46 | * iptables -nxvL -t mangle |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 47 | * |
| 48 | * ================= |
| 49 | * |
| 50 | * ndc command sequence |
| 51 | * ------------------ |
| 52 | * ndc idletimer enable |
Haoyu Bai | 98f65d3 | 2012-06-28 16:16:51 -0700 | [diff] [blame] | 53 | * ndc idletimer add <iface> <timeout> <class label> |
| 54 | * ndc idletimer remove <iface> <timeout> <class label> |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 55 | * |
| 56 | * Monitor effect on the iptables chains after each step using: |
Haoyu Bai | 8c54ec5 | 2012-07-26 15:36:53 -0700 | [diff] [blame] | 57 | * iptables -nxvL -t raw |
| 58 | * iptables -nxvL -t mangle |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 59 | * |
| 60 | * Remember that the timeout value has to be same at the time of the |
| 61 | * removal. |
| 62 | * |
Haoyu Bai | 8c54ec5 | 2012-07-26 15:36:53 -0700 | [diff] [blame] | 63 | * ================= |
| 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 Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 84 | * 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 Sharma | 1c2c27f | 2014-02-03 17:28:04 -0800 | [diff] [blame] | 96 | #define LOG_NDEBUG 0 |
| 97 | |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame^] | 98 | #include <stdint.h> |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 99 | #include <stdlib.h> |
| 100 | #include <errno.h> |
| 101 | #include <sys/socket.h> |
| 102 | #include <sys/stat.h> |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 103 | #include <sys/wait.h> |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 104 | #include <fcntl.h> |
| 105 | #include <netinet/in.h> |
| 106 | #include <arpa/inet.h> |
| 107 | #include <string.h> |
| 108 | #include <cutils/properties.h> |
| 109 | |
| 110 | #define LOG_TAG "IdletimerController" |
| 111 | #include <cutils/log.h> |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 112 | #include <logwrap/logwrap.h> |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 113 | |
| 114 | #include "IdletimerController.h" |
| 115 | #include "NetdConstants.h" |
| 116 | |
Haoyu Bai | 8c54ec5 | 2012-07-26 15:36:53 -0700 | [diff] [blame] | 117 | const char* IdletimerController::LOCAL_RAW_PREROUTING = "idletimer_raw_PREROUTING"; |
| 118 | const char* IdletimerController::LOCAL_MANGLE_POSTROUTING = "idletimer_mangle_POSTROUTING"; |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 119 | |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame^] | 120 | auto IdletimerController::execFunction = android_fork_execvp; |
| 121 | |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 122 | IdletimerController::IdletimerController() { |
| 123 | } |
| 124 | |
| 125 | IdletimerController::~IdletimerController() { |
| 126 | } |
| 127 | /* return 0 or non-zero */ |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 128 | int IdletimerController::runIpxtablesCmd(int argc, const char **argv) { |
Ashish Sharma | 1c2c27f | 2014-02-03 17:28:04 -0800 | [diff] [blame] | 129 | int resIpv4, resIpv6; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 130 | |
Ashish Sharma | 1c2c27f | 2014-02-03 17:28:04 -0800 | [diff] [blame] | 131 | // Running for IPv4 |
| 132 | argv[0] = IPTABLES_PATH; |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame^] | 133 | resIpv4 = execFunction(argc, (char **)argv, NULL, false, false); |
Ashish Sharma | 1c2c27f | 2014-02-03 17:28:04 -0800 | [diff] [blame] | 134 | |
| 135 | // Running for IPv6 |
| 136 | argv[0] = IP6TABLES_PATH; |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame^] | 137 | resIpv6 = execFunction(argc, (char **)argv, NULL, false, false); |
Ashish Sharma | 1c2c27f | 2014-02-03 17:28:04 -0800 | [diff] [blame] | 138 | |
| 139 | #if !LOG_NDEBUG |
| 140 | std::string full_cmd = argv[0]; |
| 141 | argc--; argv++; |
| 142 | for (; argc; argc--, argv++) { |
| 143 | full_cmd += " "; |
| 144 | full_cmd += argv[0]; |
| 145 | } |
| 146 | ALOGV("runCmd(%s) res_ipv4=%d, res_ipv6=%d", full_cmd.c_str(), resIpv4, resIpv6); |
| 147 | #endif |
| 148 | |
| 149 | return (resIpv4 == 0 && resIpv6 == 0) ? 0 : -1; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | bool IdletimerController::setupIptablesHooks() { |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 153 | return true; |
| 154 | } |
| 155 | |
| 156 | int IdletimerController::setDefaults() { |
Haoyu Bai | 8c54ec5 | 2012-07-26 15:36:53 -0700 | [diff] [blame] | 157 | int res; |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 158 | const char *cmd1[] = { |
Ashish Sharma | 1c2c27f | 2014-02-03 17:28:04 -0800 | [diff] [blame] | 159 | NULL, // To be filled inside runIpxtablesCmd |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 160 | "-w", |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 161 | "-t", |
| 162 | "raw", |
| 163 | "-F", |
| 164 | LOCAL_RAW_PREROUTING |
| 165 | }; |
| 166 | res = runIpxtablesCmd(ARRAY_SIZE(cmd1), cmd1); |
Haoyu Bai | 8c54ec5 | 2012-07-26 15:36:53 -0700 | [diff] [blame] | 167 | |
| 168 | if (res) |
| 169 | return res; |
| 170 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 171 | const char *cmd2[] = { |
Ashish Sharma | 1c2c27f | 2014-02-03 17:28:04 -0800 | [diff] [blame] | 172 | NULL, // To be filled inside runIpxtablesCmd |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 173 | "-w", |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 174 | "-t", |
| 175 | "mangle", |
| 176 | "-F", |
| 177 | LOCAL_MANGLE_POSTROUTING |
| 178 | }; |
| 179 | res = runIpxtablesCmd(ARRAY_SIZE(cmd2), cmd2); |
| 180 | |
Haoyu Bai | 8c54ec5 | 2012-07-26 15:36:53 -0700 | [diff] [blame] | 181 | return res; |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | int IdletimerController::enableIdletimerControl() { |
| 185 | int res = setDefaults(); |
| 186 | return res; |
| 187 | } |
| 188 | |
| 189 | int IdletimerController::disableIdletimerControl() { |
| 190 | int res = setDefaults(); |
| 191 | return res; |
| 192 | } |
| 193 | |
| 194 | int IdletimerController::modifyInterfaceIdletimer(IptOp op, const char *iface, |
Haoyu Bai | 98f65d3 | 2012-06-28 16:16:51 -0700 | [diff] [blame] | 195 | uint32_t timeout, |
| 196 | const char *classLabel) { |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 197 | int res; |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 198 | char timeout_str[11]; //enough to store any 32-bit unsigned decimal |
| 199 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 200 | if (!isIfaceName(iface)) { |
| 201 | errno = ENOENT; |
| 202 | return -1; |
| 203 | } |
| 204 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 205 | snprintf(timeout_str, sizeof(timeout_str), "%u", timeout); |
| 206 | |
| 207 | const char *cmd1[] = { |
Ashish Sharma | 1c2c27f | 2014-02-03 17:28:04 -0800 | [diff] [blame] | 208 | NULL, // To be filled inside runIpxtablesCmd |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 209 | "-w", |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 210 | "-t", |
| 211 | "raw", |
| 212 | (op == IptOpAdd) ? "-A" : "-D", |
| 213 | LOCAL_RAW_PREROUTING, |
| 214 | "-i", |
| 215 | iface, |
| 216 | "-j", |
| 217 | "IDLETIMER", |
| 218 | "--timeout", |
| 219 | timeout_str, |
| 220 | "--label", |
| 221 | classLabel, |
| 222 | "--send_nl_msg", |
| 223 | "1" |
| 224 | }; |
| 225 | res = runIpxtablesCmd(ARRAY_SIZE(cmd1), cmd1); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 226 | |
Haoyu Bai | 8c54ec5 | 2012-07-26 15:36:53 -0700 | [diff] [blame] | 227 | if (res) |
| 228 | return res; |
| 229 | |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 230 | const char *cmd2[] = { |
Ashish Sharma | 1c2c27f | 2014-02-03 17:28:04 -0800 | [diff] [blame] | 231 | NULL, // To be filled inside runIpxtablesCmd |
Paul Jensen | 94b2ab9 | 2015-08-04 10:35:05 -0400 | [diff] [blame] | 232 | "-w", |
Rom Lemarchand | 001f0a4 | 2013-01-31 12:41:03 -0800 | [diff] [blame] | 233 | "-t", |
| 234 | "mangle", |
| 235 | (op == IptOpAdd) ? "-A" : "-D", |
| 236 | LOCAL_MANGLE_POSTROUTING, |
| 237 | "-o", |
| 238 | iface, |
| 239 | "-j", |
| 240 | "IDLETIMER", |
| 241 | "--timeout", |
| 242 | timeout_str, |
| 243 | "--label", |
| 244 | classLabel, |
| 245 | "--send_nl_msg", |
| 246 | "1" |
| 247 | }; |
| 248 | res = runIpxtablesCmd(ARRAY_SIZE(cmd2), cmd2); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 249 | |
| 250 | return res; |
| 251 | } |
| 252 | |
Haoyu Bai | 98f65d3 | 2012-06-28 16:16:51 -0700 | [diff] [blame] | 253 | int IdletimerController::addInterfaceIdletimer(const char *iface, |
| 254 | uint32_t timeout, |
| 255 | const char *classLabel) { |
| 256 | return modifyInterfaceIdletimer(IptOpAdd, iface, timeout, classLabel); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Haoyu Bai | 98f65d3 | 2012-06-28 16:16:51 -0700 | [diff] [blame] | 259 | int IdletimerController::removeInterfaceIdletimer(const char *iface, |
| 260 | uint32_t timeout, |
| 261 | const char *classLabel) { |
| 262 | return modifyInterfaceIdletimer(IptOpDelete, iface, timeout, classLabel); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 263 | } |