Debian Codegraph @

misc/ssfilter_check.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <errno.h>
5
6 #include "libnetlink.h"
7 #include "ssfilter.h"
8 #include "ss_util.h"
9
10 static int dummy_filter(struct nlmsghdr *n, void *arg)
11 {
12 /* just stops rtnl_dump_filter() */
13 return -1;
14 }
15
16 static bool cgroup_filter_check(void)
17 {
18 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
19 DIAG_REQUEST(req, struct inet_diag_req_v2 r);
20 struct instr {
21 struct inet_diag_bc_op op;
22 __u64 cgroup_id;
23 } __attribute__((packed));
24 int inslen = sizeof(struct instr);
25 struct instr instr = {
26 { INET_DIAG_BC_CGROUP_COND, inslen, inslen + 4 },
27 0
28 };
29 struct rtnl_handle rth;
30 struct iovec iov[3];
31 struct msghdr msg;
32 struct rtattr rta;
33 int ret = false;
34 int iovlen = 3;
35
36 if (rtnl_open_byproto(&rth, 0, NETLINK_SOCK_DIAG))
37 return false;
38 rth.dump = MAGIC_SEQ;
39 rth.flags = RTNL_HANDLE_F_SUPPRESS_NLERR;
40
41 memset(&req.r, 0, sizeof(req.r));
42 req.r.sdiag_family = AF_INET;
43 req.r.sdiag_protocol = IPPROTO_TCP;
44 req.nlh.nlmsg_len += RTA_LENGTH(inslen);
45
46 rta.rta_type = INET_DIAG_REQ_BYTECODE;
47 rta.rta_len = RTA_LENGTH(inslen);
48
49 iov[0] = (struct iovec) { &req, sizeof(req) };
50 iov[1] = (struct iovec) { &rta, sizeof(rta) };
51 iov[2] = (struct iovec) { &instr, inslen };
52
53 msg = (struct msghdr) {
54 .msg_name = (void *)&nladdr,
55 .msg_namelen = sizeof(nladdr),
56 .msg_iov = iov,
57 .msg_iovlen = iovlen,
58 };
59
60 if (sendmsg(rth.fd, &msg, 0) < 0)
61 goto out;
62
63 if (rtnl_dump_filter(&rth, dummy_filter, NULL) < 0) {
64 ret = (errno != EINVAL);
65 goto out;
66 }
67
68 ret = true;
69
70 out:
71 rtnl_close(&rth);
72
73 return ret;
74 }
75
76
77 struct filter_check_t {
78 bool (*check)(void);
79 unsigned int checked:1,
80 supported:1;
81 };
82
83 static struct filter_check_t filter_checks[SSF__MAX] = {
84 [SSF_CGROUPCOND] = { cgroup_filter_check, 0 },
85 };
86
87 bool ssfilter_is_supported(int type)
88 {
89 struct filter_check_t f;
90
91 if (type >= SSF__MAX)
92 return false;
93
94 f = filter_checks[type];
95 if (!f.check)
96 return true;
97
98 if (!f.checked) {
99 f.supported = f.check();
100 f.checked = 1;
101 }
102
103 return f.supported;
104 }