Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 NETD_INCLUDE_FWMARK_COMMAND_H |
| 18 | #define NETD_INCLUDE_FWMARK_COMMAND_H |
| 19 | |
Michal Karpinski | 4b9b78a | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 20 | #include <arpa/inet.h> |
| 21 | #include <sys/socket.h> |
Sreeram Ramachandran | a69d947 | 2014-07-11 16:27:02 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | |
Michal Karpinski | 4b9b78a | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 24 | // Additional information sent with ON_CONNECT_COMPLETE command |
| 25 | struct FwmarkConnectInfo { |
Hugo Benichi | 794c5c7 | 2016-10-31 15:07:23 +0900 | [diff] [blame] | 26 | int error; |
Michal Karpinski | 4b9b78a | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 27 | unsigned latencyMs; |
| 28 | union { |
| 29 | sockaddr s; |
| 30 | sockaddr_in sin; |
| 31 | sockaddr_in6 sin6; |
| 32 | } addr; |
| 33 | |
Maciej Żenczykowski | 74b20e7 | 2020-04-30 16:36:21 -0700 | [diff] [blame] | 34 | FwmarkConnectInfo() : error(0), latencyMs(0) {} |
Michal Karpinski | 4b9b78a | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 35 | |
Hugo Benichi | 794c5c7 | 2016-10-31 15:07:23 +0900 | [diff] [blame] | 36 | FwmarkConnectInfo(const int connectErrno, const unsigned latency, const sockaddr* saddr) { |
| 37 | error = connectErrno; |
Michal Karpinski | 4b9b78a | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 38 | latencyMs = latency; |
| 39 | if (saddr->sa_family == AF_INET) { |
| 40 | addr.sin = *((struct sockaddr_in*) saddr); |
| 41 | } else if (saddr->sa_family == AF_INET6) { |
| 42 | addr.sin6 = *((struct sockaddr_in6*) saddr); |
| 43 | } else { |
| 44 | // Cannot happen because we only call this if shouldSetFwmark returns true, and thus |
| 45 | // the address family is one we understand. |
| 46 | addr.s.sa_family = AF_UNSPEC; |
| 47 | } |
| 48 | } |
| 49 | }; |
| 50 | |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 51 | // Commands sent from clients to the fwmark server to mark sockets (i.e., set their SO_MARK). |
Michal Karpinski | 4b9b78a | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 52 | // ON_CONNECT_COMPLETE command should be accompanied by FwmarkConnectInfo which should contain |
| 53 | // info about that connect attempt |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 54 | // TODO: rework this struct into a more flexible data structure such as union or |
| 55 | // a hierarchy class. |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 56 | struct FwmarkCommand { |
Ken Chen | 22e5fb8 | 2020-02-25 18:05:52 +0000 | [diff] [blame] | 57 | enum CmdId { |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 58 | ON_ACCEPT, |
| 59 | ON_CONNECT, |
| 60 | SELECT_NETWORK, |
| 61 | PROTECT_FROM_VPN, |
Sreeram Ramachandran | a69d947 | 2014-07-11 16:27:02 -0700 | [diff] [blame] | 62 | SELECT_FOR_USER, |
Paul Jensen | d1df597 | 2015-05-06 07:29:56 -0400 | [diff] [blame] | 63 | QUERY_USER_ACCESS, |
Michal Karpinski | 4b9b78a | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 64 | ON_CONNECT_COMPLETE, |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 65 | TAG_SOCKET, |
| 66 | UNTAG_SOCKET, |
Patrick Rohr | b371bc3 | 2022-02-01 22:43:23 +0100 | [diff] [blame] | 67 | _UNUSED_SET_COUNTERSET, // unsupported, do not use this command |
| 68 | _UNUSED_DELETE_TAGDATA, // unsupported, do not use this command |
Ken Chen | 22e5fb8 | 2020-02-25 18:05:52 +0000 | [diff] [blame] | 69 | ON_SENDMMSG, |
| 70 | ON_SENDMSG, |
| 71 | ON_SENDTO, |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 72 | } cmdId; |
| 73 | unsigned netId; // used only in the SELECT_NETWORK command; ignored otherwise. |
Chenbo Feng | 9944ba8 | 2017-10-10 17:33:20 -0700 | [diff] [blame] | 74 | uid_t uid; // used in the SELECT_FOR_USER, QUERY_USER_ACCESS, TAG_SOCKET, |
| 75 | // SET_COUNTERSET, and DELETE_TAGDATA command; ignored otherwise. |
| 76 | uint32_t trafficCtrlInfo; // used in TAG_SOCKET, SET_COUNTERSET and SET_PACIFIER command; |
| 77 | // ignored otherwise. Depend on the case, it can be a tag, a |
| 78 | // counterSet or a pacifier signal. |
Remi NGUYEN VAN | eabc5da | 2018-04-24 18:54:58 +0900 | [diff] [blame] | 79 | |
| 80 | static bool isSupportedFamily(int socketFamily) { |
| 81 | return socketFamily == AF_INET || socketFamily == AF_INET6; |
| 82 | } |
Sreeram Ramachandran | efbe05d | 2014-05-21 11:41:39 -0700 | [diff] [blame] | 83 | }; |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 84 | |
| 85 | #endif // NETD_INCLUDE_FWMARK_COMMAND_H |