Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -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 <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 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 19 | #include <base/bind.h> |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 20 | #include <base/logging.h> |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 21 | |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 22 | #include "shill/event_dispatcher.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 23 | #include "shill/io_handler.h" |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 24 | #include "shill/ip_address.h" |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 25 | #include "shill/ipconfig.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 26 | #include "shill/rtnl_handler.h" |
| 27 | #include "shill/rtnl_listener.h" |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 28 | #include "shill/rtnl_message.h" |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 29 | #include "shill/sockets.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 30 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 31 | using base::Bind; |
| 32 | using base::Unretained; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 33 | using std::string; |
| 34 | |
| 35 | namespace shill { |
| 36 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 37 | // TODO(ers): not using LAZY_INSTANCE_INITIALIZER |
| 38 | // because of http://crbug.com/114828 |
| 39 | static base::LazyInstance<RTNLHandler> g_rtnl_handler = {0, {{0}}}; |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 40 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 41 | RTNLHandler::RTNLHandler() |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 42 | : sockets_(NULL), |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 43 | in_request_(false), |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 44 | rtnl_socket_(-1), |
| 45 | request_flags_(0), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 46 | request_sequence_(0), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 47 | last_dump_sequence_(0), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 48 | rtnl_callback_(Bind(&RTNLHandler::ParseRTNL, Unretained(this))) { |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 49 | VLOG(2) << "RTNLHandler created"; |
| 50 | } |
| 51 | |
| 52 | RTNLHandler::~RTNLHandler() { |
| 53 | VLOG(2) << "RTNLHandler removed"; |
| 54 | Stop(); |
| 55 | } |
| 56 | |
| 57 | RTNLHandler* RTNLHandler::GetInstance() { |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 58 | return g_rtnl_handler.Pointer(); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 61 | void RTNLHandler::Start(EventDispatcher *dispatcher, Sockets *sockets) { |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 62 | struct sockaddr_nl addr; |
| 63 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 64 | if (sockets_) { |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 65 | return; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 66 | } |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 67 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 68 | rtnl_socket_ = sockets->Socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 69 | if (rtnl_socket_ < 0) { |
| 70 | LOG(ERROR) << "Failed to open rtnl socket"; |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | memset(&addr, 0, sizeof(addr)); |
| 75 | addr.nl_family = AF_NETLINK; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 76 | addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE | |
| 77 | RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 78 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 79 | if (sockets->Bind(rtnl_socket_, |
| 80 | reinterpret_cast<struct sockaddr *>(&addr), |
| 81 | sizeof(addr)) < 0) { |
| 82 | sockets->Close(rtnl_socket_); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 83 | rtnl_socket_ = -1; |
| 84 | LOG(ERROR) << "RTNL socket bind failed"; |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | rtnl_handler_.reset(dispatcher->CreateInputHandler(rtnl_socket_, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 89 | rtnl_callback_)); |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 90 | sockets_ = sockets; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 91 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 92 | NextRequest(last_dump_sequence_); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 93 | VLOG(2) << "RTNLHandler started"; |
| 94 | } |
| 95 | |
| 96 | void RTNLHandler::Stop() { |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 97 | if (!sockets_) |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 98 | return; |
| 99 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 100 | rtnl_handler_.reset(); |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 101 | sockets_->Close(rtnl_socket_); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 102 | in_request_ = false; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 103 | sockets_ = NULL; |
Paul Stewart | 65c40f5 | 2011-08-08 07:27:46 -0700 | [diff] [blame] | 104 | request_flags_ = 0; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 105 | VLOG(2) << "RTNLHandler stopped"; |
| 106 | } |
| 107 | |
| 108 | void RTNLHandler::AddListener(RTNLListener *to_add) { |
| 109 | std::vector<RTNLListener *>::iterator it; |
| 110 | for (it = listeners_.begin(); it != listeners_.end(); ++it) { |
| 111 | if (to_add == *it) |
| 112 | return; |
| 113 | } |
| 114 | listeners_.push_back(to_add); |
| 115 | VLOG(2) << "RTNLHandler added listener"; |
| 116 | } |
| 117 | |
| 118 | void RTNLHandler::RemoveListener(RTNLListener *to_remove) { |
| 119 | std::vector<RTNLListener *>::iterator it; |
| 120 | for (it = listeners_.begin(); it != listeners_.end(); ++it) { |
| 121 | if (to_remove == *it) { |
| 122 | listeners_.erase(it); |
| 123 | return; |
| 124 | } |
| 125 | } |
| 126 | VLOG(2) << "RTNLHandler removed listener"; |
| 127 | } |
| 128 | |
| 129 | void RTNLHandler::SetInterfaceFlags(int interface_index, unsigned int flags, |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 130 | unsigned int change) { |
Paul Stewart | 536820d | 2012-03-19 16:05:59 -0700 | [diff] [blame] | 131 | if (!sockets_) { |
| 132 | LOG(ERROR) << __func__ << " called while not started. " |
| 133 | "Assuming we are in unit tests."; |
| 134 | return; |
| 135 | } |
| 136 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 137 | struct rtnl_request { |
| 138 | struct nlmsghdr hdr; |
| 139 | struct ifinfomsg msg; |
| 140 | } req; |
| 141 | |
| 142 | request_sequence_++; |
| 143 | memset(&req, 0, sizeof(req)); |
| 144 | |
| 145 | req.hdr.nlmsg_len = sizeof(req); |
| 146 | req.hdr.nlmsg_flags = NLM_F_REQUEST; |
| 147 | req.hdr.nlmsg_pid = 0; |
| 148 | req.hdr.nlmsg_seq = request_sequence_; |
| 149 | req.hdr.nlmsg_type = RTM_NEWLINK; |
| 150 | req.msg.ifi_index = interface_index; |
| 151 | req.msg.ifi_flags = flags; |
| 152 | req.msg.ifi_change = change; |
| 153 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 154 | if (sockets_->Send(rtnl_socket_, &req, sizeof(req), 0) < 0) { |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 155 | LOG(ERROR) << "RTNL sendto failed: " << strerror(errno); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void RTNLHandler::RequestDump(int request_flags) { |
| 160 | request_flags_ |= request_flags; |
| 161 | |
mukesh agrawal | 47009f8 | 2011-08-25 14:07:35 -0700 | [diff] [blame] | 162 | VLOG(2) << "RTNLHandler got request to dump " |
| 163 | << std::showbase << std::hex |
| 164 | << request_flags |
| 165 | << std::dec << std::noshowbase; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 166 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 167 | if (!in_request_ && sockets_) |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 168 | NextRequest(last_dump_sequence_); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 171 | void RTNLHandler::DispatchEvent(int type, const RTNLMessage &msg) { |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 172 | std::vector<RTNLListener *>::iterator it; |
| 173 | for (it = listeners_.begin(); it != listeners_.end(); ++it) { |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 174 | (*it)->NotifyEvent(type, msg); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 178 | void RTNLHandler::NextRequest(uint32 seq) { |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 179 | int flag = 0; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 180 | RTNLMessage::Type type; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 181 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 182 | VLOG(2) << "RTNLHandler nextrequest " << seq << " " << last_dump_sequence_ |
mukesh agrawal | 47009f8 | 2011-08-25 14:07:35 -0700 | [diff] [blame] | 183 | << std::showbase << std::hex |
| 184 | << " " << request_flags_ |
| 185 | << std::dec << std::noshowbase; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 186 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 187 | if (seq != last_dump_sequence_) |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 188 | return; |
| 189 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 190 | if ((request_flags_ & kRequestLink) != 0) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 191 | type = RTNLMessage::kTypeLink; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 192 | flag = kRequestLink; |
| 193 | } else if ((request_flags_ & kRequestAddr) != 0) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 194 | type = RTNLMessage::kTypeAddress; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 195 | flag = kRequestAddr; |
| 196 | } else if ((request_flags_ & kRequestRoute) != 0) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 197 | type = RTNLMessage::kTypeRoute; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 198 | flag = kRequestRoute; |
| 199 | } else { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 200 | VLOG(2) << "Done with requests"; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 201 | in_request_ = false; |
| 202 | return; |
| 203 | } |
| 204 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 205 | RTNLMessage msg( |
| 206 | type, |
| 207 | RTNLMessage::kModeGet, |
| 208 | 0, |
| 209 | 0, |
| 210 | 0, |
| 211 | 0, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 212 | IPAddress::kFamilyUnknown); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 213 | CHECK(SendMessage(&msg)); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 214 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 215 | last_dump_sequence_ = msg.seq(); |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 216 | request_flags_ &= ~flag; |
| 217 | in_request_ = true; |
| 218 | } |
| 219 | |
| 220 | void RTNLHandler::ParseRTNL(InputData *data) { |
| 221 | unsigned char *buf = data->buf; |
| 222 | unsigned char *end = buf + data->len; |
| 223 | |
| 224 | while (buf < end) { |
| 225 | struct nlmsghdr *hdr = reinterpret_cast<struct nlmsghdr *>(buf); |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 226 | if (!NLMSG_OK(hdr, static_cast<unsigned int>(end - buf))) |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 227 | break; |
| 228 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 229 | VLOG(3) << __func__ << ": received payload (" << end - buf << ")"; |
| 230 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 231 | RTNLMessage msg; |
| 232 | if (!msg.Decode(ByteString(reinterpret_cast<unsigned char *>(hdr), |
| 233 | hdr->nlmsg_len))) { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 234 | VLOG(3) << __func__ << ": rtnl packet type " |
| 235 | << hdr->nlmsg_type << " length " << hdr->nlmsg_len; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 236 | switch (hdr->nlmsg_type) { |
| 237 | case NLMSG_NOOP: |
| 238 | case NLMSG_OVERRUN: |
| 239 | break; |
| 240 | case NLMSG_DONE: |
| 241 | NextRequest(hdr->nlmsg_seq); |
| 242 | break; |
| 243 | case NLMSG_ERROR: |
| 244 | { |
| 245 | struct nlmsgerr *err = |
| 246 | reinterpret_cast<nlmsgerr *>(NLMSG_DATA(hdr)); |
| 247 | LOG(ERROR) << "error " << -err->error << " (" |
| 248 | << strerror(-err->error) << ")"; |
| 249 | break; |
| 250 | } |
| 251 | default: |
| 252 | NOTIMPLEMENTED() << "Unknown NL message type."; |
| 253 | } |
| 254 | } else { |
| 255 | switch (msg.type()) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 256 | case RTNLMessage::kTypeLink: |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 257 | DispatchEvent(kRequestLink, msg); |
| 258 | break; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 259 | case RTNLMessage::kTypeAddress: |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 260 | DispatchEvent(kRequestAddr, msg); |
| 261 | break; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 262 | case RTNLMessage::kTypeRoute: |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 263 | DispatchEvent(kRequestRoute, msg); |
| 264 | break; |
| 265 | default: |
| 266 | NOTIMPLEMENTED() << "Unknown RTNL message type."; |
| 267 | } |
| 268 | } |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 269 | buf += hdr->nlmsg_len; |
| 270 | } |
| 271 | } |
| 272 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 273 | bool RTNLHandler::AddressRequest(int interface_index, |
| 274 | RTNLMessage::Mode mode, |
| 275 | int flags, |
| 276 | const IPAddress &local, |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 277 | const IPAddress &gateway, |
| 278 | const IPAddress &peer) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 279 | CHECK(local.family() == gateway.family()); |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 280 | CHECK(local.family() == peer.family()); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 281 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 282 | RTNLMessage msg( |
| 283 | RTNLMessage::kTypeAddress, |
| 284 | mode, |
| 285 | NLM_F_REQUEST | flags, |
| 286 | 0, |
| 287 | 0, |
| 288 | interface_index, |
| 289 | local.family()); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 290 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 291 | msg.set_address_status(RTNLMessage::AddressStatus( |
| 292 | local.prefix(), |
| 293 | 0, |
| 294 | 0)); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 295 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 296 | msg.SetAttribute(IFA_LOCAL, local.address()); |
| 297 | if (!gateway.IsDefault()) { |
| 298 | msg.SetAttribute(IFA_BROADCAST, gateway.address()); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 299 | } |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 300 | if (!peer.IsDefault()) { |
| 301 | msg.SetAttribute(IFA_ADDRESS, peer.address()); |
| 302 | } |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 303 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 304 | return SendMessage(&msg); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | bool RTNLHandler::AddInterfaceAddress(int interface_index, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 308 | const IPAddress &local, |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 309 | const IPAddress &broadcast, |
| 310 | const IPAddress &peer) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 311 | return AddressRequest(interface_index, |
| 312 | RTNLMessage::kModeAdd, |
| 313 | NLM_F_CREATE | NLM_F_EXCL, |
| 314 | local, |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 315 | broadcast, |
| 316 | peer); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | bool RTNLHandler::RemoveInterfaceAddress(int interface_index, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 320 | const IPAddress &local) { |
| 321 | return AddressRequest(interface_index, |
| 322 | RTNLMessage::kModeDelete, |
| 323 | 0, |
| 324 | local, |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 325 | IPAddress(local.family()), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 326 | IPAddress(local.family())); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 329 | bool RTNLHandler::RemoveInterface(int interface_index) { |
| 330 | RTNLMessage msg( |
| 331 | RTNLMessage::kTypeLink, |
| 332 | RTNLMessage::kModeDelete, |
| 333 | NLM_F_REQUEST, |
| 334 | 0, |
| 335 | 0, |
| 336 | interface_index, |
| 337 | IPAddress::kFamilyUnknown); |
| 338 | return SendMessage(&msg); |
| 339 | } |
| 340 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 341 | int RTNLHandler::GetInterfaceIndex(const string &interface_name) { |
| 342 | if (interface_name.empty()) { |
| 343 | LOG(ERROR) << "Empty interface name -- unable to obtain index."; |
| 344 | return -1; |
| 345 | } |
| 346 | struct ifreq ifr; |
| 347 | if (interface_name.size() >= sizeof(ifr.ifr_name)) { |
| 348 | LOG(ERROR) << "Interface name too long: " << interface_name.size() << " >= " |
| 349 | << sizeof(ifr.ifr_name); |
| 350 | return -1; |
| 351 | } |
| 352 | int socket = sockets_->Socket(PF_INET, SOCK_DGRAM, 0); |
| 353 | if (socket < 0) { |
| 354 | PLOG(ERROR) << "Unable to open INET socket"; |
| 355 | return -1; |
| 356 | } |
| 357 | ScopedSocketCloser socket_closer(sockets_, socket); |
| 358 | memset(&ifr, 0, sizeof(ifr)); |
| 359 | strncpy(ifr.ifr_name, interface_name.c_str(), sizeof(ifr.ifr_name)); |
| 360 | if (sockets_->Ioctl(socket, SIOCGIFINDEX, &ifr) < 0) { |
| 361 | PLOG(ERROR) << "SIOCGIFINDEX error for " << interface_name; |
| 362 | return -1; |
| 363 | } |
| 364 | return ifr.ifr_ifindex; |
| 365 | } |
| 366 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 367 | bool RTNLHandler::SendMessage(RTNLMessage *message) { |
| 368 | message->set_seq(request_sequence_++); |
| 369 | ByteString msgdata = message->Encode(); |
| 370 | |
| 371 | if (msgdata.GetLength() == 0) { |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | if (sockets_->Send(rtnl_socket_, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 376 | msgdata.GetConstData(), |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 377 | msgdata.GetLength(), |
| 378 | 0) < 0) { |
| 379 | PLOG(ERROR) << "RTNL send failed: " << strerror(errno); |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | return true; |
| 384 | } |
| 385 | |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 386 | } // namespace shill |