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; |
| 44 | using base::Unretained; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 45 | using std::map; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 46 | using std::string; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 47 | using std::vector; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 48 | |
| 49 | namespace shill { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 50 | |
| 51 | // static |
Paul Stewart | bf1861b | 2011-08-23 15:45:35 -0700 | [diff] [blame] | 52 | const char DeviceInfo::kInterfaceUevent[] = "/sys/class/net/%s/uevent"; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 53 | // static |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 54 | const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n"; |
| 55 | // static |
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 | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 57 | // static |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 58 | const char DeviceInfo::kInterfaceTunFlags[] = "/sys/class/net/%s/tun_flags"; |
| 59 | // static |
Paul Stewart | 2001a42 | 2011-12-15 10:20:09 -0800 | [diff] [blame] | 60 | const char DeviceInfo::kInterfaceType[] = "/sys/class/net/%s/type"; |
| 61 | // static |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 62 | const char *DeviceInfo::kModemDrivers[] = { |
| 63 | "gobi", |
| 64 | "QCUSBNet2k", |
| 65 | "GobiNet", |
Eric Shienbrood | 39cd564 | 2012-01-19 10:53:52 -0500 | [diff] [blame] | 66 | "cdc_ether", |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 67 | NULL |
| 68 | }; |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 69 | // static |
| 70 | const char DeviceInfo::kTunDeviceName[] = "/dev/net/tun"; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 71 | |
| 72 | DeviceInfo::DeviceInfo(ControlInterface *control_interface, |
| 73 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 74 | Metrics *metrics, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 75 | Manager *manager) |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 76 | : control_interface_(control_interface), |
| 77 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 78 | metrics_(metrics), |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 79 | manager_(manager), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 80 | link_callback_(Bind(&DeviceInfo::LinkMsgHandler, Unretained(this))), |
| 81 | address_callback_(Bind(&DeviceInfo::AddressMsgHandler, Unretained(this))), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 82 | link_listener_(NULL), |
| 83 | address_listener_(NULL), |
| 84 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 87 | DeviceInfo::~DeviceInfo() {} |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 88 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 89 | void DeviceInfo::AddDeviceToBlackList(const string &device_name) { |
| 90 | black_list_.insert(device_name); |
| 91 | } |
| 92 | |
Eric Shienbrood | 5e628a5 | 2012-03-21 16:56:59 -0400 | [diff] [blame] | 93 | bool DeviceInfo::IsDeviceBlackListed(const string &device_name) { |
| 94 | return ContainsKey(black_list_, device_name); |
| 95 | } |
| 96 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 97 | void DeviceInfo::Start() { |
| 98 | link_listener_.reset( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 99 | new RTNLListener(RTNLHandler::kRequestLink, link_callback_)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 100 | address_listener_.reset( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 101 | new RTNLListener(RTNLHandler::kRequestAddr, address_callback_)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 102 | rtnl_handler_->RequestDump(RTNLHandler::kRequestLink | |
| 103 | RTNLHandler::kRequestAddr); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 106 | void DeviceInfo::Stop() { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 107 | link_listener_.reset(); |
| 108 | address_listener_.reset(); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 111 | void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) { |
| 112 | VLOG(2) << __func__ << "(" << device->link_name() << ", " |
| 113 | << device->interface_index() << ")"; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 114 | CHECK(!GetDevice(device->interface_index()).get()); |
| 115 | infos_[device->interface_index()].device = device; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 116 | if (device->TechnologyIs(Technology::kCellular) || |
| 117 | device->TechnologyIs(Technology::kEthernet) || |
| 118 | device->TechnologyIs(Technology::kWifi)) { |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 119 | manager_->RegisterDevice(device); |
| 120 | } |
| 121 | } |
| 122 | |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 123 | void DeviceInfo::DeregisterDevice(const DeviceRefPtr &device) { |
| 124 | int interface_index = device->interface_index(); |
| 125 | |
| 126 | VLOG(2) << __func__ << "(" << device->link_name() << ", " |
| 127 | << interface_index << ")"; |
| 128 | CHECK(device->TechnologyIs(Technology::kCellular)); |
| 129 | |
| 130 | // Release reference to the device |
| 131 | map<int, Info>::iterator iter = infos_.find(interface_index); |
| 132 | if (iter != infos_.end()) { |
| 133 | VLOG(2) << "Removing device from info for index: " << interface_index; |
| 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 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 141 | Technology::Identifier DeviceInfo::GetDeviceTechnology( |
| 142 | const string &iface_name) { |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 143 | FilePath uevent_file(StringPrintf(kInterfaceUevent, iface_name.c_str())); |
| 144 | string contents; |
| 145 | if (!file_util::ReadFileToString(uevent_file, &contents)) { |
| 146 | VLOG(2) << StringPrintf("%s: device %s has no uevent file", |
| 147 | __func__, iface_name.c_str()); |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 148 | return Technology::kUnknown; |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 149 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 150 | |
| 151 | /* |
| 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. |
| 155 | */ |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 156 | if (contents.find(kInterfaceUeventWifiSignature) != string::npos) { |
| 157 | VLOG(2) << 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) { |
| 166 | VLOG(2) << StringPrintf("%s: wifi device %s is in monitor mode", |
| 167 | __func__, iface_name.c_str()); |
| 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)) { |
| 176 | VLOG(2) << StringPrintf("%s: device %s has no device symlink", |
| 177 | __func__, iface_name.c_str()); |
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 | } |
| 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()); |
| 194 | // See if driver for this interface is in a list of known modem driver names |
| 195 | for (int modem_idx = 0; kModemDrivers[modem_idx] != NULL; ++modem_idx) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 196 | // TODO(ers): should have additional checks to make sure a cdc_ether |
Eric Shienbrood | 39cd564 | 2012-01-19 10:53:52 -0500 | [diff] [blame] | 197 | // device is really a modem. flimflam uses udev to make such checks, |
| 198 | // looking to see whether a ttyACM or ttyUSB device is associated. |
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 | |
mukesh agrawal | 2c15d2c | 2012-02-21 16:09:21 -0800 | [diff] [blame] | 207 | VLOG(2) << StringPrintf("%s: device %s is defaulted to type ethernet", |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 208 | __func__, iface_name.c_str()); |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 209 | return Technology::kEthernet; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 212 | void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 213 | DCHECK(msg.type() == RTNLMessage::kTypeLink && |
| 214 | msg.mode() == RTNLMessage::kModeAdd); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 215 | int dev_index = msg.interface_index(); |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 216 | Technology::Identifier technology = Technology::kUnknown; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 217 | |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 218 | unsigned int flags = msg.link_status().flags; |
| 219 | unsigned int change = msg.link_status().change; |
Paul Stewart | 536820d | 2012-03-19 16:05:59 -0700 | [diff] [blame] | 220 | bool new_device = !ContainsKey(infos_, dev_index); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 221 | VLOG(2) << __func__ << "(index=" << dev_index |
mukesh agrawal | 47009f8 | 2011-08-25 14:07:35 -0700 | [diff] [blame] | 222 | << std::showbase << std::hex |
| 223 | << ", flags=" << flags << ", change=" << change << ")" |
Paul Stewart | 536820d | 2012-03-19 16:05:59 -0700 | [diff] [blame] | 224 | << std::dec << std::noshowbase |
| 225 | << ", new_device=" << new_device; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 226 | infos_[dev_index].flags = flags; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 227 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 228 | DeviceRefPtr device = GetDevice(dev_index); |
| 229 | if (!device.get()) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 230 | if (!msg.HasAttribute(IFLA_IFNAME)) { |
| 231 | LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!"; |
| 232 | return; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 233 | } |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 234 | ByteString b(msg.GetAttribute(IFLA_IFNAME)); |
| 235 | string link_name(reinterpret_cast<const char*>(b.GetConstData())); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 236 | VLOG(2) << "add link index " << dev_index << " name " << link_name; |
| 237 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 238 | if (!link_name.empty()) { |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 239 | if (ContainsKey(black_list_, link_name)) { |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 240 | technology = Technology::kBlacklisted; |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 241 | } else { |
| 242 | technology = GetDeviceTechnology(link_name); |
| 243 | } |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 244 | } |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 245 | string address; |
| 246 | if (msg.HasAttribute(IFLA_ADDRESS)) { |
| 247 | infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS); |
| 248 | address = StringToLowerASCII(infos_[dev_index].mac_address.HexEncode()); |
| 249 | VLOG(2) << "link index " << dev_index << " address " |
| 250 | << infos_[dev_index].mac_address.HexEncode(); |
| 251 | } else if (technology != Technology::kTunnel) { |
| 252 | LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!"; |
| 253 | return; |
| 254 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 255 | switch (technology) { |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 256 | case Technology::kCellular: |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 257 | // Cellular devices are managed by ModemInfo. |
| 258 | VLOG(2) << "Cellular link " << link_name << " at index " << dev_index |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 259 | << " -- notifying ModemInfo."; |
| 260 | manager_->modem_info()->OnDeviceInfoAvailable(link_name); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 261 | return; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 262 | case Technology::kEthernet: |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 263 | device = new Ethernet(control_interface_, dispatcher_, metrics_, |
| 264 | manager_, link_name, address, dev_index); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 265 | device->EnableIPv6Privacy(); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 266 | break; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 267 | case Technology::kWifi: |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 268 | device = new WiFi(control_interface_, dispatcher_, metrics_, manager_, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 269 | link_name, address, dev_index); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 270 | device->EnableIPv6Privacy(); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 271 | break; |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 272 | case Technology::kTunnel: |
Paul Stewart | 536820d | 2012-03-19 16:05:59 -0700 | [diff] [blame] | 273 | // Tunnel devices are managed by the VPN code. Notify the VPN Provider |
| 274 | // only if this is the first time we have seen this device index. |
| 275 | if (new_device) { |
| 276 | VLOG(2) << "Tunnel link " << link_name << " at index " << dev_index |
| 277 | << " -- notifying VPNProvider."; |
| 278 | if (!manager_->vpn_provider()->OnDeviceInfoAvailable(link_name, |
| 279 | dev_index)) { |
| 280 | // If VPN does not know anything about this tunnel, it is probably |
| 281 | // left over from a previous instance and should not exist. |
| 282 | VLOG(2) << "Tunnel link is unused. Deleting."; |
| 283 | DeleteInterface(dev_index); |
| 284 | } |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 285 | } |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 286 | return; |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 287 | default: |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 288 | device = new DeviceStub(control_interface_, dispatcher_, metrics_, |
| 289 | manager_, link_name, address, dev_index, |
| 290 | technology); |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 291 | break; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 292 | } |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 293 | RegisterDevice(device); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 294 | } |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 295 | device->LinkEvent(flags, change); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 298 | void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) { |
mukesh agrawal | 47009f8 | 2011-08-25 14:07:35 -0700 | [diff] [blame] | 299 | VLOG(2) << __func__ << "(index=" << msg.interface_index() << ")"; |
| 300 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 301 | DCHECK(msg.type() == RTNLMessage::kTypeLink && |
| 302 | msg.mode() == RTNLMessage::kModeDelete); |
Paul Stewart | 2713d6c | 2011-08-25 15:38:15 -0700 | [diff] [blame] | 303 | VLOG(2) << __func__ << "(index=" << msg.interface_index() |
| 304 | << std::showbase << std::hex |
| 305 | << ", flags=" << msg.link_status().flags |
| 306 | << ", change=" << msg.link_status().change << ")"; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 307 | RemoveInfo(msg.interface_index()); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 308 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 309 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 310 | DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const { |
| 311 | const Info *info = GetInfo(interface_index); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 312 | return info ? info->device : NULL; |
| 313 | } |
| 314 | |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 315 | bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const { |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 316 | const Info *info = GetInfo(interface_index); |
| 317 | if (!info) { |
| 318 | return false; |
| 319 | } |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 320 | *address = info->mac_address; |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 321 | return true; |
| 322 | } |
| 323 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 324 | bool DeviceInfo::GetAddresses(int interface_index, |
| 325 | vector<AddressData> *addresses) const { |
| 326 | const Info *info = GetInfo(interface_index); |
| 327 | if (!info) { |
| 328 | return false; |
| 329 | } |
| 330 | *addresses = info->ip_addresses; |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | void DeviceInfo::FlushAddresses(int interface_index) const { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 335 | VLOG(2) << __func__ << "(" << interface_index << ")"; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 336 | const Info *info = GetInfo(interface_index); |
| 337 | if (!info) { |
| 338 | return; |
| 339 | } |
| 340 | const vector<AddressData> &addresses = info->ip_addresses; |
| 341 | vector<AddressData>::const_iterator iter; |
| 342 | for (iter = addresses.begin(); iter != addresses.end(); ++iter) { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 343 | if (iter->address.family() == IPAddress::kFamilyIPv4 || |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 344 | (iter->scope == RT_SCOPE_UNIVERSE && |
| 345 | (iter->flags & ~IFA_F_TEMPORARY) == 0)) { |
| 346 | VLOG(2) << __func__ << ": removing ip address from interface " |
| 347 | << interface_index; |
| 348 | rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address); |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 353 | bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const { |
| 354 | const Info *info = GetInfo(interface_index); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 355 | if (!info) { |
| 356 | return false; |
| 357 | } |
| 358 | *flags = info->flags; |
| 359 | return true; |
| 360 | } |
| 361 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 362 | bool DeviceInfo::CreateTunnelInterface(string *interface_name) const { |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 363 | int fd = HANDLE_EINTR(open(kTunDeviceName, O_RDWR)); |
| 364 | if (fd < 0) { |
| 365 | PLOG(ERROR) << "failed to open " << kTunDeviceName; |
| 366 | return false; |
| 367 | } |
| 368 | file_util::ScopedFD scoped_fd(&fd); |
| 369 | |
| 370 | struct ifreq ifr; |
| 371 | memset(&ifr, 0, sizeof(ifr)); |
| 372 | ifr.ifr_flags = IFF_TUN | IFF_NO_PI; |
| 373 | if (HANDLE_EINTR(ioctl(fd, TUNSETIFF, &ifr))) { |
| 374 | PLOG(ERROR) << "failed to create tunnel interface"; |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | if (HANDLE_EINTR(ioctl(fd, TUNSETPERSIST, 1))) { |
| 379 | PLOG(ERROR) << "failed to set tunnel interface to be persistent"; |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | *interface_name = string(ifr.ifr_name); |
| 384 | |
| 385 | return true; |
| 386 | } |
| 387 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 388 | bool DeviceInfo::DeleteInterface(int interface_index) const { |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 389 | return rtnl_handler_->RemoveInterface(interface_index); |
| 390 | } |
| 391 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 392 | const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const { |
| 393 | map<int, Info>::const_iterator iter = infos_.find(interface_index); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 394 | if (iter == infos_.end()) { |
| 395 | return NULL; |
| 396 | } |
| 397 | return &iter->second; |
| 398 | } |
| 399 | |
| 400 | void DeviceInfo::RemoveInfo(int interface_index) { |
| 401 | map<int, Info>::iterator iter = infos_.find(interface_index); |
| 402 | if (iter != infos_.end()) { |
| 403 | VLOG(2) << "Removing info for device index: " << interface_index; |
| 404 | if (iter->second.device.get()) { |
| 405 | manager_->DeregisterDevice(iter->second.device); |
| 406 | } |
| 407 | infos_.erase(iter); |
mukesh agrawal | 47009f8 | 2011-08-25 14:07:35 -0700 | [diff] [blame] | 408 | } else { |
| 409 | VLOG(2) << __func__ << "unknown device index: " << interface_index; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 410 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 413 | void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 414 | DCHECK(msg.type() == RTNLMessage::kTypeLink); |
| 415 | if (msg.mode() == RTNLMessage::kModeAdd) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 416 | AddLinkMsgHandler(msg); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 417 | } else if (msg.mode() == RTNLMessage::kModeDelete) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 418 | DelLinkMsgHandler(msg); |
| 419 | } else { |
| 420 | NOTREACHED(); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 421 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 424 | void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 425 | VLOG(2) << __func__; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 426 | DCHECK(msg.type() == RTNLMessage::kTypeAddress); |
| 427 | int interface_index = msg.interface_index(); |
| 428 | if (!ContainsKey(infos_, interface_index)) { |
| 429 | LOG(ERROR) << "Got address type message for unknown index " |
| 430 | << interface_index; |
| 431 | return; |
| 432 | } |
| 433 | const RTNLMessage::AddressStatus &status = msg.address_status(); |
| 434 | IPAddress address(msg.family(), |
| 435 | msg.GetAttribute(IFA_ADDRESS), |
| 436 | status.prefix_len); |
| 437 | |
| 438 | vector<AddressData> &address_list = infos_[interface_index].ip_addresses; |
| 439 | vector<AddressData>::iterator iter; |
| 440 | for (iter = address_list.begin(); iter != address_list.end(); ++iter) { |
| 441 | if (address.Equals(iter->address)) { |
| 442 | break; |
| 443 | } |
| 444 | } |
| 445 | if (iter != address_list.end()) { |
| 446 | if (msg.mode() == RTNLMessage::kModeDelete) { |
| 447 | VLOG(2) << "Delete address for interface " << interface_index; |
| 448 | address_list.erase(iter); |
| 449 | } else { |
| 450 | iter->flags = status.flags; |
| 451 | iter->scope = status.scope; |
| 452 | } |
| 453 | } else if (msg.mode() == RTNLMessage::kModeAdd) { |
| 454 | address_list.push_back(AddressData(address, status.flags, status.scope)); |
| 455 | VLOG(2) << "Add address for interface " << interface_index; |
| 456 | } |
| 457 | } |
| 458 | |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 459 | } // namespace shill |