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/rtnetlink.h>
20 #include <linux/sockios.h>
21 #include <net/if.h>
22 #include <sys/socket.h>
23
24 #include "common/libs/fs/shared_fd.h"
25 #include "android-base/logging.h"
26
27 namespace cuttlefish {
28 namespace {
29 // NetlinkClient implementation.
30 // Talks to libnetlink to apply network changes.
31 class NetlinkClientImpl : public NetlinkClient {
32 public:
33 NetlinkClientImpl() = default;
34 virtual ~NetlinkClientImpl() = default;
35
36 virtual bool Send(const NetlinkRequest& message);
37
38 // Initialize NetlinkClient instance.
39 // Open netlink channel and initialize interface list.
40 // Parameter |type| specifies which netlink target to address, eg.
41 // NETLINK_ROUTE.
42 // Returns true, if initialization was successful.
43 bool OpenNetlink(int type);
44
45 private:
46 bool CheckResponse(uint32_t seq_no);
47
48 SharedFD netlink_fd_;
49 sockaddr_nl address_;
50 };
51
CheckResponse(uint32_t seq_no)52 bool NetlinkClientImpl::CheckResponse(uint32_t seq_no) {
53 uint32_t len;
54 char buf[4096];
55 struct iovec iov = { buf, sizeof(buf) };
56 struct sockaddr_nl sa;
57 struct msghdr msg {};
58 struct nlmsghdr *nh;
59
60 msg.msg_name = &sa;
61 msg.msg_namelen = sizeof(sa);
62 msg.msg_iov = &iov;
63 msg.msg_iovlen = 1;
64
65 int result = netlink_fd_->RecvMsg(&msg, 0);
66 if (result < 0) {
67 LOG(ERROR) << "Netlink error: " << strerror(errno);
68 return false;
69 }
70
71 len = static_cast<uint32_t>(result);
72 LOG(INFO) << "Received netlink response (" << len << " bytes)";
73
74 for (nh = reinterpret_cast<nlmsghdr*>(buf);
75 NLMSG_OK(nh, len);
76 nh = NLMSG_NEXT(nh, len)) {
77 if (nh->nlmsg_seq != seq_no) {
78 // This really shouldn't happen. If we see this, it means somebody is
79 // issuing netlink requests using the same socket as us, and ignoring
80 // responses.
81 LOG(WARNING) << "Sequence number mismatch: "
82 << nh->nlmsg_seq << " != " << seq_no;
83 continue;
84 }
85
86 // This is the end of multi-part message.
87 // It indicates there's nothing more netlink wants to tell us.
88 // It also means we failed to find the response to our request.
89 if (nh->nlmsg_type == NLMSG_DONE)
90 break;
91
92 // This is the 'nlmsgerr' package carrying response to our request.
93 // It carries an 'error' value (errno) along with the netlink header info
94 // that caused this error.
95 if (nh->nlmsg_type == NLMSG_ERROR) {
96 nlmsgerr* err = reinterpret_cast<nlmsgerr*>(nh + 1);
97 if (err->error < 0) {
98 LOG(ERROR) << "Failed to complete netlink request: "
99 << "Netlink error: " << err->error
100 << ", errno: " << strerror(errno);
101 return false;
102 }
103 return true;
104 }
105 }
106
107 LOG(ERROR) << "No response from netlink.";
108 return false;
109 }
110
Send(const NetlinkRequest & message)111 bool NetlinkClientImpl::Send(const NetlinkRequest& message) {
112 struct sockaddr_nl netlink_addr;
113 struct iovec netlink_iov = {
114 message.RequestData(),
115 message.RequestLength()
116 };
117 struct msghdr msg;
118 memset(&msg, 0, sizeof(msg));
119 memset(&netlink_addr, 0, sizeof(netlink_addr));
120
121 msg.msg_name = &address_;
122 msg.msg_namelen = sizeof(address_);
123 msg.msg_iov = &netlink_iov;
124 msg.msg_iovlen = sizeof(netlink_iov) / sizeof(iovec);
125
126 if (netlink_fd_->SendMsg(&msg, 0) < 0) {
127 LOG(ERROR) << "Failed to send netlink message: "
128 << strerror(errno);
129
130 return false;
131 }
132
133 return CheckResponse(message.SeqNo());
134 }
135
OpenNetlink(int type)136 bool NetlinkClientImpl::OpenNetlink(int type) {
137 netlink_fd_ = SharedFD::Socket(AF_NETLINK, SOCK_RAW, type);
138 if (!netlink_fd_->IsOpen()) return false;
139
140 address_.nl_family = AF_NETLINK;
141 address_.nl_groups = 0;
142
143 netlink_fd_->Bind(reinterpret_cast<sockaddr*>(&address_), sizeof(address_));
144
145 return true;
146 }
147
148 class NetlinkClientFactoryImpl : public NetlinkClientFactory {
149 public:
150 NetlinkClientFactoryImpl() = default;
151 ~NetlinkClientFactoryImpl() override = default;
152
New(int type)153 std::unique_ptr<NetlinkClient> New(int type) override {
154 auto client_raw = new NetlinkClientImpl();
155 // Use RVO when possible.
156 std::unique_ptr<NetlinkClient> client(client_raw);
157
158 if (!client_raw->OpenNetlink(type)) {
159 // Note: deletes client_raw.
160 client.reset();
161 }
162 return client;
163 }
164 };
165
166 } // namespace
167
Default()168 NetlinkClientFactory* NetlinkClientFactory::Default() {
169 static NetlinkClientFactory &factory = *new NetlinkClientFactoryImpl();
170 return &factory;
171 }
172
173 } // namespace cuttlefish
174