xref: /aosp_15_r20/system/netd/server/NetdConstants.cpp (revision 8542734a0dd1db395a4d42aae09c37f3c3c3e7a1)
1*8542734aSAndroid Build Coastguard Worker /*
2*8542734aSAndroid Build Coastguard Worker  * Copyright (C) 2012 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 <ctype.h>
18*8542734aSAndroid Build Coastguard Worker #include <errno.h>
19*8542734aSAndroid Build Coastguard Worker #include <fcntl.h>
20*8542734aSAndroid Build Coastguard Worker #include <netdb.h>
21*8542734aSAndroid Build Coastguard Worker #include <net/if.h>
22*8542734aSAndroid Build Coastguard Worker #include <netinet/in.h>
23*8542734aSAndroid Build Coastguard Worker #include <stdlib.h>
24*8542734aSAndroid Build Coastguard Worker #include <string.h>
25*8542734aSAndroid Build Coastguard Worker #include <sys/wait.h>
26*8542734aSAndroid Build Coastguard Worker 
27*8542734aSAndroid Build Coastguard Worker #define LOG_TAG "Netd"
28*8542734aSAndroid Build Coastguard Worker 
29*8542734aSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
30*8542734aSAndroid Build Coastguard Worker #include <cutils/sockets.h>
31*8542734aSAndroid Build Coastguard Worker #include <log/log.h>
32*8542734aSAndroid Build Coastguard Worker 
33*8542734aSAndroid Build Coastguard Worker #include "Controllers.h"
34*8542734aSAndroid Build Coastguard Worker #include "NetdConstants.h"
35*8542734aSAndroid Build Coastguard Worker #include "IptablesRestoreController.h"
36*8542734aSAndroid Build Coastguard Worker 
execIptablesRestoreWithOutput(IptablesTarget target,const std::string & commands,std::string * output)37*8542734aSAndroid Build Coastguard Worker int execIptablesRestoreWithOutput(IptablesTarget target, const std::string& commands,
38*8542734aSAndroid Build Coastguard Worker                                   std::string *output) {
39*8542734aSAndroid Build Coastguard Worker     return android::net::gCtls->iptablesRestoreCtrl.execute(target, commands, output);
40*8542734aSAndroid Build Coastguard Worker }
41*8542734aSAndroid Build Coastguard Worker 
execIptablesRestore(IptablesTarget target,const std::string & commands)42*8542734aSAndroid Build Coastguard Worker int execIptablesRestore(IptablesTarget target, const std::string& commands) {
43*8542734aSAndroid Build Coastguard Worker     return execIptablesRestoreWithOutput(target, commands, nullptr);
44*8542734aSAndroid Build Coastguard Worker }
45*8542734aSAndroid Build Coastguard Worker 
execIptablesRestoreCommand(IptablesTarget target,const std::string & table,const std::string & command,std::string * output)46*8542734aSAndroid Build Coastguard Worker int execIptablesRestoreCommand(IptablesTarget target, const std::string& table,
47*8542734aSAndroid Build Coastguard Worker                                const std::string& command, std::string *output) {
48*8542734aSAndroid Build Coastguard Worker     std::string fullCmd = android::base::StringPrintf("*%s\n%s\nCOMMIT\n", table.c_str(),
49*8542734aSAndroid Build Coastguard Worker                                                       command.c_str());
50*8542734aSAndroid Build Coastguard Worker     return execIptablesRestoreWithOutput(target, fullCmd, output);
51*8542734aSAndroid Build Coastguard Worker }
52*8542734aSAndroid Build Coastguard Worker 
53*8542734aSAndroid Build Coastguard Worker /*
54*8542734aSAndroid Build Coastguard Worker  * Check an interface name for plausibility. This should e.g. help against
55*8542734aSAndroid Build Coastguard Worker  * directory traversal.
56*8542734aSAndroid Build Coastguard Worker  */
isIfaceName(const std::string & name)57*8542734aSAndroid Build Coastguard Worker bool isIfaceName(const std::string& name) {
58*8542734aSAndroid Build Coastguard Worker     size_t i;
59*8542734aSAndroid Build Coastguard Worker     if ((name.empty()) || (name.size() > IFNAMSIZ)) {
60*8542734aSAndroid Build Coastguard Worker         return false;
61*8542734aSAndroid Build Coastguard Worker     }
62*8542734aSAndroid Build Coastguard Worker 
63*8542734aSAndroid Build Coastguard Worker     /* First character must be alphanumeric */
64*8542734aSAndroid Build Coastguard Worker     if (!isalnum(name[0])) {
65*8542734aSAndroid Build Coastguard Worker         return false;
66*8542734aSAndroid Build Coastguard Worker     }
67*8542734aSAndroid Build Coastguard Worker 
68*8542734aSAndroid Build Coastguard Worker     for (i = 1; i < name.size(); i++) {
69*8542734aSAndroid Build Coastguard Worker         if (!isalnum(name[i]) && (name[i] != '_') && (name[i] != '-')
70*8542734aSAndroid Build Coastguard Worker                 && (name[i] != ':') && (name[i] != '.')) {
71*8542734aSAndroid Build Coastguard Worker             return false;
72*8542734aSAndroid Build Coastguard Worker         }
73*8542734aSAndroid Build Coastguard Worker     }
74*8542734aSAndroid Build Coastguard Worker 
75*8542734aSAndroid Build Coastguard Worker     return true;
76*8542734aSAndroid Build Coastguard Worker }
77*8542734aSAndroid Build Coastguard Worker 
parsePrefix(const char * prefix,uint8_t * family,void * address,int size,uint8_t * prefixlen)78*8542734aSAndroid Build Coastguard Worker int parsePrefix(const char *prefix, uint8_t *family, void *address, int size, uint8_t *prefixlen) {
79*8542734aSAndroid Build Coastguard Worker     if (!prefix || !family || !address || !prefixlen) {
80*8542734aSAndroid Build Coastguard Worker         return -EFAULT;
81*8542734aSAndroid Build Coastguard Worker     }
82*8542734aSAndroid Build Coastguard Worker 
83*8542734aSAndroid Build Coastguard Worker     // Find the '/' separating address from prefix length.
84*8542734aSAndroid Build Coastguard Worker     const char *slash = strchr(prefix, '/');
85*8542734aSAndroid Build Coastguard Worker     const char *prefixlenString = slash + 1;
86*8542734aSAndroid Build Coastguard Worker     if (!slash || !*prefixlenString)
87*8542734aSAndroid Build Coastguard Worker         return -EINVAL;
88*8542734aSAndroid Build Coastguard Worker 
89*8542734aSAndroid Build Coastguard Worker     // Convert the prefix length to a uint8_t.
90*8542734aSAndroid Build Coastguard Worker     char *endptr;
91*8542734aSAndroid Build Coastguard Worker     unsigned templen;
92*8542734aSAndroid Build Coastguard Worker     templen = strtoul(prefixlenString, &endptr, 10);
93*8542734aSAndroid Build Coastguard Worker     if (*endptr || templen > 255) {
94*8542734aSAndroid Build Coastguard Worker         return -EINVAL;
95*8542734aSAndroid Build Coastguard Worker     }
96*8542734aSAndroid Build Coastguard Worker     *prefixlen = templen;
97*8542734aSAndroid Build Coastguard Worker 
98*8542734aSAndroid Build Coastguard Worker     // Copy the address part of the prefix to a local buffer. We have to copy
99*8542734aSAndroid Build Coastguard Worker     // because inet_pton and getaddrinfo operate on null-terminated address
100*8542734aSAndroid Build Coastguard Worker     // strings, but prefix is const and has '/' after the address.
101*8542734aSAndroid Build Coastguard Worker     std::string addressString(prefix, slash - prefix);
102*8542734aSAndroid Build Coastguard Worker 
103*8542734aSAndroid Build Coastguard Worker     // Parse the address.
104*8542734aSAndroid Build Coastguard Worker     addrinfo *res;
105*8542734aSAndroid Build Coastguard Worker     addrinfo hints = {
106*8542734aSAndroid Build Coastguard Worker         .ai_flags = AI_NUMERICHOST,
107*8542734aSAndroid Build Coastguard Worker     };
108*8542734aSAndroid Build Coastguard Worker     int ret = getaddrinfo(addressString.c_str(), nullptr, &hints, &res);
109*8542734aSAndroid Build Coastguard Worker     if (ret || !res) {
110*8542734aSAndroid Build Coastguard Worker         return -EINVAL;  // getaddrinfo return values are not errno values.
111*8542734aSAndroid Build Coastguard Worker     }
112*8542734aSAndroid Build Coastguard Worker 
113*8542734aSAndroid Build Coastguard Worker     // Convert the address string to raw address bytes.
114*8542734aSAndroid Build Coastguard Worker     void *rawAddress;
115*8542734aSAndroid Build Coastguard Worker     int rawLength;
116*8542734aSAndroid Build Coastguard Worker     switch (res[0].ai_family) {
117*8542734aSAndroid Build Coastguard Worker         case AF_INET: {
118*8542734aSAndroid Build Coastguard Worker             if (*prefixlen > 32) {
119*8542734aSAndroid Build Coastguard Worker                 return -EINVAL;
120*8542734aSAndroid Build Coastguard Worker             }
121*8542734aSAndroid Build Coastguard Worker             sockaddr_in *sin = (sockaddr_in *) res[0].ai_addr;
122*8542734aSAndroid Build Coastguard Worker             rawAddress = &sin->sin_addr;
123*8542734aSAndroid Build Coastguard Worker             rawLength = 4;
124*8542734aSAndroid Build Coastguard Worker             break;
125*8542734aSAndroid Build Coastguard Worker         }
126*8542734aSAndroid Build Coastguard Worker         case AF_INET6: {
127*8542734aSAndroid Build Coastguard Worker             if (*prefixlen > 128) {
128*8542734aSAndroid Build Coastguard Worker                 return -EINVAL;
129*8542734aSAndroid Build Coastguard Worker             }
130*8542734aSAndroid Build Coastguard Worker             sockaddr_in6 *sin6 = (sockaddr_in6 *) res[0].ai_addr;
131*8542734aSAndroid Build Coastguard Worker             rawAddress = &sin6->sin6_addr;
132*8542734aSAndroid Build Coastguard Worker             rawLength = 16;
133*8542734aSAndroid Build Coastguard Worker             break;
134*8542734aSAndroid Build Coastguard Worker         }
135*8542734aSAndroid Build Coastguard Worker         default: {
136*8542734aSAndroid Build Coastguard Worker             freeaddrinfo(res);
137*8542734aSAndroid Build Coastguard Worker             return -EAFNOSUPPORT;
138*8542734aSAndroid Build Coastguard Worker         }
139*8542734aSAndroid Build Coastguard Worker     }
140*8542734aSAndroid Build Coastguard Worker 
141*8542734aSAndroid Build Coastguard Worker     if (rawLength > size) {
142*8542734aSAndroid Build Coastguard Worker         freeaddrinfo(res);
143*8542734aSAndroid Build Coastguard Worker         return -ENOSPC;
144*8542734aSAndroid Build Coastguard Worker     }
145*8542734aSAndroid Build Coastguard Worker 
146*8542734aSAndroid Build Coastguard Worker     *family = res[0].ai_family;
147*8542734aSAndroid Build Coastguard Worker     memcpy(address, rawAddress, rawLength);
148*8542734aSAndroid Build Coastguard Worker     freeaddrinfo(res);
149*8542734aSAndroid Build Coastguard Worker 
150*8542734aSAndroid Build Coastguard Worker     return rawLength;
151*8542734aSAndroid Build Coastguard Worker }
152*8542734aSAndroid Build Coastguard Worker 
blockSigpipe()153*8542734aSAndroid Build Coastguard Worker void blockSigpipe() {
154*8542734aSAndroid Build Coastguard Worker     sigset_t mask;
155*8542734aSAndroid Build Coastguard Worker 
156*8542734aSAndroid Build Coastguard Worker     sigemptyset(&mask);
157*8542734aSAndroid Build Coastguard Worker     sigaddset(&mask, SIGPIPE);
158*8542734aSAndroid Build Coastguard Worker     if (sigprocmask(SIG_BLOCK, &mask, nullptr) != 0) ALOGW("WARNING: SIGPIPE not blocked");
159*8542734aSAndroid Build Coastguard Worker }
160*8542734aSAndroid Build Coastguard Worker 
setCloseOnExec(const char * sock)161*8542734aSAndroid Build Coastguard Worker void setCloseOnExec(const char *sock) {
162*8542734aSAndroid Build Coastguard Worker     int fd = android_get_control_socket(sock);
163*8542734aSAndroid Build Coastguard Worker     int flags = fcntl(fd, F_GETFD, 0);
164*8542734aSAndroid Build Coastguard Worker     if (flags == -1) {
165*8542734aSAndroid Build Coastguard Worker         ALOGE("Can't get fd flags for control socket %s", sock);
166*8542734aSAndroid Build Coastguard Worker         flags = 0;
167*8542734aSAndroid Build Coastguard Worker     }
168*8542734aSAndroid Build Coastguard Worker     flags |= FD_CLOEXEC;
169*8542734aSAndroid Build Coastguard Worker     if (fcntl(fd, F_SETFD, flags) == -1) {
170*8542734aSAndroid Build Coastguard Worker         ALOGE("Can't set control socket %s to FD_CLOEXEC", sock);
171*8542734aSAndroid Build Coastguard Worker     }
172*8542734aSAndroid Build Coastguard Worker }
173*8542734aSAndroid Build Coastguard Worker 
174*8542734aSAndroid Build Coastguard Worker // SIGTERM with timeout first, if fail, SIGKILL
stopProcess(int pid,const char * processName)175*8542734aSAndroid Build Coastguard Worker void stopProcess(int pid, const char* processName) {
176*8542734aSAndroid Build Coastguard Worker     int err = kill(pid, SIGTERM);
177*8542734aSAndroid Build Coastguard Worker     if (err) {
178*8542734aSAndroid Build Coastguard Worker         err = errno;
179*8542734aSAndroid Build Coastguard Worker     }
180*8542734aSAndroid Build Coastguard Worker     if (err == ESRCH) {
181*8542734aSAndroid Build Coastguard Worker         // This means that someone else inside netd called this helper function,
182*8542734aSAndroid Build Coastguard Worker         // which is a programming error. There's no point in calling waitpid() here since we
183*8542734aSAndroid Build Coastguard Worker         // know that the process is gone.
184*8542734aSAndroid Build Coastguard Worker         ALOGE("%s child process %d unexpectedly disappeared", processName, pid);
185*8542734aSAndroid Build Coastguard Worker         return;
186*8542734aSAndroid Build Coastguard Worker     }
187*8542734aSAndroid Build Coastguard Worker     if (err) {
188*8542734aSAndroid Build Coastguard Worker         ALOGE("Error killing %s child process %d: %s", processName, pid, strerror(err));
189*8542734aSAndroid Build Coastguard Worker     }
190*8542734aSAndroid Build Coastguard Worker     int status = 0;
191*8542734aSAndroid Build Coastguard Worker     int ret = 0;
192*8542734aSAndroid Build Coastguard Worker     for (int count = 0; ret == 0 && count < 50; count++) {
193*8542734aSAndroid Build Coastguard Worker         usleep(100000); // sleep 0.1s to wait for process stop.
194*8542734aSAndroid Build Coastguard Worker         ret = waitpid(pid, &status, WNOHANG);
195*8542734aSAndroid Build Coastguard Worker     }
196*8542734aSAndroid Build Coastguard Worker     if (ret == 0) {
197*8542734aSAndroid Build Coastguard Worker         ALOGE("Failed to SIGTERM %s pid=%d, try SIGKILL", processName, pid);
198*8542734aSAndroid Build Coastguard Worker         kill(pid, SIGKILL);
199*8542734aSAndroid Build Coastguard Worker         ret = waitpid(pid, &status, 0);
200*8542734aSAndroid Build Coastguard Worker     }
201*8542734aSAndroid Build Coastguard Worker     if (ret == -1) {
202*8542734aSAndroid Build Coastguard Worker         ALOGE("Error waiting for %s child process %d: %s", processName, pid, strerror(errno));
203*8542734aSAndroid Build Coastguard Worker     } else {
204*8542734aSAndroid Build Coastguard Worker         ALOGD("%s process %d terminated status=%d", processName, pid, status);
205*8542734aSAndroid Build Coastguard Worker     }
206*8542734aSAndroid Build Coastguard Worker }
207