Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame] | 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 | |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 17 | #ifndef _SOCK_DIAG_H |
| 18 | #define _SOCK_DIAG_H |
| 19 | |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 20 | #include <unistd.h> |
| 21 | #include <sys/socket.h> |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 22 | |
| 23 | #include <linux/netlink.h> |
| 24 | #include <linux/sock_diag.h> |
| 25 | #include <linux/inet_diag.h> |
| 26 | |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 27 | #include <functional> |
| 28 | #include <set> |
| 29 | |
Hugo Benichi | cbaa36b | 2018-01-17 12:11:43 +0900 | [diff] [blame^] | 30 | #include "Fwmark.h" |
Lorenzo Colitti | 0b733e4 | 2017-02-13 16:29:00 +0900 | [diff] [blame] | 31 | #include "NetlinkCommands.h" |
Lorenzo Colitti | fbe76b9 | 2016-09-14 02:25:05 +0900 | [diff] [blame] | 32 | #include "Permission.h" |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 33 | #include "UidRanges.h" |
| 34 | |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 35 | struct inet_diag_msg; |
Hugo Benichi | dee9481 | 2018-01-12 14:47:00 +0900 | [diff] [blame] | 36 | struct tcp_info; |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 37 | class SockDiagTest; |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 38 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 39 | namespace android { |
| 40 | namespace net { |
| 41 | |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 42 | class SockDiag { |
| 43 | |
| 44 | public: |
| 45 | static const int kBufferSize = 4096; |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 46 | |
Hugo Benichi | dee9481 | 2018-01-12 14:47:00 +0900 | [diff] [blame] | 47 | // Callback function that is called once for every socket in the sockDestroy dump. |
| 48 | // A return value of true means destroy the socket. |
Lorenzo Colitti | 0b733e4 | 2017-02-13 16:29:00 +0900 | [diff] [blame] | 49 | typedef std::function<bool(uint8_t proto, const inet_diag_msg *)> DestroyFilter; |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 50 | |
Hugo Benichi | dee9481 | 2018-01-12 14:47:00 +0900 | [diff] [blame] | 51 | // Callback function that is called once for every socket in the sockInfo dump. |
Hugo Benichi | cbaa36b | 2018-01-17 12:11:43 +0900 | [diff] [blame^] | 52 | typedef std::function<void(Fwmark mark, const struct inet_diag_msg *, const struct tcp_info *)> |
Hugo Benichi | dee9481 | 2018-01-12 14:47:00 +0900 | [diff] [blame] | 53 | TcpInfoReader; |
| 54 | |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 55 | struct DestroyRequest { |
| 56 | nlmsghdr nlh; |
| 57 | inet_diag_req_v2 req; |
| 58 | } __attribute__((__packed__)); |
| 59 | |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 60 | SockDiag() : mSock(-1), mWriteSock(-1), mSocketsDestroyed(0) {} |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 61 | bool open(); |
| 62 | virtual ~SockDiag() { closeSocks(); } |
| 63 | |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame] | 64 | int sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states); |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 65 | int sendDumpRequest(uint8_t proto, uint8_t family, const char *addrstr); |
Lorenzo Colitti | 0b733e4 | 2017-02-13 16:29:00 +0900 | [diff] [blame] | 66 | int readDiagMsg(uint8_t proto, const DestroyFilter& callback); |
Hugo Benichi | dee9481 | 2018-01-12 14:47:00 +0900 | [diff] [blame] | 67 | int readDiagMsgWithTcpInfo(const TcpInfoReader& callback); |
Lorenzo Colitti | 0b733e4 | 2017-02-13 16:29:00 +0900 | [diff] [blame] | 68 | |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 69 | int sockDestroy(uint8_t proto, const inet_diag_msg *); |
Lorenzo Colitti | e5c3c99 | 2016-07-26 17:53:50 +0900 | [diff] [blame] | 70 | // Destroys all sockets on the given IPv4 or IPv6 address. |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 71 | int destroySockets(const char *addrstr); |
Lorenzo Colitti | e5c3c99 | 2016-07-26 17:53:50 +0900 | [diff] [blame] | 72 | // Destroys all sockets for the given protocol and UID. |
| 73 | int destroySockets(uint8_t proto, uid_t uid, bool excludeLoopback); |
| 74 | // Destroys all "live" (CONNECTED, SYN_SENT, SYN_RECV) TCP sockets for the given UID ranges. |
| 75 | int destroySockets(const UidRanges& uidRanges, const std::set<uid_t>& skipUids, |
| 76 | bool excludeLoopback); |
Lorenzo Colitti | fbe76b9 | 2016-09-14 02:25:05 +0900 | [diff] [blame] | 77 | // Destroys all "live" (CONNECTED, SYN_SENT, SYN_RECV) TCP sockets that no longer have |
| 78 | // the permissions required by the specified network. |
| 79 | int destroySocketsLackingPermission(unsigned netId, Permission permission, |
| 80 | bool excludeLoopback); |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 81 | |
Hugo Benichi | dee9481 | 2018-01-12 14:47:00 +0900 | [diff] [blame] | 82 | // Dump struct tcp_info for all "live" (CONNECTED, SYN_SENT, SYN_RECV) TCP sockets. |
| 83 | int getLiveTcpInfos(const TcpInfoReader& sockInfoReader); |
| 84 | |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 85 | private: |
Lorenzo Colitti | e5c3c99 | 2016-07-26 17:53:50 +0900 | [diff] [blame] | 86 | friend class SockDiagTest; |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 87 | int mSock; |
| 88 | int mWriteSock; |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 89 | int mSocketsDestroyed; |
Hugo Benichi | dee9481 | 2018-01-12 14:47:00 +0900 | [diff] [blame] | 90 | int sendDumpRequest(uint8_t proto, uint8_t family, uint8_t extensions, uint32_t states, |
| 91 | iovec *iov, int iovcnt); |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 92 | int destroySockets(uint8_t proto, int family, const char *addrstr); |
Lorenzo Colitti | 0b733e4 | 2017-02-13 16:29:00 +0900 | [diff] [blame] | 93 | int destroyLiveSockets(DestroyFilter destroy, const char *what, iovec *iov, int iovcnt); |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 94 | bool hasSocks() { return mSock != -1 && mWriteSock != -1; } |
| 95 | void closeSocks() { close(mSock); close(mWriteSock); mSock = mWriteSock = -1; } |
Lorenzo Colitti | e5c3c99 | 2016-07-26 17:53:50 +0900 | [diff] [blame] | 96 | static bool isLoopbackSocket(const inet_diag_msg *msg); |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 97 | }; |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 98 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 99 | } // namespace net |
| 100 | } // namespace android |
| 101 | |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 102 | #endif // _SOCK_DIAG_H |