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