blob: d2eafd99823ac00853fc6fe8bf13bce2e45b172a [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
27#include "Fwmark.h"
28#include "NetdConstants.h"
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090029
Lorenzo Colitti7035f222017-02-13 18:29:00 +090030namespace android {
31namespace net {
32
Paul Jensen84c1d032014-05-30 13:29:41 -040033class NetworkController;
Daniel Drown0da73fc2012-06-20 16:51:39 -050034
Paul Jensen84c1d032014-05-30 13:29:41 -040035class ClatdController {
Luke Huang6d301232018-08-01 14:05:18 +080036 public:
Paul Jensen84c1d032014-05-30 13:29:41 -040037 explicit ClatdController(NetworkController* controller);
Daniel Drown0da73fc2012-06-20 16:51:39 -050038 virtual ~ClatdController();
39
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090040 int startClatd(const std::string& interface, const std::string& nat64Prefix,
41 std::string* v6Addr);
42 int stopClatd(const std::string& interface);
Paul Jensen84c1d032014-05-30 13:29:41 -040043
Luke Huang6d301232018-08-01 14:05:18 +080044 std::mutex mutex;
45
46 private:
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090047 struct ClatdTracker {
48 const NetworkController* netCtrl = nullptr;
49 pid_t pid = -1;
Maciej Żenczykowskic8c38aa2019-03-29 01:24:51 -070050 unsigned ifIndex;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090051 char iface[IFNAMSIZ];
52 Fwmark fwmark;
53 char fwmarkString[UINT32_STRLEN];
54 unsigned netId;
55 char netIdString[UINT32_STRLEN];
56 in_addr v4;
57 char v4Str[INET_ADDRSTRLEN];
58 in6_addr v6;
59 char v6Str[INET6_ADDRSTRLEN];
60 in6_addr dst;
61 char dstString[INET6_ADDRSTRLEN];
62
63 ClatdTracker() = default;
64 explicit ClatdTracker(const NetworkController* netCtrl) : netCtrl(netCtrl) {}
65
66 int init(const std::string& interface, const std::string& nat64Prefix);
67 };
68
69 const NetworkController* mNetCtrl;
70 std::map<std::string, ClatdTracker> mClatdTrackers;
71 ClatdTracker* getClatdTracker(const std::string& interface);
72
73 static in_addr_t selectIpv4Address(const in_addr ip, int16_t prefixlen);
74 static int generateIpv6Address(const char* iface, const in_addr v4, const in6_addr& nat64Prefix,
75 in6_addr* v6);
76 static void makeChecksumNeutral(in6_addr* v6, const in_addr v4, const in6_addr& nat64Prefix);
77
78 // For testing.
79 friend class ClatdControllerTest;
80
81 static bool (*isIpv4AddressFreeFunc)(in_addr_t);
82 static bool isIpv4AddressFree(in_addr_t addr);
Daniel Drown0da73fc2012-06-20 16:51:39 -050083};
84
Lorenzo Colitti7035f222017-02-13 18:29:00 +090085} // namespace net
86} // namespace android
87
Daniel Drown0da73fc2012-06-20 16:51:39 -050088#endif