Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 <netdb.h> |
| 19 | #include <string.h> |
| 20 | #include <netinet/in.h> |
| 21 | #include <netinet/tcp.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/uio.h> |
| 24 | |
| 25 | #include <linux/netlink.h> |
| 26 | #include <linux/sock_diag.h> |
| 27 | #include <linux/inet_diag.h> |
| 28 | |
| 29 | #define LOG_TAG "Netd" |
| 30 | |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 31 | #include <android-base/strings.h> |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 32 | #include <cutils/log.h> |
| 33 | |
| 34 | #include "NetdConstants.h" |
| 35 | #include "SockDiag.h" |
| 36 | |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 37 | #include <chrono> |
| 38 | |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 39 | #ifndef SOCK_DESTROY |
| 40 | #define SOCK_DESTROY 21 |
| 41 | #endif |
| 42 | |
| 43 | namespace { |
| 44 | |
| 45 | struct AddrinfoDeleter { |
| 46 | void operator()(addrinfo *a) { if (a) freeaddrinfo(a); } |
| 47 | }; |
| 48 | |
| 49 | typedef std::unique_ptr<addrinfo, AddrinfoDeleter> ScopedAddrinfo; |
| 50 | |
| 51 | int checkError(int fd) { |
| 52 | struct { |
| 53 | nlmsghdr h; |
| 54 | nlmsgerr err; |
| 55 | } __attribute__((__packed__)) ack; |
| 56 | ssize_t bytesread = recv(fd, &ack, sizeof(ack), MSG_DONTWAIT | MSG_PEEK); |
| 57 | if (bytesread == -1) { |
| 58 | // Read failed (error), or nothing to read (good). |
| 59 | return (errno == EAGAIN) ? 0 : -errno; |
| 60 | } else if (bytesread == (ssize_t) sizeof(ack) && ack.h.nlmsg_type == NLMSG_ERROR) { |
| 61 | // We got an error. Consume it. |
| 62 | recv(fd, &ack, sizeof(ack), 0); |
| 63 | return ack.err.error; |
| 64 | } else { |
| 65 | // The kernel replied with something. Leave it to the caller. |
| 66 | return 0; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | } // namespace |
| 71 | |
| 72 | bool SockDiag::open() { |
| 73 | if (hasSocks()) { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | mSock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_INET_DIAG); |
| 78 | mWriteSock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_INET_DIAG); |
| 79 | if (!hasSocks()) { |
| 80 | closeSocks(); |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | sockaddr_nl nl = { .nl_family = AF_NETLINK }; |
| 85 | if ((connect(mSock, reinterpret_cast<sockaddr *>(&nl), sizeof(nl)) == -1) || |
| 86 | (connect(mWriteSock, reinterpret_cast<sockaddr *>(&nl), sizeof(nl)) == -1)) { |
| 87 | closeSocks(); |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame] | 94 | int SockDiag::sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states, |
| 95 | iovec *iov, int iovcnt) { |
| 96 | struct { |
| 97 | nlmsghdr nlh; |
| 98 | inet_diag_req_v2 req; |
| 99 | } __attribute__((__packed__)) request = { |
| 100 | .nlh = { |
| 101 | .nlmsg_type = SOCK_DIAG_BY_FAMILY, |
| 102 | .nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP, |
| 103 | }, |
| 104 | .req = { |
| 105 | .sdiag_family = family, |
| 106 | .sdiag_protocol = proto, |
| 107 | .idiag_states = states, |
| 108 | }, |
| 109 | }; |
| 110 | |
| 111 | size_t len = 0; |
| 112 | iov[0].iov_base = &request; |
| 113 | iov[0].iov_len = sizeof(request); |
| 114 | for (int i = 0; i < iovcnt; i++) { |
| 115 | len += iov[i].iov_len; |
| 116 | } |
| 117 | request.nlh.nlmsg_len = len; |
| 118 | |
| 119 | if (writev(mSock, iov, iovcnt) != (ssize_t) len) { |
| 120 | return -errno; |
| 121 | } |
| 122 | |
| 123 | return checkError(mSock); |
| 124 | } |
| 125 | |
| 126 | int SockDiag::sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states) { |
| 127 | iovec iov[] = { |
| 128 | { nullptr, 0 }, |
| 129 | }; |
| 130 | return sendDumpRequest(proto, family, states, iov, ARRAY_SIZE(iov)); |
| 131 | } |
| 132 | |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 133 | int SockDiag::sendDumpRequest(uint8_t proto, uint8_t family, const char *addrstr) { |
| 134 | addrinfo hints = { .ai_flags = AI_NUMERICHOST }; |
| 135 | addrinfo *res; |
| 136 | in6_addr mapped = { .s6_addr32 = { 0, 0, htonl(0xffff), 0 } }; |
| 137 | int ret; |
| 138 | |
| 139 | // TODO: refactor the netlink parsing code out of system/core, bring it into netd, and stop |
| 140 | // doing string conversions when they're not necessary. |
| 141 | if ((ret = getaddrinfo(addrstr, nullptr, &hints, &res)) != 0) { |
| 142 | return -EINVAL; |
| 143 | } |
| 144 | |
| 145 | // So we don't have to call freeaddrinfo on every failure path. |
| 146 | ScopedAddrinfo resP(res); |
| 147 | |
| 148 | void *addr; |
| 149 | uint8_t addrlen; |
| 150 | if (res->ai_family == AF_INET && family == AF_INET) { |
| 151 | in_addr& ina = reinterpret_cast<sockaddr_in*>(res->ai_addr)->sin_addr; |
| 152 | addr = &ina; |
| 153 | addrlen = sizeof(ina); |
| 154 | } else if (res->ai_family == AF_INET && family == AF_INET6) { |
| 155 | in_addr& ina = reinterpret_cast<sockaddr_in*>(res->ai_addr)->sin_addr; |
| 156 | mapped.s6_addr32[3] = ina.s_addr; |
| 157 | addr = &mapped; |
| 158 | addrlen = sizeof(mapped); |
| 159 | } else if (res->ai_family == AF_INET6 && family == AF_INET6) { |
| 160 | in6_addr& in6a = reinterpret_cast<sockaddr_in6*>(res->ai_addr)->sin6_addr; |
| 161 | addr = &in6a; |
| 162 | addrlen = sizeof(in6a); |
| 163 | } else { |
| 164 | return -EAFNOSUPPORT; |
| 165 | } |
| 166 | |
| 167 | uint8_t prefixlen = addrlen * 8; |
| 168 | uint8_t yesjump = sizeof(inet_diag_bc_op) + sizeof(inet_diag_hostcond) + addrlen; |
| 169 | uint8_t nojump = yesjump + 4; |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 170 | |
| 171 | struct { |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 172 | nlattr nla; |
| 173 | inet_diag_bc_op op; |
| 174 | inet_diag_hostcond cond; |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame] | 175 | } __attribute__((__packed__)) attrs = { |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 176 | .nla = { |
| 177 | .nla_type = INET_DIAG_REQ_BYTECODE, |
| 178 | }, |
| 179 | .op = { |
| 180 | INET_DIAG_BC_S_COND, |
| 181 | yesjump, |
| 182 | nojump, |
| 183 | }, |
| 184 | .cond = { |
| 185 | family, |
| 186 | prefixlen, |
| 187 | -1, |
| 188 | {} |
| 189 | }, |
| 190 | }; |
| 191 | |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame] | 192 | attrs.nla.nla_len = sizeof(attrs) + addrlen; |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 193 | |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame] | 194 | iovec iov[] = { |
| 195 | { nullptr, 0 }, |
| 196 | { &attrs, sizeof(attrs) }, |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 197 | { addr, addrlen }, |
| 198 | }; |
| 199 | |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame] | 200 | uint32_t states = ~(1 << TCP_TIME_WAIT); |
| 201 | return sendDumpRequest(proto, family, states, iov, ARRAY_SIZE(iov)); |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 202 | } |
| 203 | |
Chih-Hung Hsieh | b22e4d2 | 2016-07-28 14:13:11 -0700 | [diff] [blame] | 204 | int SockDiag::readDiagMsg(uint8_t proto, const SockDiag::DumpCallback& callback) { |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 205 | char buf[kBufferSize]; |
| 206 | |
| 207 | ssize_t bytesread; |
| 208 | do { |
| 209 | bytesread = read(mSock, buf, sizeof(buf)); |
| 210 | |
| 211 | if (bytesread < 0) { |
| 212 | return -errno; |
| 213 | } |
| 214 | |
| 215 | uint32_t len = bytesread; |
| 216 | for (nlmsghdr *nlh = reinterpret_cast<nlmsghdr *>(buf); |
| 217 | NLMSG_OK(nlh, len); |
| 218 | nlh = NLMSG_NEXT(nlh, len)) { |
| 219 | switch (nlh->nlmsg_type) { |
| 220 | case NLMSG_DONE: |
| 221 | callback(proto, NULL); |
| 222 | return 0; |
| 223 | case NLMSG_ERROR: { |
| 224 | nlmsgerr *err = reinterpret_cast<nlmsgerr *>(NLMSG_DATA(nlh)); |
| 225 | return err->error; |
| 226 | } |
| 227 | default: |
| 228 | inet_diag_msg *msg = reinterpret_cast<inet_diag_msg *>(NLMSG_DATA(nlh)); |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 229 | if (callback(proto, msg)) { |
| 230 | sockDestroy(proto, msg); |
| 231 | } |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | } while (bytesread > 0); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | int SockDiag::sockDestroy(uint8_t proto, const inet_diag_msg *msg) { |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 240 | if (msg == nullptr) { |
| 241 | return 0; |
| 242 | } |
| 243 | |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 244 | DestroyRequest request = { |
| 245 | .nlh = { |
| 246 | .nlmsg_type = SOCK_DESTROY, |
| 247 | .nlmsg_flags = NLM_F_REQUEST, |
| 248 | }, |
| 249 | .req = { |
| 250 | .sdiag_family = msg->idiag_family, |
| 251 | .sdiag_protocol = proto, |
| 252 | .idiag_states = (uint32_t) (1 << msg->idiag_state), |
| 253 | .id = msg->id, |
| 254 | }, |
| 255 | }; |
| 256 | request.nlh.nlmsg_len = sizeof(request); |
| 257 | |
| 258 | if (write(mWriteSock, &request, sizeof(request)) < (ssize_t) sizeof(request)) { |
| 259 | return -errno; |
| 260 | } |
| 261 | |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 262 | int ret = checkError(mWriteSock); |
| 263 | if (!ret) mSocketsDestroyed++; |
| 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | int SockDiag::destroySockets(uint8_t proto, int family, const char *addrstr) { |
| 268 | if (!hasSocks()) { |
| 269 | return -EBADFD; |
| 270 | } |
| 271 | |
| 272 | if (int ret = sendDumpRequest(proto, family, addrstr)) { |
| 273 | return ret; |
| 274 | } |
| 275 | |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 276 | auto destroyAll = [] (uint8_t, const inet_diag_msg*) { return true; }; |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 277 | |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 278 | return readDiagMsg(proto, destroyAll); |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | int SockDiag::destroySockets(const char *addrstr) { |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame] | 282 | Stopwatch s; |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 283 | mSocketsDestroyed = 0; |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame] | 284 | |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 285 | if (!strchr(addrstr, ':')) { |
| 286 | if (int ret = destroySockets(IPPROTO_TCP, AF_INET, addrstr)) { |
| 287 | ALOGE("Failed to destroy IPv4 sockets on %s: %s", addrstr, strerror(-ret)); |
| 288 | return ret; |
| 289 | } |
| 290 | } |
| 291 | if (int ret = destroySockets(IPPROTO_TCP, AF_INET6, addrstr)) { |
| 292 | ALOGE("Failed to destroy IPv6 sockets on %s: %s", addrstr, strerror(-ret)); |
| 293 | return ret; |
| 294 | } |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 295 | |
| 296 | if (mSocketsDestroyed > 0) { |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame] | 297 | ALOGI("Destroyed %d sockets on %s in %.1f ms", mSocketsDestroyed, addrstr, s.timeTaken()); |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | return mSocketsDestroyed; |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 301 | } |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame] | 302 | |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 303 | int SockDiag::destroyLiveSockets(DumpCallback destroyFilter) { |
| 304 | int proto = IPPROTO_TCP; |
| 305 | |
| 306 | for (const int family : {AF_INET, AF_INET6}) { |
| 307 | const char *familyName = (family == AF_INET) ? "IPv4" : "IPv6"; |
| 308 | uint32_t states = (1 << TCP_ESTABLISHED) | (1 << TCP_SYN_SENT) | (1 << TCP_SYN_RECV); |
| 309 | if (int ret = sendDumpRequest(proto, family, states)) { |
| 310 | ALOGE("Failed to dump %s sockets for UID: %s", familyName, strerror(-ret)); |
| 311 | return ret; |
| 312 | } |
| 313 | if (int ret = readDiagMsg(proto, destroyFilter)) { |
| 314 | ALOGE("Failed to destroy %s sockets for UID: %s", familyName, strerror(-ret)); |
| 315 | return ret; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame] | 322 | int SockDiag::destroySockets(uint8_t proto, const uid_t uid) { |
| 323 | mSocketsDestroyed = 0; |
| 324 | Stopwatch s; |
| 325 | |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 326 | auto shouldDestroy = [uid] (uint8_t, const inet_diag_msg *msg) { |
| 327 | return (msg != nullptr && msg->idiag_uid == uid); |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame] | 328 | }; |
| 329 | |
| 330 | for (const int family : {AF_INET, AF_INET6}) { |
| 331 | const char *familyName = family == AF_INET ? "IPv4" : "IPv6"; |
| 332 | uint32_t states = (1 << TCP_ESTABLISHED) | (1 << TCP_SYN_SENT) | (1 << TCP_SYN_RECV); |
| 333 | if (int ret = sendDumpRequest(proto, family, states)) { |
| 334 | ALOGE("Failed to dump %s sockets for UID: %s", familyName, strerror(-ret)); |
| 335 | return ret; |
| 336 | } |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 337 | if (int ret = readDiagMsg(proto, shouldDestroy)) { |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame] | 338 | ALOGE("Failed to destroy %s sockets for UID: %s", familyName, strerror(-ret)); |
| 339 | return ret; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | if (mSocketsDestroyed > 0) { |
| 344 | ALOGI("Destroyed %d sockets for UID in %.1f ms", mSocketsDestroyed, s.timeTaken()); |
| 345 | } |
| 346 | |
| 347 | return 0; |
| 348 | } |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 349 | |
| 350 | int SockDiag::destroySockets(const UidRanges& uidRanges, const std::set<uid_t>& skipUids) { |
| 351 | mSocketsDestroyed = 0; |
| 352 | Stopwatch s; |
| 353 | |
| 354 | auto shouldDestroy = [&] (uint8_t, const inet_diag_msg *msg) { |
| 355 | return msg != nullptr && |
| 356 | uidRanges.hasUid(msg->idiag_uid) && |
| 357 | skipUids.find(msg->idiag_uid) == skipUids.end(); |
| 358 | }; |
| 359 | |
| 360 | if (int ret = destroyLiveSockets(shouldDestroy)) { |
| 361 | return ret; |
| 362 | } |
| 363 | |
| 364 | std::vector<uid_t> skipUidStrings; |
| 365 | for (uid_t uid : skipUids) { |
| 366 | skipUidStrings.push_back(uid); |
| 367 | } |
| 368 | std::sort(skipUidStrings.begin(), skipUidStrings.end()); |
| 369 | |
| 370 | if (mSocketsDestroyed > 0) { |
| 371 | ALOGI("Destroyed %d sockets for %s skip={%s} in %.1f ms", |
| 372 | mSocketsDestroyed, uidRanges.toString().c_str(), |
| 373 | android::base::Join(skipUidStrings, " ").c_str(), s.timeTaken()); |
| 374 | } |
| 375 | |
| 376 | return 0; |
| 377 | } |