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   fd_ = -1;
43 
44   // The kernel keeps packets under 8KiB (NLMSG_GOODSIZE),
45   // but that's a bit too large to go on the stack.
46   size_ = 8192;
47   data_ = new char[size_];
48 }
49 
~NetlinkConnection()50 NetlinkConnection::~NetlinkConnection() {
51   ErrnoRestorer errno_restorer;
52   if (fd_ != -1) close(fd_);
53   delete[] data_;
54 }
55 
SendRequest(int type)56 bool NetlinkConnection::SendRequest(int type) {
57   // Rather than force all callers to check for the unlikely event of being
58   // unable to allocate 8KiB, check here.
59   if (data_ == nullptr) return false;
60 
61   // Did we open a netlink socket yet?
62   if (fd_ == -1) {
63     fd_ = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
64   }
65 
66   // Construct and send the message.
67   struct NetlinkMessage {
68     nlmsghdr hdr;
69     rtgenmsg msg;
70   } request;
71   memset(&request, 0, sizeof(request));
72   request.hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
73   request.hdr.nlmsg_type = type;
74   request.hdr.nlmsg_len = sizeof(request);
75   request.msg.rtgen_family = AF_UNSPEC; // All families.
76   return (TEMP_FAILURE_RETRY(send(fd_, &request, sizeof(request), 0)) == sizeof(request));
77 }
78 
ReadResponses(void callback (void *,nlmsghdr *),void * context)79 bool NetlinkConnection::ReadResponses(void callback(void*, nlmsghdr*), void* context) {
80   // Read through all the responses, handing interesting ones to the callback.
81   ssize_t bytes_read;
82   while ((bytes_read = TEMP_FAILURE_RETRY(recv(fd_, data_, size_, 0))) > 0) {
83     nlmsghdr* hdr = reinterpret_cast<nlmsghdr*>(data_);
84     for (; NLMSG_OK(hdr, static_cast<size_t>(bytes_read)); hdr = NLMSG_NEXT(hdr, bytes_read)) {
85       if (hdr->nlmsg_type == NLMSG_DONE) return true;
86       if (hdr->nlmsg_type == NLMSG_ERROR) return false;
87       callback(context, hdr);
88     }
89   }
90 
91   // We only get here if recv fails before we see a NLMSG_DONE.
92   return false;
93 }
94