Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -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 <errno.h> |
| 6 | #include <time.h> |
| 7 | #include <unistd.h> |
| 8 | #include <string.h> |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 9 | #include <sys/ioctl.h> |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 10 | #include <sys/socket.h> |
| 11 | #include <arpa/inet.h> |
| 12 | #include <netinet/ether.h> |
| 13 | #include <net/if.h> |
| 14 | #include <net/if_arp.h> |
| 15 | #include <linux/netlink.h> |
| 16 | #include <linux/rtnetlink.h> |
| 17 | #include <fcntl.h> |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 18 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 19 | #include <base/logging.h> |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 20 | |
| 21 | #include "shill/io_handler.h" |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 22 | #include "shill/ip_address.h" |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 23 | #include "shill/ipconfig.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 24 | #include "shill/rtnl_handler.h" |
| 25 | #include "shill/rtnl_listener.h" |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 26 | #include "shill/rtnl_message.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 27 | #include "shill/shill_event.h" |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 28 | #include "shill/sockets.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 29 | |
| 30 | using std::string; |
| 31 | |
| 32 | namespace shill { |
| 33 | |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 34 | static base::LazyInstance<RTNLHandler> g_rtnl_handler( |
| 35 | base::LINKER_INITIALIZED); |
| 36 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 37 | RTNLHandler::RTNLHandler() |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 38 | : sockets_(NULL), |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 39 | in_request_(false), |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 40 | rtnl_socket_(-1), |
| 41 | request_flags_(0), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 42 | request_sequence_(0), |
| 43 | rtnl_callback_(NewCallback(this, &RTNLHandler::ParseRTNL)) { |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 44 | VLOG(2) << "RTNLHandler created"; |
| 45 | } |
| 46 | |
| 47 | RTNLHandler::~RTNLHandler() { |
| 48 | VLOG(2) << "RTNLHandler removed"; |
| 49 | Stop(); |
| 50 | } |
| 51 | |
| 52 | RTNLHandler* RTNLHandler::GetInstance() { |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 53 | return g_rtnl_handler.Pointer(); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 56 | void RTNLHandler::Start(EventDispatcher *dispatcher, Sockets *sockets) { |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 57 | struct sockaddr_nl addr; |
| 58 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 59 | if (sockets_) { |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 60 | return; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 61 | } |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 62 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 63 | rtnl_socket_ = sockets->Socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 64 | if (rtnl_socket_ < 0) { |
| 65 | LOG(ERROR) << "Failed to open rtnl socket"; |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | memset(&addr, 0, sizeof(addr)); |
| 70 | addr.nl_family = AF_NETLINK; |
| 71 | addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE; |
| 72 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 73 | if (sockets->Bind(rtnl_socket_, |
| 74 | reinterpret_cast<struct sockaddr *>(&addr), |
| 75 | sizeof(addr)) < 0) { |
| 76 | sockets->Close(rtnl_socket_); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 77 | rtnl_socket_ = -1; |
| 78 | LOG(ERROR) << "RTNL socket bind failed"; |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | rtnl_handler_.reset(dispatcher->CreateInputHandler(rtnl_socket_, |
| 83 | rtnl_callback_.get())); |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 84 | sockets_ = sockets; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 85 | |
| 86 | NextRequest(request_sequence_); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 87 | VLOG(2) << "RTNLHandler started"; |
| 88 | } |
| 89 | |
| 90 | void RTNLHandler::Stop() { |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 91 | if (!sockets_) |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 92 | return; |
| 93 | |
| 94 | rtnl_handler_.reset(NULL); |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 95 | sockets_->Close(rtnl_socket_); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 96 | in_request_ = false; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 97 | sockets_ = NULL; |
Paul Stewart | 65c40f5 | 2011-08-08 07:27:46 -0700 | [diff] [blame] | 98 | request_flags_ = 0; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 99 | VLOG(2) << "RTNLHandler stopped"; |
| 100 | } |
| 101 | |
| 102 | void RTNLHandler::AddListener(RTNLListener *to_add) { |
| 103 | std::vector<RTNLListener *>::iterator it; |
| 104 | for (it = listeners_.begin(); it != listeners_.end(); ++it) { |
| 105 | if (to_add == *it) |
| 106 | return; |
| 107 | } |
| 108 | listeners_.push_back(to_add); |
| 109 | VLOG(2) << "RTNLHandler added listener"; |
| 110 | } |
| 111 | |
| 112 | void RTNLHandler::RemoveListener(RTNLListener *to_remove) { |
| 113 | std::vector<RTNLListener *>::iterator it; |
| 114 | for (it = listeners_.begin(); it != listeners_.end(); ++it) { |
| 115 | if (to_remove == *it) { |
| 116 | listeners_.erase(it); |
| 117 | return; |
| 118 | } |
| 119 | } |
| 120 | VLOG(2) << "RTNLHandler removed listener"; |
| 121 | } |
| 122 | |
| 123 | void RTNLHandler::SetInterfaceFlags(int interface_index, unsigned int flags, |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 124 | unsigned int change) { |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 125 | struct rtnl_request { |
| 126 | struct nlmsghdr hdr; |
| 127 | struct ifinfomsg msg; |
| 128 | } req; |
| 129 | |
| 130 | request_sequence_++; |
| 131 | memset(&req, 0, sizeof(req)); |
| 132 | |
| 133 | req.hdr.nlmsg_len = sizeof(req); |
| 134 | req.hdr.nlmsg_flags = NLM_F_REQUEST; |
| 135 | req.hdr.nlmsg_pid = 0; |
| 136 | req.hdr.nlmsg_seq = request_sequence_; |
| 137 | req.hdr.nlmsg_type = RTM_NEWLINK; |
| 138 | req.msg.ifi_index = interface_index; |
| 139 | req.msg.ifi_flags = flags; |
| 140 | req.msg.ifi_change = change; |
| 141 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 142 | if (sockets_->Send(rtnl_socket_, &req, sizeof(req), 0) < 0) { |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 143 | LOG(ERROR) << "RTNL sendto failed: " << strerror(errno); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void RTNLHandler::RequestDump(int request_flags) { |
| 148 | request_flags_ |= request_flags; |
| 149 | |
| 150 | VLOG(2) << "RTNLHandler got request to dump " << request_flags; |
| 151 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 152 | if (!in_request_ && sockets_) |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 153 | NextRequest(request_sequence_); |
| 154 | } |
| 155 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 156 | void RTNLHandler::DispatchEvent(int type, const RTNLMessage &msg) { |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 157 | std::vector<RTNLListener *>::iterator it; |
| 158 | for (it = listeners_.begin(); it != listeners_.end(); ++it) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 159 | (*it)->NotifyEvent(type, msg); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | void RTNLHandler::NextRequest(uint32_t seq) { |
| 164 | struct rtnl_request { |
| 165 | struct nlmsghdr hdr; |
| 166 | struct rtgenmsg msg; |
| 167 | } req; |
| 168 | struct sockaddr_nl addr; |
| 169 | int flag = 0; |
| 170 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 171 | VLOG(2) << "RTNLHandler nextrequest " << seq << " " << request_sequence_ |
| 172 | << " " << request_flags_; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 173 | |
| 174 | if (seq != request_sequence_) |
| 175 | return; |
| 176 | |
| 177 | request_sequence_++; |
| 178 | memset(&req, 0, sizeof(req)); |
| 179 | |
| 180 | req.hdr.nlmsg_len = sizeof(req); |
| 181 | req.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; |
| 182 | req.hdr.nlmsg_pid = 0; |
| 183 | req.hdr.nlmsg_seq = request_sequence_; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 184 | |
| 185 | if ((request_flags_ & kRequestLink) != 0) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 186 | req.msg.rtgen_family = AF_INET; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 187 | req.hdr.nlmsg_type = RTM_GETLINK; |
| 188 | flag = kRequestLink; |
| 189 | } else if ((request_flags_ & kRequestAddr) != 0) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 190 | req.msg.rtgen_family = AF_INET; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 191 | req.hdr.nlmsg_type = RTM_GETADDR; |
| 192 | flag = kRequestAddr; |
| 193 | } else if ((request_flags_ & kRequestRoute) != 0) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 194 | req.msg.rtgen_family = AF_INET; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 195 | req.hdr.nlmsg_type = RTM_GETROUTE; |
| 196 | flag = kRequestRoute; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 197 | } else if ((request_flags_ & kRequestAddr6) != 0) { |
| 198 | req.msg.rtgen_family = AF_INET6; |
| 199 | req.hdr.nlmsg_type = RTM_GETADDR; |
| 200 | flag = kRequestAddr6; |
| 201 | } else if ((request_flags_ & kRequestRoute6) != 0) { |
| 202 | req.msg.rtgen_family = AF_INET6; |
| 203 | req.hdr.nlmsg_type = RTM_GETROUTE; |
| 204 | flag = kRequestRoute6; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 205 | } else { |
| 206 | in_request_ = false; |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | memset(&addr, 0, sizeof(addr)); |
| 211 | addr.nl_family = AF_NETLINK; |
| 212 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 213 | if (sockets_->SendTo(rtnl_socket_, |
| 214 | &req, |
| 215 | sizeof(req), |
| 216 | 0, |
| 217 | reinterpret_cast<struct sockaddr *>(&addr), |
| 218 | sizeof(addr)) < 0) { |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 219 | LOG(ERROR) << "RTNL sendto failed"; |
| 220 | return; |
| 221 | } |
| 222 | request_flags_ &= ~flag; |
| 223 | in_request_ = true; |
| 224 | } |
| 225 | |
| 226 | void RTNLHandler::ParseRTNL(InputData *data) { |
| 227 | unsigned char *buf = data->buf; |
| 228 | unsigned char *end = buf + data->len; |
| 229 | |
| 230 | while (buf < end) { |
| 231 | struct nlmsghdr *hdr = reinterpret_cast<struct nlmsghdr *>(buf); |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 232 | if (!NLMSG_OK(hdr, static_cast<unsigned int>(end - buf))) |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 233 | break; |
| 234 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 235 | RTNLMessage msg; |
| 236 | if (!msg.Decode(ByteString(reinterpret_cast<unsigned char *>(hdr), |
| 237 | hdr->nlmsg_len))) { |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 238 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 239 | switch (hdr->nlmsg_type) { |
| 240 | case NLMSG_NOOP: |
| 241 | case NLMSG_OVERRUN: |
| 242 | break; |
| 243 | case NLMSG_DONE: |
| 244 | NextRequest(hdr->nlmsg_seq); |
| 245 | break; |
| 246 | case NLMSG_ERROR: |
| 247 | { |
| 248 | struct nlmsgerr *err = |
| 249 | reinterpret_cast<nlmsgerr *>(NLMSG_DATA(hdr)); |
| 250 | LOG(ERROR) << "error " << -err->error << " (" |
| 251 | << strerror(-err->error) << ")"; |
| 252 | break; |
| 253 | } |
| 254 | default: |
| 255 | NOTIMPLEMENTED() << "Unknown NL message type."; |
| 256 | } |
| 257 | } else { |
| 258 | switch (msg.type()) { |
| 259 | case RTNLMessage::kMessageTypeLink: |
| 260 | DispatchEvent(kRequestLink, msg); |
| 261 | break; |
| 262 | case RTNLMessage::kMessageTypeAddress: |
| 263 | DispatchEvent(kRequestAddr, msg); |
| 264 | break; |
| 265 | case RTNLMessage::kMessageTypeRoute: |
| 266 | DispatchEvent(kRequestRoute, msg); |
| 267 | break; |
| 268 | default: |
| 269 | NOTIMPLEMENTED() << "Unknown RTNL message type."; |
| 270 | } |
| 271 | } |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 272 | buf += hdr->nlmsg_len; |
| 273 | } |
| 274 | } |
| 275 | |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 276 | static bool AddAtribute(struct nlmsghdr *hdr, int max_msg_size, int attr_type, |
| 277 | const void *attr_data, int attr_len) { |
| 278 | int len = RTA_LENGTH(attr_len); |
| 279 | int new_msg_size = NLMSG_ALIGN(hdr->nlmsg_len) + RTA_ALIGN(len); |
| 280 | struct rtattr *rt_attr; |
| 281 | |
| 282 | if (new_msg_size > max_msg_size) |
| 283 | return false; |
| 284 | |
| 285 | rt_attr = reinterpret_cast<struct rtattr *> (reinterpret_cast<unsigned char *> |
| 286 | (hdr) + |
| 287 | NLMSG_ALIGN(hdr->nlmsg_len)); |
| 288 | rt_attr->rta_type = attr_type; |
| 289 | rt_attr->rta_len = len; |
| 290 | memcpy(RTA_DATA(rt_attr), attr_data, attr_len); |
| 291 | hdr->nlmsg_len = new_msg_size; |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | bool RTNLHandler::AddressRequest(int interface_index, int cmd, int flags, |
| 296 | const IPConfig &ipconfig) { |
| 297 | const IPConfig::Properties &properties = ipconfig.properties(); |
| 298 | int address_family; |
| 299 | int address_size; |
| 300 | unsigned char *attrs, *attrs_end; |
| 301 | int max_msg_size; |
| 302 | struct { |
| 303 | struct nlmsghdr hdr; |
| 304 | struct ifaddrmsg ifa; |
| 305 | unsigned char attrs[256]; |
| 306 | } req; |
| 307 | union { |
| 308 | in_addr ip4; |
| 309 | in6_addr in6; |
| 310 | } addr; |
| 311 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 312 | if (properties.address_family == IPAddress::kAddressFamilyIPv4) { |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 313 | address_family = AF_INET; |
| 314 | address_size = sizeof(struct in_addr); |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 315 | } else if (properties.address_family == IPAddress::kAddressFamilyIPv6) { |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 316 | address_family = AF_INET6; |
| 317 | address_size = sizeof(struct in6_addr); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 318 | } else { |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 319 | return false; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 320 | } |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 321 | |
| 322 | request_sequence_++; |
| 323 | memset(&req, 0, sizeof(req)); |
| 324 | req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
| 325 | req.hdr.nlmsg_flags = NLM_F_REQUEST | flags; |
| 326 | req.hdr.nlmsg_type = cmd; |
| 327 | req.ifa.ifa_family = address_family; |
| 328 | req.ifa.ifa_index = interface_index; |
| 329 | |
| 330 | max_msg_size = req.hdr.nlmsg_len + sizeof(req.attrs); |
| 331 | |
| 332 | // TODO(pstew): This code only works for Ethernet-like setups, |
| 333 | // not with devices that have a peer address like PPP. |
| 334 | if (inet_pton(address_family, properties.address.c_str(), &addr) <= 0 || |
| 335 | !AddAtribute(&req.hdr, max_msg_size, IFA_LOCAL, &addr, address_size)) |
| 336 | return false; |
| 337 | |
| 338 | if (inet_pton(address_family, properties.broadcast_address.c_str(), |
| 339 | &addr) <= 0 || |
| 340 | !AddAtribute(&req.hdr, max_msg_size, IFA_BROADCAST, &addr, address_size)) |
| 341 | return false; |
| 342 | |
| 343 | req.ifa.ifa_prefixlen = properties.subnet_cidr; |
| 344 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 345 | if (sockets_->Send(rtnl_socket_, &req, req.hdr.nlmsg_len, 0) < 0) { |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 346 | LOG(ERROR) << "RTNL sendto failed: " << strerror(errno); |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | return true; |
| 351 | } |
| 352 | |
| 353 | bool RTNLHandler::AddInterfaceAddress(int interface_index, |
| 354 | const IPConfig &ipconfig) { |
| 355 | return AddressRequest(interface_index, RTM_NEWADDR, NLM_F_CREATE | NLM_F_EXCL, |
| 356 | ipconfig); |
| 357 | } |
| 358 | |
| 359 | bool RTNLHandler::RemoveInterfaceAddress(int interface_index, |
| 360 | const IPConfig &ipconfig) { |
| 361 | return AddressRequest(interface_index, RTM_DELADDR, 0, ipconfig); |
| 362 | } |
| 363 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 364 | int RTNLHandler::GetInterfaceIndex(const string &interface_name) { |
| 365 | if (interface_name.empty()) { |
| 366 | LOG(ERROR) << "Empty interface name -- unable to obtain index."; |
| 367 | return -1; |
| 368 | } |
| 369 | struct ifreq ifr; |
| 370 | if (interface_name.size() >= sizeof(ifr.ifr_name)) { |
| 371 | LOG(ERROR) << "Interface name too long: " << interface_name.size() << " >= " |
| 372 | << sizeof(ifr.ifr_name); |
| 373 | return -1; |
| 374 | } |
| 375 | int socket = sockets_->Socket(PF_INET, SOCK_DGRAM, 0); |
| 376 | if (socket < 0) { |
| 377 | PLOG(ERROR) << "Unable to open INET socket"; |
| 378 | return -1; |
| 379 | } |
| 380 | ScopedSocketCloser socket_closer(sockets_, socket); |
| 381 | memset(&ifr, 0, sizeof(ifr)); |
| 382 | strncpy(ifr.ifr_name, interface_name.c_str(), sizeof(ifr.ifr_name)); |
| 383 | if (sockets_->Ioctl(socket, SIOCGIFINDEX, &ifr) < 0) { |
| 384 | PLOG(ERROR) << "SIOCGIFINDEX error for " << interface_name; |
| 385 | return -1; |
| 386 | } |
| 387 | return ifr.ifr_ifindex; |
| 388 | } |
| 389 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 390 | bool RTNLHandler::SendMessage(RTNLMessage *message) { |
| 391 | message->set_seq(request_sequence_++); |
| 392 | ByteString msgdata = message->Encode(); |
| 393 | |
| 394 | if (msgdata.GetLength() == 0) { |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | if (sockets_->Send(rtnl_socket_, |
| 399 | msgdata.GetData(), |
| 400 | msgdata.GetLength(), |
| 401 | 0) < 0) { |
| 402 | PLOG(ERROR) << "RTNL send failed: " << strerror(errno); |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | return true; |
| 407 | } |
| 408 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 409 | } // namespace shill |