Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 5 | #include "shill/device_info.h" |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 6 | |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 7 | #include <arpa/inet.h> |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 8 | #include <fcntl.h> |
| 9 | #include <linux/if_tun.h> |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 10 | #include <linux/netlink.h> |
| 11 | #include <linux/rtnetlink.h> |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 12 | #include <net/if.h> |
| 13 | #include <net/if_arp.h> |
| 14 | #include <netinet/ether.h> |
| 15 | #include <string.h> |
| 16 | #include <sys/ioctl.h> |
| 17 | #include <sys/socket.h> |
| 18 | #include <time.h> |
| 19 | #include <unistd.h> |
| 20 | |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 23 | #include <base/bind.h> |
Paul Stewart | bf1861b | 2011-08-23 15:45:35 -0700 | [diff] [blame] | 24 | #include <base/file_util.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 25 | #include <base/logging.h> |
| 26 | #include <base/memory/scoped_ptr.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 27 | #include <base/stl_util.h> |
Paul Stewart | 2001a42 | 2011-12-15 10:20:09 -0800 | [diff] [blame] | 28 | #include <base/string_number_conversions.h> |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 29 | #include <base/string_util.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 30 | #include <base/stringprintf.h> |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 31 | |
| 32 | #include "shill/control_interface.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 33 | #include "shill/device.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 34 | #include "shill/device_stub.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 35 | #include "shill/ethernet.h" |
| 36 | #include "shill/manager.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 37 | #include "shill/rtnl_handler.h" |
| 38 | #include "shill/rtnl_listener.h" |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 39 | #include "shill/rtnl_message.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 40 | #include "shill/scope_logger.h" |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 41 | #include "shill/service.h" |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 42 | #include "shill/virtio_ethernet.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 43 | #include "shill/wifi.h" |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 44 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 45 | using base::Bind; |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 46 | using base::StringPrintf; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 47 | using base::Unretained; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 48 | using std::map; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 49 | using std::string; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 50 | using std::vector; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 51 | |
| 52 | namespace shill { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 53 | |
| 54 | // static |
Jason Glasgow | abc5403 | 2012-04-20 16:08:32 -0400 | [diff] [blame] | 55 | const char DeviceInfo::kModemPseudoDeviceNamePrefix[] = "pseudomodem"; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 56 | const char DeviceInfo::kDeviceInfoRoot[] = "/sys/class/net"; |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 57 | const char DeviceInfo::kDriverVirtioNet[] = "virtio_net"; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 58 | const char DeviceInfo::kInterfaceUevent[] = "uevent"; |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 59 | const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n"; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 60 | const char DeviceInfo::kInterfaceDevice[] = "device"; |
| 61 | const char DeviceInfo::kInterfaceDriver[] = "device/driver"; |
| 62 | const char DeviceInfo::kInterfaceTunFlags[] = "tun_flags"; |
| 63 | const char DeviceInfo::kInterfaceType[] = "type"; |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 64 | const char DeviceInfo::kDriverCdcEther[] = "cdc_ether"; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 65 | const char *DeviceInfo::kModemDrivers[] = { |
| 66 | "gobi", |
| 67 | "QCUSBNet2k", |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 68 | "GobiNet" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 69 | }; |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 70 | const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun"; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 71 | |
| 72 | DeviceInfo::DeviceInfo(ControlInterface *control_interface, |
| 73 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 74 | Metrics *metrics, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 75 | Manager *manager) |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 76 | : control_interface_(control_interface), |
| 77 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 78 | metrics_(metrics), |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 79 | manager_(manager), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 80 | link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))), |
| 81 | address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 82 | link_listener_(NULL), |
| 83 | address_listener_(NULL), |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 84 | device_info_root_(kDeviceInfoRoot), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 85 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 88 | DeviceInfo::~DeviceInfo() {} |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 89 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 90 | void DeviceInfo::AddDeviceToBlackList(const string &device_name) { |
| 91 | black_list_.insert(device_name); |
| 92 | } |
| 93 | |
Eric Shienbrood | 5e628a5 | 2012-03-21 16:56:59 -0400 | [diff] [blame] | 94 | bool DeviceInfo::IsDeviceBlackListed(const string &device_name) { |
| 95 | return ContainsKey(black_list_, device_name); |
| 96 | } |
| 97 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 98 | void DeviceInfo::Start() { |
| 99 | link_listener_.reset( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 100 | new RTNLListener(RTNLHandler::kRequestLink, link_callback_)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 101 | address_listener_.reset( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 102 | new RTNLListener(RTNLHandler::kRequestAddr, address_callback_)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 103 | rtnl_handler_->RequestDump(RTNLHandler::kRequestLink | |
| 104 | RTNLHandler::kRequestAddr); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 107 | void DeviceInfo::Stop() { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 108 | link_listener_.reset(); |
| 109 | address_listener_.reset(); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 112 | void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 113 | SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", " |
| 114 | << device->interface_index() << ")"; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 115 | CHECK(!GetDevice(device->interface_index()).get()); |
| 116 | infos_[device->interface_index()].device = device; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 117 | if (device->TechnologyIs(Technology::kCellular) || |
| 118 | device->TechnologyIs(Technology::kEthernet) || |
| 119 | device->TechnologyIs(Technology::kWifi)) { |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 120 | manager_->RegisterDevice(device); |
| 121 | } |
| 122 | } |
| 123 | |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 124 | void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) { |
| 125 | int interface_index = device->interface_index(); |
| 126 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 127 | SLOG(Device, 2) << __func__ << "(" << device->link_name() << ", " |
| 128 | << interface_index << ")"; |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 129 | CHECK(device->TechnologyIs(Technology::kCellular)); |
| 130 | |
| 131 | // Release reference to the device |
| 132 | map<int, Info>::iterator iter = infos_.find(interface_index); |
| 133 | if (iter != infos_.end()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 134 | SLOG(Device, 2) << "Removing device from info for index: " |
| 135 | << interface_index; |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 136 | manager_->DeregisterDevice(device); |
| 137 | // Release the reference to the device, but maintain the mapping |
| 138 | // for the index. That will be cleaned up by an RTNL message. |
| 139 | iter->second.device = NULL; |
| 140 | } |
| 141 | } |
| 142 | |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 143 | FilePath DeviceInfo::GetDeviceInfoPath(const string &iface_name, |
| 144 | const string &path_name) { |
| 145 | return device_info_root_.Append(iface_name).Append(path_name); |
| 146 | } |
| 147 | |
| 148 | bool DeviceInfo::GetDeviceInfoContents(const string &iface_name, |
| 149 | const string &path_name, |
| 150 | string *contents_out) { |
| 151 | return file_util::ReadFileToString(GetDeviceInfoPath(iface_name, path_name), |
| 152 | contents_out); |
| 153 | } |
| 154 | bool DeviceInfo::GetDeviceInfoSymbolicLink(const string &iface_name, |
| 155 | const string &path_name, |
| 156 | FilePath *path_out) { |
| 157 | return file_util::ReadSymbolicLink(GetDeviceInfoPath(iface_name, path_name), |
| 158 | path_out); |
| 159 | } |
| 160 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 161 | Technology::Identifier DeviceInfo::GetDeviceTechnology( |
| 162 | const string &iface_name) { |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 163 | string type_string; |
| 164 | int arp_type = ARPHRD_VOID;; |
| 165 | if (GetDeviceInfoContents(iface_name, kInterfaceType, &type_string) && |
| 166 | TrimString(type_string, "\n", &type_string) && |
| 167 | !base::StringToInt(type_string, &arp_type)) { |
| 168 | arp_type = ARPHRD_VOID; |
| 169 | } |
| 170 | |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 171 | string contents; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 172 | if (!GetDeviceInfoContents(iface_name, kInterfaceUevent, &contents)) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 173 | SLOG(Device, 2) << StringPrintf("%s: device %s has no uevent file", |
| 174 | __func__, iface_name.c_str()); |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 175 | return Technology::kUnknown; |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 176 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 177 | |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 178 | // If the "uevent" file contains the string "DEVTYPE=wlan\n" at the |
| 179 | // start of the file or after a newline, we can safely assume this |
| 180 | // is a wifi device. |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 181 | if (contents.find(kInterfaceUeventWifiSignature) != string::npos) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 182 | SLOG(Device, 2) |
| 183 | << StringPrintf("%s: device %s has wifi signature in uevent file", |
| 184 | __func__, iface_name.c_str()); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 185 | if (arp_type == ARPHRD_IEEE80211_RADIOTAP) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 186 | SLOG(Device, 2) << StringPrintf("%s: wifi device %s is in monitor mode", |
| 187 | __func__, iface_name.c_str()); |
Paul Stewart | 2001a42 | 2011-12-15 10:20:09 -0800 | [diff] [blame] | 188 | return Technology::kWiFiMonitor; |
| 189 | } |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 190 | return Technology::kWifi; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 193 | FilePath driver_path; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 194 | if (!GetDeviceInfoSymbolicLink(iface_name, kInterfaceDriver, &driver_path)) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 195 | SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink", |
| 196 | __func__, iface_name.c_str()); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 197 | if (arp_type == ARPHRD_LOOPBACK) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 198 | SLOG(Device, 2) << StringPrintf("%s: device %s is a loopback device", |
| 199 | __func__, iface_name.c_str()); |
Paul Stewart | e81eb70 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 200 | return Technology::kLoopback; |
| 201 | } |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 202 | if (arp_type == ARPHRD_PPP) { |
| 203 | SLOG(Device, 2) << StringPrintf("%s: device %s is a ppp device", |
| 204 | __func__, iface_name.c_str()); |
| 205 | return Technology::kPPP; |
| 206 | } |
| 207 | string tun_flags_str; |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 208 | int tun_flags = 0; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 209 | if (GetDeviceInfoContents(iface_name, kInterfaceTunFlags, &tun_flags_str) && |
| 210 | TrimString(tun_flags_str, "\n", &tun_flags_str) && |
| 211 | base::HexStringToInt(tun_flags_str, &tun_flags) && |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 212 | (tun_flags & IFF_TUN)) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 213 | SLOG(Device, 2) << StringPrintf("%s: device %s is tun device", |
| 214 | __func__, iface_name.c_str()); |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 215 | return Technology::kTunnel; |
| 216 | } |
Paul Stewart | e81eb70 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 217 | return Technology::kUnknown; |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | string driver_name(driver_path.BaseName().value()); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 221 | // See if driver for this interface is in a list of known modem driver names. |
| 222 | for (size_t modem_idx = 0; modem_idx < arraysize(kModemDrivers); |
| 223 | ++modem_idx) { |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 224 | if (driver_name == kModemDrivers[modem_idx]) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 225 | SLOG(Device, 2) |
| 226 | << StringPrintf("%s: device %s is matched with modem driver %s", |
| 227 | __func__, iface_name.c_str(), driver_name.c_str()); |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 228 | return Technology::kCellular; |
| 229 | } |
| 230 | } |
| 231 | |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 232 | // For cdc_ether devices, make sure it's a modem because this driver |
| 233 | // can be used for other ethernet devices. |
| 234 | if (driver_name == kDriverCdcEther && |
| 235 | IsCdcEtherModemDevice(iface_name)) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 236 | SLOG(Device, 2) << StringPrintf("%s: device %s is a modem cdc_ether device", |
| 237 | __func__, iface_name.c_str()); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 238 | return Technology::kCellular; |
| 239 | } |
| 240 | |
Jason Glasgow | abc5403 | 2012-04-20 16:08:32 -0400 | [diff] [blame] | 241 | // Special case for pseudo modems which are used for testing |
| 242 | if (iface_name.find(kModemPseudoDeviceNamePrefix) == 0) { |
| 243 | SLOG(Device, 2) << StringPrintf( |
| 244 | "%s: device %s is a pseudo modem for testing", |
| 245 | __func__, iface_name.c_str()); |
| 246 | return Technology::kCellular; |
| 247 | } |
| 248 | |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 249 | // Special case for the virtio driver, used when run under KVM. See also |
| 250 | // the comment in VirtioEthernet::Start. |
| 251 | if (driver_name == kDriverVirtioNet) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 252 | SLOG(Device, 2) << StringPrintf("%s: device %s is virtio ethernet", |
| 253 | __func__, iface_name.c_str()); |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 254 | return Technology::kVirtioEthernet; |
| 255 | } |
| 256 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 257 | SLOG(Device, 2) << StringPrintf("%s: device %s, with driver %s, " |
| 258 | "is defaulted to type ethernet", |
| 259 | __func__, iface_name.c_str(), |
| 260 | driver_name.c_str()); |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 261 | return Technology::kEthernet; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 264 | bool DeviceInfo::IsCdcEtherModemDevice(const std::string &iface_name) { |
| 265 | // A cdc_ether device is a modem device if it also exposes tty interfaces. |
| 266 | // To determine this, we look for the existence of the tty interface in the |
| 267 | // USB device sysfs tree. |
| 268 | // |
| 269 | // A typical sysfs dir hierarchy for a cdc_ether modem USB device is as |
| 270 | // follows: |
| 271 | // |
| 272 | // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2 |
| 273 | // 1-2:1.0 |
| 274 | // tty |
| 275 | // ttyACM0 |
| 276 | // 1-2:1.1 |
| 277 | // net |
| 278 | // usb0 |
| 279 | // 1-2:1.2 |
| 280 | // tty |
| 281 | // ttyACM1 |
| 282 | // ... |
| 283 | // |
| 284 | // /sys/class/net/usb0/device symlinks to |
| 285 | // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/1-2:1.1 |
Thieu Le | b27beee | 2012-04-20 09:19:06 -0700 | [diff] [blame] | 286 | // |
| 287 | // Note that some modem devices have the tty directory one level deeper |
| 288 | // (eg. E362), so the device tree for the tty interface is: |
| 289 | // /sys/devices/pci0000:00/0000:00:1d.7/usb/1-2/1-2:1.0/ttyUSB0/tty/ttyUSB0 |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 290 | |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 291 | FilePath device_file = GetDeviceInfoPath(iface_name, kInterfaceDevice); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 292 | FilePath device_path; |
| 293 | if (!file_util::ReadSymbolicLink(device_file, &device_path)) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 294 | SLOG(Device, 2) << StringPrintf("%s: device %s has no device symlink", |
| 295 | __func__, iface_name.c_str()); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 296 | return false; |
| 297 | } |
| 298 | if (!device_path.IsAbsolute()) { |
| 299 | device_path = device_file.DirName().Append(device_path); |
| 300 | file_util::AbsolutePath(&device_path); |
| 301 | } |
| 302 | |
| 303 | // Look for tty interface by enumerating all directories under the parent |
Thieu Le | b27beee | 2012-04-20 09:19:06 -0700 | [diff] [blame] | 304 | // USB device and see if there's a subdirectory "tty" inside. In other |
| 305 | // words, using the example dir hierarchy above, find |
| 306 | // /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-2/.../tty. |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 307 | // If this exists, then this is a modem device. |
Thieu Le | b27beee | 2012-04-20 09:19:06 -0700 | [diff] [blame] | 308 | return HasSubdir(device_path.DirName(), FilePath("tty")); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | // static |
Thieu Le | b27beee | 2012-04-20 09:19:06 -0700 | [diff] [blame] | 312 | bool DeviceInfo::HasSubdir(const FilePath &base_dir, const FilePath &subdir) { |
| 313 | file_util::FileEnumerator::FileType type = |
| 314 | static_cast<file_util::FileEnumerator::FileType>( |
| 315 | file_util::FileEnumerator::DIRECTORIES | |
| 316 | file_util::FileEnumerator::SHOW_SYM_LINKS); |
| 317 | file_util::FileEnumerator dir_enum(base_dir, true, type); |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 318 | for (FilePath curr_dir = dir_enum.Next(); !curr_dir.empty(); |
| 319 | curr_dir = dir_enum.Next()) { |
Thieu Le | b27beee | 2012-04-20 09:19:06 -0700 | [diff] [blame] | 320 | if (curr_dir.BaseName() == subdir) |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 321 | return true; |
| 322 | } |
| 323 | return false; |
| 324 | } |
| 325 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 326 | void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 327 | DCHECK(msg.type() == RTNLMessage::kTypeLink && |
| 328 | msg.mode() == RTNLMessage::kModeAdd); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 329 | int dev_index = msg.interface_index(); |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 330 | Technology::Identifier technology = Technology::kUnknown; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 331 | |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 332 | unsigned int flags = msg.link_status().flags; |
| 333 | unsigned int change = msg.link_status().change; |
Paul Stewart | 536820d | 2012-03-19 16:05:59 -0700 | [diff] [blame] | 334 | bool new_device = !ContainsKey(infos_, dev_index); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 335 | SLOG(Device, 2) << __func__ << "(index=" << dev_index |
| 336 | << std::showbase << std::hex |
| 337 | << ", flags=" << flags << ", change=" << change << ")" |
| 338 | << std::dec << std::noshowbase |
| 339 | << ", new_device=" << new_device; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 340 | infos_[dev_index].flags = flags; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 341 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 342 | DeviceRefPtr device = GetDevice(dev_index); |
| 343 | if (!device.get()) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 344 | if (!msg.HasAttribute(IFLA_IFNAME)) { |
| 345 | LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!"; |
| 346 | return; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 347 | } |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 348 | ByteString b(msg.GetAttribute(IFLA_IFNAME)); |
| 349 | string link_name(reinterpret_cast<const char*>(b.GetConstData())); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 350 | SLOG(Device, 2) << "add link index " << dev_index << " name " << link_name; |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 351 | infos_[dev_index].name = link_name; |
| 352 | indices_[link_name] = dev_index; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 353 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 354 | if (!link_name.empty()) { |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 355 | if (ContainsKey(black_list_, link_name)) { |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 356 | technology = Technology::kBlacklisted; |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 357 | } else { |
| 358 | technology = GetDeviceTechnology(link_name); |
| 359 | } |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 360 | } |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 361 | string address; |
| 362 | if (msg.HasAttribute(IFLA_ADDRESS)) { |
| 363 | infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS); |
| 364 | address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode()); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 365 | SLOG(Device, 2) << "link index " << dev_index << " address " |
| 366 | << infos_[dev_index].mac_address.HexEncode(); |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 367 | } else if (technology != Technology::kTunnel && |
| 368 | technology != Technology::kPPP) { |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 369 | LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!"; |
| 370 | return; |
| 371 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 372 | switch (technology) { |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 373 | case Technology::kCellular: |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 374 | // Cellular devices are managed by ModemInfo. |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 375 | SLOG(Device, 2) << "Cellular link " << link_name |
| 376 | << " at index " << dev_index |
| 377 | << " -- notifying ModemInfo."; |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 378 | manager_->modem_info()->OnDeviceInfoAvailable(link_name); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 379 | return; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 380 | case Technology::kEthernet: |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 381 | device = new Ethernet(control_interface_, dispatcher_, metrics_, |
| 382 | manager_, link_name, address, dev_index); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 383 | device->EnableIPv6Privacy(); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 384 | break; |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 385 | case Technology::kVirtioEthernet: |
| 386 | device = new VirtioEthernet(control_interface_, dispatcher_, metrics_, |
| 387 | manager_, link_name, address, dev_index); |
| 388 | device->EnableIPv6Privacy(); |
| 389 | break; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 390 | case Technology::kWifi: |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 391 | device = new WiFi(control_interface_, dispatcher_, metrics_, manager_, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 392 | link_name, address, dev_index); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 393 | device->EnableIPv6Privacy(); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 394 | break; |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 395 | case Technology::kPPP: |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 396 | case Technology::kTunnel: |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 397 | // Tunnel and PPP devices are managed by the VPN code (PPP for |
| 398 | // l2tpipsec). Notify the VPN Provider only if this is the first |
| 399 | // time we have seen this device index. |
Paul Stewart | 536820d | 2012-03-19 16:05:59 -0700 | [diff] [blame] | 400 | if (new_device) { |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 401 | SLOG(Device, 2) << "Tunnel / PPP link " << link_name |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 402 | << " at index " << dev_index |
| 403 | << " -- notifying VPNProvider."; |
Paul Stewart | 536820d | 2012-03-19 16:05:59 -0700 | [diff] [blame] | 404 | if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name, |
Paul Stewart | ca876ee | 2012-04-21 08:55:58 -0700 | [diff] [blame] | 405 | dev_index) && |
| 406 | technology == Technology::kTunnel) { |
Paul Stewart | 536820d | 2012-03-19 16:05:59 -0700 | [diff] [blame] | 407 | // If VPN does not know anything about this tunnel, it is probably |
| 408 | // left over from a previous instance and should not exist. |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 409 | SLOG(Device, 2) << "Tunnel link is unused. Deleting."; |
Paul Stewart | 536820d | 2012-03-19 16:05:59 -0700 | [diff] [blame] | 410 | DeleteInterface(dev_index); |
| 411 | } |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 412 | } |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 413 | return; |
Paul Stewart | e81eb70 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 414 | case Technology::kLoopback: |
| 415 | // Loopback devices are largely ignored, but we should make sure the |
| 416 | // link is enabled. |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 417 | SLOG(Device, 2) << "Bringing up loopback device " << link_name |
| 418 | << " at index " << dev_index; |
Paul Stewart | e81eb70 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 419 | rtnl_handler_->SetInterfaceFlags(dev_index, IFF_UP, IFF_UP); |
| 420 | return; |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 421 | default: |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 422 | device = new DeviceStub(control_interface_, dispatcher_, metrics_, |
| 423 | manager_, link_name, address, dev_index, |
| 424 | technology); |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 425 | break; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 426 | } |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 427 | RegisterDevice(device); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 428 | } |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 429 | device->LinkEvent(flags, change); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 432 | void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 433 | SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() << ")"; |
mukesh agrawal | 47009f8 | 2011-08-25 14:07:35 -0700 | [diff] [blame] | 434 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 435 | DCHECK(msg.type() == RTNLMessage::kTypeLink && |
| 436 | msg.mode() == RTNLMessage::kModeDelete); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 437 | SLOG(Device, 2) << __func__ << "(index=" << msg.interface_index() |
| 438 | << std::showbase << std::hex |
| 439 | << ", flags=" << msg.link_status().flags |
| 440 | << ", change=" << msg.link_status().change << ")"; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 441 | RemoveInfo(msg.interface_index()); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 442 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 443 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 444 | DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const { |
| 445 | const Info *info = GetInfo(interface_index); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 446 | return info ? info->device : NULL; |
| 447 | } |
| 448 | |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 449 | int DeviceInfo::GetIndex(const string &interface_name) const { |
| 450 | map<string, int>::const_iterator it = indices_.find(interface_name); |
| 451 | return it == indices_.end() ? -1 : it->second; |
| 452 | } |
| 453 | |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 454 | bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const { |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 455 | const Info *info = GetInfo(interface_index); |
| 456 | if (!info) { |
| 457 | return false; |
| 458 | } |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 459 | *address = info->mac_address; |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 460 | return true; |
| 461 | } |
| 462 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 463 | bool DeviceInfo::GetAddresses(int interface_index, |
| 464 | vector<AddressData> *addresses) const { |
| 465 | const Info *info = GetInfo(interface_index); |
| 466 | if (!info) { |
| 467 | return false; |
| 468 | } |
| 469 | *addresses = info->ip_addresses; |
| 470 | return true; |
| 471 | } |
| 472 | |
| 473 | void DeviceInfo::FlushAddresses(int interface_index) const { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 474 | SLOG(Device, 2) << __func__ << "(" << interface_index << ")"; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 475 | const Info *info = GetInfo(interface_index); |
| 476 | if (!info) { |
| 477 | return; |
| 478 | } |
| 479 | const vector<AddressData> &addresses = info->ip_addresses; |
| 480 | vector<AddressData>::const_iterator iter; |
| 481 | for (iter = addresses.begin(); iter != addresses.end(); ++iter) { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 482 | if (iter->address.family() == IPAddress::kFamilyIPv4 || |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 483 | (iter->scope == RT_SCOPE_UNIVERSE && |
| 484 | (iter->flags & ~IFA_F_TEMPORARY) == 0)) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 485 | SLOG(Device, 2) << __func__ << ": removing ip address from interface " |
| 486 | << interface_index; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 487 | rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address); |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 492 | bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const { |
| 493 | const Info *info = GetInfo(interface_index); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 494 | if (!info) { |
| 495 | return false; |
| 496 | } |
| 497 | *flags = info->flags; |
| 498 | return true; |
| 499 | } |
| 500 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 501 | bool DeviceInfo::CreateTunnelInterface(string *interface_name) const { |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 502 | int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR)); |
| 503 | if (fd < 0) { |
| 504 | PLOG(ERROR) << "failed to open " << kTunDeviceName; |
| 505 | return false; |
| 506 | } |
| 507 | file_util::ScopedFD scoped_fd(&fd); |
| 508 | |
| 509 | struct ifreq ifr; |
| 510 | memset(&ifr, 0, sizeof(ifr)); |
| 511 | ifr.ifr_flags = IFF_TUN | IFF_NO_PI; |
| 512 | if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) { |
| 513 | PLOG(ERROR) << "failed to create tunnel interface"; |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) { |
| 518 | PLOG(ERROR) << "failed to set tunnel interface to be persistent"; |
| 519 | return false; |
| 520 | } |
| 521 | |
| 522 | *interface_name = string(ifr.ifr_name); |
| 523 | |
| 524 | return true; |
| 525 | } |
| 526 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 527 | bool DeviceInfo::DeleteInterface(int interface_index) const { |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 528 | return rtnl_handler_->RemoveInterface(interface_index); |
| 529 | } |
| 530 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 531 | const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const { |
| 532 | map<int, Info>::const_iterator iter = infos_.find(interface_index); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 533 | if (iter == infos_.end()) { |
| 534 | return NULL; |
| 535 | } |
| 536 | return &iter->second; |
| 537 | } |
| 538 | |
| 539 | void DeviceInfo::RemoveInfo(int interface_index) { |
| 540 | map<int, Info>::iterator iter = infos_.find(interface_index); |
| 541 | if (iter != infos_.end()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 542 | SLOG(Device, 2) << "Removing info for device index: " << interface_index; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 543 | if (iter->second.device.get()) { |
| 544 | manager_->DeregisterDevice(iter->second.device); |
| 545 | } |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 546 | indices_.erase(iter->second.name); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 547 | infos_.erase(iter); |
mukesh agrawal | 47009f8 | 2011-08-25 14:07:35 -0700 | [diff] [blame] | 548 | } else { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 549 | SLOG(Device, 2) << __func__ << ": Unknown device index: " |
| 550 | << interface_index; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 551 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 554 | void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 555 | DCHECK(msg.type() == RTNLMessage::kTypeLink); |
| 556 | if (msg.mode() == RTNLMessage::kModeAdd) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 557 | AddLinkMsgHandler(msg); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 558 | } else if (msg.mode() == RTNLMessage::kModeDelete) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 559 | DelLinkMsgHandler(msg); |
| 560 | } else { |
| 561 | NOTREACHED(); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 562 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 565 | void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 566 | SLOG(Device, 2) << __func__; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 567 | DCHECK(msg.type() == RTNLMessage::kTypeAddress); |
| 568 | int interface_index = msg.interface_index(); |
| 569 | if (!ContainsKey(infos_, interface_index)) { |
| 570 | LOG(ERROR) << "Got address type message for unknown index " |
| 571 | << interface_index; |
| 572 | return; |
| 573 | } |
| 574 | const RTNLMessage::AddressStatus &status = msg.address_status(); |
| 575 | IPAddress address(msg.family(), |
| 576 | msg.GetAttribute(IFA_ADDRESS), |
| 577 | status.prefix_len); |
| 578 | |
| 579 | vector<AddressData> &address_list = infos_[interface_index].ip_addresses; |
| 580 | vector<AddressData>::iterator iter; |
| 581 | for (iter = address_list.begin(); iter != address_list.end(); ++iter) { |
| 582 | if (address.Equals(iter->address)) { |
| 583 | break; |
| 584 | } |
| 585 | } |
| 586 | if (iter != address_list.end()) { |
| 587 | if (msg.mode() == RTNLMessage::kModeDelete) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 588 | SLOG(Device, 2) << "Delete address for interface " << interface_index; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 589 | address_list.erase(iter); |
| 590 | } else { |
| 591 | iter->flags = status.flags; |
| 592 | iter->scope = status.scope; |
| 593 | } |
| 594 | } else if (msg.mode() == RTNLMessage::kModeAdd) { |
| 595 | address_list.push_back(AddressData(address, status.flags, status.scope)); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 596 | SLOG(Device, 2) << "Add address for interface " << interface_index; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 600 | } // namespace shill |