• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
InterfaceController()49 InterfaceController::InterfaceController()
50 	: sendCommand_(NULL) {
51 	// Initial IPv6 settings.
52 	// By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces.
53 	// This causes RAs to work or not work based on whether forwarding is on, and causes routes
54 	// learned from RAs to go away when forwarding is turned on. Make this behaviour predictable
55 	// by always setting accept_ra to 2.
56 	setAcceptRA("2");
57 
58 	libh_ = dlopen(if_cmd_lib_file_name, RTLD_NOW | RTLD_LOCAL);
59 	if (libh_ == NULL) {
60 		const char *err_str = dlerror();
61 		ALOGW("Warning (%s) while opening the net interface command library", err_str ? err_str : "unknown");
62 	} else {
63 		sendCommandInit_ = (int (*)(void))dlsym(libh_, set_cmd_init_func_name);
64 		if (sendCommandInit_ == NULL) {
65 			const char *err_str = dlerror();
66 			ALOGW("Error (%s) while searching for the interface command init function", err_str ? err_str : "unknown");
67 		} else if (sendCommandInit_()) {
68 			ALOGE("Can't init the interface command API");
69 			return;
70 		}
71 		sendCommandFini_ = (int (*)(void))dlsym(libh_, set_cmd_fini_func_name);
72 		if (sendCommandFini_ == NULL) {
73 			const char *err_str = dlerror();
74 			ALOGW("Error (%s) while searching for the interface command fini function", err_str ? err_str : "unknown");
75 		}
76 		sendCommand_ = (int (*)(int, char **, char **))dlsym(libh_, set_cmd_func_name);
77 		if (sendCommand_ == NULL) {
78 			const char *err_str = dlerror();
79 			ALOGE("Error (%s) while searching for the interface command function", err_str ? err_str : "unknown");
80 			return;
81 		}
82 	}
83 }
84 
~InterfaceController()85 InterfaceController::~InterfaceController() {
86 	if (sendCommandFini_) {
87 		if (sendCommandFini_()) {
88 			ALOGE("Can't shutdown the interface command API");
89 		}
90 	}
91 	if (libh_) {
92 		int err = dlclose(libh_);
93 		if (err) {
94 			const char *err_str = dlerror();
95 			ALOGE("Error (%s) while closing the net interface command library", err_str ? err_str : "unknown");
96 		}
97 	}
98 }
99 
100 /*
101  * Arguments:
102  *	  argv[2] - wlan interface
103  *	  argv[3] - command
104  *	  argv[4] - argument
105  *	  rbuf	- returned buffer
106  */
interfaceCommand(int argc,char * argv[],char ** rbuf)107 int InterfaceController::interfaceCommand(int argc, char *argv[], char **rbuf) {
108 	int ret = -ENOSYS;
109 	if (sendCommand_)
110 		ret = sendCommand_(argc, argv, rbuf);
111 
112 	return ret;
113 }
114 
writeIPv6ProcPath(const char * interface,const char * setting,const char * value)115 int InterfaceController::writeIPv6ProcPath(const char *interface, const char *setting, const char *value) {
116 	char *path;
117 	asprintf(&path, "%s/%s/%s", ipv6_proc_path, interface, setting);
118 	int success = writeFile(path, value, strlen(value));
119 	free(path);
120 	return success;
121 }
122 
setEnableIPv6(const char * interface,const int on)123 int InterfaceController::setEnableIPv6(const char *interface, const int on) {
124 	// When disable_ipv6 changes from 1 to 0, the kernel starts autoconf.
125 	// When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf
126 	// addresses and routes and disables IPv6 on the interface.
127 	const char *disable_ipv6 = on ? "0" : "1";
128 	return writeIPv6ProcPath(interface, "disable_ipv6", disable_ipv6);
129 }
130 
setIPv6PrivacyExtensions(const char * interface,const int on)131 int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) {
132 	// 0: disable IPv6 privacy addresses
133 	// 0: enable IPv6 privacy addresses and prefer them over non-privacy ones.
134 	return writeIPv6ProcPath(interface, "use_tempaddr", on ? "2" : "0");
135 }
136 
isInterfaceName(const char * name)137 int InterfaceController::isInterfaceName(const char *name) {
138 	return strcmp(name, ".") &&
139 		strcmp(name, "..") &&
140 		strcmp(name, "default") &&
141 		strcmp(name, "all");
142 }
143 
setAcceptRA(const char * value)144 int InterfaceController::setAcceptRA(const char *value) {
145 	// Set the default value, which is used by any interfaces that are created in the future.
146 	writeIPv6ProcPath("default", "accept_ra", value);
147 
148 	// Set the value on all the interfaces.
149 	DIR *dir = opendir(ipv6_proc_path);
150 	if (!dir) {
151 		ALOGE("Can't list %s: %s", ipv6_proc_path, strerror(errno));
152 		return -errno;
153 	}
154 	struct dirent *d;
155 	while((d = readdir(dir)) != NULL) {
156 		if (d->d_type == DT_DIR && isInterfaceName(d->d_name)) {
157 			if (writeIPv6ProcPath(d->d_name, "accept_ra", value) < 0) {
158 				ALOGE("Can't write to %s/%s/accept_ra: %s", ipv6_proc_path,
159 				      d->d_name, strerror(errno));
160 			}
161 		}
162 	}
163 	closedir(dir);
164 	return 0;
165 }
166