blob: 5d5a0262430a069ca963ce3a5ea129e48fa432cd [file] [log] [blame]
Elliott Hughesed57b982016-01-15 21:02:56 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include "bionic_netlink.h"
30
31#include <errno.h>
32#include <linux/netlink.h>
33#include <linux/rtnetlink.h>
34#include <string.h>
35#include <stdlib.h>
36#include <sys/socket.h>
37#include <unistd.h>
38
39#include "private/ErrnoRestorer.h"
40
41NetlinkConnection::NetlinkConnection() {
Elliott Hughesed57b982016-01-15 21:02:56 -080042 // The kernel keeps packets under 8KiB (NLMSG_GOODSIZE),
43 // but that's a bit too large to go on the stack.
44 size_ = 8192;
45 data_ = new char[size_];
46}
47
48NetlinkConnection::~NetlinkConnection() {
Elliott Hughesed57b982016-01-15 21:02:56 -080049 delete[] data_;
50}
51
52bool NetlinkConnection::SendRequest(int type) {
53 // Rather than force all callers to check for the unlikely event of being
54 // unable to allocate 8KiB, check here.
55 if (data_ == nullptr) return false;
56
57 // Did we open a netlink socket yet?
Elliott Hughes6cb70ad2019-10-31 15:37:32 -070058 if (fd_.get() == -1) {
59 fd_.reset(socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE));
60 if (fd_.get() == -1) return false;
Elliott Hughesed57b982016-01-15 21:02:56 -080061 }
62
63 // Construct and send the message.
64 struct NetlinkMessage {
65 nlmsghdr hdr;
66 rtgenmsg msg;
67 } request;
68 memset(&request, 0, sizeof(request));
69 request.hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
70 request.hdr.nlmsg_type = type;
71 request.hdr.nlmsg_len = sizeof(request);
72 request.msg.rtgen_family = AF_UNSPEC; // All families.
Elliott Hughes6cb70ad2019-10-31 15:37:32 -070073 return (TEMP_FAILURE_RETRY(send(fd_.get(), &request, sizeof(request), 0)) == sizeof(request));
Elliott Hughesed57b982016-01-15 21:02:56 -080074}
75
76bool NetlinkConnection::ReadResponses(void callback(void*, nlmsghdr*), void* context) {
77 // Read through all the responses, handing interesting ones to the callback.
78 ssize_t bytes_read;
Elliott Hughes6cb70ad2019-10-31 15:37:32 -070079 while ((bytes_read = TEMP_FAILURE_RETRY(recv(fd_.get(), data_, size_, 0))) > 0) {
Elliott Hughesed57b982016-01-15 21:02:56 -080080 nlmsghdr* hdr = reinterpret_cast<nlmsghdr*>(data_);
81 for (; NLMSG_OK(hdr, static_cast<size_t>(bytes_read)); hdr = NLMSG_NEXT(hdr, bytes_read)) {
82 if (hdr->nlmsg_type == NLMSG_DONE) return true;
Elliott Hughes22950682016-10-14 12:15:32 -070083 if (hdr->nlmsg_type == NLMSG_ERROR) {
84 nlmsgerr* err = reinterpret_cast<nlmsgerr*>(NLMSG_DATA(hdr));
85 errno = (hdr->nlmsg_len >= NLMSG_LENGTH(sizeof(nlmsgerr))) ? -err->error : EIO;
86 return false;
87 }
Elliott Hughesed57b982016-01-15 21:02:56 -080088 callback(context, hdr);
89 }
90 }
91
92 // We only get here if recv fails before we see a NLMSG_DONE.
93 return false;
94}