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