Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 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 | |
chrisweir | 3fb244a | 2020-06-29 13:05:17 -0700 | [diff] [blame] | 17 | #include <libnetdevice/NetlinkSocket.h> |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 18 | |
Tomasz Wasilczyk | a987273 | 2020-06-02 18:03:27 -0700 | [diff] [blame] | 19 | #include <libnetdevice/printer.h> |
| 20 | |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 21 | #include <android-base/logging.h> |
| 22 | |
Tomasz Wasilczyk | 55f2193 | 2019-12-20 09:20:24 -0800 | [diff] [blame] | 23 | namespace android::netdevice { |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 24 | |
Tomasz Wasilczyk | a987273 | 2020-06-02 18:03:27 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Print all outbound/inbound Netlink messages. |
| 27 | */ |
| 28 | static constexpr bool kSuperVerbose = false; |
| 29 | |
chrisweir | 06c2c0d | 2020-06-18 16:06:40 -0700 | [diff] [blame] | 30 | NetlinkSocket::NetlinkSocket(int protocol, unsigned int pid, uint32_t groups) |
| 31 | : mProtocol(protocol) { |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 32 | mFd.reset(socket(AF_NETLINK, SOCK_RAW, protocol)); |
| 33 | if (!mFd.ok()) { |
chrisweir | 1173a72 | 2020-02-26 14:39:56 -0800 | [diff] [blame] | 34 | PLOG(ERROR) << "Can't open Netlink socket"; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 35 | mFailed = true; |
| 36 | return; |
| 37 | } |
| 38 | |
chrisweir | 06c2c0d | 2020-06-18 16:06:40 -0700 | [diff] [blame] | 39 | sockaddr_nl sa = {}; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 40 | sa.nl_family = AF_NETLINK; |
chrisweir | 06c2c0d | 2020-06-18 16:06:40 -0700 | [diff] [blame] | 41 | sa.nl_pid = pid; |
| 42 | sa.nl_groups = groups; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 43 | |
chrisweir | 06c2c0d | 2020-06-18 16:06:40 -0700 | [diff] [blame] | 44 | if (bind(mFd.get(), reinterpret_cast<sockaddr*>(&sa), sizeof(sa)) < 0) { |
chrisweir | 1173a72 | 2020-02-26 14:39:56 -0800 | [diff] [blame] | 45 | PLOG(ERROR) << "Can't bind Netlink socket"; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 46 | mFd.reset(); |
| 47 | mFailed = true; |
| 48 | } |
| 49 | } |
| 50 | |
chrisweir | 06c2c0d | 2020-06-18 16:06:40 -0700 | [diff] [blame] | 51 | bool NetlinkSocket::send(nlmsghdr* nlmsg, size_t totalLen) { |
Tomasz Wasilczyk | a987273 | 2020-06-02 18:03:27 -0700 | [diff] [blame] | 52 | if constexpr (kSuperVerbose) { |
| 53 | nlmsg->nlmsg_seq = mSeq; |
chrisweir | 06c2c0d | 2020-06-18 16:06:40 -0700 | [diff] [blame] | 54 | LOG(VERBOSE) << (mFailed ? "(not) " : "") |
| 55 | << "sending Netlink message: " << toString({nlmsg, totalLen}, mProtocol); |
Tomasz Wasilczyk | a987273 | 2020-06-02 18:03:27 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 58 | if (mFailed) return false; |
| 59 | |
| 60 | nlmsg->nlmsg_pid = 0; // kernel |
| 61 | nlmsg->nlmsg_seq = mSeq++; |
| 62 | nlmsg->nlmsg_flags |= NLM_F_ACK; |
| 63 | |
chrisweir | 06c2c0d | 2020-06-18 16:06:40 -0700 | [diff] [blame] | 64 | iovec iov = {nlmsg, nlmsg->nlmsg_len}; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 65 | |
chrisweir | 06c2c0d | 2020-06-18 16:06:40 -0700 | [diff] [blame] | 66 | sockaddr_nl sa = {}; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 67 | sa.nl_family = AF_NETLINK; |
| 68 | |
chrisweir | 06c2c0d | 2020-06-18 16:06:40 -0700 | [diff] [blame] | 69 | msghdr msg = {}; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 70 | msg.msg_name = &sa; |
| 71 | msg.msg_namelen = sizeof(sa); |
| 72 | msg.msg_iov = &iov; |
| 73 | msg.msg_iovlen = 1; |
| 74 | |
| 75 | if (sendmsg(mFd.get(), &msg, 0) < 0) { |
chrisweir | 1173a72 | 2020-02-26 14:39:56 -0800 | [diff] [blame] | 76 | PLOG(ERROR) << "Can't send Netlink message"; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 77 | return false; |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
chrisweir | 06c2c0d | 2020-06-18 16:06:40 -0700 | [diff] [blame] | 82 | bool NetlinkSocket::send(const nlbuf<nlmsghdr>& msg, const sockaddr_nl& sa) { |
| 83 | if constexpr (kSuperVerbose) { |
| 84 | LOG(VERBOSE) << (mFailed ? "(not) " : "") |
| 85 | << "sending Netlink message: " << toString(msg, mProtocol); |
| 86 | } |
| 87 | |
| 88 | if (mFailed) return false; |
| 89 | const auto rawMsg = msg.getRaw(); |
| 90 | const auto bytesSent = sendto(mFd.get(), rawMsg.ptr(), rawMsg.len(), 0, |
| 91 | reinterpret_cast<const sockaddr*>(&sa), sizeof(sa)); |
| 92 | if (bytesSent < 0) { |
| 93 | PLOG(ERROR) << "Can't send Netlink message"; |
| 94 | return false; |
| 95 | } |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | std::optional<nlbuf<nlmsghdr>> NetlinkSocket::receive(void* buf, size_t bufLen) { |
| 100 | sockaddr_nl sa = {}; |
| 101 | return receive(buf, bufLen, sa); |
| 102 | } |
| 103 | |
| 104 | std::optional<nlbuf<nlmsghdr>> NetlinkSocket::receive(void* buf, size_t bufLen, sockaddr_nl& sa) { |
| 105 | if (mFailed) return std::nullopt; |
| 106 | |
| 107 | socklen_t saLen = sizeof(sa); |
| 108 | if (bufLen == 0) { |
| 109 | LOG(ERROR) << "Receive buffer has zero size!"; |
| 110 | return std::nullopt; |
| 111 | } |
| 112 | const auto bytesReceived = |
| 113 | recvfrom(mFd.get(), buf, bufLen, MSG_TRUNC, reinterpret_cast<sockaddr*>(&sa), &saLen); |
| 114 | if (bytesReceived <= 0) { |
| 115 | PLOG(ERROR) << "Failed to receive Netlink message"; |
| 116 | return std::nullopt; |
| 117 | } else if (unsigned(bytesReceived) > bufLen) { |
| 118 | PLOG(ERROR) << "Received data larger than the receive buffer! " << bytesReceived << " > " |
| 119 | << bufLen; |
| 120 | return std::nullopt; |
| 121 | } |
| 122 | |
| 123 | nlbuf<nlmsghdr> msg(reinterpret_cast<nlmsghdr*>(buf), bytesReceived); |
| 124 | if constexpr (kSuperVerbose) { |
| 125 | LOG(VERBOSE) << "received " << toString(msg, mProtocol); |
| 126 | } |
| 127 | return msg; |
| 128 | } |
| 129 | |
| 130 | /* TODO(161389935): Migrate receiveAck to use nlmsg<> internally. Possibly reuse |
| 131 | * NetlinkSocket::receive(). */ |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 132 | bool NetlinkSocket::receiveAck() { |
| 133 | if (mFailed) return false; |
| 134 | |
| 135 | char buf[8192]; |
| 136 | |
chrisweir | 06c2c0d | 2020-06-18 16:06:40 -0700 | [diff] [blame] | 137 | sockaddr_nl sa; |
| 138 | iovec iov = {buf, sizeof(buf)}; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 139 | |
chrisweir | 06c2c0d | 2020-06-18 16:06:40 -0700 | [diff] [blame] | 140 | msghdr msg = {}; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 141 | msg.msg_name = &sa; |
| 142 | msg.msg_namelen = sizeof(sa); |
| 143 | msg.msg_iov = &iov; |
| 144 | msg.msg_iovlen = 1; |
| 145 | |
| 146 | const ssize_t status = recvmsg(mFd.get(), &msg, 0); |
| 147 | if (status < 0) { |
chrisweir | 1173a72 | 2020-02-26 14:39:56 -0800 | [diff] [blame] | 148 | PLOG(ERROR) << "Failed to receive Netlink message"; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 149 | return false; |
| 150 | } |
| 151 | size_t remainingLen = status; |
| 152 | |
| 153 | if (msg.msg_flags & MSG_TRUNC) { |
| 154 | LOG(ERROR) << "Failed to receive Netlink message: truncated"; |
| 155 | return false; |
| 156 | } |
| 157 | |
chrisweir | 06c2c0d | 2020-06-18 16:06:40 -0700 | [diff] [blame] | 158 | for (auto nlmsg = reinterpret_cast<nlmsghdr*>(buf); NLMSG_OK(nlmsg, remainingLen); |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 159 | nlmsg = NLMSG_NEXT(nlmsg, remainingLen)) { |
Tomasz Wasilczyk | a987273 | 2020-06-02 18:03:27 -0700 | [diff] [blame] | 160 | if constexpr (kSuperVerbose) { |
| 161 | LOG(VERBOSE) << "received Netlink response: " |
chrisweir | 06c2c0d | 2020-06-18 16:06:40 -0700 | [diff] [blame] | 162 | << toString({nlmsg, nlmsg->nlmsg_len}, mProtocol); |
Tomasz Wasilczyk | a987273 | 2020-06-02 18:03:27 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 165 | // We're looking for error/ack message only, ignoring others. |
| 166 | if (nlmsg->nlmsg_type != NLMSG_ERROR) { |
| 167 | LOG(WARNING) << "Received unexpected Netlink message (ignored): " << nlmsg->nlmsg_type; |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | // Found error/ack message, return status. |
chrisweir | 06c2c0d | 2020-06-18 16:06:40 -0700 | [diff] [blame] | 172 | const auto nlerr = reinterpret_cast<nlmsgerr*>(NLMSG_DATA(nlmsg)); |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 173 | if (nlerr->error != 0) { |
Tomasz Wasilczyk | 1accab9 | 2020-07-01 08:08:43 -0700 | [diff] [blame] | 174 | LOG(ERROR) << "Received Netlink error message: " << strerror(-nlerr->error); |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 175 | return false; |
| 176 | } |
| 177 | return true; |
| 178 | } |
| 179 | // Couldn't find any error/ack messages. |
| 180 | return false; |
| 181 | } |
| 182 | |
chrisweir | 06c2c0d | 2020-06-18 16:06:40 -0700 | [diff] [blame] | 183 | std::optional<unsigned int> NetlinkSocket::getSocketPid() { |
| 184 | sockaddr_nl sa = {}; |
| 185 | socklen_t sasize = sizeof(sa); |
| 186 | if (getsockname(mFd.get(), reinterpret_cast<sockaddr*>(&sa), &sasize) < 0) { |
| 187 | PLOG(ERROR) << "Failed to getsockname() for netlink_fd!"; |
| 188 | return std::nullopt; |
| 189 | } |
| 190 | return sa.nl_pid; |
| 191 | } |
| 192 | |
Tomasz Wasilczyk | 55f2193 | 2019-12-20 09:20:24 -0800 | [diff] [blame] | 193 | } // namespace android::netdevice |