|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <asm/unistd.h> |
| 6 | +#include <bpf/libbpf.h> |
| 7 | +#include <bpf/bpf.h> |
| 8 | +#include <linux/netfilter.h> |
| 9 | + |
| 10 | + |
| 11 | +static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr, unsigned int size) |
| 12 | +{ |
| 13 | + return syscall(__NR_bpf, cmd, attr, size); |
| 14 | +} |
| 15 | +struct ipv4_lpm_key { |
| 16 | + __u32 prefixlen; |
| 17 | + __u32 data; |
| 18 | +}; |
| 19 | + |
| 20 | +int main(int argc, char **argv) |
| 21 | +{ |
| 22 | + int prog_fd, map_fd; |
| 23 | + int err; |
| 24 | + struct bpf_object *obj; |
| 25 | + struct bpf_program *prog; |
| 26 | + union bpf_attr attr = { }; |
| 27 | + |
| 28 | + obj = bpf_object__open_file("./netfilter_ip4_blocklist.bpf.o", NULL); |
| 29 | + if (libbpf_get_error(obj)) { |
| 30 | + printf("fail to open bpf file\n"); |
| 31 | + return 1; |
| 32 | + } |
| 33 | + prog = bpf_object__find_program_by_name(obj, "netfilter_ip4block"); |
| 34 | + if (!prog) { |
| 35 | + printf("fail to find bpf program\n"); |
| 36 | + return 1; |
| 37 | + } |
| 38 | + bpf_program__set_type(prog, BPF_PROG_TYPE_NETFILTER); |
| 39 | + if (bpf_object__load(obj)) { |
| 40 | + printf("loading BPF object file failed\n"); |
| 41 | + return 1; |
| 42 | + } |
| 43 | + map_fd = bpf_object__find_map_fd_by_name(obj, "ipv4_lpm_map"); |
| 44 | + if (map_fd < 0) { |
| 45 | + printf("Fail to locate trie ipv4_lpm_map\n"); |
| 46 | + return 1; |
| 47 | + } |
| 48 | + /* attach to netfilter forward handler */ |
| 49 | + prog_fd = bpf_program__fd(prog); |
| 50 | + attr.link_create.prog_fd = prog_fd; |
| 51 | + attr.link_create.attach_type = BPF_NETFILTER; |
| 52 | + attr.link_create.netfilter.pf = NFPROTO_IPV4; |
| 53 | + attr.link_create.netfilter.hooknum = NF_INET_FORWARD; |
| 54 | + attr.link_create.netfilter.priority = -128; |
| 55 | + err = sys_bpf(BPF_LINK_CREATE, &attr, sizeof(attr)); |
| 56 | + if (err < 0) { |
| 57 | + perror("Fail to link bpf program to netfilter forward hook\n"); |
| 58 | + return 1; |
| 59 | + } |
| 60 | + /* attach to netfilter output handler */ |
| 61 | + attr.link_create.netfilter.hooknum = NF_INET_LOCAL_OUT; |
| 62 | + err = sys_bpf(BPF_LINK_CREATE, &attr, sizeof(attr)); |
| 63 | + if (err < 0) { |
| 64 | + perror("Fail to link bpf program to netfilter output hook\n"); |
| 65 | + return 1; |
| 66 | + } |
| 67 | + printf("bpf program/map loaded....\n"); |
| 68 | + /* add rules */ |
| 69 | + { |
| 70 | + struct ipv4_lpm_key key; |
| 71 | + __u32 value = 0; |
| 72 | + __u8 *p = (__u8 *) &key.data; |
| 73 | + /* block 192.168.11.107/32 */ |
| 74 | + key.prefixlen = 27; |
| 75 | + /* same as key.data = 0x6B0BA8C0; on a little-endian machine */ |
| 76 | + p[0] = 192; |
| 77 | + p[1] = 168; |
| 78 | + p[2] = 11; |
| 79 | + p[3] = 107; |
| 80 | + bpf_map_update_elem(map_fd, &key, &value, BPF_ANY); |
| 81 | + /* block 192.168.11.107/24 */ |
| 82 | + key.prefixlen = 24; |
| 83 | + value++; |
| 84 | + bpf_map_update_elem(map_fd, &key, &value, BPF_ANY); |
| 85 | + /* block 192.168.11.107/27 */ |
| 86 | + key.prefixlen = 32; |
| 87 | + value++; |
| 88 | + bpf_map_update_elem(map_fd, &key, &value, BPF_ANY); |
| 89 | + /* remove rule */ |
| 90 | + /* bpf_map_delete_elem(map_fd, &key); */ |
| 91 | + printf("rules inserted, ready to work\n"); |
| 92 | + } |
| 93 | + while (1) |
| 94 | + sleep(600); |
| 95 | + return 0; |
| 96 | +} |
0 commit comments