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