blob: c215ed44356e3a2592af132c2bae839e96d1f3ad [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 Żenczykowski74b20e72020-04-30 16:36:21 -070034 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 {
Ken Chen22e5fb82020-02-25 18:05:52 +000057 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,
Patrick Rohrb371bc32022-02-01 22:43:23 +010067 _UNUSED_SET_COUNTERSET, // unsupported, do not use this command
68 _UNUSED_DELETE_TAGDATA, // unsupported, do not use this command
Ken Chen22e5fb82020-02-25 18:05:52 +000069 ON_SENDMMSG,
70 ON_SENDMSG,
71 ON_SENDTO,
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -070072 } cmdId;
73 unsigned netId; // used only in the SELECT_NETWORK command; ignored otherwise.
Chenbo Feng9944ba82017-10-10 17:33:20 -070074 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 VANeabc5da2018-04-24 18:54:58 +090079
80 static bool isSupportedFamily(int socketFamily) {
81 return socketFamily == AF_INET || socketFamily == AF_INET6;
82 }
Sreeram Ramachandranefbe05d2014-05-21 11:41:39 -070083};
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -070084
85#endif // NETD_INCLUDE_FWMARK_COMMAND_H