xref: /aosp_15_r20/system/netd/server/NetlinkCommands.cpp (revision 8542734a0dd1db395a4d42aae09c37f3c3c3e7a1)
1*8542734aSAndroid Build Coastguard Worker /*
2*8542734aSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*8542734aSAndroid Build Coastguard Worker  *
4*8542734aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8542734aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8542734aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8542734aSAndroid Build Coastguard Worker  *
8*8542734aSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8542734aSAndroid Build Coastguard Worker  *
10*8542734aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8542734aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8542734aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8542734aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8542734aSAndroid Build Coastguard Worker  * limitations under the License.
15*8542734aSAndroid Build Coastguard Worker  */
16*8542734aSAndroid Build Coastguard Worker 
17*8542734aSAndroid Build Coastguard Worker #include <errno.h>
18*8542734aSAndroid Build Coastguard Worker #include <linux/netlink.h>
19*8542734aSAndroid Build Coastguard Worker #include <linux/rtnetlink.h>
20*8542734aSAndroid Build Coastguard Worker #include <sys/socket.h>
21*8542734aSAndroid Build Coastguard Worker #include <sys/types.h>
22*8542734aSAndroid Build Coastguard Worker #include <sys/uio.h>
23*8542734aSAndroid Build Coastguard Worker #include <unistd.h>
24*8542734aSAndroid Build Coastguard Worker 
25*8542734aSAndroid Build Coastguard Worker #define LOG_TAG "Netd"
26*8542734aSAndroid Build Coastguard Worker #include <log/log.h>
27*8542734aSAndroid Build Coastguard Worker 
28*8542734aSAndroid Build Coastguard Worker #include "NetdConstants.h"
29*8542734aSAndroid Build Coastguard Worker #include "NetlinkCommands.h"
30*8542734aSAndroid Build Coastguard Worker 
31*8542734aSAndroid Build Coastguard Worker namespace android {
32*8542734aSAndroid Build Coastguard Worker namespace net {
33*8542734aSAndroid Build Coastguard Worker 
openNetlinkSocket(int protocol)34*8542734aSAndroid Build Coastguard Worker int openNetlinkSocket(int protocol) {
35*8542734aSAndroid Build Coastguard Worker     int sock = socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, protocol);
36*8542734aSAndroid Build Coastguard Worker     if (sock == -1) {
37*8542734aSAndroid Build Coastguard Worker         return -errno;
38*8542734aSAndroid Build Coastguard Worker     }
39*8542734aSAndroid Build Coastguard Worker     if (connect(sock, reinterpret_cast<const sockaddr*>(&KERNEL_NLADDR),
40*8542734aSAndroid Build Coastguard Worker                 sizeof(KERNEL_NLADDR)) == -1) {
41*8542734aSAndroid Build Coastguard Worker         close(sock);
42*8542734aSAndroid Build Coastguard Worker         return -errno;
43*8542734aSAndroid Build Coastguard Worker     }
44*8542734aSAndroid Build Coastguard Worker     return sock;
45*8542734aSAndroid Build Coastguard Worker }
46*8542734aSAndroid Build Coastguard Worker 
recvNetlinkAck(int sock)47*8542734aSAndroid Build Coastguard Worker int recvNetlinkAck(int sock) {
48*8542734aSAndroid Build Coastguard Worker     struct {
49*8542734aSAndroid Build Coastguard Worker         nlmsghdr msg;
50*8542734aSAndroid Build Coastguard Worker         nlmsgerr err;
51*8542734aSAndroid Build Coastguard Worker     } response;
52*8542734aSAndroid Build Coastguard Worker 
53*8542734aSAndroid Build Coastguard Worker     int ret = recv(sock, &response, sizeof(response), 0);
54*8542734aSAndroid Build Coastguard Worker 
55*8542734aSAndroid Build Coastguard Worker     if (ret == -1) {
56*8542734aSAndroid Build Coastguard Worker         ret = -errno;
57*8542734aSAndroid Build Coastguard Worker         ALOGE("netlink recv failed (%s)", strerror(-ret));
58*8542734aSAndroid Build Coastguard Worker         return ret;
59*8542734aSAndroid Build Coastguard Worker     }
60*8542734aSAndroid Build Coastguard Worker 
61*8542734aSAndroid Build Coastguard Worker     if (ret != sizeof(response)) {
62*8542734aSAndroid Build Coastguard Worker         ALOGE("bad netlink response message size (%d != %zu)", ret, sizeof(response));
63*8542734aSAndroid Build Coastguard Worker         return -EBADMSG;
64*8542734aSAndroid Build Coastguard Worker     }
65*8542734aSAndroid Build Coastguard Worker 
66*8542734aSAndroid Build Coastguard Worker     return response.err.error;  // Netlink errors are negative errno.
67*8542734aSAndroid Build Coastguard Worker }
68*8542734aSAndroid Build Coastguard Worker 
69*8542734aSAndroid Build Coastguard Worker // Disable optimizations in ASan build.
70*8542734aSAndroid Build Coastguard Worker // ASan reports an out-of-bounds 32-bit(!) access in the first loop of the function (over iov[]).
71*8542734aSAndroid Build Coastguard Worker // TODO: verify if this bug is still present.
72*8542734aSAndroid Build Coastguard Worker #ifdef __clang__
73*8542734aSAndroid Build Coastguard Worker #if __has_feature(address_sanitizer)
74*8542734aSAndroid Build Coastguard Worker #define OPTNONE [[clang::optnone]]
75*8542734aSAndroid Build Coastguard Worker #endif
76*8542734aSAndroid Build Coastguard Worker #endif
77*8542734aSAndroid Build Coastguard Worker 
78*8542734aSAndroid Build Coastguard Worker #ifndef OPTNONE
79*8542734aSAndroid Build Coastguard Worker #define OPTNONE /* nop */
80*8542734aSAndroid Build Coastguard Worker #endif
81*8542734aSAndroid Build Coastguard Worker 
82*8542734aSAndroid Build Coastguard Worker // Sends a netlink request and possibly expects an ack.
83*8542734aSAndroid Build Coastguard Worker // |iov| is an array of struct iovec that contains the netlink message payload.
84*8542734aSAndroid Build Coastguard Worker // The netlink header is generated by this function based on |action| and |flags|.
85*8542734aSAndroid Build Coastguard Worker // 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)86*8542734aSAndroid Build Coastguard Worker OPTNONE int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen,
87*8542734aSAndroid Build Coastguard Worker                                const NetlinkDumpCallback* callback) {
88*8542734aSAndroid Build Coastguard Worker     int sock = openNetlinkSocket(NETLINK_ROUTE);
89*8542734aSAndroid Build Coastguard Worker     if (sock < 0) {
90*8542734aSAndroid Build Coastguard Worker         return sock;
91*8542734aSAndroid Build Coastguard Worker     }
92*8542734aSAndroid Build Coastguard Worker 
93*8542734aSAndroid Build Coastguard Worker     nlmsghdr nlmsg = {
94*8542734aSAndroid Build Coastguard Worker         .nlmsg_type = action,
95*8542734aSAndroid Build Coastguard Worker         .nlmsg_flags = flags,
96*8542734aSAndroid Build Coastguard Worker     };
97*8542734aSAndroid Build Coastguard Worker     iov[0].iov_base = &nlmsg;
98*8542734aSAndroid Build Coastguard Worker     iov[0].iov_len = sizeof(nlmsg);
99*8542734aSAndroid Build Coastguard Worker     for (int i = 0; i < iovlen; ++i) {
100*8542734aSAndroid Build Coastguard Worker         nlmsg.nlmsg_len += iov[i].iov_len;
101*8542734aSAndroid Build Coastguard Worker     }
102*8542734aSAndroid Build Coastguard Worker 
103*8542734aSAndroid Build Coastguard Worker     ssize_t writevRet = writev(sock, iov, iovlen);
104*8542734aSAndroid Build Coastguard Worker     // Don't let pointers to the stack escape.
105*8542734aSAndroid Build Coastguard Worker     iov[0] = {nullptr, 0};
106*8542734aSAndroid Build Coastguard Worker     int ret = 0;
107*8542734aSAndroid Build Coastguard Worker     if (writevRet == -1) {
108*8542734aSAndroid Build Coastguard Worker         ret = -errno;
109*8542734aSAndroid Build Coastguard Worker         ALOGE("netlink socket connect/writev failed (%s)", strerror(-ret));
110*8542734aSAndroid Build Coastguard Worker         close(sock);
111*8542734aSAndroid Build Coastguard Worker         return ret;
112*8542734aSAndroid Build Coastguard Worker     }
113*8542734aSAndroid Build Coastguard Worker 
114*8542734aSAndroid Build Coastguard Worker     if (flags & NLM_F_ACK) {
115*8542734aSAndroid Build Coastguard Worker         ret = recvNetlinkAck(sock);
116*8542734aSAndroid Build Coastguard Worker     } else if ((flags & NLM_F_DUMP) && callback != nullptr) {
117*8542734aSAndroid Build Coastguard Worker         ret = processNetlinkDump(sock, *callback);
118*8542734aSAndroid Build Coastguard Worker     }
119*8542734aSAndroid Build Coastguard Worker 
120*8542734aSAndroid Build Coastguard Worker     close(sock);
121*8542734aSAndroid Build Coastguard Worker 
122*8542734aSAndroid Build Coastguard Worker     return ret;
123*8542734aSAndroid Build Coastguard Worker }
124*8542734aSAndroid Build Coastguard Worker 
sendNetlinkRequest(uint16_t action,uint16_t flags,iovec * iov,int iovlen)125*8542734aSAndroid Build Coastguard Worker int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
126*8542734aSAndroid Build Coastguard Worker     return sendNetlinkRequest(action, flags, iov, iovlen, nullptr);
127*8542734aSAndroid Build Coastguard Worker }
128*8542734aSAndroid Build Coastguard Worker 
processNetlinkDump(int sock,const NetlinkDumpCallback & callback)129*8542734aSAndroid Build Coastguard Worker int processNetlinkDump(int sock, const NetlinkDumpCallback& callback) {
130*8542734aSAndroid Build Coastguard Worker     char buf[kNetlinkDumpBufferSize];
131*8542734aSAndroid Build Coastguard Worker 
132*8542734aSAndroid Build Coastguard Worker     ssize_t bytesread;
133*8542734aSAndroid Build Coastguard Worker     do {
134*8542734aSAndroid Build Coastguard Worker         bytesread = read(sock, buf, sizeof(buf));
135*8542734aSAndroid Build Coastguard Worker 
136*8542734aSAndroid Build Coastguard Worker         if (bytesread < 0) {
137*8542734aSAndroid Build Coastguard Worker             return -errno;
138*8542734aSAndroid Build Coastguard Worker         }
139*8542734aSAndroid Build Coastguard Worker 
140*8542734aSAndroid Build Coastguard Worker         uint32_t len = bytesread;
141*8542734aSAndroid Build Coastguard Worker         for (nlmsghdr *nlh = reinterpret_cast<nlmsghdr *>(buf);
142*8542734aSAndroid Build Coastguard Worker              NLMSG_OK(nlh, len);
143*8542734aSAndroid Build Coastguard Worker              nlh = NLMSG_NEXT(nlh, len)) {
144*8542734aSAndroid Build Coastguard Worker             switch (nlh->nlmsg_type) {
145*8542734aSAndroid Build Coastguard Worker               case NLMSG_DONE:
146*8542734aSAndroid Build Coastguard Worker                 return 0;
147*8542734aSAndroid Build Coastguard Worker               case NLMSG_ERROR: {
148*8542734aSAndroid Build Coastguard Worker                 nlmsgerr *err = reinterpret_cast<nlmsgerr *>(NLMSG_DATA(nlh));
149*8542734aSAndroid Build Coastguard Worker                 return err->error;
150*8542734aSAndroid Build Coastguard Worker               }
151*8542734aSAndroid Build Coastguard Worker               default:
152*8542734aSAndroid Build Coastguard Worker                 callback(nlh);
153*8542734aSAndroid Build Coastguard Worker             }
154*8542734aSAndroid Build Coastguard Worker         }
155*8542734aSAndroid Build Coastguard Worker     } while (bytesread > 0);
156*8542734aSAndroid Build Coastguard Worker 
157*8542734aSAndroid Build Coastguard Worker     return 0;
158*8542734aSAndroid Build Coastguard Worker }
159*8542734aSAndroid Build Coastguard Worker 
rtNetlinkFlush(uint16_t getAction,uint16_t deleteAction,const char * what,const NetlinkDumpFilter & shouldDelete)160*8542734aSAndroid Build Coastguard Worker int rtNetlinkFlush(uint16_t getAction, uint16_t deleteAction, const char* what,
161*8542734aSAndroid Build Coastguard Worker                    const NetlinkDumpFilter& shouldDelete) {
162*8542734aSAndroid Build Coastguard Worker     // RTM_GETxxx is always RTM_DELxxx + 1, see <linux/rtnetlink.h>.
163*8542734aSAndroid Build Coastguard Worker     if (getAction != deleteAction + 1) {
164*8542734aSAndroid Build Coastguard Worker         ALOGE("Unknown flush type getAction=%d deleteAction=%d", getAction, deleteAction);
165*8542734aSAndroid Build Coastguard Worker         return -EINVAL;
166*8542734aSAndroid Build Coastguard Worker     }
167*8542734aSAndroid Build Coastguard Worker 
168*8542734aSAndroid Build Coastguard Worker     int writeSock = openNetlinkSocket(NETLINK_ROUTE);
169*8542734aSAndroid Build Coastguard Worker     if (writeSock < 0) {
170*8542734aSAndroid Build Coastguard Worker         return writeSock;
171*8542734aSAndroid Build Coastguard Worker     }
172*8542734aSAndroid Build Coastguard Worker 
173*8542734aSAndroid Build Coastguard Worker     NetlinkDumpCallback callback = [writeSock, deleteAction, shouldDelete, what] (nlmsghdr *nlh) {
174*8542734aSAndroid Build Coastguard Worker         if (!shouldDelete(nlh)) return;
175*8542734aSAndroid Build Coastguard Worker 
176*8542734aSAndroid Build Coastguard Worker         nlh->nlmsg_type = deleteAction;
177*8542734aSAndroid Build Coastguard Worker         nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
178*8542734aSAndroid Build Coastguard Worker         if (write(writeSock, nlh, nlh->nlmsg_len) == -1) {
179*8542734aSAndroid Build Coastguard Worker             ALOGE("Error writing flush request: %s", strerror(errno));
180*8542734aSAndroid Build Coastguard Worker             return;
181*8542734aSAndroid Build Coastguard Worker         }
182*8542734aSAndroid Build Coastguard Worker 
183*8542734aSAndroid Build Coastguard Worker         int ret = recvNetlinkAck(writeSock);
184*8542734aSAndroid Build Coastguard Worker         // A flush works by dumping routes and deleting each route as it's returned, and it can
185*8542734aSAndroid Build Coastguard Worker         // fail if something else deletes the route between the dump and the delete. This can
186*8542734aSAndroid Build Coastguard Worker         // happen, for example, if an interface goes down while we're trying to flush its routes.
187*8542734aSAndroid Build Coastguard Worker         // So ignore ENOENT.
188*8542734aSAndroid Build Coastguard Worker         if (ret != 0 && ret != -ENOENT) {
189*8542734aSAndroid Build Coastguard Worker             ALOGW("Flushing %s: %s", what, strerror(-ret));
190*8542734aSAndroid Build Coastguard Worker         }
191*8542734aSAndroid Build Coastguard Worker     };
192*8542734aSAndroid Build Coastguard Worker 
193*8542734aSAndroid Build Coastguard Worker     int ret = 0;
194*8542734aSAndroid Build Coastguard Worker     for (const int family : { AF_INET, AF_INET6 }) {
195*8542734aSAndroid Build Coastguard Worker         // struct fib_rule_hdr and struct rtmsg are functionally identical.
196*8542734aSAndroid Build Coastguard Worker         rtmsg rule = {
197*8542734aSAndroid Build Coastguard Worker             .rtm_family = static_cast<uint8_t>(family),
198*8542734aSAndroid Build Coastguard Worker         };
199*8542734aSAndroid Build Coastguard Worker         iovec iov[] = {
200*8542734aSAndroid Build Coastguard Worker             { nullptr,  0 },
201*8542734aSAndroid Build Coastguard Worker             { &rule, sizeof(rule) },
202*8542734aSAndroid Build Coastguard Worker         };
203*8542734aSAndroid Build Coastguard Worker         uint16_t flags = NETLINK_DUMP_FLAGS;
204*8542734aSAndroid Build Coastguard Worker 
205*8542734aSAndroid Build Coastguard Worker         if ((ret = sendNetlinkRequest(getAction, flags, iov, ARRAY_SIZE(iov), &callback)) != 0) {
206*8542734aSAndroid Build Coastguard Worker             break;
207*8542734aSAndroid Build Coastguard Worker         }
208*8542734aSAndroid Build Coastguard Worker     }
209*8542734aSAndroid Build Coastguard Worker 
210*8542734aSAndroid Build Coastguard Worker     close(writeSock);
211*8542734aSAndroid Build Coastguard Worker 
212*8542734aSAndroid Build Coastguard Worker     return ret;
213*8542734aSAndroid Build Coastguard Worker }
214*8542734aSAndroid Build Coastguard Worker 
getRtmU32Attribute(const nlmsghdr * nlh,int attribute)215*8542734aSAndroid Build Coastguard Worker uint32_t getRtmU32Attribute(const nlmsghdr* nlh, int attribute) {
216*8542734aSAndroid Build Coastguard Worker     uint32_t rta_len = RTM_PAYLOAD(nlh);
217*8542734aSAndroid Build Coastguard Worker     rtmsg *msg = reinterpret_cast<rtmsg *>(NLMSG_DATA(nlh));
218*8542734aSAndroid Build Coastguard Worker     rtattr *rta = reinterpret_cast<rtattr *> RTM_RTA(msg);
219*8542734aSAndroid Build Coastguard Worker     for (; RTA_OK(rta, rta_len); rta = RTA_NEXT(rta, rta_len)) {
220*8542734aSAndroid Build Coastguard Worker         if (rta->rta_type == attribute) {
221*8542734aSAndroid Build Coastguard Worker             return *(static_cast<uint32_t *>(RTA_DATA(rta)));
222*8542734aSAndroid Build Coastguard Worker         }
223*8542734aSAndroid Build Coastguard Worker     }
224*8542734aSAndroid Build Coastguard Worker     return 0;
225*8542734aSAndroid Build Coastguard Worker }
226*8542734aSAndroid Build Coastguard Worker 
227*8542734aSAndroid Build Coastguard Worker }  // namespace net
228*8542734aSAndroid Build Coastguard Worker }  // namespace android
229