blob: 7cb08d8a35d7ed7520969a5eb2ca318c0aa77943 [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
Bernie Innocenti762dcf42019-06-14 19:52:49 +090068// Disable optimizations in ASan build.
69// ASan reports an out-of-bounds 32-bit(!) access in the first loop of the function (over iov[]).
70// TODO: verify if this bug is still present.
71#ifdef __clang__
72#if __has_feature(address_sanitizer)
73#define OPTNONE [[clang::optnone]]
74#endif
75#endif
76
77#ifndef OPTNONE
78#define OPTNONE /* nop */
79#endif
80
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090081// Sends a netlink request and possibly expects an ack.
82// |iov| is an array of struct iovec that contains the netlink message payload.
83// The netlink header is generated by this function based on |action| and |flags|.
84// Returns -errno if there was an error or if the kernel reported an error.
Bernie Innocenti762dcf42019-06-14 19:52:49 +090085OPTNONE int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen,
86 const NetlinkDumpCallback* callback) {
George Burgess IVbda08aa2021-12-06 23:50:24 -080087 int sock = openNetlinkSocket(NETLINK_ROUTE);
88 if (sock < 0) {
89 return sock;
90 }
91
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090092 nlmsghdr nlmsg = {
93 .nlmsg_type = action,
94 .nlmsg_flags = flags,
95 };
96 iov[0].iov_base = &nlmsg;
97 iov[0].iov_len = sizeof(nlmsg);
98 for (int i = 0; i < iovlen; ++i) {
99 nlmsg.nlmsg_len += iov[i].iov_len;
100 }
101
George Burgess IVbda08aa2021-12-06 23:50:24 -0800102 ssize_t writevRet = writev(sock, iov, iovlen);
103 // Don't let pointers to the stack escape.
104 iov[0] = {nullptr, 0};
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +0900105 int ret = 0;
George Burgess IVbda08aa2021-12-06 23:50:24 -0800106 if (writevRet == -1) {
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +0900107 ret = -errno;
108 ALOGE("netlink socket connect/writev failed (%s)", strerror(-ret));
109 close(sock);
110 return ret;
111 }
112
113 if (flags & NLM_F_ACK) {
114 ret = recvNetlinkAck(sock);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900115 } else if ((flags & NLM_F_DUMP) && callback != nullptr) {
116 ret = processNetlinkDump(sock, *callback);
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +0900117 }
118
119 close(sock);
120
121 return ret;
122}
123
124int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
125 return sendNetlinkRequest(action, flags, iov, iovlen, nullptr);
126}
127
128int processNetlinkDump(int sock, const NetlinkDumpCallback& callback) {
129 char buf[kNetlinkDumpBufferSize];
130
131 ssize_t bytesread;
132 do {
133 bytesread = read(sock, buf, sizeof(buf));
134
135 if (bytesread < 0) {
136 return -errno;
137 }
138
139 uint32_t len = bytesread;
140 for (nlmsghdr *nlh = reinterpret_cast<nlmsghdr *>(buf);
141 NLMSG_OK(nlh, len);
142 nlh = NLMSG_NEXT(nlh, len)) {
143 switch (nlh->nlmsg_type) {
144 case NLMSG_DONE:
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +0900145 return 0;
146 case NLMSG_ERROR: {
147 nlmsgerr *err = reinterpret_cast<nlmsgerr *>(NLMSG_DATA(nlh));
148 return err->error;
149 }
150 default:
151 callback(nlh);
152 }
153 }
154 } while (bytesread > 0);
155
156 return 0;
157}
158
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900159int rtNetlinkFlush(uint16_t getAction, uint16_t deleteAction, const char* what,
160 const NetlinkDumpFilter& shouldDelete) {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900161 // RTM_GETxxx is always RTM_DELxxx + 1, see <linux/rtnetlink.h>.
162 if (getAction != deleteAction + 1) {
163 ALOGE("Unknown flush type getAction=%d deleteAction=%d", getAction, deleteAction);
164 return -EINVAL;
165 }
166
Nathan Harold1a371532017-01-30 12:30:48 -0800167 int writeSock = openNetlinkSocket(NETLINK_ROUTE);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900168 if (writeSock < 0) {
169 return writeSock;
170 }
171
172 NetlinkDumpCallback callback = [writeSock, deleteAction, shouldDelete, what] (nlmsghdr *nlh) {
173 if (!shouldDelete(nlh)) return;
174
175 nlh->nlmsg_type = deleteAction;
176 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
177 if (write(writeSock, nlh, nlh->nlmsg_len) == -1) {
178 ALOGE("Error writing flush request: %s", strerror(errno));
179 return;
180 }
181
182 int ret = recvNetlinkAck(writeSock);
183 // A flush works by dumping routes and deleting each route as it's returned, and it can
184 // fail if something else deletes the route between the dump and the delete. This can
185 // happen, for example, if an interface goes down while we're trying to flush its routes.
186 // So ignore ENOENT.
187 if (ret != 0 && ret != -ENOENT) {
188 ALOGW("Flushing %s: %s", what, strerror(-ret));
189 }
190 };
191
192 int ret = 0;
193 for (const int family : { AF_INET, AF_INET6 }) {
194 // struct fib_rule_hdr and struct rtmsg are functionally identical.
195 rtmsg rule = {
196 .rtm_family = static_cast<uint8_t>(family),
197 };
198 iovec iov[] = {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700199 { nullptr, 0 },
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900200 { &rule, sizeof(rule) },
201 };
202 uint16_t flags = NETLINK_DUMP_FLAGS;
203
204 if ((ret = sendNetlinkRequest(getAction, flags, iov, ARRAY_SIZE(iov), &callback)) != 0) {
205 break;
206 }
207 }
208
209 close(writeSock);
210
211 return ret;
212}
213
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900214uint32_t getRtmU32Attribute(const nlmsghdr* nlh, int attribute) {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900215 uint32_t rta_len = RTM_PAYLOAD(nlh);
216 rtmsg *msg = reinterpret_cast<rtmsg *>(NLMSG_DATA(nlh));
217 rtattr *rta = reinterpret_cast<rtattr *> RTM_RTA(msg);
218 for (; RTA_OK(rta, rta_len); rta = RTA_NEXT(rta, rta_len)) {
219 if (rta->rta_type == attribute) {
220 return *(static_cast<uint32_t *>(RTA_DATA(rta)));
221 }
222 }
223 return 0;
224}
225
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +0900226} // namespace net
227} // namespace android