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 NETSYS_CLAT_UTILS_H
16 #define NETSYS_CLAT_UTILS_H
17 
18 #include <linux/if_tun.h>
19 #include <netinet/icmp6.h>
20 #include <netinet/in.h>
21 #include <netinet/ip6.h>
22 #include <string>
23 #include <sys/uio.h>
24 
25 #include "clat_constants.h"
26 #include "inet_addr.h"
27 
28 namespace OHOS {
29 namespace nmd {
30 using namespace OHOS::NetManagerStandard;
31 typedef struct {
32     std::string v6Iface;
33     std::string tunIface;
34     INetAddr v4Addr;
35     INetAddr v6Addr;
36     std::string nat64PrefixStr;
37     int tunFd;
38     int readSock6;
39     int writeSock6;
40 } ClatdTracker;
41 
42 typedef struct {
43     ip6_hdr v6Header;
44     nd_neighbor_solicit ns;
45     uint8_t nonceOptType;
46     uint8_t nonceOptLen;
47     uint8_t nonce[6];
48 } ClatdDadPacket;
49 
50 typedef struct {
51     __u8 flags;
52     __u8 gsoType;
53     __u16 hdrLen;     /* Ethernet + IP + tcp/udp hdrs */
54     __u16 gsoSize;    /* Bytes to append to hdr_len per frame */
55     __u16 csumStart;  /* Position to start checksumming from */
56     __u16 csumOffset; /* Offset after that to place checksum */
57 } virtioNetHdr;
58 
59 typedef struct {
60     virtioNetHdr vnet;
61     uint8_t payload[CLAT_DATA_LINK_HDR_LEN + CLAT_MAX_MTU];
62     char pad; // +1 to make packet truncation obvious
63 } ClatdReadV6Buf;
64 
65 typedef struct {
66     tun_pi tunProtocolInfo;
67     uint8_t payload[CLAT_MAX_MTU];
68     char pad; // +1 to make packet truncation obvious
69 } ClatdReadTunBuf;
70 
71 typedef iovec ClatdPacket[CLATD_MAX];
72 
73 void FreeTunV4Addr(const std::string &v4AddrStr);
74 int32_t SelectIpv4Address(const std::string &initV4AddrStr, int prefixLen, std::string &v4AddrStr);
75 int32_t GenerateIpv6Address(const std::string &v6IfaceStr, const std::string &v4AddrStr, const std::string &prefix64Str,
76                             uint32_t mark, std::string &v6AddrStr);
77 uint16_t CalChecksum(const void *data, int len);
78 uint32_t AddChecksum(uint32_t sum, const void *data, int len);
79 uint16_t Checksum32To16(uint32_t sum32);
80 uint16_t AdjustChecksum(uint16_t oldSum16, uint32_t oldSumHdr, uint32_t newSumHdr);
81 int32_t CreateTunInterface(const std::string &tunIface, int &fd);
82 int32_t OpenPacketSocket(int &readSock6);
83 int32_t OpenRawSocket6(const uint32_t mark, int &writeSock6);
84 int32_t ConfigureWriteSocket(const int sockFd, const std::string &v6Iface);
85 int32_t ConfigureReadSocket(const int sockFd, const std::string &addrStr, int ifIndex);
86 int32_t SetTunInterfaceAddress(const std::string &ifName, const std::string &tunAddr, int32_t prefix);
87 
88 } // namespace nmd
89 } // namespace OHOS
90 #endif