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