blob: 5e545eb7d8da0c8e7969c944a599b74a182c03fa [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
Lorenzo Colitti7035f222017-02-13 18:29:00 +090036namespace android {
37namespace net {
38
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090039class SockDiag {
40
41 public:
42 static const int kBufferSize = 4096;
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090043
44 // Callback function that is called once for every socket in the dump. A return value of true
45 // means destroy the socket.
46 typedef std::function<bool(uint8_t proto, const inet_diag_msg *)> DumpCallback;
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090047
48 struct DestroyRequest {
49 nlmsghdr nlh;
50 inet_diag_req_v2 req;
51 } __attribute__((__packed__));
52
Lorenzo Colittif32fc592016-02-15 01:09:14 +090053 SockDiag() : mSock(-1), mWriteSock(-1), mSocketsDestroyed(0) {}
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090054 bool open();
55 virtual ~SockDiag() { closeSocks(); }
56
Lorenzo Colitti94a7b432016-03-24 16:47:12 +090057 int sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090058 int sendDumpRequest(uint8_t proto, uint8_t family, const char *addrstr);
Chih-Hung Hsiehb22e4d22016-07-28 14:13:11 -070059 int readDiagMsg(uint8_t proto, const DumpCallback& callback);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090060 int sockDestroy(uint8_t proto, const inet_diag_msg *);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +090061 // Destroys all sockets on the given IPv4 or IPv6 address.
Lorenzo Colittif32fc592016-02-15 01:09:14 +090062 int destroySockets(const char *addrstr);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +090063 // Destroys all sockets for the given protocol and UID.
64 int destroySockets(uint8_t proto, uid_t uid, bool excludeLoopback);
65 // Destroys all "live" (CONNECTED, SYN_SENT, SYN_RECV) TCP sockets for the given UID ranges.
66 int destroySockets(const UidRanges& uidRanges, const std::set<uid_t>& skipUids,
67 bool excludeLoopback);
Lorenzo Colittifbe76b92016-09-14 02:25:05 +090068 // Destroys all "live" (CONNECTED, SYN_SENT, SYN_RECV) TCP sockets that no longer have
69 // the permissions required by the specified network.
70 int destroySocketsLackingPermission(unsigned netId, Permission permission,
71 bool excludeLoopback);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090072
73 private:
Lorenzo Colittie5c3c992016-07-26 17:53:50 +090074 friend class SockDiagTest;
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090075 int mSock;
76 int mWriteSock;
Lorenzo Colittif32fc592016-02-15 01:09:14 +090077 int mSocketsDestroyed;
Lorenzo Colitti94a7b432016-03-24 16:47:12 +090078 int sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states, iovec *iov, int iovcnt);
Lorenzo Colittif32fc592016-02-15 01:09:14 +090079 int destroySockets(uint8_t proto, int family, const char *addrstr);
Lorenzo Colittifbe76b92016-09-14 02:25:05 +090080 int destroyLiveSockets(DumpCallback destroy, const char *what, iovec *iov, int iovcnt);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090081 bool hasSocks() { return mSock != -1 && mWriteSock != -1; }
82 void closeSocks() { close(mSock); close(mWriteSock); mSock = mWriteSock = -1; }
Lorenzo Colittie5c3c992016-07-26 17:53:50 +090083 static bool isLoopbackSocket(const inet_diag_msg *msg);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090084};
Robin Lee2cf56172016-09-13 18:55:42 +090085
Lorenzo Colitti7035f222017-02-13 18:29:00 +090086} // namespace net
87} // namespace android
88
Robin Lee2cf56172016-09-13 18:55:42 +090089#endif // _SOCK_DIAG_H