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 int sock = openNetlinkSocket(NETLINK_ROUTE);
88 if (sock < 0) {
89 return sock;
90 }
91
92 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
102 ssize_t writevRet = writev(sock, iov, iovlen);
103 // Don't let pointers to the stack escape.
104 iov[0] = {nullptr, 0};
105 int ret = 0;
106 if (writevRet == -1) {
107 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);
115 } else if ((flags & NLM_F_DUMP) && callback != nullptr) {
116 ret = processNetlinkDump(sock, *callback);
117 }
118
119 close(sock);
120
121 return ret;
122 }
123
sendNetlinkRequest(uint16_t action,uint16_t flags,iovec * iov,int iovlen)124 int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
125 return sendNetlinkRequest(action, flags, iov, iovlen, nullptr);
126 }
127
processNetlinkDump(int sock,const NetlinkDumpCallback & callback)128 int 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:
145 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
rtNetlinkFlush(uint16_t getAction,uint16_t deleteAction,const char * what,const NetlinkDumpFilter & shouldDelete)159 int rtNetlinkFlush(uint16_t getAction, uint16_t deleteAction, const char* what,
160 const NetlinkDumpFilter& shouldDelete) {
161 // 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
167 int writeSock = openNetlinkSocket(NETLINK_ROUTE);
168 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[] = {
199 { nullptr, 0 },
200 { &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
getRtmU32Attribute(const nlmsghdr * nlh,int attribute)214 uint32_t getRtmU32Attribute(const nlmsghdr* nlh, int attribute) {
215 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
226 } // namespace net
227 } // namespace android
228