blob: 396b915487aa62429f68b34413d760cfe8834c2e [file] [log] [blame]
Lorenzo Colitti94a7b432016-03-24 16:47:12 +09001/*
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
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090017#include <unistd.h>
18#include <sys/socket.h>
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090019
20#include <linux/netlink.h>
21#include <linux/sock_diag.h>
22#include <linux/inet_diag.h>
23
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090024#include <functional>
25#include <set>
26
Lorenzo Colittifbe76b92016-09-14 02:25:05 +090027#include "Permission.h"
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090028#include "UidRanges.h"
29
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090030struct inet_diag_msg;
Lorenzo Colittif32fc592016-02-15 01:09:14 +090031class SockDiagTest;
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090032
33class SockDiag {
34
35 public:
36 static const int kBufferSize = 4096;
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090037
38 // Callback function that is called once for every socket in the dump. A return value of true
39 // means destroy the socket.
40 typedef std::function<bool(uint8_t proto, const inet_diag_msg *)> DumpCallback;
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090041
42 struct DestroyRequest {
43 nlmsghdr nlh;
44 inet_diag_req_v2 req;
45 } __attribute__((__packed__));
46
Lorenzo Colittif32fc592016-02-15 01:09:14 +090047 SockDiag() : mSock(-1), mWriteSock(-1), mSocketsDestroyed(0) {}
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090048 bool open();
49 virtual ~SockDiag() { closeSocks(); }
50
Lorenzo Colitti94a7b432016-03-24 16:47:12 +090051 int sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090052 int sendDumpRequest(uint8_t proto, uint8_t family, const char *addrstr);
Chih-Hung Hsiehb22e4d22016-07-28 14:13:11 -070053 int readDiagMsg(uint8_t proto, const DumpCallback& callback);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090054 int sockDestroy(uint8_t proto, const inet_diag_msg *);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +090055 // Destroys all sockets on the given IPv4 or IPv6 address.
Lorenzo Colittif32fc592016-02-15 01:09:14 +090056 int destroySockets(const char *addrstr);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +090057 // Destroys all sockets for the given protocol and UID.
58 int destroySockets(uint8_t proto, uid_t uid, bool excludeLoopback);
59 // Destroys all "live" (CONNECTED, SYN_SENT, SYN_RECV) TCP sockets for the given UID ranges.
60 int destroySockets(const UidRanges& uidRanges, const std::set<uid_t>& skipUids,
61 bool excludeLoopback);
Lorenzo Colittifbe76b92016-09-14 02:25:05 +090062 // Destroys all "live" (CONNECTED, SYN_SENT, SYN_RECV) TCP sockets that no longer have
63 // the permissions required by the specified network.
64 int destroySocketsLackingPermission(unsigned netId, Permission permission,
65 bool excludeLoopback);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090066
67 private:
Lorenzo Colittie5c3c992016-07-26 17:53:50 +090068 friend class SockDiagTest;
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090069 int mSock;
70 int mWriteSock;
Lorenzo Colittif32fc592016-02-15 01:09:14 +090071 int mSocketsDestroyed;
Lorenzo Colitti94a7b432016-03-24 16:47:12 +090072 int sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states, iovec *iov, int iovcnt);
Lorenzo Colittif32fc592016-02-15 01:09:14 +090073 int destroySockets(uint8_t proto, int family, const char *addrstr);
Lorenzo Colittifbe76b92016-09-14 02:25:05 +090074 int destroyLiveSockets(DumpCallback destroy, const char *what, iovec *iov, int iovcnt);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090075 bool hasSocks() { return mSock != -1 && mWriteSock != -1; }
76 void closeSocks() { close(mSock); close(mWriteSock); mSock = mWriteSock = -1; }
Lorenzo Colittie5c3c992016-07-26 17:53:50 +090077 static bool isLoopbackSocket(const inet_diag_msg *msg);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090078};