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