• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (fd_ == -1) return false;
65   }
66 
67   // Construct and send the message.
68   struct NetlinkMessage {
69     nlmsghdr hdr;
70     rtgenmsg msg;
71   } request;
72   memset(&request, 0, sizeof(request));
73   request.hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
74   request.hdr.nlmsg_type = type;
75   request.hdr.nlmsg_len = sizeof(request);
76   request.msg.rtgen_family = AF_UNSPEC; // All families.
77   return (TEMP_FAILURE_RETRY(send(fd_, &request, sizeof(request), 0)) == sizeof(request));
78 }
79 
ReadResponses(void callback (void *,nlmsghdr *),void * context)80 bool NetlinkConnection::ReadResponses(void callback(void*, nlmsghdr*), void* context) {
81   // Read through all the responses, handing interesting ones to the callback.
82   ssize_t bytes_read;
83   while ((bytes_read = TEMP_FAILURE_RETRY(recv(fd_, data_, size_, 0))) > 0) {
84     nlmsghdr* hdr = reinterpret_cast<nlmsghdr*>(data_);
85     for (; NLMSG_OK(hdr, static_cast<size_t>(bytes_read)); hdr = NLMSG_NEXT(hdr, bytes_read)) {
86       if (hdr->nlmsg_type == NLMSG_DONE) return true;
87       if (hdr->nlmsg_type == NLMSG_ERROR) {
88         nlmsgerr* err = reinterpret_cast<nlmsgerr*>(NLMSG_DATA(hdr));
89         errno = (hdr->nlmsg_len >= NLMSG_LENGTH(sizeof(nlmsgerr))) ? -err->error : EIO;
90         return false;
91       }
92       callback(context, hdr);
93     }
94   }
95 
96   // We only get here if recv fails before we see a NLMSG_DONE.
97   return false;
98 }
99