blob: 745c09e3d36474da58ae6f995a493626cbc118a0 [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
Hugo Benichicbaa36b2018-01-17 12:11:43 +090030#include "Fwmark.h"
Lorenzo Colitti0b733e42017-02-13 16:29:00 +090031#include "NetlinkCommands.h"
Lorenzo Colittifbe76b92016-09-14 02:25:05 +090032#include "Permission.h"
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090033#include "UidRanges.h"
34
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090035struct inet_diag_msg;
Hugo Benichidee94812018-01-12 14:47:00 +090036struct tcp_info;
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090037
Lorenzo Colitti7035f222017-02-13 18:29:00 +090038namespace android {
39namespace net {
40
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090041class SockDiag {
42
43 public:
44 static const int kBufferSize = 4096;
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090045
Hugo Benichidee94812018-01-12 14:47:00 +090046 // Callback function that is called once for every socket in the sockDestroy dump.
47 // A return value of true means destroy the socket.
Lorenzo Colitti0b733e42017-02-13 16:29:00 +090048 typedef std::function<bool(uint8_t proto, const inet_diag_msg *)> DestroyFilter;
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090049
Hugo Benichidee94812018-01-12 14:47:00 +090050 // Callback function that is called once for every socket in the sockInfo dump.
Hugo Benichi54bfc7c2018-01-23 14:16:52 +090051 // 'tcp_info_length' is the length in bytes of the INET_DIAG_INFO attribute read from Netlink.
52 // Knowing this length is necessary for handling struct tcp_info serialized from different
53 // kernel versions.
54 typedef std::function<void(Fwmark mark, const struct inet_diag_msg *, const struct tcp_info *,
55 uint32_t tcp_info_length)> TcpInfoReader;
Hugo Benichidee94812018-01-12 14:47:00 +090056
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090057 struct DestroyRequest {
58 nlmsghdr nlh;
59 inet_diag_req_v2 req;
60 } __attribute__((__packed__));
61
Lorenzo Colittif32fc592016-02-15 01:09:14 +090062 SockDiag() : mSock(-1), mWriteSock(-1), mSocketsDestroyed(0) {}
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090063 bool open();
64 virtual ~SockDiag() { closeSocks(); }
65
Lorenzo Colitti94a7b432016-03-24 16:47:12 +090066 int sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090067 int sendDumpRequest(uint8_t proto, uint8_t family, const char *addrstr);
Lorenzo Colitti0b733e42017-02-13 16:29:00 +090068 int readDiagMsg(uint8_t proto, const DestroyFilter& callback);
Hugo Benichidee94812018-01-12 14:47:00 +090069 int readDiagMsgWithTcpInfo(const TcpInfoReader& callback);
Lorenzo Colitti0b733e42017-02-13 16:29:00 +090070
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090071 int sockDestroy(uint8_t proto, const inet_diag_msg *);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +090072 // Destroys all sockets on the given IPv4 or IPv6 address.
Lorenzo Colittif32fc592016-02-15 01:09:14 +090073 int destroySockets(const char *addrstr);
Lorenzo Colittie5c3c992016-07-26 17:53:50 +090074 // Destroys all sockets for the given protocol and UID.
75 int destroySockets(uint8_t proto, uid_t uid, bool excludeLoopback);
76 // Destroys all "live" (CONNECTED, SYN_SENT, SYN_RECV) TCP sockets for the given UID ranges.
77 int destroySockets(const UidRanges& uidRanges, const std::set<uid_t>& skipUids,
78 bool excludeLoopback);
Lorenzo Colittifbe76b92016-09-14 02:25:05 +090079 // Destroys all "live" (CONNECTED, SYN_SENT, SYN_RECV) TCP sockets that no longer have
80 // the permissions required by the specified network.
81 int destroySocketsLackingPermission(unsigned netId, Permission permission,
82 bool excludeLoopback);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090083
Hugo Benichidee94812018-01-12 14:47:00 +090084 // Dump struct tcp_info for all "live" (CONNECTED, SYN_SENT, SYN_RECV) TCP sockets.
85 int getLiveTcpInfos(const TcpInfoReader& sockInfoReader);
86
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090087 private:
Lorenzo Colittie5c3c992016-07-26 17:53:50 +090088 friend class SockDiagTest;
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090089 int mSock;
90 int mWriteSock;
Lorenzo Colittif32fc592016-02-15 01:09:14 +090091 int mSocketsDestroyed;
Hugo Benichidee94812018-01-12 14:47:00 +090092 int sendDumpRequest(uint8_t proto, uint8_t family, uint8_t extensions, uint32_t states,
93 iovec *iov, int iovcnt);
Lorenzo Colittif32fc592016-02-15 01:09:14 +090094 int destroySockets(uint8_t proto, int family, const char *addrstr);
Bernie Innocenti15bb55c2018-06-03 16:19:51 +090095 int destroyLiveSockets(const DestroyFilter& destroy, const char *what, iovec *iov, int iovcnt);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090096 bool hasSocks() { return mSock != -1 && mWriteSock != -1; }
97 void closeSocks() { close(mSock); close(mWriteSock); mSock = mWriteSock = -1; }
Lorenzo Colittie5c3c992016-07-26 17:53:50 +090098 static bool isLoopbackSocket(const inet_diag_msg *msg);
Lorenzo Colitti8464e1e2016-02-05 00:57:26 +090099};
Robin Lee2cf56172016-09-13 18:55:42 +0900100
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900101} // namespace net
102} // namespace android
103
Robin Lee2cf56172016-09-13 18:55:42 +0900104#endif // _SOCK_DIAG_H