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