1b1b8bc3fSopenharmony_ci/*
2b1b8bc3fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License.
5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at
6b1b8bc3fSopenharmony_ci *
7b1b8bc3fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b1b8bc3fSopenharmony_ci *
9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and
13b1b8bc3fSopenharmony_ci * limitations under the License.
14b1b8bc3fSopenharmony_ci */
15b1b8bc3fSopenharmony_ci#ifndef NETSYS_CLAT_UTILS_H
16b1b8bc3fSopenharmony_ci#define NETSYS_CLAT_UTILS_H
17b1b8bc3fSopenharmony_ci
18b1b8bc3fSopenharmony_ci#include <linux/if_tun.h>
19b1b8bc3fSopenharmony_ci#include <netinet/icmp6.h>
20b1b8bc3fSopenharmony_ci#include <netinet/in.h>
21b1b8bc3fSopenharmony_ci#include <netinet/ip6.h>
22b1b8bc3fSopenharmony_ci#include <string>
23b1b8bc3fSopenharmony_ci#include <sys/uio.h>
24b1b8bc3fSopenharmony_ci
25b1b8bc3fSopenharmony_ci#include "clat_constants.h"
26b1b8bc3fSopenharmony_ci#include "inet_addr.h"
27b1b8bc3fSopenharmony_ci
28b1b8bc3fSopenharmony_cinamespace OHOS {
29b1b8bc3fSopenharmony_cinamespace nmd {
30b1b8bc3fSopenharmony_ciusing namespace OHOS::NetManagerStandard;
31b1b8bc3fSopenharmony_citypedef struct {
32b1b8bc3fSopenharmony_ci    std::string v6Iface;
33b1b8bc3fSopenharmony_ci    std::string tunIface;
34b1b8bc3fSopenharmony_ci    INetAddr v4Addr;
35b1b8bc3fSopenharmony_ci    INetAddr v6Addr;
36b1b8bc3fSopenharmony_ci    std::string nat64PrefixStr;
37b1b8bc3fSopenharmony_ci    int tunFd;
38b1b8bc3fSopenharmony_ci    int readSock6;
39b1b8bc3fSopenharmony_ci    int writeSock6;
40b1b8bc3fSopenharmony_ci} ClatdTracker;
41b1b8bc3fSopenharmony_ci
42b1b8bc3fSopenharmony_citypedef struct {
43b1b8bc3fSopenharmony_ci    ip6_hdr v6Header;
44b1b8bc3fSopenharmony_ci    nd_neighbor_solicit ns;
45b1b8bc3fSopenharmony_ci    uint8_t nonceOptType;
46b1b8bc3fSopenharmony_ci    uint8_t nonceOptLen;
47b1b8bc3fSopenharmony_ci    uint8_t nonce[6];
48b1b8bc3fSopenharmony_ci} ClatdDadPacket;
49b1b8bc3fSopenharmony_ci
50b1b8bc3fSopenharmony_citypedef struct {
51b1b8bc3fSopenharmony_ci    __u8 flags;
52b1b8bc3fSopenharmony_ci    __u8 gsoType;
53b1b8bc3fSopenharmony_ci    __u16 hdrLen;     /* Ethernet + IP + tcp/udp hdrs */
54b1b8bc3fSopenharmony_ci    __u16 gsoSize;    /* Bytes to append to hdr_len per frame */
55b1b8bc3fSopenharmony_ci    __u16 csumStart;  /* Position to start checksumming from */
56b1b8bc3fSopenharmony_ci    __u16 csumOffset; /* Offset after that to place checksum */
57b1b8bc3fSopenharmony_ci} virtioNetHdr;
58b1b8bc3fSopenharmony_ci
59b1b8bc3fSopenharmony_citypedef struct {
60b1b8bc3fSopenharmony_ci    virtioNetHdr vnet;
61b1b8bc3fSopenharmony_ci    uint8_t payload[CLAT_DATA_LINK_HDR_LEN + CLAT_MAX_MTU];
62b1b8bc3fSopenharmony_ci    char pad; // +1 to make packet truncation obvious
63b1b8bc3fSopenharmony_ci} ClatdReadV6Buf;
64b1b8bc3fSopenharmony_ci
65b1b8bc3fSopenharmony_citypedef struct {
66b1b8bc3fSopenharmony_ci    tun_pi tunProtocolInfo;
67b1b8bc3fSopenharmony_ci    uint8_t payload[CLAT_MAX_MTU];
68b1b8bc3fSopenharmony_ci    char pad; // +1 to make packet truncation obvious
69b1b8bc3fSopenharmony_ci} ClatdReadTunBuf;
70b1b8bc3fSopenharmony_ci
71b1b8bc3fSopenharmony_citypedef iovec ClatdPacket[CLATD_MAX];
72b1b8bc3fSopenharmony_ci
73b1b8bc3fSopenharmony_civoid FreeTunV4Addr(const std::string &v4AddrStr);
74b1b8bc3fSopenharmony_ciint32_t SelectIpv4Address(const std::string &initV4AddrStr, int prefixLen, std::string &v4AddrStr);
75b1b8bc3fSopenharmony_ciint32_t GenerateIpv6Address(const std::string &v6IfaceStr, const std::string &v4AddrStr, const std::string &prefix64Str,
76b1b8bc3fSopenharmony_ci                            uint32_t mark, std::string &v6AddrStr);
77b1b8bc3fSopenharmony_ciuint16_t CalChecksum(const void *data, int len);
78b1b8bc3fSopenharmony_ciuint32_t AddChecksum(uint32_t sum, const void *data, int len);
79b1b8bc3fSopenharmony_ciuint16_t Checksum32To16(uint32_t sum32);
80b1b8bc3fSopenharmony_ciuint16_t AdjustChecksum(uint16_t oldSum16, uint32_t oldSumHdr, uint32_t newSumHdr);
81b1b8bc3fSopenharmony_ciint32_t CreateTunInterface(const std::string &tunIface, int &fd);
82b1b8bc3fSopenharmony_ciint32_t OpenPacketSocket(int &readSock6);
83b1b8bc3fSopenharmony_ciint32_t OpenRawSocket6(const uint32_t mark, int &writeSock6);
84b1b8bc3fSopenharmony_ciint32_t ConfigureWriteSocket(const int sockFd, const std::string &v6Iface);
85b1b8bc3fSopenharmony_ciint32_t ConfigureReadSocket(const int sockFd, const std::string &addrStr, int ifIndex);
86b1b8bc3fSopenharmony_ciint32_t SetTunInterfaceAddress(const std::string &ifName, const std::string &tunAddr, int32_t prefix);
87b1b8bc3fSopenharmony_ci
88b1b8bc3fSopenharmony_ci} // namespace nmd
89b1b8bc3fSopenharmony_ci} // namespace OHOS
90b1b8bc3fSopenharmony_ci#endif