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