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 
20 #include <arpa/inet.h>
21 #include <sys/socket.h>
22 #include <sys/types.h>
23 
24 // Additional information sent with ON_CONNECT_COMPLETE command
25 struct FwmarkConnectInfo {
26     int error;
27     unsigned latencyMs;
28     union {
29         sockaddr s;
30         sockaddr_in sin;
31         sockaddr_in6 sin6;
32     } addr;
33 
FwmarkConnectInfoFwmarkConnectInfo34     FwmarkConnectInfo() {}
35 
FwmarkConnectInfoFwmarkConnectInfo36     FwmarkConnectInfo(const int connectErrno, const unsigned latency, const sockaddr* saddr) {
37         error = connectErrno;
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 
51 // Commands sent from clients to the fwmark server to mark sockets (i.e., set their SO_MARK).
52 // ON_CONNECT_COMPLETE command should be accompanied by FwmarkConnectInfo which should  contain
53 // info about that connect attempt
54 struct FwmarkCommand {
55     enum {
56         ON_ACCEPT,
57         ON_CONNECT,
58         SELECT_NETWORK,
59         PROTECT_FROM_VPN,
60         SELECT_FOR_USER,
61         QUERY_USER_ACCESS,
62         ON_CONNECT_COMPLETE,
63     } cmdId;
64     unsigned netId;  // used only in the SELECT_NETWORK command; ignored otherwise.
65     uid_t uid;  // used only in the SELECT_FOR_USER and QUERY_USER_ACCESS commands;
66                 // ignored otherwise.
67 };
68 
69 #endif  // NETD_INCLUDE_FWMARK_COMMAND_H
70