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 // 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
sendNetlinkRequest(uint16_t action,uint16_t flags,iovec * iov,int iovlen,const NetlinkDumpCallback * callback)77 WARN_UNUSED_RESULT int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen,
78 const NetlinkDumpCallback *callback) {
79 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
89 int sock = openNetlinkSocket(NETLINK_ROUTE);
90 if (sock < 0) {
91 return sock;
92 }
93
94 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);
105 } else if ((flags & NLM_F_DUMP) && callback != nullptr) {
106 ret = processNetlinkDump(sock, *callback);
107 }
108
109 close(sock);
110
111 return ret;
112 }
113
sendNetlinkRequest(uint16_t action,uint16_t flags,iovec * iov,int iovlen)114 int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
115 return sendNetlinkRequest(action, flags, iov, iovlen, nullptr);
116 }
117
processNetlinkDump(int sock,const NetlinkDumpCallback & callback)118 int 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:
135 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
rtNetlinkFlush(uint16_t getAction,uint16_t deleteAction,const char * what,const NetlinkDumpFilter & shouldDelete)149 WARN_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
157 int writeSock = openNetlinkSocket(NETLINK_ROUTE);
158 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[] = {
189 { nullptr, 0 },
190 { &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
getRtmU32Attribute(const nlmsghdr * nlh,int attribute)204 uint32_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
216 } // namespace net
217 } // namespace android
218