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 #ifndef _SOCK_DIAG_H
18 #define _SOCK_DIAG_H
19 
20 #include <unistd.h>
21 #include <sys/socket.h>
22 
23 #include <linux/netlink.h>
24 #include <linux/sock_diag.h>
25 #include <linux/inet_diag.h>
26 
27 #include <functional>
28 #include <set>
29 
30 #include "Fwmark.h"
31 #include "NetlinkCommands.h"
32 #include "Permission.h"
33 #include "UidRanges.h"
34 
35 struct inet_diag_msg;
36 struct tcp_info;
37 
38 namespace android {
39 namespace net {
40 
41 class SockDiag {
42 
43   public:
44     static const int kBufferSize = 4096;
45 
46     // Callback function that is called once for every socket in the sockDestroy dump.
47     // A return value of true means destroy the socket.
48     typedef std::function<bool(uint8_t proto, const inet_diag_msg *)> DestroyFilter;
49 
50     // Callback function that is called once for every socket in the sockInfo dump.
51     // '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;
56 
57     struct DestroyRequest {
58         nlmsghdr nlh;
59         inet_diag_req_v2 req;
60     } __attribute__((__packed__));
61 
SockDiag()62     SockDiag() : mSock(-1), mWriteSock(-1), mSocketsDestroyed(0) {}
63     bool open();
~SockDiag()64     virtual ~SockDiag() { closeSocks(); }
65 
66     int sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states);
67     int sendDumpRequest(uint8_t proto, uint8_t family, const char *addrstr);
68     int readDiagMsg(uint8_t proto, const DestroyFilter& callback);
69     int readDiagMsgWithTcpInfo(const TcpInfoReader& callback);
70 
71     int sockDestroy(uint8_t proto, const inet_diag_msg *);
72     // Destroys all sockets on the given IPv4 or IPv6 address.
73     int destroySockets(const char *addrstr);
74     // 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);
79     // 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);
83 
84     // Dump struct tcp_info for all "live" (CONNECTED, SYN_SENT, SYN_RECV) TCP sockets.
85     int getLiveTcpInfos(const TcpInfoReader& sockInfoReader);
86 
87   private:
88     friend class SockDiagTest;
89     int mSock;
90     int mWriteSock;
91     int mSocketsDestroyed;
92     int sendDumpRequest(uint8_t proto, uint8_t family, uint8_t extensions, uint32_t states,
93                         iovec *iov, int iovcnt);
94     int destroySockets(uint8_t proto, int family, const char *addrstr);
95     int destroyLiveSockets(const DestroyFilter& destroy, const char *what, iovec *iov, int iovcnt);
hasSocks()96     bool hasSocks() { return mSock != -1 && mWriteSock != -1; }
closeSocks()97     void closeSocks() { close(mSock); close(mWriteSock); mSock = mWriteSock = -1; }
98     static bool isLoopbackSocket(const inet_diag_msg *msg);
99 };
100 
101 }  // namespace net
102 }  // namespace android
103 
104 #endif  // _SOCK_DIAG_H
105