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