blob: d53a77460551c40fbf78c37354f3a79a1964e3a8 [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
Robin Lee2cf56172016-09-13 18:55:42 +090017#ifndef _SOCK_DIAG_H
18#define _SOCK_DIAG_H
19
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090020#include <unistd.h>
21#include <sys/socket.h>
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090022
23#include <linux/netlink.h>
24#include <linux/sock_diag.h>
25#include <linux/inet_diag.h>
26
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090027#include <functional>
28#include <set>
29
Lorenzo Colittifbe76b92016-09-14 02:25:05 +090030#include "Permission.h"
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090031#include "UidRanges.h"
32
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090033struct inet_diag_msg;
Lorenzo Colittif32fc592016-02-15 01:09:14 +090034class SockDiagTest;
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090035
36class SockDiag {
37
38 public:
39 static const int kBufferSize = 4096;
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090040
41 // Callback function that is called once for every socket in the dump. A return value of true
42 // means destroy the socket.
43 typedef std::function<bool(uint8_t proto, const inet_diag_msg *)> DumpCallback;
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090044
45 struct DestroyRequest {
46 nlmsghdr nlh;
47 inet_diag_req_v2 req;
48 } __attribute__((__packed__));
49
Lorenzo Colittif32fc592016-02-15 01:09:14 +090050 SockDiag() : mSock(-1), mWriteSock(-1), mSocketsDestroyed(0) {}
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090051 bool open();
52 virtual ~SockDiag() { closeSocks(); }
53
Lorenzo Colitti94a7b432016-03-24 16:47:12 +090054 int sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090055 int sendDumpRequest(uint8_t proto, uint8_t family, const char *addrstr);
Chih-Hung Hsiehb22e4d22016-07-28 14:13:11 -070056 int readDiagMsg(uint8_t proto, const DumpCallback& callback);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090057 int sockDestroy(uint8_t proto, const inet_diag_msg *);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +090058 // Destroys all sockets on the given IPv4 or IPv6 address.
Lorenzo Colittif32fc592016-02-15 01:09:14 +090059 int destroySockets(const char *addrstr);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +090060 // Destroys all sockets for the given protocol and UID.
61 int destroySockets(uint8_t proto, uid_t uid, bool excludeLoopback);
62 // Destroys all "live" (CONNECTED, SYN_SENT, SYN_RECV) TCP sockets for the given UID ranges.
63 int destroySockets(const UidRanges& uidRanges, const std::set<uid_t>& skipUids,
64 bool excludeLoopback);
Lorenzo Colittifbe76b92016-09-14 02:25:05 +090065 // Destroys all "live" (CONNECTED, SYN_SENT, SYN_RECV) TCP sockets that no longer have
66 // the permissions required by the specified network.
67 int destroySocketsLackingPermission(unsigned netId, Permission permission,
68 bool excludeLoopback);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090069
70 private:
Lorenzo Colittie5c3c992016-07-26 17:53:50 +090071 friend class SockDiagTest;
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090072 int mSock;
73 int mWriteSock;
Lorenzo Colittif32fc592016-02-15 01:09:14 +090074 int mSocketsDestroyed;
Lorenzo Colitti94a7b432016-03-24 16:47:12 +090075 int sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states, iovec *iov, int iovcnt);
Lorenzo Colittif32fc592016-02-15 01:09:14 +090076 int destroySockets(uint8_t proto, int family, const char *addrstr);
Lorenzo Colittifbe76b92016-09-14 02:25:05 +090077 int destroyLiveSockets(DumpCallback destroy, const char *what, iovec *iov, int iovcnt);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090078 bool hasSocks() { return mSock != -1 && mWriteSock != -1; }
79 void closeSocks() { close(mSock); close(mWriteSock); mSock = mWriteSock = -1; }
Lorenzo Colittie5c3c992016-07-26 17:53:50 +090080 static bool isLoopbackSocket(const inet_diag_msg *msg);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090081};
Robin Lee2cf56172016-09-13 18:55:42 +090082
83#endif // _SOCK_DIAG_H