1*c7838e3dSMahe Tardy // SPDX-License-Identifier: GPL-2.0
2*c7838e3dSMahe Tardy /* Copyright (c) 2026 Isovalent */
3*c7838e3dSMahe Tardy
4*c7838e3dSMahe Tardy #include "vmlinux.h"
5*c7838e3dSMahe Tardy #include <bpf/bpf_helpers.h>
6*c7838e3dSMahe Tardy #include <bpf/bpf_tracing.h>
7*c7838e3dSMahe Tardy #include <bpf/bpf_endian.h>
8*c7838e3dSMahe Tardy #include "bpf_tracing_net.h"
9*c7838e3dSMahe Tardy #include "ksock_common.h"
10*c7838e3dSMahe Tardy
11*c7838e3dSMahe Tardy char send_data[32] = "hello from bpf ksock";
12*c7838e3dSMahe Tardy
13*c7838e3dSMahe Tardy __be32 ipv4_remote;
14*c7838e3dSMahe Tardy __u16 remote_port;
15*c7838e3dSMahe Tardy int target_pid;
16*c7838e3dSMahe Tardy int send_ret = -1;
17*c7838e3dSMahe Tardy
18*c7838e3dSMahe Tardy SEC("syscall")
ksock_setup(void * ctx)19*c7838e3dSMahe Tardy int ksock_setup(void *ctx)
20*c7838e3dSMahe Tardy {
21*c7838e3dSMahe Tardy struct bpf_ksock_create_opts create_opts = {};
22*c7838e3dSMahe Tardy union bpf_ksock_addr addr = {};
23*c7838e3dSMahe Tardy struct bpf_ksock *ks;
24*c7838e3dSMahe Tardy int err = 0;
25*c7838e3dSMahe Tardy
26*c7838e3dSMahe Tardy create_opts.family = AF_INET;
27*c7838e3dSMahe Tardy create_opts.type = SOCK_DGRAM;
28*c7838e3dSMahe Tardy create_opts.protocol = IPPROTO_UDP;
29*c7838e3dSMahe Tardy
30*c7838e3dSMahe Tardy ks = bpf_ksock_create(&create_opts, sizeof(create_opts), &err);
31*c7838e3dSMahe Tardy if (!ks)
32*c7838e3dSMahe Tardy return err;
33*c7838e3dSMahe Tardy
34*c7838e3dSMahe Tardy addr.sin.sin_family = AF_INET;
35*c7838e3dSMahe Tardy addr.sin.sin_port = bpf_htons(remote_port);
36*c7838e3dSMahe Tardy addr.sin.sin_addr.s_addr = ipv4_remote;
37*c7838e3dSMahe Tardy
38*c7838e3dSMahe Tardy err = bpf_ksock_connect(ks, &addr, sizeof(addr));
39*c7838e3dSMahe Tardy if (err) {
40*c7838e3dSMahe Tardy bpf_ksock_release(ks);
41*c7838e3dSMahe Tardy return err;
42*c7838e3dSMahe Tardy }
43*c7838e3dSMahe Tardy
44*c7838e3dSMahe Tardy err = ksock_ctx_insert(ks);
45*c7838e3dSMahe Tardy if (err && err != -EEXIST)
46*c7838e3dSMahe Tardy return err;
47*c7838e3dSMahe Tardy return 0;
48*c7838e3dSMahe Tardy }
49*c7838e3dSMahe Tardy
50*c7838e3dSMahe Tardy SEC("lsm.s/socket_bind")
BPF_PROG(ksock_socket_bind,struct socket * sock,struct sockaddr * address,int addrlen,int ret)51*c7838e3dSMahe Tardy int BPF_PROG(ksock_socket_bind, struct socket *sock, struct sockaddr *address,
52*c7838e3dSMahe Tardy int addrlen, int ret)
53*c7838e3dSMahe Tardy {
54*c7838e3dSMahe Tardy struct bpf_ksock *ks;
55*c7838e3dSMahe Tardy u32 pid = bpf_get_current_pid_tgid() >> 32;
56*c7838e3dSMahe Tardy
57*c7838e3dSMahe Tardy if (ret || pid != target_pid)
58*c7838e3dSMahe Tardy return ret;
59*c7838e3dSMahe Tardy
60*c7838e3dSMahe Tardy ks = ksock_ctx_get();
61*c7838e3dSMahe Tardy if (!ks) {
62*c7838e3dSMahe Tardy send_ret = -ENOENT;
63*c7838e3dSMahe Tardy return ret;
64*c7838e3dSMahe Tardy }
65*c7838e3dSMahe Tardy
66*c7838e3dSMahe Tardy send_ret = bpf_ksock_send(ks, send_data, sizeof(send_data));
67*c7838e3dSMahe Tardy bpf_ksock_release(ks);
68*c7838e3dSMahe Tardy
69*c7838e3dSMahe Tardy return ret;
70*c7838e3dSMahe Tardy }
71*c7838e3dSMahe Tardy
72*c7838e3dSMahe Tardy char __license[] SEC("license") = "GPL";
73