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 | |
| 30 | NetlinkSocket::NetlinkSocket(int protocol) : mProtocol(protocol) { |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 31 | mFd.reset(socket(AF_NETLINK, SOCK_RAW, protocol)); |
| 32 | if (!mFd.ok()) { |
chrisweir | 1173a72 | 2020-02-26 14:39:56 -0800 | [diff] [blame] | 33 | PLOG(ERROR) << "Can't open Netlink socket"; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 34 | mFailed = true; |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | struct sockaddr_nl sa = {}; |
| 39 | sa.nl_family = AF_NETLINK; |
| 40 | |
| 41 | if (bind(mFd.get(), reinterpret_cast<struct sockaddr*>(&sa), sizeof(sa)) < 0) { |
chrisweir | 1173a72 | 2020-02-26 14:39:56 -0800 | [diff] [blame] | 42 | PLOG(ERROR) << "Can't bind Netlink socket"; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 43 | mFd.reset(); |
| 44 | mFailed = true; |
| 45 | } |
| 46 | } |
| 47 | |
Tomasz Wasilczyk | a987273 | 2020-06-02 18:03:27 -0700 | [diff] [blame] | 48 | bool NetlinkSocket::send(struct nlmsghdr* nlmsg, size_t totalLen) { |
| 49 | if constexpr (kSuperVerbose) { |
| 50 | nlmsg->nlmsg_seq = mSeq; |
| 51 | LOG(VERBOSE) << (mFailed ? "(not)" : "") |
| 52 | << "sending Netlink message: " << toString(nlmsg, totalLen, mProtocol); |
| 53 | } |
| 54 | |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 55 | if (mFailed) return false; |
| 56 | |
| 57 | nlmsg->nlmsg_pid = 0; // kernel |
| 58 | nlmsg->nlmsg_seq = mSeq++; |
| 59 | nlmsg->nlmsg_flags |= NLM_F_ACK; |
| 60 | |
| 61 | struct iovec iov = {nlmsg, nlmsg->nlmsg_len}; |
| 62 | |
| 63 | struct sockaddr_nl sa = {}; |
| 64 | sa.nl_family = AF_NETLINK; |
| 65 | |
| 66 | struct msghdr msg = {}; |
| 67 | msg.msg_name = &sa; |
| 68 | msg.msg_namelen = sizeof(sa); |
| 69 | msg.msg_iov = &iov; |
| 70 | msg.msg_iovlen = 1; |
| 71 | |
| 72 | if (sendmsg(mFd.get(), &msg, 0) < 0) { |
chrisweir | 1173a72 | 2020-02-26 14:39:56 -0800 | [diff] [blame] | 73 | PLOG(ERROR) << "Can't send Netlink message"; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | bool NetlinkSocket::receiveAck() { |
| 80 | if (mFailed) return false; |
| 81 | |
| 82 | char buf[8192]; |
| 83 | |
| 84 | struct sockaddr_nl sa; |
| 85 | struct iovec iov = {buf, sizeof(buf)}; |
| 86 | |
| 87 | struct msghdr msg = {}; |
| 88 | msg.msg_name = &sa; |
| 89 | msg.msg_namelen = sizeof(sa); |
| 90 | msg.msg_iov = &iov; |
| 91 | msg.msg_iovlen = 1; |
| 92 | |
| 93 | const ssize_t status = recvmsg(mFd.get(), &msg, 0); |
| 94 | if (status < 0) { |
chrisweir | 1173a72 | 2020-02-26 14:39:56 -0800 | [diff] [blame] | 95 | PLOG(ERROR) << "Failed to receive Netlink message"; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | size_t remainingLen = status; |
| 99 | |
| 100 | if (msg.msg_flags & MSG_TRUNC) { |
| 101 | LOG(ERROR) << "Failed to receive Netlink message: truncated"; |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | for (auto nlmsg = reinterpret_cast<struct nlmsghdr*>(buf); NLMSG_OK(nlmsg, remainingLen); |
| 106 | nlmsg = NLMSG_NEXT(nlmsg, remainingLen)) { |
Tomasz Wasilczyk | a987273 | 2020-06-02 18:03:27 -0700 | [diff] [blame] | 107 | if constexpr (kSuperVerbose) { |
| 108 | LOG(VERBOSE) << "received Netlink response: " |
| 109 | << toString(nlmsg, sizeof(buf), mProtocol); |
| 110 | } |
| 111 | |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 112 | // We're looking for error/ack message only, ignoring others. |
| 113 | if (nlmsg->nlmsg_type != NLMSG_ERROR) { |
| 114 | LOG(WARNING) << "Received unexpected Netlink message (ignored): " << nlmsg->nlmsg_type; |
| 115 | continue; |
| 116 | } |
| 117 | |
| 118 | // Found error/ack message, return status. |
| 119 | auto nlerr = reinterpret_cast<struct nlmsgerr*>(NLMSG_DATA(nlmsg)); |
| 120 | if (nlerr->error != 0) { |
| 121 | LOG(ERROR) << "Received Netlink error message: " << nlerr->error; |
| 122 | return false; |
| 123 | } |
| 124 | return true; |
| 125 | } |
| 126 | // Couldn't find any error/ack messages. |
| 127 | return false; |
| 128 | } |
| 129 | |
Tomasz Wasilczyk | 55f2193 | 2019-12-20 09:20:24 -0800 | [diff] [blame] | 130 | } // namespace android::netdevice |