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