1 /*
2  * Copyright (C) 2019 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 
17 #include "NetlinkSocket.h"
18 
19 #include <android-base/logging.h>
20 
21 namespace android::netdevice {
22 
NetlinkSocket(int protocol)23 NetlinkSocket::NetlinkSocket(int protocol) {
24     mFd.reset(socket(AF_NETLINK, SOCK_RAW, protocol));
25     if (!mFd.ok()) {
26         PLOG(ERROR) << "Can't open Netlink socket";
27         mFailed = true;
28         return;
29     }
30 
31     struct sockaddr_nl sa = {};
32     sa.nl_family = AF_NETLINK;
33 
34     if (bind(mFd.get(), reinterpret_cast<struct sockaddr*>(&sa), sizeof(sa)) < 0) {
35         PLOG(ERROR) << "Can't bind Netlink socket";
36         mFd.reset();
37         mFailed = true;
38     }
39 }
40 
send(struct nlmsghdr * nlmsg)41 bool NetlinkSocket::send(struct nlmsghdr* nlmsg) {
42     if (mFailed) return false;
43 
44     nlmsg->nlmsg_pid = 0;  // kernel
45     nlmsg->nlmsg_seq = mSeq++;
46     nlmsg->nlmsg_flags |= NLM_F_ACK;
47 
48     struct iovec iov = {nlmsg, nlmsg->nlmsg_len};
49 
50     struct sockaddr_nl sa = {};
51     sa.nl_family = AF_NETLINK;
52 
53     struct msghdr msg = {};
54     msg.msg_name = &sa;
55     msg.msg_namelen = sizeof(sa);
56     msg.msg_iov = &iov;
57     msg.msg_iovlen = 1;
58 
59     if (sendmsg(mFd.get(), &msg, 0) < 0) {
60         PLOG(ERROR) << "Can't send Netlink message";
61         return false;
62     }
63     return true;
64 }
65 
receiveAck()66 bool NetlinkSocket::receiveAck() {
67     if (mFailed) return false;
68 
69     char buf[8192];
70 
71     struct sockaddr_nl sa;
72     struct iovec iov = {buf, sizeof(buf)};
73 
74     struct msghdr msg = {};
75     msg.msg_name = &sa;
76     msg.msg_namelen = sizeof(sa);
77     msg.msg_iov = &iov;
78     msg.msg_iovlen = 1;
79 
80     const ssize_t status = recvmsg(mFd.get(), &msg, 0);
81     if (status < 0) {
82         PLOG(ERROR) << "Failed to receive Netlink message";
83         return false;
84     }
85     size_t remainingLen = status;
86 
87     if (msg.msg_flags & MSG_TRUNC) {
88         LOG(ERROR) << "Failed to receive Netlink message: truncated";
89         return false;
90     }
91 
92     for (auto nlmsg = reinterpret_cast<struct nlmsghdr*>(buf); NLMSG_OK(nlmsg, remainingLen);
93          nlmsg = NLMSG_NEXT(nlmsg, remainingLen)) {
94         // We're looking for error/ack message only, ignoring others.
95         if (nlmsg->nlmsg_type != NLMSG_ERROR) {
96             LOG(WARNING) << "Received unexpected Netlink message (ignored): " << nlmsg->nlmsg_type;
97             continue;
98         }
99 
100         // Found error/ack message, return status.
101         auto nlerr = reinterpret_cast<struct nlmsgerr*>(NLMSG_DATA(nlmsg));
102         if (nlerr->error != 0) {
103             LOG(ERROR) << "Received Netlink error message: " << nlerr->error;
104             return false;
105         }
106         return true;
107     }
108     // Couldn't find any error/ack messages.
109     return false;
110 }
111 
112 }  // namespace android::netdevice
113