1 /*
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>
18 #include <linux/netlink.h>
19 #include <linux/rtnetlink.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <sys/uio.h>
23 #include <unistd.h>
24
25 #define LOG_TAG "Netd"
26 #include <log/log.h>
27
28 #include "NetdConstants.h"
29 #include "NetlinkCommands.h"
30
31 namespace android {
32 namespace net {
33
openNetlinkSocket(int protocol)34 int openNetlinkSocket(int protocol) {
35 int sock = socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, protocol);
36 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
recvNetlinkAck(int sock)46 int 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 // 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
81 // 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.
sendNetlinkRequest(uint16_t action,uint16_t flags,iovec * iov,int iovlen,const NetlinkDumpCallback * callback)85 OPTNONE int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen,
86 const NetlinkDumpCallback* callback) {
87 nlmsghdr nlmsg = {
88 .nlmsg_type = action,
89 .nlmsg_flags = flags,
90 };
91 iov[0].iov_base = &nlmsg;
92 iov[0].iov_len = sizeof(nlmsg);
93 for (int i = 0; i < iovlen; ++i) {
94 nlmsg.nlmsg_len += iov[i].iov_len;
95 }
96
97 int sock = openNetlinkSocket(NETLINK_ROUTE);
98 if (sock < 0) {
99 return sock;
100 }
101
102 int ret = 0;
103
104 if (writev(sock, iov, iovlen) == -1) {
105 ret = -errno;
106 ALOGE("netlink socket connect/writev failed (%s)", strerror(-ret));
107 close(sock);
108 return ret;
109 }
110
111 if (flags & NLM_F_ACK) {
112 ret = recvNetlinkAck(sock);
113 } else if ((flags & NLM_F_DUMP) && callback != nullptr) {
114 ret = processNetlinkDump(sock, *callback);
115 }
116
117 close(sock);
118
119 return ret;
120 }
121
sendNetlinkRequest(uint16_t action,uint16_t flags,iovec * iov,int iovlen)122 int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
123 return sendNetlinkRequest(action, flags, iov, iovlen, nullptr);
124 }
125
processNetlinkDump(int sock,const NetlinkDumpCallback & callback)126 int processNetlinkDump(int sock, const NetlinkDumpCallback& callback) {
127 char buf[kNetlinkDumpBufferSize];
128
129 ssize_t bytesread;
130 do {
131 bytesread = read(sock, buf, sizeof(buf));
132
133 if (bytesread < 0) {
134 return -errno;
135 }
136
137 uint32_t len = bytesread;
138 for (nlmsghdr *nlh = reinterpret_cast<nlmsghdr *>(buf);
139 NLMSG_OK(nlh, len);
140 nlh = NLMSG_NEXT(nlh, len)) {
141 switch (nlh->nlmsg_type) {
142 case NLMSG_DONE:
143 return 0;
144 case NLMSG_ERROR: {
145 nlmsgerr *err = reinterpret_cast<nlmsgerr *>(NLMSG_DATA(nlh));
146 return err->error;
147 }
148 default:
149 callback(nlh);
150 }
151 }
152 } while (bytesread > 0);
153
154 return 0;
155 }
156
rtNetlinkFlush(uint16_t getAction,uint16_t deleteAction,const char * what,const NetlinkDumpFilter & shouldDelete)157 int rtNetlinkFlush(uint16_t getAction, uint16_t deleteAction, const char* what,
158 const NetlinkDumpFilter& shouldDelete) {
159 // RTM_GETxxx is always RTM_DELxxx + 1, see <linux/rtnetlink.h>.
160 if (getAction != deleteAction + 1) {
161 ALOGE("Unknown flush type getAction=%d deleteAction=%d", getAction, deleteAction);
162 return -EINVAL;
163 }
164
165 int writeSock = openNetlinkSocket(NETLINK_ROUTE);
166 if (writeSock < 0) {
167 return writeSock;
168 }
169
170 NetlinkDumpCallback callback = [writeSock, deleteAction, shouldDelete, what] (nlmsghdr *nlh) {
171 if (!shouldDelete(nlh)) return;
172
173 nlh->nlmsg_type = deleteAction;
174 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
175 if (write(writeSock, nlh, nlh->nlmsg_len) == -1) {
176 ALOGE("Error writing flush request: %s", strerror(errno));
177 return;
178 }
179
180 int ret = recvNetlinkAck(writeSock);
181 // A flush works by dumping routes and deleting each route as it's returned, and it can
182 // fail if something else deletes the route between the dump and the delete. This can
183 // happen, for example, if an interface goes down while we're trying to flush its routes.
184 // So ignore ENOENT.
185 if (ret != 0 && ret != -ENOENT) {
186 ALOGW("Flushing %s: %s", what, strerror(-ret));
187 }
188 };
189
190 int ret = 0;
191 for (const int family : { AF_INET, AF_INET6 }) {
192 // struct fib_rule_hdr and struct rtmsg are functionally identical.
193 rtmsg rule = {
194 .rtm_family = static_cast<uint8_t>(family),
195 };
196 iovec iov[] = {
197 { nullptr, 0 },
198 { &rule, sizeof(rule) },
199 };
200 uint16_t flags = NETLINK_DUMP_FLAGS;
201
202 if ((ret = sendNetlinkRequest(getAction, flags, iov, ARRAY_SIZE(iov), &callback)) != 0) {
203 break;
204 }
205 }
206
207 close(writeSock);
208
209 return ret;
210 }
211
getRtmU32Attribute(const nlmsghdr * nlh,int attribute)212 uint32_t getRtmU32Attribute(const nlmsghdr* nlh, int attribute) {
213 uint32_t rta_len = RTM_PAYLOAD(nlh);
214 rtmsg *msg = reinterpret_cast<rtmsg *>(NLMSG_DATA(nlh));
215 rtattr *rta = reinterpret_cast<rtattr *> RTM_RTA(msg);
216 for (; RTA_OK(rta, rta_len); rta = RTA_NEXT(rta, rta_len)) {
217 if (rta->rta_type == attribute) {
218 return *(static_cast<uint32_t *>(RTA_DATA(rta)));
219 }
220 }
221 return 0;
222 }
223
224 } // namespace net
225 } // namespace android
226