blob: 00ab6d1efc51f8e40bc88633686a9dcd0eb997ef [file] [log] [blame]
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -07001/*
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 *
Sasha Levitskiy329c3b42012-07-30 16:11:23 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -07009 *
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
Sasha Levitskiy329c3b42012-07-30 16:11:23 -070022#include <dlfcn.h>
23
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070024#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/ioctl.h>
27#include <sys/types.h>
28
29#include <netinet/in.h>
30#include <arpa/inet.h>
31
32#define LOG_TAG "InterfaceController"
33#include <cutils/log.h>
34#include <netutils/ifc.h>
35#include <private/android_filesystem_config.h>
36
Lorenzo Colitti70afde62013-03-04 17:58:40 +090037#include "NetdConstants.h"
38
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070039#include "InterfaceController.h"
40
Sasha Levitskiy329c3b42012-07-30 16:11:23 -070041char if_cmd_lib_file_name[] = "/system/lib/libnetcmdiface.so";
42char set_cmd_func_name[] = "net_iface_send_command";
43char set_cmd_init_func_name[] = "net_iface_send_command_init";
44char set_cmd_fini_func_name[] = "net_iface_send_command_fini";
45
46InterfaceController::InterfaceController()
47 : sendCommand_(NULL) {
48 libh_ = dlopen(if_cmd_lib_file_name, RTLD_NOW | RTLD_LOCAL);
49 if (libh_ == NULL) {
50 const char *err_str = dlerror();
Sasha Levitskiy87482aa2012-09-11 11:04:29 -070051 ALOGW("Warning (%s) while opening the net interface command library", err_str ? err_str : "unknown");
Sasha Levitskiy329c3b42012-07-30 16:11:23 -070052 } else {
53 sendCommandInit_ = (int (*)(void))dlsym(libh_, set_cmd_init_func_name);
54 if (sendCommandInit_ == NULL) {
55 const char *err_str = dlerror();
56 ALOGW("Error (%s) while searching for the interface command init function", err_str ? err_str : "unknown");
57 } else if (sendCommandInit_()) {
58 ALOGE("Can't init the interface command API");
59 return;
60 }
61 sendCommandFini_ = (int (*)(void))dlsym(libh_, set_cmd_fini_func_name);
62 if (sendCommandFini_ == NULL) {
63 const char *err_str = dlerror();
64 ALOGW("Error (%s) while searching for the interface command fini function", err_str ? err_str : "unknown");
65 }
66 sendCommand_ = (int (*)(int, char **, char **))dlsym(libh_, set_cmd_func_name);
67 if (sendCommand_ == NULL) {
68 const char *err_str = dlerror();
69 ALOGE("Error (%s) while searching for the interface command function", err_str ? err_str : "unknown");
70 return;
71 }
72 }
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070073}
74
75InterfaceController::~InterfaceController() {
Sasha Levitskiy329c3b42012-07-30 16:11:23 -070076 if (sendCommandFini_) {
77 if (sendCommandFini_()) {
78 ALOGE("Can't shutdown the interface command API");
79 }
80 }
81 if (libh_) {
82 int err = dlclose(libh_);
83 if (err) {
84 const char *err_str = dlerror();
85 ALOGE("Error (%s) while closing the net interface command library", err_str ? err_str : "unknown");
86 }
87 }
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070088}
89
90/*
91 * Arguments:
Sasha Levitskiy329c3b42012-07-30 16:11:23 -070092 * argv[2] - wlan interface
93 * argv[3] - command
94 * argv[4] - argument
95 * rbuf - returned buffer
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070096 */
97int InterfaceController::interfaceCommand(int argc, char *argv[], char **rbuf) {
Sasha Levitskiy329c3b42012-07-30 16:11:23 -070098 int ret = -ENOSYS;
99 if (sendCommand_)
100 ret = sendCommand_(argc, argv, rbuf);
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -0700101
Sasha Levitskiy329c3b42012-07-30 16:11:23 -0700102 return ret;
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -0700103}
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900104
105int InterfaceController::writeIPv6ProcPath(const char *interface, const char *setting, const char *value) {
106 char *path;
107 asprintf(&path, "/proc/sys/net/ipv6/conf/%s/%s", interface, setting);
108 int success = writeFile(path, value, strlen(value));
109 free(path);
110 return success;
111}
112
113int InterfaceController::setEnableIPv6(const char *interface, const int on) {
Lorenzo Colitticfd1a582013-03-05 10:04:00 +0900114 // When IPv6 is on, accept RAs regardless of forwarding state.
115 // When IPv6 is off, accept RAs only if forwarding is off (the default).
116 const char *accept_ra = on ? "2" : "1";
117 if (writeIPv6ProcPath(interface, "accept_ra", accept_ra)) {
118 return -1;
119 }
120
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900121 // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf.
122 // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf
123 // addresses and routes and disables IPv6 on the interface.
124 const char *disable_ipv6 = on ? "0" : "1";
125 return writeIPv6ProcPath(interface, "disable_ipv6", disable_ipv6);
126}
127
128int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) {
129 // 0: disable IPv6 privacy addresses
130 // 0: enable IPv6 privacy addresses and prefer them over non-privacy ones.
131 return writeIPv6ProcPath(interface, "use_tempaddr", on ? "2" : "0");
132}