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