1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2021 Facebook */ 3 #include <vmlinux.h> 4 #include <bpf/bpf_helpers.h> 5 6 extern int bpf_kfunc_call_test2(struct sock *sk, __u32 a, __u32 b) __ksym; 7 extern __u64 bpf_kfunc_call_test1(struct sock *sk, __u32 a, __u64 b, 8 __u32 c, __u64 d) __ksym; 9 10 extern struct prog_test_ref_kfunc *bpf_kfunc_call_test_acquire(unsigned long *sp) __ksym; 11 extern void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) __ksym; 12 extern void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) __ksym; 13 extern void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) __ksym; 14 extern void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) __ksym; 15 extern void bpf_kfunc_call_test_mem_len_pass1(void *mem, int len) __ksym; 16 extern void bpf_kfunc_call_test_mem_len_fail2(__u64 *mem, int len) __ksym; 17 extern int *bpf_kfunc_call_test_get_rdwr_mem(struct prog_test_ref_kfunc *p, const int rdwr_buf_size) __ksym; 18 extern int *bpf_kfunc_call_test_get_rdonly_mem(struct prog_test_ref_kfunc *p, const int rdonly_buf_size) __ksym; 19 20 SEC("tc") 21 int kfunc_call_test2(struct __sk_buff *skb) 22 { 23 struct bpf_sock *sk = skb->sk; 24 25 if (!sk) 26 return -1; 27 28 sk = bpf_sk_fullsock(sk); 29 if (!sk) 30 return -1; 31 32 return bpf_kfunc_call_test2((struct sock *)sk, 1, 2); 33 } 34 35 SEC("tc") 36 int kfunc_call_test1(struct __sk_buff *skb) 37 { 38 struct bpf_sock *sk = skb->sk; 39 __u64 a = 1ULL << 32; 40 __u32 ret; 41 42 if (!sk) 43 return -1; 44 45 sk = bpf_sk_fullsock(sk); 46 if (!sk) 47 return -1; 48 49 a = bpf_kfunc_call_test1((struct sock *)sk, 1, a | 2, 3, a | 4); 50 ret = a >> 32; /* ret should be 2 */ 51 ret += (__u32)a; /* ret should be 12 */ 52 53 return ret; 54 } 55 56 SEC("tc") 57 int kfunc_call_test_ref_btf_id(struct __sk_buff *skb) 58 { 59 struct prog_test_ref_kfunc *pt; 60 unsigned long s = 0; 61 int ret = 0; 62 63 pt = bpf_kfunc_call_test_acquire(&s); 64 if (pt) { 65 if (pt->a != 42 || pt->b != 108) 66 ret = -1; 67 bpf_kfunc_call_test_release(pt); 68 } 69 return ret; 70 } 71 72 SEC("tc") 73 int kfunc_call_test_pass(struct __sk_buff *skb) 74 { 75 struct prog_test_pass1 p1 = {}; 76 struct prog_test_pass2 p2 = {}; 77 short a = 0; 78 __u64 b = 0; 79 long c = 0; 80 char d = 0; 81 int e = 0; 82 83 bpf_kfunc_call_test_pass_ctx(skb); 84 bpf_kfunc_call_test_pass1(&p1); 85 bpf_kfunc_call_test_pass2(&p2); 86 87 bpf_kfunc_call_test_mem_len_pass1(&a, sizeof(a)); 88 bpf_kfunc_call_test_mem_len_pass1(&b, sizeof(b)); 89 bpf_kfunc_call_test_mem_len_pass1(&c, sizeof(c)); 90 bpf_kfunc_call_test_mem_len_pass1(&d, sizeof(d)); 91 bpf_kfunc_call_test_mem_len_pass1(&e, sizeof(e)); 92 bpf_kfunc_call_test_mem_len_fail2(&b, -1); 93 94 return 0; 95 } 96 97 struct syscall_test_args { 98 __u8 data[16]; 99 size_t size; 100 }; 101 102 SEC("syscall") 103 int kfunc_syscall_test(struct syscall_test_args *args) 104 { 105 const long size = args->size; 106 107 if (size > sizeof(args->data)) 108 return -7; /* -E2BIG */ 109 110 bpf_kfunc_call_test_mem_len_pass1(&args->data, sizeof(args->data)); 111 bpf_kfunc_call_test_mem_len_pass1(&args->data, sizeof(*args)); 112 bpf_kfunc_call_test_mem_len_pass1(&args->data, size); 113 114 return 0; 115 } 116 117 SEC("syscall") 118 int kfunc_syscall_test_null(struct syscall_test_args *args) 119 { 120 /* Must be called with args as a NULL pointer 121 * we do not check for it to have the verifier consider that 122 * the pointer might not be null, and so we can load it. 123 * 124 * So the following can not be added: 125 * 126 * if (args) 127 * return -22; 128 */ 129 130 bpf_kfunc_call_test_mem_len_pass1(args, 0); 131 132 return 0; 133 } 134 135 SEC("tc") 136 int kfunc_call_test_get_mem(struct __sk_buff *skb) 137 { 138 struct prog_test_ref_kfunc *pt; 139 unsigned long s = 0; 140 int *p = NULL; 141 int ret = 0; 142 143 pt = bpf_kfunc_call_test_acquire(&s); 144 if (pt) { 145 p = bpf_kfunc_call_test_get_rdwr_mem(pt, 2 * sizeof(int)); 146 if (p) { 147 p[0] = 42; 148 ret = p[1]; /* 108 */ 149 } else { 150 ret = -1; 151 } 152 153 if (ret >= 0) { 154 p = bpf_kfunc_call_test_get_rdonly_mem(pt, 2 * sizeof(int)); 155 if (p) 156 ret = p[0]; /* 42 */ 157 else 158 ret = -1; 159 } 160 161 bpf_kfunc_call_test_release(pt); 162 } 163 return ret; 164 } 165 166 char _license[] SEC("license") = "GPL"; 167