xref: /aosp_15_r20/system/netd/netutils_wrappers/NetUtilsWrapper-1.0.cpp (revision 8542734a0dd1db395a4d42aae09c37f3c3c3e7a1)
1*8542734aSAndroid Build Coastguard Worker /*
2*8542734aSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*8542734aSAndroid Build Coastguard Worker  *
4*8542734aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8542734aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8542734aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8542734aSAndroid Build Coastguard Worker  *
8*8542734aSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8542734aSAndroid Build Coastguard Worker  *
10*8542734aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8542734aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8542734aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8542734aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8542734aSAndroid Build Coastguard Worker  * limitations under the License.
15*8542734aSAndroid Build Coastguard Worker  */
16*8542734aSAndroid Build Coastguard Worker 
17*8542734aSAndroid Build Coastguard Worker #include <regex>
18*8542734aSAndroid Build Coastguard Worker #include <string>
19*8542734aSAndroid Build Coastguard Worker 
20*8542734aSAndroid Build Coastguard Worker #include <libgen.h>
21*8542734aSAndroid Build Coastguard Worker #include <stdio.h>
22*8542734aSAndroid Build Coastguard Worker #include <stdlib.h>
23*8542734aSAndroid Build Coastguard Worker #include <string.h>
24*8542734aSAndroid Build Coastguard Worker #include <unistd.h>
25*8542734aSAndroid Build Coastguard Worker 
26*8542734aSAndroid Build Coastguard Worker #include <android-base/strings.h>
27*8542734aSAndroid Build Coastguard Worker 
28*8542734aSAndroid Build Coastguard Worker #define LOG_TAG "NetUtilsWrapper"
29*8542734aSAndroid Build Coastguard Worker #include <log/log.h>
30*8542734aSAndroid Build Coastguard Worker 
31*8542734aSAndroid Build Coastguard Worker #include "NetUtilsWrapper.h"
32*8542734aSAndroid Build Coastguard Worker 
33*8542734aSAndroid Build Coastguard Worker #define SYSTEM_DIRNAME  "/system/bin/"
34*8542734aSAndroid Build Coastguard Worker 
35*8542734aSAndroid Build Coastguard Worker #define OEM_IFACE "[^ ]*oem[0-9]+"
36*8542734aSAndroid Build Coastguard Worker #define RMNET_IFACE "(r_)?rmnet_(data)?[0-9]+"
37*8542734aSAndroid Build Coastguard Worker #define CCMNI_IFACE "cc(3)?mni[0-9]+"
38*8542734aSAndroid Build Coastguard Worker #define WWAN_IFACE "wwan[0-9]+"
39*8542734aSAndroid Build Coastguard Worker #define VENDOR_IFACE "(" OEM_IFACE "|" RMNET_IFACE "|" CCMNI_IFACE "|" WWAN_IFACE ")"
40*8542734aSAndroid Build Coastguard Worker #define VENDOR_CHAIN "(oem_.*|nm_.*|qcom_.*)"
41*8542734aSAndroid Build Coastguard Worker 
42*8542734aSAndroid Build Coastguard Worker // List of net utils wrapped by this program
43*8542734aSAndroid Build Coastguard Worker // The list MUST be in descending order of string length
44*8542734aSAndroid Build Coastguard Worker const char *netcmds[] = {
45*8542734aSAndroid Build Coastguard Worker     "ip6tables",
46*8542734aSAndroid Build Coastguard Worker     "iptables",
47*8542734aSAndroid Build Coastguard Worker     "ndc",
48*8542734aSAndroid Build Coastguard Worker     "tc",
49*8542734aSAndroid Build Coastguard Worker     "ip",
50*8542734aSAndroid Build Coastguard Worker     nullptr,
51*8542734aSAndroid Build Coastguard Worker };
52*8542734aSAndroid Build Coastguard Worker 
53*8542734aSAndroid Build Coastguard Worker // List of regular expressions of expected commands.
54*8542734aSAndroid Build Coastguard Worker const char *EXPECTED_REGEXPS[] = {
55*8542734aSAndroid Build Coastguard Worker #define CMD "^" SYSTEM_DIRNAME
56*8542734aSAndroid Build Coastguard Worker     // Create, delete, and manage OEM networks.
57*8542734aSAndroid Build Coastguard Worker     CMD "ndc network (create|destroy) (oem|handle)[0-9]+( |$)",
58*8542734aSAndroid Build Coastguard Worker     CMD "ndc network interface (add|remove) (oem|handle)[0-9]+ " VENDOR_IFACE,
59*8542734aSAndroid Build Coastguard Worker     CMD "ndc network route (add|remove) (oem|handle)[0-9]+ ",
60*8542734aSAndroid Build Coastguard Worker     CMD "ndc ipfwd (enable|disable) ",
61*8542734aSAndroid Build Coastguard Worker     CMD "ndc ipfwd (add|remove) .*" VENDOR_IFACE,
62*8542734aSAndroid Build Coastguard Worker 
63*8542734aSAndroid Build Coastguard Worker     // Manage vendor iptables rules.
64*8542734aSAndroid Build Coastguard Worker     CMD "ip(6)?tables -w.* (-A|-C|-D|-F|-I|-N|-X) " VENDOR_CHAIN,
65*8542734aSAndroid Build Coastguard Worker     CMD "ip(6)?tables -w.* (-i|-o) " VENDOR_IFACE,
66*8542734aSAndroid Build Coastguard Worker 
67*8542734aSAndroid Build Coastguard Worker     // Manage IPsec state.
68*8542734aSAndroid Build Coastguard Worker     CMD "ip xfrm .*",
69*8542734aSAndroid Build Coastguard Worker 
70*8542734aSAndroid Build Coastguard Worker     // Manage vendor interfaces.
71*8542734aSAndroid Build Coastguard Worker     CMD "tc .* dev " VENDOR_IFACE,
72*8542734aSAndroid Build Coastguard Worker     CMD "ip( -4| -6)? (addr|address) (add|del|delete|flush).* dev " VENDOR_IFACE,
73*8542734aSAndroid Build Coastguard Worker 
74*8542734aSAndroid Build Coastguard Worker     // Other activities observed on current devices. In future releases, these should be supported
75*8542734aSAndroid Build Coastguard Worker     // in a way that is less likely to interfere with general Android networking behaviour.
76*8542734aSAndroid Build Coastguard Worker     CMD "tc qdisc del dev root",
77*8542734aSAndroid Build Coastguard Worker     CMD "ip(6)?tables -w .* -j " VENDOR_CHAIN,
78*8542734aSAndroid Build Coastguard Worker     CMD "iptables -w -t mangle -[AD] PREROUTING -m socket --nowildcard --restore-skmark -j ACCEPT",
79*8542734aSAndroid Build Coastguard Worker     CMD "ndc network interface (add|remove) oem[0-9]+$",  // Invalid command: no interface removed.
80*8542734aSAndroid Build Coastguard Worker #undef CMD
81*8542734aSAndroid Build Coastguard Worker };
82*8542734aSAndroid Build Coastguard Worker 
checkExpectedCommand(int argc,char ** argv)83*8542734aSAndroid Build Coastguard Worker bool checkExpectedCommand(int argc, char **argv) {
84*8542734aSAndroid Build Coastguard Worker     static bool loggedError = false;
85*8542734aSAndroid Build Coastguard Worker     std::vector<const char*> allArgs(argc);
86*8542734aSAndroid Build Coastguard Worker     for (int i = 0; i < argc; i++) {
87*8542734aSAndroid Build Coastguard Worker         allArgs[i] = argv[i];
88*8542734aSAndroid Build Coastguard Worker     }
89*8542734aSAndroid Build Coastguard Worker     std::string fullCmd = android::base::Join(allArgs, ' ');
90*8542734aSAndroid Build Coastguard Worker     for (size_t i = 0; i < ARRAY_SIZE(EXPECTED_REGEXPS); i++) {
91*8542734aSAndroid Build Coastguard Worker         const std::regex expectedRegexp(EXPECTED_REGEXPS[i], std::regex_constants::extended);
92*8542734aSAndroid Build Coastguard Worker         if (std::regex_search(fullCmd, expectedRegexp)) {
93*8542734aSAndroid Build Coastguard Worker             return true;
94*8542734aSAndroid Build Coastguard Worker         }
95*8542734aSAndroid Build Coastguard Worker     }
96*8542734aSAndroid Build Coastguard Worker     if (!loggedError) {
97*8542734aSAndroid Build Coastguard Worker         ALOGI("Unexpected command: %s", fullCmd.c_str());
98*8542734aSAndroid Build Coastguard Worker         fprintf(stderr, LOG_TAG ": Unexpected command: %s\n", fullCmd.c_str());
99*8542734aSAndroid Build Coastguard Worker         loggedError = true;
100*8542734aSAndroid Build Coastguard Worker     }
101*8542734aSAndroid Build Coastguard Worker     return false;
102*8542734aSAndroid Build Coastguard Worker }
103*8542734aSAndroid Build Coastguard Worker 
104*8542734aSAndroid Build Coastguard Worker 
105*8542734aSAndroid Build Coastguard Worker // This is the only gateway for vendor programs to reach net utils.
doMain(int argc,char ** argv)106*8542734aSAndroid Build Coastguard Worker int doMain(int argc, char **argv) {
107*8542734aSAndroid Build Coastguard Worker     char *progname = argv[0];
108*8542734aSAndroid Build Coastguard Worker     char *basename = nullptr;
109*8542734aSAndroid Build Coastguard Worker 
110*8542734aSAndroid Build Coastguard Worker     basename = strrchr(progname, '/');
111*8542734aSAndroid Build Coastguard Worker     basename = basename ? basename + 1 : progname;
112*8542734aSAndroid Build Coastguard Worker 
113*8542734aSAndroid Build Coastguard Worker     for (int i = 0; netcmds[i]; ++i) {
114*8542734aSAndroid Build Coastguard Worker         size_t len = strlen(netcmds[i]);
115*8542734aSAndroid Build Coastguard Worker         if (!strncmp(basename, netcmds[i], len)) {
116*8542734aSAndroid Build Coastguard Worker             // truncate to match netcmds[i]
117*8542734aSAndroid Build Coastguard Worker             basename[len] = '\0';
118*8542734aSAndroid Build Coastguard Worker 
119*8542734aSAndroid Build Coastguard Worker             // hardcode the path to /system so it cannot be overwritten
120*8542734aSAndroid Build Coastguard Worker             char *cmd;
121*8542734aSAndroid Build Coastguard Worker             if (asprintf(&cmd, "%s%s", SYSTEM_DIRNAME, basename) == -1) {
122*8542734aSAndroid Build Coastguard Worker                 perror("asprintf");
123*8542734aSAndroid Build Coastguard Worker                 exit(EXIT_FAILURE);
124*8542734aSAndroid Build Coastguard Worker             }
125*8542734aSAndroid Build Coastguard Worker             argv[0] = cmd;
126*8542734aSAndroid Build Coastguard Worker             if (checkExpectedCommand(argc, argv)) {
127*8542734aSAndroid Build Coastguard Worker                 execv(cmd, argv);
128*8542734aSAndroid Build Coastguard Worker             }
129*8542734aSAndroid Build Coastguard Worker         }
130*8542734aSAndroid Build Coastguard Worker     }
131*8542734aSAndroid Build Coastguard Worker 
132*8542734aSAndroid Build Coastguard Worker     // Invalid command. Reject and fail.
133*8542734aSAndroid Build Coastguard Worker     exit(EXIT_FAILURE);
134*8542734aSAndroid Build Coastguard Worker }
135