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