1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef CLAT_CONSTANTS_H
16 #define CLAT_CONSTANTS_H
17 
18 #include <string>
19 
20 namespace OHOS {
21 namespace NetManagerStandard {
22 enum Nat464UpdateFlag {
23     NAT464_SERVICE_CONTINUE,
24     NAT464_SERVICE_STOP,
25 };
26 
27 enum Nat464ServiceState {
28     NAT464_SERVICE_STATE_IDLE,
29     NAT464_SERVICE_STATE_DISCOVERING,
30     NAT464_SERVICE_STATE_RUNNING,
31 };
32 
33 enum ClatdConvertType {
34     CONVERT_FROM_V6_TO_V4,
35     CONVERT_FROM_V4_TO_V6,
36 };
37 
38 enum ClatdPacketPosition {
39     CLATD_TUNHDR,
40     CLATD_IPHDR,
41     CLATD_FRAGHDR,
42     CLATD_TPHDR,
43     CLATD_ICMP_IPHDR,
44     CLATD_ICMP_FRAGHDR,
45     CLATD_ICMP_TPHDR,
46     CLATD_PAYLOAD,
47     CLATD_MAX
48 };
49 
50 static constexpr size_t IP_TP_PACKET_POSITION_DELTA = 2; // packet position delta between CLATD_IPHDR and CLATD_TPHDR
51 
52 static constexpr size_t TCP_HDR_MIN_LEN = 20;
53 static constexpr size_t TCP_HDR_MAX_LEN = 60;
54 static constexpr int IPV4_HDR_MIN_LEN = 20;
55 static constexpr int IPV6_HDR_LEN = 40;
56 static constexpr int FRAG_HDR_LEN = 8;
57 static constexpr int MTU_DELTA = IPV6_HDR_LEN - IPV4_HDR_MIN_LEN + FRAG_HDR_LEN;
58 static constexpr int CLAT_MAX_MTU = 65536 + MTU_DELTA;
59 static constexpr int CLAT_DATA_LINK_HDR_LEN = 22; // 14 bytes ethernet header + at most 8 bytes VLAN Tag
60 
61 static constexpr int CLAT_IPV6_MIN_MTU = 1280;
62 static constexpr uint16_t TP_CSUM_UNNECESSARY = 1;
63 
64 static constexpr const char *CLAT_PREFIX = "tunv4-";
65 static constexpr int CLAT_PREFIX_BYTE_LEN = 12;
66 static constexpr int CLAT_SUFFIX_OFFSET_IN_32 = 3; // 96-bit prefix + 32-bit suffix, the suffix is the 4th 32-bit word
67 
68 static constexpr int WORD_32BIT_IN_BYTE_UNIT = 4; // 4-bytes unit for tcphdr->doff and iphdr->ihl
69 
70 static constexpr uint8_t IPV6_VERSION_FLAG = 0x60;
71 static constexpr int V4ADDR_BIT_LEN = 32;
72 static constexpr int V6ADDR_BIT_LEN = 128;
73 static constexpr const char *INIT_V4ADDR_STRING = "192.0.0.0"; // Reserved ip addr in RFC 5736
74 static constexpr int INIT_V4ADDR_PREFIX_BIT_LEN = 29;
75 
76 static constexpr size_t CLAT_V6ADDR_RANDOMIZE_OFFSET = 8;
77 static constexpr size_t CLAT_V6ADDR_RANDOMIZE_BIT_LENGTH = 8;
78 static constexpr size_t CLAT_V6ADDR_NEUTRALIZE_OFFSET = 3;
79 
80 static constexpr uint16_t WKN_DNS_PORT = 53;
81 static constexpr const char *DEFAULT_V4_ADDR = "0.0.0.0/0";
82 
83 static constexpr const char *SOLICITED_NODE_PREFIX = "ff02::1:ff00:0000"; // Solicited-Node Address in RFC 4291
84 static constexpr size_t SOLICITED_NODE_SUFFIX_OFFSET = 13;
85 
86 static constexpr uint16_t IPV6_FRAG_OFFSET_BIT_SUPPLEMENTARY = 3; // offset field in ipv6 fragment header is top 13 bit
87 
88 static constexpr int INVALID_IFINDEX = 0;
89 
90 static constexpr const char *IFACE_LINK_UP = "up";
91 
92 static constexpr size_t IPV6_SRC_OFFSET = 8;
93 static constexpr uint8_t NDP_NOUNCE_OPT = 14;
94 
95 static constexpr const char *IPV4_ONLY_HOST = "ipv4only.arpa.";
96 
97 static constexpr uint32_t INITIAL_DISCOVERY_CYCLE_MS = 100;
98 
99 static constexpr uint32_t MAX_DISCOVERY_CYCLE_MS = 100000;
100 
101 static constexpr uint32_t DISCOVERY_CYCLE_MULTIPLIER = 2;
102 
103 static constexpr uint32_t CLATD_TIMER_CYCLE_MS = 5000;
104 
105 } // namespace NetManagerStandard
106 } // namespace OHOS
107 #endif