^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* eBPF example program:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * - Loads eBPF program
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * The eBPF program loads a filter from file and attaches the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * program to a cgroup using BPF_PROG_ATTACH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <net/if.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <bpf/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "bpf_insn.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "bpf_load.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static int usage(const char *argv0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) printf("Usage: %s cg-path filter-path [filter-id]\n", argv0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int cg_fd, ret, filter_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (argc < 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return usage(argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) cg_fd = open(argv[1], O_DIRECTORY | O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (cg_fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) printf("Failed to open cgroup path: '%s'\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (load_bpf_file(argv[2]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) printf("Output from kernel verifier:\n%s\n-------\n", bpf_log_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (argc > 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) filter_id = atoi(argv[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (filter_id >= prog_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) printf("Invalid program id; program not found in file\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) ret = bpf_prog_attach(prog_fd[filter_id], cg_fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) BPF_CGROUP_INET_SOCK_CREATE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) printf("Failed to attach prog to cgroup: '%s'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return EXIT_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }