1 | // SPDX-License-Identifier: GPL-2.0 |
2 | #define _GNU_SOURCE |
3 | #include <sys/uio.h> |
4 | #include <errno.h> |
5 | #include <stdio.h> |
6 | #include <sys/socket.h> |
7 | #include <fcntl.h> |
8 | #include <unistd.h> |
9 | #include "../../include/uapi/linux/bpf.h" |
10 | #include <asm/unistd.h> |
11 | #include "msgfmt.h" |
12 | |
13 | FILE *debug_f; |
14 | |
15 | static int handle_get_cmd(struct mbox_request *cmd) |
16 | { |
17 | switch (cmd->cmd) { |
18 | case 0: |
19 | return 0; |
20 | default: |
21 | break; |
22 | } |
23 | return -ENOPROTOOPT; |
24 | } |
25 | |
26 | static int handle_set_cmd(struct mbox_request *cmd) |
27 | { |
28 | return -ENOPROTOOPT; |
29 | } |
30 | |
31 | static void loop(void) |
32 | { |
33 | while (1) { |
34 | struct mbox_request req; |
35 | struct mbox_reply reply; |
36 | int n; |
37 | |
38 | n = read(fd: 0, buf: &req, nbytes: sizeof(req)); |
39 | if (n != sizeof(req)) { |
40 | fprintf(stream: debug_f, format: "invalid request %d\n" , n); |
41 | return; |
42 | } |
43 | |
44 | reply.status = req.is_set ? |
45 | handle_set_cmd(cmd: &req) : |
46 | handle_get_cmd(cmd: &req); |
47 | |
48 | n = write(fd: 1, buf: &reply, n: sizeof(reply)); |
49 | if (n != sizeof(reply)) { |
50 | fprintf(stream: debug_f, format: "reply failed %d\n" , n); |
51 | return; |
52 | } |
53 | } |
54 | } |
55 | |
56 | int main(void) |
57 | { |
58 | debug_f = fopen(filename: "/dev/kmsg" , modes: "w" ); |
59 | setvbuf(stream: debug_f, buf: 0, _IOLBF, n: 0); |
60 | fprintf(stream: debug_f, format: "<5>Started bpfilter\n" ); |
61 | loop(); |
62 | fclose(stream: debug_f); |
63 | return 0; |
64 | } |
65 | |