1 /*
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 
NetlinkConnection()41 NetlinkConnection::NetlinkConnection() {
42   // 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 
~NetlinkConnection()48 NetlinkConnection::~NetlinkConnection() {
49   delete[] data_;
50 }
51 
SendRequest(int type)52 bool 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?
58   if (fd_.get() == -1) {
59     fd_.reset(socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE));
60     if (fd_.get() == -1) return false;
61   }
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.
73   return (TEMP_FAILURE_RETRY(send(fd_.get(), &request, sizeof(request), 0)) == sizeof(request));
74 }
75 
ReadResponses(void callback (void *,nlmsghdr *),void * context)76 bool 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;
79   while ((bytes_read = TEMP_FAILURE_RETRY(recv(fd_.get(), data_, size_, 0))) > 0) {
80     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;
83       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       }
88       callback(context, hdr);
89     }
90   }
91 
92   // We only get here if recv fails before we see a NLMSG_DONE.
93   return false;
94 }
95