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