2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <netinet/in.h>
27 #define LOG_TAG "Netd"
29 #include <cutils/log.h>
30 #include <logwrap/logwrap.h>
32 #include "NetdConstants.h"
34 const char * const OEM_SCRIPT_PATH = "/system/bin/oem-iptables-init.sh";
35 const char * const IPTABLES_PATH = "/system/bin/iptables";
36 const char * const IP6TABLES_PATH = "/system/bin/ip6tables";
37 const char * const TC_PATH = "/system/bin/tc";
38 const char * const IP_PATH = "/system/bin/ip";
39 const char * const ADD = "add";
40 const char * const DEL = "del";
42 static void logExecError(const char* argv[], int res, int status) {
43 const char** argp = argv;
44 std::string args = "";
50 ALOGE("exec() res=%d, status=%d for %s", res, status, args.c_str());
53 static int execIptablesCommand(int argc, const char *argv[], bool silent) {
57 res = android_fork_execvp(argc, (char **)argv, &status, false,
59 if (res || !WIFEXITED(status) || WEXITSTATUS(status)) {
61 logExecError(argv, res, status);
65 if (!WIFEXITED(status))
68 return WEXITSTATUS(status);
71 static int execIptables(IptablesTarget target, bool silent, va_list args) {
72 /* Read arguments from incoming va_list; we expect the list to be NULL terminated. */
73 std::list<const char*> argsList;
74 argsList.push_back(NULL);
77 arg = va_arg(args, const char *);
78 argsList.push_back(arg);
82 const char* argv[argsList.size()];
83 std::list<const char*>::iterator it;
84 for (it = argsList.begin(); it != argsList.end(); it++, i++) {
89 if (target == V4 || target == V4V6) {
90 argv[0] = IPTABLES_PATH;
91 res |= execIptablesCommand(argsList.size(), argv, silent);
93 if (target == V6 || target == V4V6) {
94 argv[0] = IP6TABLES_PATH;
95 res |= execIptablesCommand(argsList.size(), argv, silent);
100 int execIptables(IptablesTarget target, ...) {
102 va_start(args, target);
103 int res = execIptables(target, false, args);
108 int execIptablesSilently(IptablesTarget target, ...) {
110 va_start(args, target);
111 int res = execIptables(target, true, args);
117 * Check an interface name for plausibility. This should e.g. help against
118 * directory traversal.
120 bool isIfaceName(const char *name) {
122 size_t name_len = strlen(name);
123 if ((name_len == 0) || (name_len > IFNAMSIZ)) {
127 /* First character must be alphanumeric */
128 if (!isalnum(name[0])) {
132 for (i = 1; i < name_len; i++) {
133 if (!isalnum(name[i]) && (name[i] != '_') && (name[i] != '-') && (name[i] != ':')) {
141 int parsePrefix(const char *prefix, uint8_t *family, void *address, int size, uint8_t *prefixlen) {
142 if (!prefix || !family || !address || !prefixlen) {
146 // Find the '/' separating address from prefix length.
147 const char *slash = strchr(prefix, '/');
148 const char *prefixlenString = slash + 1;
149 if (!slash || !*prefixlenString)
152 // Convert the prefix length to a uint8_t.
155 templen = strtoul(prefixlenString, &endptr, 10);
156 if (*endptr || templen > 255) {
159 *prefixlen = templen;
161 // Copy the address part of the prefix to a local buffer. We have to copy
162 // because inet_pton and getaddrinfo operate on null-terminated address
163 // strings, but prefix is const and has '/' after the address.
164 std::string addressString(prefix, slash - prefix);
166 // Parse the address.
169 .ai_flags = AI_NUMERICHOST,
171 int ret = getaddrinfo(addressString.c_str(), NULL, &hints, &res);
173 return -EINVAL; // getaddrinfo return values are not errno values.
176 // Convert the address string to raw address bytes.
179 switch (res[0].ai_family) {
181 if (*prefixlen > 32) {
184 sockaddr_in *sin = (sockaddr_in *) res[0].ai_addr;
185 rawAddress = &sin->sin_addr;
190 if (*prefixlen > 128) {
193 sockaddr_in6 *sin6 = (sockaddr_in6 *) res[0].ai_addr;
194 rawAddress = &sin6->sin6_addr;
200 return -EAFNOSUPPORT;
204 if (rawLength > size) {
209 *family = res[0].ai_family;
210 memcpy(address, rawAddress, rawLength);