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 
100     // This is the 'nlmsgerr' package carrying response to our request.
101     // It carries an 'error' value (errno) along with the netlink header info
102     // that caused this error.
103     if (nh->nlmsg_type == NLMSG_ERROR) {
104       nlmsgerr* err = reinterpret_cast<nlmsgerr*>(nh + 1);
105       if (err->error < 0) {
106         LOG(ERROR) << "Failed to complete netlink request: "
107                    << "Netlink error: " << err->error
108                    << ", errno: " << strerror(errno);
109         return false;
110       }
111       return true;
112     }
113   }
114 
115   LOG(ERROR) << "No response from netlink.";
116   return false;
117 }
118 
Send(const NetlinkRequest & message)119 bool NetlinkClientImpl::Send(const NetlinkRequest& message) {
120   struct sockaddr_nl netlink_addr;
121   struct iovec netlink_iov = {
122     message.RequestData(),
123     message.RequestLength()
124   };
125   struct msghdr msg;
126   memset(&msg, 0, sizeof(msg));
127   memset(&netlink_addr, 0, sizeof(netlink_addr));
128 
129   msg.msg_name = &address_;
130   msg.msg_namelen = sizeof(address_);
131   msg.msg_iov = &netlink_iov;
132   msg.msg_iovlen = sizeof(netlink_iov) / sizeof(iovec);
133 
134   if (netlink_fd_->SendMsg(&msg, 0) < 0) {
135     LOG(ERROR) << "Failed to send netlink message: "
136                << strerror(errno);
137 
138     return false;
139   }
140 
141   return CheckResponse(message.SeqNo());
142 }
143 
OpenNetlink(int type)144 bool NetlinkClientImpl::OpenNetlink(int type) {
145   netlink_fd_ = SharedFD::Socket(AF_NETLINK, SOCK_RAW, type);
146   if (!netlink_fd_->IsOpen()) {
147     return false;
148   }
149 
150   address_.nl_family = AF_NETLINK;
151   address_.nl_groups = 0;
152 
153   netlink_fd_->Bind(reinterpret_cast<sockaddr*>(&address_), sizeof(address_));
154 
155   return true;
156 }
157 
158 class NetlinkClientFactoryImpl : public NetlinkClientFactory {
159  public:
160   NetlinkClientFactoryImpl() = default;
161   ~NetlinkClientFactoryImpl() override = default;
162 
New(int type)163   std::unique_ptr<NetlinkClient> New(int type) override {
164     auto client_raw = new NetlinkClientImpl();
165     // Use RVO when possible.
166     std::unique_ptr<NetlinkClient> client(client_raw);
167 
168     if (!client_raw->OpenNetlink(type)) {
169       // Note: deletes client_raw.
170       client.reset();
171     }
172     return client;
173   }
174 };
175 
176 }  // namespace
177 
Default()178 NetlinkClientFactory* NetlinkClientFactory::Default() {
179   static NetlinkClientFactory &factory = *new NetlinkClientFactoryImpl();
180   return &factory;
181 }
182 
183 }  // namespace cuttlefish
184