1b1b8bc3fSopenharmony_ci/*
2b1b8bc3fSopenharmony_ci* Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved.
3b1b8bc3fSopenharmony_ci
4b1b8bc3fSopenharmony_ci* The bpf_def.h is dual licensed: you can use it either under the terms of
5b1b8bc3fSopenharmony_ci* the GPL V2, or the 3-Clause BSD license, at your option.
6b1b8bc3fSopenharmony_ci* See the LICENSE file in the root of this repository for complete details.
7b1b8bc3fSopenharmony_ci*/
8b1b8bc3fSopenharmony_ci
9b1b8bc3fSopenharmony_ci#ifndef NETMANAGER_BASE_BPF_DEF_H
10b1b8bc3fSopenharmony_ci#define NETMANAGER_BASE_BPF_DEF_H
11b1b8bc3fSopenharmony_ci
12b1b8bc3fSopenharmony_ci#include <linux/bpf.h>
13b1b8bc3fSopenharmony_ci
14b1b8bc3fSopenharmony_ci#define GET_IP_SEGMENT(ip, seg) (((ip) >> (((seg)-1) * 8)) & 0xFF)
15b1b8bc3fSopenharmony_ci#define IS_MATCHED_IP(ip, target) \
16b1b8bc3fSopenharmony_ci    GET_IP_SEGMENT(ip, 1) == (target)[0] && GET_IP_SEGMENT(ip, 2) == (target)[1] && \
17b1b8bc3fSopenharmony_ci    GET_IP_SEGMENT(ip, 3) == (target)[2] && GET_IP_SEGMENT(ip, 4) == (target)[3]    \
18b1b8bc3fSopenharmony_ci
19b1b8bc3fSopenharmony_cistatic const uint32_t WLAN_IPv4[] = {172, 17, 1, 2};
20b1b8bc3fSopenharmony_cistatic const uint32_t CELLULAR_IPv4[] = {172, 17, 0, 2};
21b1b8bc3fSopenharmony_cistatic const int32_t IFACE_TYPE_CELLULAR = 1;
22b1b8bc3fSopenharmony_cistatic const int32_t IFACE_TYPE_WIFI = 2;
23b1b8bc3fSopenharmony_cistatic const int32_t APP_STATS_MAP_SIZE = 5000;
24b1b8bc3fSopenharmony_cistatic const int32_t IFACE_STATS_MAP_SIZE = 1000;
25b1b8bc3fSopenharmony_cistatic const int32_t IFACE_NAME_MAP_SIZE = 1000;
26b1b8bc3fSopenharmony_cistatic const int32_t OH_SOCK_PERMISSION_MAP_SIZE = 1000;
27b1b8bc3fSopenharmony_cistatic const int32_t BROKER_SOCK_PERMISSION_MAP_SIZE = 1000;
28b1b8bc3fSopenharmony_cistatic const int32_t UID_ACCESS_POLICY_ARRAY_SIZE = 65535;
29b1b8bc3fSopenharmony_cistatic const int32_t NET_NS_MAP_SIZE = 5000;
30b1b8bc3fSopenharmony_cistatic const uint64_t SOCK_COOKIE_ID_NULL = UINT64_MAX;
31b1b8bc3fSopenharmony_cistatic const int32_t SIM_UID_MAX = 20000;
32b1b8bc3fSopenharmony_cistatic const int32_t SIM_UID_MIN = 10000;
33b1b8bc3fSopenharmony_cistatic const uint64_t DEFAULT_BROKER_UID_KEY = 65536;
34b1b8bc3fSopenharmony_cistatic const uint32_t VLAN_HEADER_LENGTH = 8;    // vlan header length
35b1b8bc3fSopenharmony_cistatic const uint32_t IPV4_HEADERS_LENGTH = 54; // transfer header:20, ip header:20, link header:14
36b1b8bc3fSopenharmony_cistatic const uint32_t IPV6_HEADERS_LENGTH = 74; // transfer header:20, ip header:40, link header:14
37b1b8bc3fSopenharmony_cienum { IFNAME_SIZE = 32 };
38b1b8bc3fSopenharmony_cienum { DEFAULT_NETWORK_BEARER_MAP_KEY = 0 };
39b1b8bc3fSopenharmony_ci
40b1b8bc3fSopenharmony_citypedef struct {
41b1b8bc3fSopenharmony_ci    enum bpf_map_type type;
42b1b8bc3fSopenharmony_ci    __u32 key_size;
43b1b8bc3fSopenharmony_ci    __u32 value_size;
44b1b8bc3fSopenharmony_ci    __u32 max_entries;
45b1b8bc3fSopenharmony_ci    __u32 map_flags;
46b1b8bc3fSopenharmony_ci    __u32 inner_map_idx;
47b1b8bc3fSopenharmony_ci    __u32 numa_node;
48b1b8bc3fSopenharmony_ci} bpf_map_def;
49b1b8bc3fSopenharmony_ci
50b1b8bc3fSopenharmony_citypedef struct {
51b1b8bc3fSopenharmony_ci    __u32 uId;
52b1b8bc3fSopenharmony_ci    __u32 ifIndex;
53b1b8bc3fSopenharmony_ci    __u32 ifType;
54b1b8bc3fSopenharmony_ci} stats_key;
55b1b8bc3fSopenharmony_ci
56b1b8bc3fSopenharmony_citypedef struct {
57b1b8bc3fSopenharmony_ci    __u64 rxPackets;
58b1b8bc3fSopenharmony_ci    __u64 rxBytes;
59b1b8bc3fSopenharmony_ci    __u64 txPackets;
60b1b8bc3fSopenharmony_ci    __u64 txBytes;
61b1b8bc3fSopenharmony_ci} stats_value;
62b1b8bc3fSopenharmony_ci
63b1b8bc3fSopenharmony_citypedef struct {
64b1b8bc3fSopenharmony_ci    char name[IFNAME_SIZE];
65b1b8bc3fSopenharmony_ci} iface_name;
66b1b8bc3fSopenharmony_ci
67b1b8bc3fSopenharmony_citypedef struct {
68b1b8bc3fSopenharmony_ci    __u8 wifiPolicy;
69b1b8bc3fSopenharmony_ci    __u8 cellularPolicy;
70b1b8bc3fSopenharmony_ci    __u8 configSetFromFlag;
71b1b8bc3fSopenharmony_ci    __u8 diagAckFlag;
72b1b8bc3fSopenharmony_ci    __u32 netIfIndex;
73b1b8bc3fSopenharmony_ci} uid_access_policy_value;
74b1b8bc3fSopenharmony_ci
75b1b8bc3fSopenharmony_cienum network_bearer_type {
76b1b8bc3fSopenharmony_ci    NETWORK_BEARER_TYPE_INITIAL = 0,
77b1b8bc3fSopenharmony_ci    NETWORK_BEARER_TYPE_CELLULAR,
78b1b8bc3fSopenharmony_ci    NETWORK_BEARER_TYPE_WIFI,
79b1b8bc3fSopenharmony_ci};
80b1b8bc3fSopenharmony_ci
81b1b8bc3fSopenharmony_ci// network stats begin
82b1b8bc3fSopenharmony_citypedef __u64 iface_stats_key;
83b1b8bc3fSopenharmony_citypedef stats_value iface_stats_value;
84b1b8bc3fSopenharmony_ci
85b1b8bc3fSopenharmony_citypedef __u64 app_uid_stats_key;
86b1b8bc3fSopenharmony_citypedef stats_value app_uid_stats_value;
87b1b8bc3fSopenharmony_ci
88b1b8bc3fSopenharmony_citypedef __u64 sock_netns_key;
89b1b8bc3fSopenharmony_citypedef __u64 sock_netns_value;
90b1b8bc3fSopenharmony_ci
91b1b8bc3fSopenharmony_citypedef stats_key app_uid_sim_stats_key;
92b1b8bc3fSopenharmony_citypedef stats_value app_uid_sim_stats_value;
93b1b8bc3fSopenharmony_ci
94b1b8bc3fSopenharmony_citypedef stats_key app_uid_if_stats_key;
95b1b8bc3fSopenharmony_citypedef stats_value app_uid_if_stats_value;
96b1b8bc3fSopenharmony_ci
97b1b8bc3fSopenharmony_citypedef __u64 socket_cookie_stats_key;
98b1b8bc3fSopenharmony_citypedef stats_value app_cookie_stats_value;
99b1b8bc3fSopenharmony_ci// network stats end
100b1b8bc3fSopenharmony_ci
101b1b8bc3fSopenharmony_ci// internet permission begin
102b1b8bc3fSopenharmony_citypedef __u32 sock_permission_key;
103b1b8bc3fSopenharmony_citypedef __u8 sock_permission_value;
104b1b8bc3fSopenharmony_ci// internet permission end
105b1b8bc3fSopenharmony_ci
106b1b8bc3fSopenharmony_citypedef __u32 net_bear_id_key;
107b1b8bc3fSopenharmony_citypedef __u32 net_bear_type_map_value;
108b1b8bc3fSopenharmony_ci
109b1b8bc3fSopenharmony_citypedef __u16 net_index;
110b1b8bc3fSopenharmony_citypedef __u8 net_interface_name_id;
111b1b8bc3fSopenharmony_ci
112b1b8bc3fSopenharmony_citypedef __u32 app_uid_key;
113b1b8bc3fSopenharmony_ci#endif /* NETMANAGER_BASE_BPF_DEF_H */
114