blob: 26b3fcd25c27052ecd18b670a179ce2a63536ccc [file] [log] [blame]
Daniel Drown0da73fc2012-06-20 16:51:39 -05001/*
2 * Copyright (C) 2008 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 _CLATD_CONTROLLER_H
18#define _CLATD_CONTROLLER_H
19
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090020#include <map>
Bernie Innocenti51a0e0f2018-10-05 20:24:06 +090021#include <mutex>
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090022#include <string>
23
24#include <linux/if.h>
25#include <netinet/in.h>
26
Maciej Żenczykowski55262712019-03-29 23:44:56 -070027#include <android-base/thread_annotations.h>
28
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090029#include "Fwmark.h"
30#include "NetdConstants.h"
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -070031#include "bpf/BpfMap.h"
32#include "netdbpf/bpf_shared.h"
Maciej Żenczykowski55262712019-03-29 23:44:56 -070033#include "netdutils/DumpWriter.h"
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090034
Lorenzo Colitti7035f222017-02-13 18:29:00 +090035namespace android {
36namespace net {
37
Paul Jensen84c1d032014-05-30 13:29:41 -040038class NetworkController;
Daniel Drown0da73fc2012-06-20 16:51:39 -050039
Paul Jensen84c1d032014-05-30 13:29:41 -040040class ClatdController {
Luke Huang6d301232018-08-01 14:05:18 +080041 public:
Maciej Żenczykowski0d403122019-04-24 13:28:12 -070042 explicit ClatdController(NetworkController* controller) EXCLUDES(mutex)
43 : mNetCtrl(controller){};
44 virtual ~ClatdController() EXCLUDES(mutex){};
Daniel Drown0da73fc2012-06-20 16:51:39 -050045
Maciej Żenczykowski56280272019-03-30 03:32:51 -070046 /* First thing init/startClatd/stopClatd/dump do is grab the mutex. */
47 void init(void) EXCLUDES(mutex);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -070048
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090049 int startClatd(const std::string& interface, const std::string& nat64Prefix,
Maciej Żenczykowski56280272019-03-30 03:32:51 -070050 std::string* v6Addr) EXCLUDES(mutex);
51 int stopClatd(const std::string& interface) EXCLUDES(mutex);
Paul Jensen84c1d032014-05-30 13:29:41 -040052
Maciej Żenczykowski55262712019-03-29 23:44:56 -070053 void dump(netdutils::DumpWriter& dw) EXCLUDES(mutex);
54
Lorenzo Colitti91fd5802019-06-28 19:22:01 +090055 static constexpr const char LOCAL_RAW_PREROUTING[] = "clat_raw_PREROUTING";
56
Luke Huang6d301232018-08-01 14:05:18 +080057 private:
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090058 struct ClatdTracker {
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090059 pid_t pid = -1;
Maciej Żenczykowskic8c38aa2019-03-29 01:24:51 -070060 unsigned ifIndex;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090061 char iface[IFNAMSIZ];
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -070062 unsigned v4ifIndex;
63 char v4iface[IFNAMSIZ];
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090064 Fwmark fwmark;
65 char fwmarkString[UINT32_STRLEN];
66 unsigned netId;
67 char netIdString[UINT32_STRLEN];
68 in_addr v4;
69 char v4Str[INET_ADDRSTRLEN];
70 in6_addr v6;
71 char v6Str[INET6_ADDRSTRLEN];
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -070072 in6_addr pfx96;
73 char pfx96String[INET6_ADDRSTRLEN];
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090074
Maciej Żenczykowski4657e512019-05-08 06:35:08 -070075 int init(unsigned networkId, const std::string& interface, const std::string& v4interface,
76 const std::string& nat64Prefix);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090077 };
78
Maciej Żenczykowski56280272019-03-30 03:32:51 -070079 std::mutex mutex;
80
81 const NetworkController* mNetCtrl GUARDED_BY(mutex);
82 std::map<std::string, ClatdTracker> mClatdTrackers GUARDED_BY(mutex);
83 ClatdTracker* getClatdTracker(const std::string& interface) REQUIRES(mutex);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090084
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -080085 void dumpEgress(netdutils::DumpWriter& dw) REQUIRES(mutex);
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -080086 void dumpIngress(netdutils::DumpWriter& dw) REQUIRES(mutex);
Maciej Żenczykowski4c262172019-12-16 11:31:24 -080087 void dumpTrackers(netdutils::DumpWriter& dw) REQUIRES(mutex);
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -080088
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090089 static in_addr_t selectIpv4Address(const in_addr ip, int16_t prefixlen);
90 static int generateIpv6Address(const char* iface, const in_addr v4, const in6_addr& nat64Prefix,
91 in6_addr* v6);
92 static void makeChecksumNeutral(in6_addr* v6, const in_addr v4, const in6_addr& nat64Prefix);
93
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -070094 enum eClatEbpfMode {
95 ClatEbpfDisabled, // <4.9 kernel || <P api shipping level -- will not work
96 ClatEbpfMaybe, // >=4.9 kernel && P api shipping level -- might work
97 ClatEbpfEnabled, // >=4.9 kernel && >=Q api shipping level -- must work
98 };
Maciej Żenczykowski56280272019-03-30 03:32:51 -070099 eClatEbpfMode mClatEbpfMode GUARDED_BY(mutex);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900100 eClatEbpfMode getEbpfMode() EXCLUDES(mutex) {
101 std::lock_guard guard(mutex);
102 return mClatEbpfMode;
103 }
104
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700105 base::unique_fd mNetlinkFd GUARDED_BY(mutex);
106 bpf::BpfMap<ClatIngressKey, ClatIngressValue> mClatIngressMap GUARDED_BY(mutex);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700107
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700108 void maybeStartBpf(const ClatdTracker& tracker) REQUIRES(mutex);
109 void maybeStopBpf(const ClatdTracker& tracker) REQUIRES(mutex);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900110 void maybeSetIptablesDropRule(bool add, const char* pfx96Str, const char* v6Str)
111 REQUIRES(mutex);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700112
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900113 // For testing.
114 friend class ClatdControllerTest;
115
116 static bool (*isIpv4AddressFreeFunc)(in_addr_t);
117 static bool isIpv4AddressFree(in_addr_t addr);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900118 static int (*iptablesRestoreFunction)(IptablesTarget target, const std::string& commands);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500119};
120
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900121} // namespace net
122} // namespace android
123
Daniel Drown0da73fc2012-06-20 16:51:39 -0500124#endif