Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 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 | * |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 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 | |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 22 | #include <dlfcn.h> |
| 23 | |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 24 | #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 Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame^] | 37 | #include "NetdConstants.h" |
| 38 | |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 39 | #include "InterfaceController.h" |
| 40 | |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 41 | char if_cmd_lib_file_name[] = "/system/lib/libnetcmdiface.so"; |
| 42 | char set_cmd_func_name[] = "net_iface_send_command"; |
| 43 | char set_cmd_init_func_name[] = "net_iface_send_command_init"; |
| 44 | char set_cmd_fini_func_name[] = "net_iface_send_command_fini"; |
| 45 | |
| 46 | InterfaceController::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 Levitskiy | 87482aa | 2012-09-11 11:04:29 -0700 | [diff] [blame] | 51 | ALOGW("Warning (%s) while opening the net interface command library", err_str ? err_str : "unknown"); |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 52 | } 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 Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | InterfaceController::~InterfaceController() { |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 76 | 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 Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Arguments: |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 92 | * argv[2] - wlan interface |
| 93 | * argv[3] - command |
| 94 | * argv[4] - argument |
| 95 | * rbuf - returned buffer |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 96 | */ |
| 97 | int InterfaceController::interfaceCommand(int argc, char *argv[], char **rbuf) { |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 98 | int ret = -ENOSYS; |
| 99 | if (sendCommand_) |
| 100 | ret = sendCommand_(argc, argv, rbuf); |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 101 | |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 102 | return ret; |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 103 | } |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame^] | 104 | |
| 105 | int 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 | |
| 113 | int InterfaceController::setEnableIPv6(const char *interface, const int on) { |
| 114 | // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf. |
| 115 | // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf |
| 116 | // addresses and routes and disables IPv6 on the interface. |
| 117 | const char *disable_ipv6 = on ? "0" : "1"; |
| 118 | return writeIPv6ProcPath(interface, "disable_ipv6", disable_ipv6); |
| 119 | } |
| 120 | |
| 121 | int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) { |
| 122 | // 0: disable IPv6 privacy addresses |
| 123 | // 0: enable IPv6 privacy addresses and prefer them over non-privacy ones. |
| 124 | return writeIPv6ProcPath(interface, "use_tempaddr", on ? "2" : "0"); |
| 125 | } |