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 | |
| 5 | #include <time.h> |
| 6 | |
| 7 | #include <unistd.h> |
| 8 | #include <string.h> |
| 9 | #include <sys/socket.h> |
| 10 | #include <arpa/inet.h> |
| 11 | #include <netinet/ether.h> |
| 12 | #include <net/if.h> |
| 13 | #include <net/if_arp.h> |
| 14 | #include <linux/netlink.h> |
| 15 | #include <linux/rtnetlink.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 16 | #include <fcntl.h> |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 17 | #include <string> |
| 18 | |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 19 | #include <base/callback_old.h> |
Paul Stewart | bf1861b | 2011-08-23 15:45:35 -0700 | [diff] [blame] | 20 | #include <base/file_util.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 21 | #include <base/logging.h> |
| 22 | #include <base/memory/scoped_ptr.h> |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 23 | #include <base/stl_util-inl.h> |
Paul Stewart | 2001a42 | 2011-12-15 10:20:09 -0800 | [diff] [blame] | 24 | #include <base/string_number_conversions.h> |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 25 | #include <base/string_util.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 26 | #include <base/stringprintf.h> |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 27 | |
| 28 | #include "shill/control_interface.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 29 | #include "shill/device.h" |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 30 | #include "shill/device_info.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 31 | #include "shill/device_stub.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 32 | #include "shill/ethernet.h" |
| 33 | #include "shill/manager.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 34 | #include "shill/rtnl_handler.h" |
| 35 | #include "shill/rtnl_listener.h" |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 36 | #include "shill/rtnl_message.h" |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 37 | #include "shill/service.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 38 | #include "shill/wifi.h" |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 39 | |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 40 | using std::map; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 41 | using std::string; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 42 | using std::vector; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 43 | |
| 44 | namespace shill { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 45 | |
| 46 | // static |
Paul Stewart | bf1861b | 2011-08-23 15:45:35 -0700 | [diff] [blame] | 47 | const char DeviceInfo::kInterfaceUevent[] = "/sys/class/net/%s/uevent"; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 48 | // static |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 49 | const char DeviceInfo::kInterfaceUeventWifiSignature[] = "DEVTYPE=wlan\n"; |
| 50 | // static |
Paul Stewart | bf1861b | 2011-08-23 15:45:35 -0700 | [diff] [blame] | 51 | const char DeviceInfo::kInterfaceDriver[] = "/sys/class/net/%s/device/driver"; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 52 | // static |
Paul Stewart | 2001a42 | 2011-12-15 10:20:09 -0800 | [diff] [blame] | 53 | const char DeviceInfo::kInterfaceType[] = "/sys/class/net/%s/type"; |
| 54 | // static |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 55 | const char *DeviceInfo::kModemDrivers[] = { |
| 56 | "gobi", |
| 57 | "QCUSBNet2k", |
| 58 | "GobiNet", |
| 59 | NULL |
| 60 | }; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 61 | |
| 62 | DeviceInfo::DeviceInfo(ControlInterface *control_interface, |
| 63 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame^] | 64 | Metrics *metrics, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 65 | Manager *manager) |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 66 | : control_interface_(control_interface), |
| 67 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame^] | 68 | metrics_(metrics), |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 69 | manager_(manager), |
| 70 | link_callback_(NewCallback(this, &DeviceInfo::LinkMsgHandler)), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 71 | address_callback_(NewCallback(this, &DeviceInfo::AddressMsgHandler)), |
| 72 | link_listener_(NULL), |
| 73 | address_listener_(NULL), |
| 74 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 77 | DeviceInfo::~DeviceInfo() {} |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 78 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 79 | void DeviceInfo::AddDeviceToBlackList(const string &device_name) { |
| 80 | black_list_.insert(device_name); |
| 81 | } |
| 82 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 83 | void DeviceInfo::Start() { |
| 84 | link_listener_.reset( |
| 85 | new RTNLListener(RTNLHandler::kRequestLink, link_callback_.get())); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 86 | address_listener_.reset( |
| 87 | new RTNLListener(RTNLHandler::kRequestAddr, address_callback_.get())); |
| 88 | rtnl_handler_->RequestDump(RTNLHandler::kRequestLink | |
| 89 | RTNLHandler::kRequestAddr); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 92 | void DeviceInfo::Stop() { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 93 | link_listener_.reset(); |
| 94 | address_listener_.reset(); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 97 | void DeviceInfo::RegisterDevice(const DeviceRefPtr &device) { |
| 98 | VLOG(2) << __func__ << "(" << device->link_name() << ", " |
| 99 | << device->interface_index() << ")"; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 100 | CHECK(!GetDevice(device->interface_index()).get()); |
| 101 | infos_[device->interface_index()].device = device; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 102 | if (device->TechnologyIs(Technology::kCellular) || |
| 103 | device->TechnologyIs(Technology::kEthernet) || |
| 104 | device->TechnologyIs(Technology::kWifi)) { |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 105 | manager_->RegisterDevice(device); |
| 106 | } |
| 107 | } |
| 108 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 109 | Technology::Identifier DeviceInfo::GetDeviceTechnology( |
| 110 | const string &iface_name) { |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 111 | FilePath uevent_file(StringPrintf(kInterfaceUevent, iface_name.c_str())); |
| 112 | string contents; |
| 113 | if (!file_util::ReadFileToString(uevent_file, &contents)) { |
| 114 | VLOG(2) << StringPrintf("%s: device %s has no uevent file", |
| 115 | __func__, iface_name.c_str()); |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 116 | return Technology::kUnknown; |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 117 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * If the "uevent" file contains the string "DEVTYPE=wlan\n" at the |
| 121 | * start of the file or after a newline, we can safely assume this |
| 122 | * is a wifi device. |
| 123 | */ |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 124 | if (contents.find(kInterfaceUeventWifiSignature) != string::npos) { |
| 125 | VLOG(2) << StringPrintf("%s: device %s has wifi signature in uevent file", |
| 126 | __func__, iface_name.c_str()); |
Paul Stewart | 2001a42 | 2011-12-15 10:20:09 -0800 | [diff] [blame] | 127 | FilePath type_file(StringPrintf(kInterfaceType, iface_name.c_str())); |
| 128 | string type_string; |
| 129 | int type_val = 0; |
| 130 | if (file_util::ReadFileToString(type_file, &type_string) && |
| 131 | TrimString(type_string, "\n", &type_string) && |
| 132 | base::StringToInt(type_string, &type_val) && |
| 133 | type_val == ARPHRD_IEEE80211_RADIOTAP) { |
| 134 | VLOG(2) << StringPrintf("%s: wifi device %s is in monitor mode", |
| 135 | __func__, iface_name.c_str()); |
| 136 | return Technology::kWiFiMonitor; |
| 137 | } |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 138 | return Technology::kWifi; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 141 | FilePath driver_file(StringPrintf(kInterfaceDriver, iface_name.c_str())); |
| 142 | FilePath driver_path; |
| 143 | if (!file_util::ReadSymbolicLink(driver_file, &driver_path)) { |
| 144 | VLOG(2) << StringPrintf("%s: device %s has no device symlink", |
| 145 | __func__, iface_name.c_str()); |
| 146 | return Technology::kUnknown; |
| 147 | } |
| 148 | |
| 149 | string driver_name(driver_path.BaseName().value()); |
| 150 | // See if driver for this interface is in a list of known modem driver names |
| 151 | for (int modem_idx = 0; kModemDrivers[modem_idx] != NULL; ++modem_idx) { |
| 152 | if (driver_name == kModemDrivers[modem_idx]) { |
| 153 | VLOG(2) << StringPrintf("%s: device %s is matched with modem driver %s", |
| 154 | __func__, iface_name.c_str(), |
| 155 | driver_name.c_str()); |
| 156 | return Technology::kCellular; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | VLOG(2) << StringPrintf("%s: device %s is is defaulted to type ethernet", |
| 161 | __func__, iface_name.c_str()); |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 162 | return Technology::kEthernet; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 165 | void DeviceInfo::AddLinkMsgHandler(const RTNLMessage &msg) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 166 | DCHECK(msg.type() == RTNLMessage::kTypeLink && |
| 167 | msg.mode() == RTNLMessage::kModeAdd); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 168 | int dev_index = msg.interface_index(); |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 169 | Technology::Identifier technology = Technology::kUnknown; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 170 | |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 171 | unsigned int flags = msg.link_status().flags; |
| 172 | unsigned int change = msg.link_status().change; |
| 173 | VLOG(2) << __func__ << "(index=" << dev_index |
mukesh agrawal | 47009f8 | 2011-08-25 14:07:35 -0700 | [diff] [blame] | 174 | << std::showbase << std::hex |
| 175 | << ", flags=" << flags << ", change=" << change << ")" |
| 176 | << std::dec << std::noshowbase; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 177 | infos_[dev_index].flags = flags; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 178 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 179 | DeviceRefPtr device = GetDevice(dev_index); |
| 180 | if (!device.get()) { |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 181 | if (msg.HasAttribute(IFLA_ADDRESS)) { |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 182 | infos_[dev_index].mac_address = msg.GetAttribute(IFLA_ADDRESS); |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 183 | VLOG(2) << "link index " << dev_index << " address " |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 184 | << infos_[dev_index].mac_address.HexEncode(); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 185 | } else { |
| 186 | LOG(ERROR) << "Add Link message does not have IFLA_ADDRESS!"; |
| 187 | return; |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 188 | } |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 189 | if (!msg.HasAttribute(IFLA_IFNAME)) { |
| 190 | LOG(ERROR) << "Add Link message does not have IFLA_IFNAME!"; |
| 191 | return; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 192 | } |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 193 | ByteString b(msg.GetAttribute(IFLA_IFNAME)); |
| 194 | string link_name(reinterpret_cast<const char*>(b.GetConstData())); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 195 | VLOG(2) << "add link index " << dev_index << " name " << link_name; |
| 196 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 197 | if (!link_name.empty()) { |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 198 | if (ContainsKey(black_list_, link_name)) { |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 199 | technology = Technology::kBlacklisted; |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 200 | } else { |
| 201 | technology = GetDeviceTechnology(link_name); |
| 202 | } |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 203 | } |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 204 | string address = |
| 205 | StringToLowerASCII(infos_[dev_index].mac_address.HexEncode()); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 206 | switch (technology) { |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 207 | case Technology::kCellular: |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 208 | // Cellular devices are managed by ModemInfo. |
| 209 | VLOG(2) << "Cellular link " << link_name << " at index " << dev_index |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 210 | << " -- notifying ModemInfo."; |
| 211 | manager_->modem_info()->OnDeviceInfoAvailable(link_name); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 212 | return; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 213 | case Technology::kEthernet: |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame^] | 214 | device = new Ethernet(control_interface_, dispatcher_, metrics_, |
| 215 | manager_, link_name, address, dev_index); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 216 | device->EnableIPv6Privacy(); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 217 | break; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 218 | case Technology::kWifi: |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame^] | 219 | device = new WiFi(control_interface_, dispatcher_, metrics_, manager_, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 220 | link_name, address, dev_index); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 221 | device->EnableIPv6Privacy(); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 222 | break; |
| 223 | default: |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame^] | 224 | device = new DeviceStub(control_interface_, dispatcher_, metrics_, |
| 225 | manager_, link_name, address, dev_index, |
| 226 | technology); |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 227 | break; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 228 | } |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 229 | RegisterDevice(device); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 230 | } |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 231 | device->LinkEvent(flags, change); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 234 | void DeviceInfo::DelLinkMsgHandler(const RTNLMessage &msg) { |
mukesh agrawal | 47009f8 | 2011-08-25 14:07:35 -0700 | [diff] [blame] | 235 | VLOG(2) << __func__ << "(index=" << msg.interface_index() << ")"; |
| 236 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 237 | DCHECK(msg.type() == RTNLMessage::kTypeLink && |
| 238 | msg.mode() == RTNLMessage::kModeDelete); |
Paul Stewart | 2713d6c | 2011-08-25 15:38:15 -0700 | [diff] [blame] | 239 | VLOG(2) << __func__ << "(index=" << msg.interface_index() |
| 240 | << std::showbase << std::hex |
| 241 | << ", flags=" << msg.link_status().flags |
| 242 | << ", change=" << msg.link_status().change << ")"; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 243 | RemoveInfo(msg.interface_index()); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 244 | } |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 245 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 246 | DeviceRefPtr DeviceInfo::GetDevice(int interface_index) const { |
| 247 | const Info *info = GetInfo(interface_index); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 248 | return info ? info->device : NULL; |
| 249 | } |
| 250 | |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 251 | bool DeviceInfo::GetMACAddress(int interface_index, ByteString *address) const { |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 252 | const Info *info = GetInfo(interface_index); |
| 253 | if (!info) { |
| 254 | return false; |
| 255 | } |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 256 | *address = info->mac_address; |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 257 | return true; |
| 258 | } |
| 259 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 260 | bool DeviceInfo::GetAddresses(int interface_index, |
| 261 | vector<AddressData> *addresses) const { |
| 262 | const Info *info = GetInfo(interface_index); |
| 263 | if (!info) { |
| 264 | return false; |
| 265 | } |
| 266 | *addresses = info->ip_addresses; |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | void DeviceInfo::FlushAddresses(int interface_index) const { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 271 | VLOG(2) << __func__ << "(" << interface_index << ")"; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 272 | const Info *info = GetInfo(interface_index); |
| 273 | if (!info) { |
| 274 | return; |
| 275 | } |
| 276 | const vector<AddressData> &addresses = info->ip_addresses; |
| 277 | vector<AddressData>::const_iterator iter; |
| 278 | for (iter = addresses.begin(); iter != addresses.end(); ++iter) { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 279 | if (iter->address.family() == IPAddress::kFamilyIPv4 || |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 280 | (iter->scope == RT_SCOPE_UNIVERSE && |
| 281 | (iter->flags & ~IFA_F_TEMPORARY) == 0)) { |
| 282 | VLOG(2) << __func__ << ": removing ip address from interface " |
| 283 | << interface_index; |
| 284 | rtnl_handler_->RemoveInterfaceAddress(interface_index, iter->address); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 289 | bool DeviceInfo::GetFlags(int interface_index, unsigned int *flags) const { |
| 290 | const Info *info = GetInfo(interface_index); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 291 | if (!info) { |
| 292 | return false; |
| 293 | } |
| 294 | *flags = info->flags; |
| 295 | return true; |
| 296 | } |
| 297 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 298 | const DeviceInfo::Info *DeviceInfo::GetInfo(int interface_index) const { |
| 299 | map<int, Info>::const_iterator iter = infos_.find(interface_index); |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 300 | if (iter == infos_.end()) { |
| 301 | return NULL; |
| 302 | } |
| 303 | return &iter->second; |
| 304 | } |
| 305 | |
| 306 | void DeviceInfo::RemoveInfo(int interface_index) { |
| 307 | map<int, Info>::iterator iter = infos_.find(interface_index); |
| 308 | if (iter != infos_.end()) { |
| 309 | VLOG(2) << "Removing info for device index: " << interface_index; |
| 310 | if (iter->second.device.get()) { |
| 311 | manager_->DeregisterDevice(iter->second.device); |
| 312 | } |
| 313 | infos_.erase(iter); |
mukesh agrawal | 47009f8 | 2011-08-25 14:07:35 -0700 | [diff] [blame] | 314 | } else { |
| 315 | VLOG(2) << __func__ << "unknown device index: " << interface_index; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 316 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 319 | void DeviceInfo::LinkMsgHandler(const RTNLMessage &msg) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 320 | DCHECK(msg.type() == RTNLMessage::kTypeLink); |
| 321 | if (msg.mode() == RTNLMessage::kModeAdd) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 322 | AddLinkMsgHandler(msg); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 323 | } else if (msg.mode() == RTNLMessage::kModeDelete) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 324 | DelLinkMsgHandler(msg); |
| 325 | } else { |
| 326 | NOTREACHED(); |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 327 | } |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 330 | void DeviceInfo::AddressMsgHandler(const RTNLMessage &msg) { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 331 | VLOG(2) << __func__; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 332 | DCHECK(msg.type() == RTNLMessage::kTypeAddress); |
| 333 | int interface_index = msg.interface_index(); |
| 334 | if (!ContainsKey(infos_, interface_index)) { |
| 335 | LOG(ERROR) << "Got address type message for unknown index " |
| 336 | << interface_index; |
| 337 | return; |
| 338 | } |
| 339 | const RTNLMessage::AddressStatus &status = msg.address_status(); |
| 340 | IPAddress address(msg.family(), |
| 341 | msg.GetAttribute(IFA_ADDRESS), |
| 342 | status.prefix_len); |
| 343 | |
| 344 | vector<AddressData> &address_list = infos_[interface_index].ip_addresses; |
| 345 | vector<AddressData>::iterator iter; |
| 346 | for (iter = address_list.begin(); iter != address_list.end(); ++iter) { |
| 347 | if (address.Equals(iter->address)) { |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | if (iter != address_list.end()) { |
| 352 | if (msg.mode() == RTNLMessage::kModeDelete) { |
| 353 | VLOG(2) << "Delete address for interface " << interface_index; |
| 354 | address_list.erase(iter); |
| 355 | } else { |
| 356 | iter->flags = status.flags; |
| 357 | iter->scope = status.scope; |
| 358 | } |
| 359 | } else if (msg.mode() == RTNLMessage::kModeAdd) { |
| 360 | address_list.push_back(AddressData(address, status.flags, status.scope)); |
| 361 | VLOG(2) << "Add address for interface " << interface_index; |
| 362 | } |
| 363 | } |
| 364 | |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 365 | } // namespace shill |