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 | |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame^] | 17 | #include <unistd.h> |
| 18 | #include <sys/socket.h> |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 19 | |
| 20 | #include <linux/netlink.h> |
| 21 | #include <linux/sock_diag.h> |
| 22 | #include <linux/inet_diag.h> |
| 23 | |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame^] | 24 | #include <functional> |
| 25 | #include <set> |
| 26 | |
| 27 | #include "UidRanges.h" |
| 28 | |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 29 | struct inet_diag_msg; |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 30 | class SockDiagTest; |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 31 | |
| 32 | class SockDiag { |
| 33 | |
| 34 | public: |
| 35 | static const int kBufferSize = 4096; |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame^] | 36 | |
| 37 | // Callback function that is called once for every socket in the dump. A return value of true |
| 38 | // means destroy the socket. |
| 39 | typedef std::function<bool(uint8_t proto, const inet_diag_msg *)> DumpCallback; |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 40 | |
| 41 | struct DestroyRequest { |
| 42 | nlmsghdr nlh; |
| 43 | inet_diag_req_v2 req; |
| 44 | } __attribute__((__packed__)); |
| 45 | |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 46 | SockDiag() : mSock(-1), mWriteSock(-1), mSocketsDestroyed(0) {} |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 47 | bool open(); |
| 48 | virtual ~SockDiag() { closeSocks(); } |
| 49 | |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame] | 50 | int sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states); |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 51 | int sendDumpRequest(uint8_t proto, uint8_t family, const char *addrstr); |
| 52 | int readDiagMsg(uint8_t proto, DumpCallback callback); |
| 53 | int sockDestroy(uint8_t proto, const inet_diag_msg *); |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 54 | int destroySockets(const char *addrstr); |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame] | 55 | int destroySockets(uint8_t proto, uid_t uid); |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame^] | 56 | int destroySockets(const UidRanges& uidRanges, const std::set<uid_t>& skipUids); |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 57 | |
| 58 | private: |
| 59 | int mSock; |
| 60 | int mWriteSock; |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 61 | int mSocketsDestroyed; |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame] | 62 | int sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states, iovec *iov, int iovcnt); |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 63 | int destroySockets(uint8_t proto, int family, const char *addrstr); |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame^] | 64 | int destroyLiveSockets(DumpCallback destroy); |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 65 | bool hasSocks() { return mSock != -1 && mWriteSock != -1; } |
| 66 | void closeSocks() { close(mSock); close(mWriteSock); mSock = mWriteSock = -1; } |
| 67 | }; |