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 uint32_t CELLULAR_IPv42[] = {127, 0, 0, 6};
22 static const int32_t IFACE_TYPE_CELLULAR = 1;
23 static const int32_t IFACE_TYPE_WIFI = 2;
24 static const int32_t APP_STATS_MAP_SIZE = 5000;
25 static const int32_t IFACE_STATS_MAP_SIZE = 1000;
26 static const int32_t IFACE_NAME_MAP_SIZE = 1000;
27 static const int32_t OH_SOCK_PERMISSION_MAP_SIZE = 1000;
28 static const int32_t BROKER_SOCK_PERMISSION_MAP_SIZE = 1000;
29 static const int32_t UID_ACCESS_POLICY_ARRAY_SIZE = 65535;
30 static const int32_t NET_NS_MAP_SIZE = 5000;
31 static const uint64_t SOCK_COOKIE_ID_NULL = UINT64_MAX;
32 static const int32_t SIM_UID_MAX = 20000;
33 static const int32_t SIM_UID_MIN = 10000;
34 static const uint64_t DEFAULT_BROKER_UID_KEY = 65536;
35 static const uint32_t VLAN_HEADER_LENGTH = 8;    // vlan header length
36 static const uint32_t IPV4_HEADERS_LENGTH = 54; // transfer header:20, ip header:20, link header:14
37 static const uint32_t IPV6_HEADERS_LENGTH = 74; // transfer header:20, ip header:40, link header:14
38 enum { IFNAME_SIZE = 32 };
39 enum { DEFAULT_NETWORK_BEARER_MAP_KEY = 0 };
40 
41 typedef struct {
42     enum bpf_map_type type;
43     __u32 key_size;
44     __u32 value_size;
45     __u32 max_entries;
46     __u32 map_flags;
47     __u32 inner_map_idx;
48     __u32 numa_node;
49 } bpf_map_def;
50 
51 typedef struct {
52     __u32 uId;
53     __u32 ifIndex;
54     __u32 ifType;
55 } stats_key;
56 
57 typedef struct {
58     __u64 rxPackets;
59     __u64 rxBytes;
60     __u64 txPackets;
61     __u64 txBytes;
62 } stats_value;
63 
64 typedef struct {
65     char name[IFNAME_SIZE];
66 } iface_name;
67 
68 typedef struct {
69     __u8 wifiPolicy;
70     __u8 cellularPolicy;
71     __u8 configSetFromFlag;
72     __u8 diagAckFlag;
73     __u32 netIfIndex;
74 } uid_access_policy_value;
75 
76 enum network_bearer_type {
77     NETWORK_BEARER_TYPE_INITIAL = 0,
78     NETWORK_BEARER_TYPE_CELLULAR,
79     NETWORK_BEARER_TYPE_WIFI,
80 };
81 
82 // network stats begin
83 typedef __u64 iface_stats_key;
84 typedef stats_value iface_stats_value;
85 
86 typedef __u64 app_uid_stats_key;
87 typedef stats_value app_uid_stats_value;
88 
89 typedef __u64 sock_netns_key;
90 typedef __u64 sock_netns_value;
91 
92 typedef stats_key app_uid_sim_stats_key;
93 typedef stats_value app_uid_sim_stats_value;
94 
95 typedef stats_key app_uid_if_stats_key;
96 typedef stats_value app_uid_if_stats_value;
97 
98 typedef __u64 socket_cookie_stats_key;
99 typedef stats_value app_cookie_stats_value;
100 // network stats end
101 
102 // internet permission begin
103 typedef __u32 sock_permission_key;
104 typedef __u8 sock_permission_value;
105 // internet permission end
106 
107 typedef __u32 net_bear_id_key;
108 typedef __u32 net_bear_type_map_value;
109 
110 typedef __u16 net_index;
111 typedef __u8 net_interface_name_id;
112 
113 typedef __u32 app_uid_key;
114 #endif /* NETMANAGER_BASE_BPF_DEF_H */
115