blob: 83e62d3b788e2bdfe5169658f213bca07de5e791 [file] [log] [blame]
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -07001/*
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 Karpinski4b9b78a2016-10-06 19:33:55 +010020#include <arpa/inet.h>
21#include <sys/socket.h>
Sreeram Ramachandrana69d9472014-07-11 16:27:02 -070022#include <sys/types.h>
23
Michal Karpinski4b9b78a2016-10-06 19:33:55 +010024// Additional information sent with ON_CONNECT_COMPLETE command
25struct FwmarkConnectInfo {
Hugo Benichi794c5c72016-10-31 15:07:23 +090026 int error;
Michal Karpinski4b9b78a2016-10-06 19:33:55 +010027 unsigned latencyMs;
28 union {
29 sockaddr s;
30 sockaddr_in sin;
31 sockaddr_in6 sin6;
32 } addr;
33
Maciej Żenczykowski60741972020-05-01 01:30:16 +000034 FwmarkConnectInfo() : error(0), latencyMs(0) {}
Michal Karpinski4b9b78a2016-10-06 19:33:55 +010035
Hugo Benichi794c5c72016-10-31 15:07:23 +090036 FwmarkConnectInfo(const int connectErrno, const unsigned latency, const sockaddr* saddr) {
37 error = connectErrno;
Michal Karpinski4b9b78a2016-10-06 19:33:55 +010038 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 Ramachandranf4cfad32014-05-21 08:54:07 -070051// Commands sent from clients to the fwmark server to mark sockets (i.e., set their SO_MARK).
Michal Karpinski4b9b78a2016-10-06 19:33:55 +010052// ON_CONNECT_COMPLETE command should be accompanied by FwmarkConnectInfo which should contain
53// info about that connect attempt
Chenbo Feng9944ba82017-10-10 17:33:20 -070054// TODO: rework this struct into a more flexible data structure such as union or
55// a hierarchy class.
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -070056struct FwmarkCommand {
Praveen Moongalam Thyagarajanf24eb882019-12-18 11:59:47 -080057 enum CmdId {
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -070058 ON_ACCEPT,
59 ON_CONNECT,
60 SELECT_NETWORK,
61 PROTECT_FROM_VPN,
Sreeram Ramachandrana69d9472014-07-11 16:27:02 -070062 SELECT_FOR_USER,
Paul Jensend1df5972015-05-06 07:29:56 -040063 QUERY_USER_ACCESS,
Michal Karpinski4b9b78a2016-10-06 19:33:55 +010064 ON_CONNECT_COMPLETE,
Chenbo Feng9944ba82017-10-10 17:33:20 -070065 TAG_SOCKET,
66 UNTAG_SOCKET,
67 // TODO: use binder to pass the following two request in future after we
68 // completely get rid of qtaguid module, since these are privileged
69 // command.
70 SET_COUNTERSET,
71 DELETE_TAGDATA,
Praveen Moongalam Thyagarajanf24eb882019-12-18 11:59:47 -080072 ON_SENDMMSG,
73 ON_SENDMSG,
74 ON_SENDTO,
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -070075 } cmdId;
76 unsigned netId; // used only in the SELECT_NETWORK command; ignored otherwise.
Chenbo Feng9944ba82017-10-10 17:33:20 -070077 uid_t uid; // used in the SELECT_FOR_USER, QUERY_USER_ACCESS, TAG_SOCKET,
78 // SET_COUNTERSET, and DELETE_TAGDATA command; ignored otherwise.
79 uint32_t trafficCtrlInfo; // used in TAG_SOCKET, SET_COUNTERSET and SET_PACIFIER command;
80 // ignored otherwise. Depend on the case, it can be a tag, a
81 // counterSet or a pacifier signal.
Remi NGUYEN VANeabc5da2018-04-24 18:54:58 +090082
83 static bool isSupportedFamily(int socketFamily) {
84 return socketFamily == AF_INET || socketFamily == AF_INET6;
85 }
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -070086};
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070087
88#endif // NETD_INCLUDE_FWMARK_COMMAND_H