blob: 7817169876ea474869cd928a517e36c9f491c0b0 [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
Tomasz Wasilczyk55f21932019-12-20 09:20:24 -080021namespace android::netdevice {
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070022
23NetlinkSocket::NetlinkSocket(int protocol) {
24 mFd.reset(socket(AF_NETLINK, SOCK_RAW, protocol));
25 if (!mFd.ok()) {
chrisweir75a80f62020-02-26 14:39:56 -080026 PLOG(ERROR) << "Can't open Netlink socket";
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070027 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) {
chrisweir75a80f62020-02-26 14:39:56 -080035 PLOG(ERROR) << "Can't bind Netlink socket";
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070036 mFd.reset();
37 mFailed = true;
38 }
39}
40
41bool 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) {
chrisweir75a80f62020-02-26 14:39:56 -080060 PLOG(ERROR) << "Can't send Netlink message";
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070061 return false;
62 }
63 return true;
64}
65
66bool 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) {
chrisweir75a80f62020-02-26 14:39:56 -080082 PLOG(ERROR) << "Failed to receive Netlink message";
Tomasz Wasilczyk87329672019-07-12 11:43:00 -070083 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
Tomasz Wasilczyk55f21932019-12-20 09:20:24 -0800112} // namespace android::netdevice