Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 1 | /* |
| 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 Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 20 | #include <map> |
Bernie Innocenti | 51a0e0f | 2018-10-05 20:24:06 +0900 | [diff] [blame] | 21 | #include <mutex> |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 22 | #include <string> |
| 23 | |
| 24 | #include <linux/if.h> |
| 25 | #include <netinet/in.h> |
| 26 | |
Maciej Żenczykowski | 5526271 | 2019-03-29 23:44:56 -0700 | [diff] [blame] | 27 | #include <android-base/thread_annotations.h> |
| 28 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 29 | #include "Fwmark.h" |
| 30 | #include "NetdConstants.h" |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 31 | #include "bpf/BpfMap.h" |
| 32 | #include "netdbpf/bpf_shared.h" |
Maciej Żenczykowski | 5526271 | 2019-03-29 23:44:56 -0700 | [diff] [blame] | 33 | #include "netdutils/DumpWriter.h" |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 34 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace net { |
| 37 | |
Paul Jensen | 84c1d03 | 2014-05-30 13:29:41 -0400 | [diff] [blame] | 38 | class NetworkController; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 39 | |
Paul Jensen | 84c1d03 | 2014-05-30 13:29:41 -0400 | [diff] [blame] | 40 | class ClatdController { |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 41 | public: |
Maciej Żenczykowski | 0d40312 | 2019-04-24 13:28:12 -0700 | [diff] [blame] | 42 | explicit ClatdController(NetworkController* controller) EXCLUDES(mutex) |
| 43 | : mNetCtrl(controller){}; |
| 44 | virtual ~ClatdController() EXCLUDES(mutex){}; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 45 | |
Maciej Żenczykowski | 5628027 | 2019-03-30 03:32:51 -0700 | [diff] [blame] | 46 | /* First thing init/startClatd/stopClatd/dump do is grab the mutex. */ |
| 47 | void init(void) EXCLUDES(mutex); |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 48 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 49 | int startClatd(const std::string& interface, const std::string& nat64Prefix, |
Maciej Żenczykowski | 5628027 | 2019-03-30 03:32:51 -0700 | [diff] [blame] | 50 | std::string* v6Addr) EXCLUDES(mutex); |
| 51 | int stopClatd(const std::string& interface) EXCLUDES(mutex); |
Paul Jensen | 84c1d03 | 2014-05-30 13:29:41 -0400 | [diff] [blame] | 52 | |
Maciej Żenczykowski | 5526271 | 2019-03-29 23:44:56 -0700 | [diff] [blame] | 53 | void dump(netdutils::DumpWriter& dw) EXCLUDES(mutex); |
| 54 | |
Lorenzo Colitti | 91fd580 | 2019-06-28 19:22:01 +0900 | [diff] [blame] | 55 | static constexpr const char LOCAL_RAW_PREROUTING[] = "clat_raw_PREROUTING"; |
| 56 | |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 57 | private: |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 58 | struct ClatdTracker { |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 59 | pid_t pid = -1; |
Maciej Żenczykowski | c8c38aa | 2019-03-29 01:24:51 -0700 | [diff] [blame] | 60 | unsigned ifIndex; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 61 | char iface[IFNAMSIZ]; |
Maciej Żenczykowski | f4b44fe | 2019-04-08 16:18:50 -0700 | [diff] [blame] | 62 | unsigned v4ifIndex; |
| 63 | char v4iface[IFNAMSIZ]; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 64 | Fwmark fwmark; |
| 65 | char fwmarkString[UINT32_STRLEN]; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 66 | in_addr v4; |
| 67 | char v4Str[INET_ADDRSTRLEN]; |
| 68 | in6_addr v6; |
| 69 | char v6Str[INET6_ADDRSTRLEN]; |
Maciej Żenczykowski | 1c06f9c | 2019-03-29 23:19:19 -0700 | [diff] [blame] | 70 | in6_addr pfx96; |
| 71 | char pfx96String[INET6_ADDRSTRLEN]; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 72 | |
Maciej Żenczykowski | 4657e51 | 2019-05-08 06:35:08 -0700 | [diff] [blame] | 73 | int init(unsigned networkId, const std::string& interface, const std::string& v4interface, |
| 74 | const std::string& nat64Prefix); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 75 | }; |
| 76 | |
Maciej Żenczykowski | 5628027 | 2019-03-30 03:32:51 -0700 | [diff] [blame] | 77 | std::mutex mutex; |
| 78 | |
| 79 | const NetworkController* mNetCtrl GUARDED_BY(mutex); |
| 80 | std::map<std::string, ClatdTracker> mClatdTrackers GUARDED_BY(mutex); |
| 81 | ClatdTracker* getClatdTracker(const std::string& interface) REQUIRES(mutex); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 82 | |
Maciej Żenczykowski | 1afbd99 | 2019-12-16 11:44:14 -0800 | [diff] [blame] | 83 | void dumpEgress(netdutils::DumpWriter& dw) REQUIRES(mutex); |
Maciej Żenczykowski | 7dffa6f | 2019-12-16 11:20:44 -0800 | [diff] [blame] | 84 | void dumpIngress(netdutils::DumpWriter& dw) REQUIRES(mutex); |
Maciej Żenczykowski | 4c26217 | 2019-12-16 11:31:24 -0800 | [diff] [blame] | 85 | void dumpTrackers(netdutils::DumpWriter& dw) REQUIRES(mutex); |
Maciej Żenczykowski | 7dffa6f | 2019-12-16 11:20:44 -0800 | [diff] [blame] | 86 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 87 | static in_addr_t selectIpv4Address(const in_addr ip, int16_t prefixlen); |
| 88 | static int generateIpv6Address(const char* iface, const in_addr v4, const in6_addr& nat64Prefix, |
| 89 | in6_addr* v6); |
| 90 | static void makeChecksumNeutral(in6_addr* v6, const in_addr v4, const in6_addr& nat64Prefix); |
| 91 | |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 92 | enum eClatEbpfMode { |
| 93 | ClatEbpfDisabled, // <4.9 kernel || <P api shipping level -- will not work |
| 94 | ClatEbpfMaybe, // >=4.9 kernel && P api shipping level -- might work |
| 95 | ClatEbpfEnabled, // >=4.9 kernel && >=Q api shipping level -- must work |
| 96 | }; |
Maciej Żenczykowski | 5628027 | 2019-03-30 03:32:51 -0700 | [diff] [blame] | 97 | eClatEbpfMode mClatEbpfMode GUARDED_BY(mutex); |
Lorenzo Colitti | 91fd580 | 2019-06-28 19:22:01 +0900 | [diff] [blame] | 98 | eClatEbpfMode getEbpfMode() EXCLUDES(mutex) { |
| 99 | std::lock_guard guard(mutex); |
| 100 | return mClatEbpfMode; |
| 101 | } |
| 102 | |
Maciej Żenczykowski | 0bd8da7 | 2019-12-16 15:16:28 -0800 | [diff] [blame] | 103 | bpf::BpfMap<ClatEgressKey, ClatEgressValue> mClatEgressMap GUARDED_BY(mutex); |
Maciej Żenczykowski | 5628027 | 2019-03-30 03:32:51 -0700 | [diff] [blame] | 104 | bpf::BpfMap<ClatIngressKey, ClatIngressValue> mClatIngressMap GUARDED_BY(mutex); |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 105 | |
Maciej Żenczykowski | 5628027 | 2019-03-30 03:32:51 -0700 | [diff] [blame] | 106 | void maybeStartBpf(const ClatdTracker& tracker) REQUIRES(mutex); |
| 107 | void maybeStopBpf(const ClatdTracker& tracker) REQUIRES(mutex); |
Maciej Żenczykowski | f007de6 | 2019-12-23 15:27:53 -0800 | [diff] [blame] | 108 | void setIptablesDropRule(bool add, const char* iface, const char* pfx96Str, const char* v6Str) |
| 109 | REQUIRES(mutex); |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 110 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 111 | // For testing. |
| 112 | friend class ClatdControllerTest; |
| 113 | |
| 114 | static bool (*isIpv4AddressFreeFunc)(in_addr_t); |
| 115 | static bool isIpv4AddressFree(in_addr_t addr); |
Lorenzo Colitti | 91fd580 | 2019-06-28 19:22:01 +0900 | [diff] [blame] | 116 | static int (*iptablesRestoreFunction)(IptablesTarget target, const std::string& commands); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 117 | }; |
| 118 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 119 | } // namespace net |
| 120 | } // namespace android |
| 121 | |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 122 | #endif |