Sandeep Patil | 9b9e977 | 2017-04-14 19:05:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Lorenzo Colitti | 2dd1bc3 | 2017-04-17 13:51:47 +0900 | [diff] [blame] | 17 | #include <regex> |
| 18 | #include <string> |
| 19 | |
Sandeep Patil | 9b9e977 | 2017-04-14 19:05:50 -0700 | [diff] [blame] | 20 | #include <libgen.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
| 25 | |
Lorenzo Colitti | 2dd1bc3 | 2017-04-17 13:51:47 +0900 | [diff] [blame] | 26 | #include <android-base/strings.h> |
| 27 | |
| 28 | #define LOG_TAG "NetUtilsWrapper" |
Logan Chien | 3f46148 | 2018-04-23 14:31:32 +0800 | [diff] [blame^] | 29 | #include <log/log.h> |
Lorenzo Colitti | 2dd1bc3 | 2017-04-17 13:51:47 +0900 | [diff] [blame] | 30 | |
| 31 | #include "NetUtilsWrapper.h" |
| 32 | |
Sandeep Patil | 9b9e977 | 2017-04-14 19:05:50 -0700 | [diff] [blame] | 33 | #define SYSTEM_DIRNAME "/system/bin/" |
| 34 | |
Lorenzo Colitti | 2dd1bc3 | 2017-04-17 13:51:47 +0900 | [diff] [blame] | 35 | #define OEM_IFACE "[^ ]*oem[0-9]+" |
| 36 | #define RMNET_IFACE "(r_)?rmnet_(data)?[0-9]+" |
| 37 | #define VENDOR_IFACE "(" OEM_IFACE "|" RMNET_IFACE ")" |
| 38 | #define VENDOR_CHAIN "(oem_.*|nm_.*|qcom_.*)" |
| 39 | |
Sandeep Patil | 9b9e977 | 2017-04-14 19:05:50 -0700 | [diff] [blame] | 40 | // List of net utils wrapped by this program |
| 41 | // The list MUST be in descending order of string length |
| 42 | const char *netcmds[] = { |
| 43 | "ip6tables", |
| 44 | "iptables", |
| 45 | "ndc", |
| 46 | "tc", |
| 47 | "ip", |
| 48 | NULL, |
| 49 | }; |
| 50 | |
Lorenzo Colitti | 2dd1bc3 | 2017-04-17 13:51:47 +0900 | [diff] [blame] | 51 | // List of regular expressions of expected commands. |
| 52 | const char *EXPECTED_REGEXPS[] = { |
| 53 | #define CMD "^" SYSTEM_DIRNAME |
| 54 | // Create, delete, and manage OEM networks. |
Subash Abhinov Kasiviswanathan | 4150d6c | 2017-07-25 19:20:32 -0600 | [diff] [blame] | 55 | CMD "ndc network (create|destroy) (oem|handle)[0-9]+( |$)", |
| 56 | CMD "ndc network interface (add|remove) (oem|handle)[0-9]+ " VENDOR_IFACE, |
| 57 | CMD "ndc network route (add|remove) (oem|handle)[0-9]+ ", |
Lorenzo Colitti | 2dd1bc3 | 2017-04-17 13:51:47 +0900 | [diff] [blame] | 58 | CMD "ndc ipfwd (enable|disable) ", |
| 59 | CMD "ndc ipfwd (add|remove) .*" VENDOR_IFACE, |
| 60 | |
| 61 | // Manage vendor iptables rules. |
| 62 | CMD "ip(6)?tables -w.* (-A|-D|-F|-I|-N|-X) " VENDOR_CHAIN, |
| 63 | CMD "ip(6)?tables -w.* (-i|-o) " VENDOR_IFACE, |
| 64 | |
| 65 | // Manage IPsec state. |
| 66 | CMD "ip xfrm .*", |
| 67 | |
| 68 | // Manage vendor interfaces. |
| 69 | CMD "tc .* dev " VENDOR_IFACE, |
| 70 | CMD "ip( -4| -6)? (addr|address) (add|del|delete|flush).* dev " VENDOR_IFACE, |
| 71 | |
| 72 | // Other activities observed on current devices. In future releases, these should be supported |
| 73 | // in a way that is less likely to interfere with general Android networking behaviour. |
| 74 | CMD "tc qdisc del dev root", |
| 75 | CMD "ip( -4| -6)? rule .* goto 13000 prio 11999", |
| 76 | CMD "ip( -4| -6)? rule .* prio 25000", |
| 77 | CMD "ip(6)?tables -w .* -j " VENDOR_CHAIN, |
| 78 | CMD "iptables -w -t mangle -[AD] PREROUTING -m socket --nowildcard --restore-skmark -j ACCEPT", |
| 79 | CMD "ndc network interface (add|remove) oem[0-9]+$", // Invalid command: no interface removed. |
| 80 | #undef CMD |
| 81 | }; |
| 82 | |
| 83 | bool checkExpectedCommand(int argc, char **argv) { |
Lorenzo Colitti | 3394786 | 2017-05-23 08:43:11 +0900 | [diff] [blame] | 84 | static bool loggedError = false; |
Lorenzo Colitti | 2dd1bc3 | 2017-04-17 13:51:47 +0900 | [diff] [blame] | 85 | std::vector<const char*> allArgs(argc); |
| 86 | for (int i = 0; i < argc; i++) { |
| 87 | allArgs[i] = argv[i]; |
| 88 | } |
| 89 | std::string fullCmd = android::base::Join(allArgs, ' '); |
| 90 | for (size_t i = 0; i < ARRAY_SIZE(EXPECTED_REGEXPS); i++) { |
| 91 | const std::regex expectedRegexp(EXPECTED_REGEXPS[i], std::regex_constants::extended); |
| 92 | if (std::regex_search(fullCmd, expectedRegexp)) { |
| 93 | return true; |
| 94 | } |
| 95 | } |
Lorenzo Colitti | 3394786 | 2017-05-23 08:43:11 +0900 | [diff] [blame] | 96 | if (!loggedError) { |
| 97 | ALOGI("Unexpected command: %s", fullCmd.c_str()); |
Lorenzo Colitti | ab9c653 | 2017-05-29 11:27:39 +0900 | [diff] [blame] | 98 | fprintf(stderr, LOG_TAG ": Unexpected command: %s\n", fullCmd.c_str()); |
Lorenzo Colitti | 3394786 | 2017-05-23 08:43:11 +0900 | [diff] [blame] | 99 | loggedError = true; |
| 100 | } |
Lorenzo Colitti | 2dd1bc3 | 2017-04-17 13:51:47 +0900 | [diff] [blame] | 101 | return false; |
| 102 | } |
| 103 | |
| 104 | |
Sandeep Patil | 9b9e977 | 2017-04-14 19:05:50 -0700 | [diff] [blame] | 105 | // This is the only gateway for vendor programs to reach net utils. |
Lorenzo Colitti | 2dd1bc3 | 2017-04-17 13:51:47 +0900 | [diff] [blame] | 106 | int doMain(int argc, char **argv) { |
Sandeep Patil | 9b9e977 | 2017-04-14 19:05:50 -0700 | [diff] [blame] | 107 | char *progname = argv[0]; |
| 108 | char *basename = NULL; |
| 109 | |
| 110 | basename = strrchr(progname, '/'); |
| 111 | basename = basename ? basename + 1 : progname; |
| 112 | |
| 113 | for (int i = 0; netcmds[i]; ++i) { |
| 114 | size_t len = strlen(netcmds[i]); |
| 115 | if (!strncmp(basename, netcmds[i], len)) { |
| 116 | // truncate to match netcmds[i] |
| 117 | basename[len] = '\0'; |
| 118 | |
| 119 | // hardcode the path to /system so it cannot be overwritten |
| 120 | char *cmd; |
| 121 | if (asprintf(&cmd, "%s%s", SYSTEM_DIRNAME, basename) == -1) { |
| 122 | perror("asprintf"); |
| 123 | exit(EXIT_FAILURE); |
| 124 | } |
| 125 | argv[0] = cmd; |
Lorenzo Colitti | 3394786 | 2017-05-23 08:43:11 +0900 | [diff] [blame] | 126 | if (checkExpectedCommand(argc, argv)) { |
| 127 | execv(cmd, argv); |
| 128 | } |
Sandeep Patil | 9b9e977 | 2017-04-14 19:05:50 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
Lorenzo Colitti | 3394786 | 2017-05-23 08:43:11 +0900 | [diff] [blame] | 132 | // Invalid command. Reject and fail. |
Sandeep Patil | 9b9e977 | 2017-04-14 19:05:50 -0700 | [diff] [blame] | 133 | exit(EXIT_FAILURE); |
| 134 | } |