Home | History | Annotate | Download | only in netd
      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 
     17 #include <stdlib.h>
     18 #include <errno.h>
     19 #include <fcntl.h>
     20 #include <string.h>
     21 #include <dirent.h>
     22 
     23 #include <dlfcn.h>
     24 
     25 #include <sys/socket.h>
     26 #include <sys/stat.h>
     27 #include <sys/ioctl.h>
     28 #include <sys/types.h>
     29 
     30 #include <netinet/in.h>
     31 #include <arpa/inet.h>
     32 
     33 #define LOG_TAG "InterfaceController"
     34 #include <cutils/log.h>
     35 #include <netutils/ifc.h>
     36 #include <private/android_filesystem_config.h>
     37 
     38 #include "NetdConstants.h"
     39 
     40 #include "InterfaceController.h"
     41 
     42 char if_cmd_lib_file_name[] = "/system/lib/libnetcmdiface.so";
     43 char set_cmd_func_name[] = "net_iface_send_command";
     44 char set_cmd_init_func_name[] = "net_iface_send_command_init";
     45 char set_cmd_fini_func_name[] = "net_iface_send_command_fini";
     46 
     47 const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf";
     48 
     49 const char sys_net_path[] = "/sys/class/net";
     50 
     51 InterfaceController::InterfaceController()
     52 	: sendCommand_(NULL) {
     53 	// Initial IPv6 settings.
     54 	// By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces.
     55 	// This causes RAs to work or not work based on whether forwarding is on, and causes routes
     56 	// learned from RAs to go away when forwarding is turned on. Make this behaviour predictable
     57 	// by always setting accept_ra to 2.
     58 	setAcceptRA("2");
     59 
     60 	libh_ = dlopen(if_cmd_lib_file_name, RTLD_NOW | RTLD_LOCAL);
     61 	if (libh_ == NULL) {
     62 		const char *err_str = dlerror();
     63 		ALOGW("Warning (%s) while opening the net interface command library", err_str ? err_str : "unknown");
     64 	} else {
     65 		sendCommandInit_ = (int (*)(void))dlsym(libh_, set_cmd_init_func_name);
     66 		if (sendCommandInit_ == NULL) {
     67 			const char *err_str = dlerror();
     68 			ALOGW("Error (%s) while searching for the interface command init function", err_str ? err_str : "unknown");
     69 		} else if (sendCommandInit_()) {
     70 			ALOGE("Can't init the interface command API");
     71 			return;
     72 		}
     73 		sendCommandFini_ = (int (*)(void))dlsym(libh_, set_cmd_fini_func_name);
     74 		if (sendCommandFini_ == NULL) {
     75 			const char *err_str = dlerror();
     76 			ALOGW("Error (%s) while searching for the interface command fini function", err_str ? err_str : "unknown");
     77 		}
     78 		sendCommand_ = (int (*)(int, char **, char **))dlsym(libh_, set_cmd_func_name);
     79 		if (sendCommand_ == NULL) {
     80 			const char *err_str = dlerror();
     81 			ALOGE("Error (%s) while searching for the interface command function", err_str ? err_str : "unknown");
     82 			return;
     83 		}
     84 	}
     85 }
     86 
     87 InterfaceController::~InterfaceController() {
     88 	if (sendCommandFini_) {
     89 		if (sendCommandFini_()) {
     90 			ALOGE("Can't shutdown the interface command API");
     91 		}
     92 	}
     93 	if (libh_) {
     94 		int err = dlclose(libh_);
     95 		if (err) {
     96 			const char *err_str = dlerror();
     97 			ALOGE("Error (%s) while closing the net interface command library", err_str ? err_str : "unknown");
     98 		}
     99 	}
    100 }
    101 
    102 /*
    103  * Arguments:
    104  *	  argv[2] - wlan interface
    105  *	  argv[3] - command
    106  *	  argv[4] - argument
    107  *	  rbuf	- returned buffer
    108  */
    109 int InterfaceController::interfaceCommand(int argc, char *argv[], char **rbuf) {
    110 	int ret = -ENOSYS;
    111 	if (sendCommand_)
    112 		ret = sendCommand_(argc, argv, rbuf);
    113 
    114 	return ret;
    115 }
    116 
    117 int InterfaceController::writeIPv6ProcPath(const char *interface, const char *setting, const char *value) {
    118 	char *path;
    119 	asprintf(&path, "%s/%s/%s", ipv6_proc_path, interface, setting);
    120 	int success = writeFile(path, value, strlen(value));
    121 	free(path);
    122 	return success;
    123 }
    124 
    125 int InterfaceController::setEnableIPv6(const char *interface, const int on) {
    126 	// When disable_ipv6 changes from 1 to 0, the kernel starts autoconf.
    127 	// When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf
    128 	// addresses and routes and disables IPv6 on the interface.
    129 	const char *disable_ipv6 = on ? "0" : "1";
    130 	return writeIPv6ProcPath(interface, "disable_ipv6", disable_ipv6);
    131 }
    132 
    133 int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) {
    134 	// 0: disable IPv6 privacy addresses
    135 	// 0: enable IPv6 privacy addresses and prefer them over non-privacy ones.
    136 	return writeIPv6ProcPath(interface, "use_tempaddr", on ? "2" : "0");
    137 }
    138 
    139 int InterfaceController::isInterfaceName(const char *name) {
    140 	return strcmp(name, ".") &&
    141 		strcmp(name, "..") &&
    142 		strcmp(name, "default") &&
    143 		strcmp(name, "all");
    144 }
    145 
    146 int InterfaceController::setAcceptRA(const char *value) {
    147 	// Set the default value, which is used by any interfaces that are created in the future.
    148 	writeIPv6ProcPath("default", "accept_ra", value);
    149 
    150 	// Set the value on all the interfaces.
    151 	DIR *dir = opendir(ipv6_proc_path);
    152 	if (!dir) {
    153 		ALOGE("Can't list %s: %s", ipv6_proc_path, strerror(errno));
    154 		return -errno;
    155 	}
    156 	struct dirent *d;
    157 	while((d = readdir(dir)) != NULL) {
    158 		if (d->d_type == DT_DIR && isInterfaceName(d->d_name)) {
    159 			if (writeIPv6ProcPath(d->d_name, "accept_ra", value) < 0) {
    160 				ALOGE("Can't write to %s/%s/accept_ra: %s", ipv6_proc_path,
    161 				      d->d_name, strerror(errno));
    162 			}
    163 		}
    164 	}
    165 	closedir(dir);
    166 	return 0;
    167 }
    168 
    169 int InterfaceController::getMtu(const char *interface, int *mtu)
    170 {
    171 	char buf[16];
    172 	int size = sizeof(buf);
    173 	char *path;
    174 	asprintf(&path, "%s/%s/mtu", sys_net_path, interface);
    175 	int success = readFile(path, buf, &size);
    176 	if (!success && mtu)
    177 		*mtu = atoi(buf);
    178 	free(path);
    179 	return success;
    180 
    181 }
    182 
    183 int InterfaceController::setMtu(const char *interface, const char *mtu)
    184 {
    185 	char *path;
    186 	asprintf(&path, "%s/%s/mtu", sys_net_path, interface);
    187 	int success = writeFile(path, mtu, strlen(mtu));
    188 	free(path);
    189 	return success;
    190 }
    191