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