1 /* Common BPF helpers to be used by all BPF programs loaded by Android */ 2 3 #include <linux/bpf.h> 4 #include <stdbool.h> 5 #include <stdint.h> 6 7 /* place things in different elf sections */ 8 #define SEC(NAME) __attribute__((section(NAME), used)) 9 10 /* 11 * Helper functions called from eBPF programs written in C. These are 12 * implemented in the kernel sources. 13 */ 14 15 /* generic functions */ 16 17 /* 18 * Type-unsafe bpf map functions - avoid if possible. 19 * 20 * Using these it is possible to pass in keys/values of the wrong type/size, 21 * or, for 'unsafe_bpf_map_lookup_elem' receive into a pointer to the wrong type. 22 * You will not get a compile time failure, and for certain types of errors you 23 * might not even get a failure from the kernel's ebpf verifier during program load, 24 * instead stuff might just not work right at runtime. 25 * 26 * Instead please use: 27 * DEFINE_BPF_MAP(foo_map, TYPE, KeyType, ValueType, num_entries) 28 * where TYPE can be something like HASH or ARRAY, and num_entries is an integer. 29 * 30 * This defines the map (hence this should not be used in a header file included 31 * from multiple locations) and provides type safe accessors: 32 * ValueType * bpf_foo_map_lookup_elem(KeyType *) 33 * int bpf_foo_map_update_elem(KeyType *, ValueType *, flags) 34 * int bpf_foo_map_delete_elem(KeyType *) 35 * 36 * This will make sure that if you change the type of a map you'll get compile 37 * errors at any spots you forget to update with the new type. 38 */ 39 static void* (*unsafe_bpf_map_lookup_elem)(void* map, void* key) = (void*)BPF_FUNC_map_lookup_elem; 40 static int (*unsafe_bpf_map_update_elem)(void* map, void* key, void* value, 41 unsigned long long flags) = (void*) 42 BPF_FUNC_map_update_elem; 43 static int (*unsafe_bpf_map_delete_elem)(void* map, void* key) = (void*)BPF_FUNC_map_delete_elem; 44 45 /* type safe macro to declare a map and related accessor functions */ 46 #define DEFINE_BPF_MAP_NO_ACCESSORS(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries) \ 47 struct bpf_map_def SEC("maps") the_map = { \ 48 .type = BPF_MAP_TYPE_##TYPE, \ 49 .key_size = sizeof(TypeOfKey), \ 50 .value_size = sizeof(TypeOfValue), \ 51 .max_entries = (num_entries), \ 52 }; 53 54 #define DEFINE_BPF_MAP(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries) \ 55 DEFINE_BPF_MAP_NO_ACCESSORS(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries) \ 56 \ 57 static inline __always_inline __unused TypeOfValue* bpf_##the_map##_lookup_elem( \ 58 TypeOfKey* k) { \ 59 return unsafe_bpf_map_lookup_elem(&the_map, k); \ 60 }; \ 61 \ 62 static inline __always_inline __unused int bpf_##the_map##_update_elem( \ 63 TypeOfKey* k, TypeOfValue* v, unsigned long long flags) { \ 64 return unsafe_bpf_map_update_elem(&the_map, k, v, flags); \ 65 }; \ 66 \ 67 static inline __always_inline __unused int bpf_##the_map##_delete_elem(TypeOfKey* k) { \ 68 return unsafe_bpf_map_delete_elem(&the_map, k); \ 69 }; 70 71 static int (*bpf_probe_read)(void* dst, int size, void* unsafe_ptr) = (void*) BPF_FUNC_probe_read; 72 static unsigned long long (*bpf_ktime_get_ns)(void) = (void*) BPF_FUNC_ktime_get_ns; 73 static int (*bpf_trace_printk)(const char* fmt, int fmt_size, ...) = (void*) BPF_FUNC_trace_printk; 74 static unsigned long long (*bpf_get_current_pid_tgid)(void) = (void*) BPF_FUNC_get_current_pid_tgid; 75 static unsigned long long (*bpf_get_current_uid_gid)(void) = (void*) BPF_FUNC_get_current_uid_gid; 76 static unsigned long long (*bpf_get_smp_processor_id)(void) = (void*) BPF_FUNC_get_smp_processor_id; 77 /* networking */ 78 static uint64_t (*bpf_get_socket_cookie)(struct __sk_buff* skb) = (void*)BPF_FUNC_get_socket_cookie; 79 static uint32_t (*bpf_get_socket_uid)(struct __sk_buff* skb) = (void*)BPF_FUNC_get_socket_uid; 80 static int (*bpf_skb_load_bytes)(struct __sk_buff* skb, int off, void* to, 81 int len) = (void*)BPF_FUNC_skb_load_bytes; 82 83 static int (*bpf_skb_change_proto)(struct __sk_buff* skb, __be16 proto, 84 __u64 flags) = (void*)BPF_FUNC_skb_change_proto; 85 static int (*bpf_l3_csum_replace)(struct __sk_buff* skb, __u32 offset, __u64 from, __u64 to, 86 __u64 flags) = (void*)BPF_FUNC_l3_csum_replace; 87 static int (*bpf_l4_csum_replace)(struct __sk_buff* skb, __u32 offset, __u64 from, __u64 to, 88 __u64 flags) = (void*)BPF_FUNC_l4_csum_replace; 89 static int (*bpf_redirect)(__u32 ifindex, __u64 flags) = (void*)BPF_FUNC_redirect; 90 91 /* 92 * Map structure to be used by Android eBPF C programs. The Android eBPF loader 93 * uses this structure from eBPF object to create maps at boot time. 94 * 95 * The eBPF C program should define structure in the maps section using 96 * SEC("maps") otherwise it will be ignored by the eBPF loader. 97 * 98 * For example: 99 * struct bpf_map_def SEC("maps") mymap { .type=... , .key_size=... } 100 */ 101 struct bpf_map_def { 102 unsigned int type; 103 unsigned int key_size; 104 unsigned int value_size; 105 unsigned int max_entries; 106 unsigned int map_flags; 107 unsigned int pad1; 108 unsigned int pad2; 109 }; 110