blob: 7af33fea15dc0306eba0c4ef9f4fc186b5c1044a [file] [log] [blame]
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +09001/*
2 * Copyright (C) 2017 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
17#include <errno.h>
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090018#include <linux/netlink.h>
19#include <linux/rtnetlink.h>
Tom Cherryb2c91362019-01-15 20:40:52 -080020#include <sys/socket.h>
21#include <sys/types.h>
22#include <sys/uio.h>
23#include <unistd.h>
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090024
25#define LOG_TAG "Netd"
Logan Chien3f461482018-04-23 14:31:32 +080026#include <log/log.h>
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090027
28#include "NetdConstants.h"
29#include "NetlinkCommands.h"
30
31namespace android {
32namespace net {
33
Nathan Harold1a371532017-01-30 12:30:48 -080034int openNetlinkSocket(int protocol) {
35 int sock = socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, protocol);
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090036 if (sock == -1) {
37 return -errno;
38 }
39 if (connect(sock, reinterpret_cast<const sockaddr*>(&KERNEL_NLADDR),
40 sizeof(KERNEL_NLADDR)) == -1) {
41 return -errno;
42 }
43 return sock;
44}
45
46int recvNetlinkAck(int sock) {
47 struct {
48 nlmsghdr msg;
49 nlmsgerr err;
50 } response;
51
52 int ret = recv(sock, &response, sizeof(response), 0);
53
54 if (ret == -1) {
55 ret = -errno;
56 ALOGE("netlink recv failed (%s)", strerror(-ret));
57 return ret;
58 }
59
60 if (ret != sizeof(response)) {
61 ALOGE("bad netlink response message size (%d != %zu)", ret, sizeof(response));
62 return -EBADMSG;
63 }
64
65 return response.err.error; // Netlink errors are negative errno.
66}
67
68// Sends a netlink request and possibly expects an ack.
69// |iov| is an array of struct iovec that contains the netlink message payload.
70// The netlink header is generated by this function based on |action| and |flags|.
71// Returns -errno if there was an error or if the kernel reported an error.
72#ifdef __clang__
73#if __has_feature(address_sanitizer)
74__attribute__((optnone))
75#endif
76#endif
77WARN_UNUSED_RESULT int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen,
Lorenzo Colittif3e299a2017-02-14 17:24:28 +090078 const NetlinkDumpCallback *callback) {
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090079 nlmsghdr nlmsg = {
80 .nlmsg_type = action,
81 .nlmsg_flags = flags,
82 };
83 iov[0].iov_base = &nlmsg;
84 iov[0].iov_len = sizeof(nlmsg);
85 for (int i = 0; i < iovlen; ++i) {
86 nlmsg.nlmsg_len += iov[i].iov_len;
87 }
88
Nathan Harold1a371532017-01-30 12:30:48 -080089 int sock = openNetlinkSocket(NETLINK_ROUTE);
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090090 if (sock < 0) {
91 return sock;
92 }
93
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090094 int ret = 0;
95
96 if (writev(sock, iov, iovlen) == -1) {
97 ret = -errno;
98 ALOGE("netlink socket connect/writev failed (%s)", strerror(-ret));
99 close(sock);
100 return ret;
101 }
102
103 if (flags & NLM_F_ACK) {
104 ret = recvNetlinkAck(sock);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900105 } else if ((flags & NLM_F_DUMP) && callback != nullptr) {
106 ret = processNetlinkDump(sock, *callback);
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +0900107 }
108
109 close(sock);
110
111 return ret;
112}
113
114int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
115 return sendNetlinkRequest(action, flags, iov, iovlen, nullptr);
116}
117
118int processNetlinkDump(int sock, const NetlinkDumpCallback& callback) {
119 char buf[kNetlinkDumpBufferSize];
120
121 ssize_t bytesread;
122 do {
123 bytesread = read(sock, buf, sizeof(buf));
124
125 if (bytesread < 0) {
126 return -errno;
127 }
128
129 uint32_t len = bytesread;
130 for (nlmsghdr *nlh = reinterpret_cast<nlmsghdr *>(buf);
131 NLMSG_OK(nlh, len);
132 nlh = NLMSG_NEXT(nlh, len)) {
133 switch (nlh->nlmsg_type) {
134 case NLMSG_DONE:
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +0900135 return 0;
136 case NLMSG_ERROR: {
137 nlmsgerr *err = reinterpret_cast<nlmsgerr *>(NLMSG_DATA(nlh));
138 return err->error;
139 }
140 default:
141 callback(nlh);
142 }
143 }
144 } while (bytesread > 0);
145
146 return 0;
147}
148
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900149WARN_UNUSED_RESULT int rtNetlinkFlush(uint16_t getAction, uint16_t deleteAction,
150 const char *what, const NetlinkDumpFilter& shouldDelete) {
151 // RTM_GETxxx is always RTM_DELxxx + 1, see <linux/rtnetlink.h>.
152 if (getAction != deleteAction + 1) {
153 ALOGE("Unknown flush type getAction=%d deleteAction=%d", getAction, deleteAction);
154 return -EINVAL;
155 }
156
Nathan Harold1a371532017-01-30 12:30:48 -0800157 int writeSock = openNetlinkSocket(NETLINK_ROUTE);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900158 if (writeSock < 0) {
159 return writeSock;
160 }
161
162 NetlinkDumpCallback callback = [writeSock, deleteAction, shouldDelete, what] (nlmsghdr *nlh) {
163 if (!shouldDelete(nlh)) return;
164
165 nlh->nlmsg_type = deleteAction;
166 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
167 if (write(writeSock, nlh, nlh->nlmsg_len) == -1) {
168 ALOGE("Error writing flush request: %s", strerror(errno));
169 return;
170 }
171
172 int ret = recvNetlinkAck(writeSock);
173 // A flush works by dumping routes and deleting each route as it's returned, and it can
174 // fail if something else deletes the route between the dump and the delete. This can
175 // happen, for example, if an interface goes down while we're trying to flush its routes.
176 // So ignore ENOENT.
177 if (ret != 0 && ret != -ENOENT) {
178 ALOGW("Flushing %s: %s", what, strerror(-ret));
179 }
180 };
181
182 int ret = 0;
183 for (const int family : { AF_INET, AF_INET6 }) {
184 // struct fib_rule_hdr and struct rtmsg are functionally identical.
185 rtmsg rule = {
186 .rtm_family = static_cast<uint8_t>(family),
187 };
188 iovec iov[] = {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700189 { nullptr, 0 },
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900190 { &rule, sizeof(rule) },
191 };
192 uint16_t flags = NETLINK_DUMP_FLAGS;
193
194 if ((ret = sendNetlinkRequest(getAction, flags, iov, ARRAY_SIZE(iov), &callback)) != 0) {
195 break;
196 }
197 }
198
199 close(writeSock);
200
201 return ret;
202}
203
204uint32_t getRtmU32Attribute(const nlmsghdr *nlh, int attribute) {
205 uint32_t rta_len = RTM_PAYLOAD(nlh);
206 rtmsg *msg = reinterpret_cast<rtmsg *>(NLMSG_DATA(nlh));
207 rtattr *rta = reinterpret_cast<rtattr *> RTM_RTA(msg);
208 for (; RTA_OK(rta, rta_len); rta = RTA_NEXT(rta, rta_len)) {
209 if (rta->rta_type == attribute) {
210 return *(static_cast<uint32_t *>(RTA_DATA(rta)));
211 }
212 }
213 return 0;
214}
215
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +0900216} // namespace net
217} // namespace android