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