1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "common/libs/net/netlink_client.h"
17
18 #include <errno.h>
19 #include <linux/netlink.h>
20 #include <sys/socket.h>
21 #include <sys/uio.h>
22
23 #include <cstdint>
24 #include <cstring>
25 #include <memory>
26 #include <string>
27 #include "ostream" // for operator<<, basic_ostream
28
29 #include <android-base/logging.h>
30
31 #include "common/libs/fs/shared_fd.h"
32 #include "common/libs/net/netlink_request.h"
33
34 namespace cuttlefish {
35 namespace {
36 // NetlinkClient implementation.
37 // Talks to libnetlink to apply network changes.
38 class NetlinkClientImpl : public NetlinkClient {
39 public:
40 NetlinkClientImpl() = default;
41 virtual ~NetlinkClientImpl() = default;
42
43 virtual bool Send(const NetlinkRequest& message);
44
45 // Initialize NetlinkClient instance.
46 // Open netlink channel and initialize interface list.
47 // Parameter |type| specifies which netlink target to address, eg.
48 // NETLINK_ROUTE.
49 // Returns true, if initialization was successful.
50 bool OpenNetlink(int type);
51
52 private:
53 bool CheckResponse(uint32_t seq_no);
54
55 SharedFD netlink_fd_;
56 sockaddr_nl address_;
57 };
58
CheckResponse(uint32_t seq_no)59 bool NetlinkClientImpl::CheckResponse(uint32_t seq_no) {
60 uint32_t len;
61 char buf[4096];
62 struct iovec iov = { buf, sizeof(buf) };
63 struct sockaddr_nl sa;
64 struct msghdr msg {};
65 struct nlmsghdr *nh;
66
67 msg.msg_name = &sa;
68 msg.msg_namelen = sizeof(sa);
69 msg.msg_iov = &iov;
70 msg.msg_iovlen = 1;
71
72 int result = netlink_fd_->RecvMsg(&msg, 0);
73 if (result < 0) {
74 LOG(ERROR) << "Netlink error: " << strerror(errno);
75 return false;
76 }
77
78 len = static_cast<uint32_t>(result);
79 LOG(INFO) << "Received netlink response (" << len << " bytes)";
80
81 for (nh = reinterpret_cast<nlmsghdr*>(buf);
82 NLMSG_OK(nh, len);
83 nh = NLMSG_NEXT(nh, len)) {
84 if (nh->nlmsg_seq != seq_no) {
85 // This really shouldn't happen. If we see this, it means somebody is
86 // issuing netlink requests using the same socket as us, and ignoring
87 // responses.
88 LOG(WARNING) << "Sequence number mismatch: "
89 << nh->nlmsg_seq << " != " << seq_no;
90 continue;
91 }
92
93 // This is the end of multi-part message.
94 // It indicates there's nothing more netlink wants to tell us.
95 // It also means we failed to find the response to our request.
96 if (nh->nlmsg_type == NLMSG_DONE)
97 break;
98
99 // This is the 'nlmsgerr' package carrying response to our request.
100 // It carries an 'error' value (errno) along with the netlink header info
101 // that caused this error.
102 if (nh->nlmsg_type == NLMSG_ERROR) {
103 nlmsgerr* err = reinterpret_cast<nlmsgerr*>(nh + 1);
104 if (err->error < 0) {
105 LOG(ERROR) << "Failed to complete netlink request: "
106 << "Netlink error: " << err->error
107 << ", errno: " << strerror(errno);
108 return false;
109 }
110 return true;
111 }
112 }
113
114 LOG(ERROR) << "No response from netlink.";
115 return false;
116 }
117
Send(const NetlinkRequest & message)118 bool NetlinkClientImpl::Send(const NetlinkRequest& message) {
119 struct sockaddr_nl netlink_addr;
120 struct iovec netlink_iov = {
121 message.RequestData(),
122 message.RequestLength()
123 };
124 struct msghdr msg;
125 memset(&msg, 0, sizeof(msg));
126 memset(&netlink_addr, 0, sizeof(netlink_addr));
127
128 msg.msg_name = &address_;
129 msg.msg_namelen = sizeof(address_);
130 msg.msg_iov = &netlink_iov;
131 msg.msg_iovlen = sizeof(netlink_iov) / sizeof(iovec);
132
133 if (netlink_fd_->SendMsg(&msg, 0) < 0) {
134 LOG(ERROR) << "Failed to send netlink message: "
135 << strerror(errno);
136
137 return false;
138 }
139
140 return CheckResponse(message.SeqNo());
141 }
142
OpenNetlink(int type)143 bool NetlinkClientImpl::OpenNetlink(int type) {
144 netlink_fd_ = SharedFD::Socket(AF_NETLINK, SOCK_RAW, type);
145 if (!netlink_fd_->IsOpen()) return false;
146
147 address_.nl_family = AF_NETLINK;
148 address_.nl_groups = 0;
149
150 netlink_fd_->Bind(reinterpret_cast<sockaddr*>(&address_), sizeof(address_));
151
152 return true;
153 }
154
155 class NetlinkClientFactoryImpl : public NetlinkClientFactory {
156 public:
157 NetlinkClientFactoryImpl() = default;
158 ~NetlinkClientFactoryImpl() override = default;
159
New(int type)160 std::unique_ptr<NetlinkClient> New(int type) override {
161 auto client_raw = new NetlinkClientImpl();
162 // Use RVO when possible.
163 std::unique_ptr<NetlinkClient> client(client_raw);
164
165 if (!client_raw->OpenNetlink(type)) {
166 // Note: deletes client_raw.
167 client.reset();
168 }
169 return client;
170 }
171 };
172
173 } // namespace
174
Default()175 NetlinkClientFactory* NetlinkClientFactory::Default() {
176 static NetlinkClientFactory &factory = *new NetlinkClientFactoryImpl();
177 return &factory;
178 }
179
180 } // namespace cuttlefish
181