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