|
| 1 | +/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */ |
| 2 | +/* Copyright Authors of Kmesh */ |
| 3 | + |
| 4 | +#include "../../config/kmesh_marcos_def.h" |
| 5 | +#include <linux/in.h> |
| 6 | +#include <stddef.h> |
| 7 | +#include <stdbool.h> |
| 8 | +#include <stdint.h> |
| 9 | +#include <linux/bpf.h> |
| 10 | +#include <sys/socket.h> |
| 11 | +#include <bpf/bpf_helpers.h> |
| 12 | +#include <bpf/bpf_endian.h> |
| 13 | +#include "map_config.h" |
| 14 | + |
| 15 | +#include "errno.h" |
| 16 | + |
| 17 | +struct bpf_mem_ptr { |
| 18 | + void *ptr; |
| 19 | + __u32 size; |
| 20 | +}; |
| 21 | + |
| 22 | +extern int bpf_parse_header_msg_func(void *src, int src__sz) __ksym; |
| 23 | +extern int bpf_km_header_strnstr_func(void *ctx, int ctx__sz, const char *key, int key__sz, const char *subptr) __ksym; |
| 24 | +extern int bpf_km_header_strncmp_func(const char *key, int key__sz, const char *target, int target__sz, int opt) __ksym; |
| 25 | +extern int bpf_setsockopt_func(void *bpf_mem, int bpf_mem__sz, int optname, const char *optval, int optval__sz) __ksym; |
| 26 | +extern int bpf_getsockopt_func(void *bpf_mem, int bpf_mem__sz, int optname, char *optval, int optval__sz) __ksym; |
| 27 | + |
| 28 | +#define bpf_km_header_strncmp bpf_km_header_strncmp_func |
| 29 | + |
| 30 | +static int bpf_km_header_strnstr(void *ctx, const char *key, int key__sz, const char *subptr, int subptr__sz) |
| 31 | +{ |
| 32 | + struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)}; |
| 33 | + return bpf_km_header_strnstr_func(&msg_tmp, sizeof(struct bpf_mem_ptr), key, key__sz, subptr); |
| 34 | +} |
| 35 | + |
| 36 | +static int bpf_parse_header_msg(struct bpf_sock_addr *ctx) |
| 37 | +{ |
| 38 | + struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)}; |
| 39 | + return bpf_parse_header_msg_func(&msg_tmp, sizeof(struct bpf_mem_ptr)); |
| 40 | +} |
| 41 | + |
| 42 | +// Due to the limitation of bpf verifier, optval and optval__sz are required to correspond. |
| 43 | +// The strnlen function cannot be used here, so the string is redefined. |
| 44 | +static int bpf_km_setsockopt(struct bpf_sock_addr *ctx, int level, int optname, const char *optval, int optval__sz) |
| 45 | +{ |
| 46 | + const char kmesh_module_name[] = "kmesh_defer"; |
| 47 | + if (level != IPPROTO_TCP || optval__sz != sizeof(kmesh_module_name)) |
| 48 | + return -1; |
| 49 | + |
| 50 | + struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)}; |
| 51 | + return bpf_setsockopt_func( |
| 52 | + &msg_tmp, sizeof(struct bpf_mem_ptr), optname, (void *)kmesh_module_name, sizeof(kmesh_module_name)); |
| 53 | +} |
| 54 | + |
| 55 | +static int bpf_km_getsockopt(struct bpf_sock_addr *ctx, int level, int optname, char *optval, int optval__sz) |
| 56 | +{ |
| 57 | + if (level != IPPROTO_TCP) { |
| 58 | + return -1; |
| 59 | + } |
| 60 | + struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)}; |
| 61 | + return bpf_getsockopt_func(&msg_tmp, sizeof(struct bpf_mem_ptr), optname, (void *)optval, optval__sz); |
| 62 | +} |
0 commit comments