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