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