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