Debian Codegraph @

src/expr/socket.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */cxx . . $ `<file>/src/expr/socket.c`/
2 /*
3 * Copyright (c) 2018 Máté Eckl <[email protected]>
4 */
5
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <arpa/inet.h>
10 #include <errno.h>
11 #include <linux/netfilter/nf_tables.h>cxx . . $ `<file>/include/linux/netfilter/nf_tables.h`/
12
13 #include "internal.h"cxx . . $ `<file>/include/internal.h`/
14 #include <libmnl/libmnl.h>
15 #include <libnftnl/expr.h>cxx . . $ `<file>/include/libnftnl/expr.h`/
16 #include <libnftnl/rule.h>cxx . . $ `<file>/include/libnftnl/rule.h`/
17
18 struct nftnl_expr_socketcxx . . $ nftnl_expr_socket# {
19 enum nft_socket_keyscxx . . $ nft_socket_keys# keycxx . . $ nftnl_expr_socket#key.;
20 enum nft_registerscxx . . $ nft_registers# dregcxx . . $ nftnl_expr_socket#dreg.;
21 uint32_tcxx . . $ uint32_t# levelcxx . . $ nftnl_expr_socket#level.;
22 };
23
24 static int
25 nftnl_expr_socket_setcxx . . $ nftnl_expr_socket_set(211371a3e3842127).(struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type,
26 const void *data, uint32_tcxx . . $ uint32_t# data_len, uint32_tcxx . . $ uint32_t# byteorder)
27 {
28 struct nftnl_expr_socketcxx . . $ nftnl_expr_socket# *socket = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
29
30 switch (type) {
31 case NFTNL_EXPR_SOCKET_KEYcxx . . $ NFTNL_EXPR_SOCKET_KEY.:
32 memcpycxx . . $ memcpy(cc817cb175f282d).(&socket->keycxx . . $ nftnl_expr_socket#key., data, data_len);
33 break;
34 case NFTNL_EXPR_SOCKET_DREGcxx . . $ NFTNL_EXPR_SOCKET_DREG.:
35 memcpycxx . . $ memcpy(cc817cb175f282d).(&socket->dregcxx . . $ nftnl_expr_socket#dreg., data, data_len);
36 break;
37 case NFTNL_EXPR_SOCKET_LEVELcxx . . $ NFTNL_EXPR_SOCKET_LEVEL.:
38 memcpycxx . . $ memcpy(cc817cb175f282d).(&socket->levelcxx . . $ nftnl_expr_socket#level., data, data_len);
39 break;
40 }
41 return 0;
42 }
43
44 static const void *
45 nftnl_expr_socket_getcxx . . $ nftnl_expr_socket_get(9de263c4f9e71c4b).(const struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type,
46 uint32_tcxx . . $ uint32_t# *data_len)
47 {
48 struct nftnl_expr_socketcxx . . $ nftnl_expr_socket# *socket = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
49
50 switch (type) {
51 case NFTNL_EXPR_SOCKET_KEYcxx . . $ NFTNL_EXPR_SOCKET_KEY.:
52 *data_len = sizeof(socket->keycxx . . $ nftnl_expr_socket#key.);
53 return &socket->keycxx . . $ nftnl_expr_socket#key.;
54 case NFTNL_EXPR_SOCKET_DREGcxx . . $ NFTNL_EXPR_SOCKET_DREG.:
55 *data_len = sizeof(socket->dregcxx . . $ nftnl_expr_socket#dreg.);
56 return &socket->dregcxx . . $ nftnl_expr_socket#dreg.;
57 case NFTNL_EXPR_SOCKET_LEVELcxx . . $ NFTNL_EXPR_SOCKET_LEVEL.:
58 *data_len = sizeof(socket->levelcxx . . $ nftnl_expr_socket#level.);
59 return &socket->levelcxx . . $ nftnl_expr_socket#level.;
60 }
61 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
62 }
63
64 static int nftnl_expr_socket_cbcxx . . $ nftnl_expr_socket_cb(de2ef18c48fd675c).(const struct nlattrcxx . . $ nlattr# *attr, void *data)
65 {
66 const struct nlattrcxx . . $ nlattr# **tb = data;
67 int type = mnl_attr_get_typecxx . . $ mnl_attr_get_type(1da47bcc4c4b975).(attr);
68
69 if (mnl_attr_type_validcxx . . $ mnl_attr_type_valid(47df1cce1c7be855).(attr, NFTA_SOCKET_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:1108:9`!) < 0)
70 return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!;
71
72 switch (type) {
73 case NFTA_SOCKET_KEYcxx . . $ NFTA_SOCKET_KEY.:
74 case NFTA_SOCKET_DREGcxx . . $ NFTA_SOCKET_DREG.:
75 case NFTA_SOCKET_LEVELcxx . . $ NFTA_SOCKET_LEVEL.:
76 if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_U32cxx . . $ MNL_TYPE_U32.) < 0)
77 abi_breakagecxx . . $ `include/utils.h:28:9`!();
78 break;
79 }
80
81 tb[type] = attr;
82 return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!;
83 }
84
85 static void
86 nftnl_expr_socket_buildcxx . . $ nftnl_expr_socket_build(93c7528a9cf90b41).(struct nlmsghdrcxx . . $ nlmsghdr# *nlh, const struct nftnl_exprcxx . . $ nftnl_expr# *e)
87 {
88 struct nftnl_expr_socketcxx . . $ nftnl_expr_socket# *socket = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
89
90 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_SOCKET_KEYcxx . . $ NFTNL_EXPR_SOCKET_KEY.))
91 mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_SOCKET_KEYcxx . . $ NFTA_SOCKET_KEY., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(socket->key));
92 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_SOCKET_DREGcxx . . $ NFTNL_EXPR_SOCKET_DREG.))
93 mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_SOCKET_DREGcxx . . $ NFTA_SOCKET_DREG., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(socket->dreg));
94 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_SOCKET_LEVELcxx . . $ NFTNL_EXPR_SOCKET_LEVEL.))
95 mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_SOCKET_LEVELcxx . . $ NFTA_SOCKET_LEVEL., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(socket->level));
96 }
97
98 static int
99 nftnl_expr_socket_parsecxx . . $ nftnl_expr_socket_parse(ed61207049d8af9a).(struct nftnl_exprcxx . . $ nftnl_expr# *e, struct nlattrcxx . . $ nlattr# *attr)
100 {
101 struct nftnl_expr_socketcxx . . $ nftnl_expr_socket# *socket = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
102 struct nlattrcxx . . $ nlattr# *tb[NFTA_SOCKET_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:1108:9`!+1] = {};
103
104 if (mnl_attr_parse_nestedcxx . . $ mnl_attr_parse_nested(f99c0e9fa94f99d9).(attr, nftnl_expr_socket_cbcxx . . $ nftnl_expr_socket_cb(de2ef18c48fd675c)., tb) < 0)
105 return -1;
106
107 if (tb[NFTA_SOCKET_KEYcxx . . $ NFTA_SOCKET_KEY.]) {
108 socket->keycxx . . $ nftnl_expr_socket#key. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_SOCKET_KEY]));
109 e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_SOCKET_KEYcxx . . $ NFTNL_EXPR_SOCKET_KEY.);
110 }
111 if (tb[NFTA_SOCKET_DREGcxx . . $ NFTA_SOCKET_DREG.]) {
112 socket->dregcxx . . $ nftnl_expr_socket#dreg. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_SOCKET_DREG]));
113 e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_SOCKET_DREGcxx . . $ NFTNL_EXPR_SOCKET_DREG.);
114 }
115 if (tb[NFTA_SOCKET_LEVELcxx . . $ NFTA_SOCKET_LEVEL.]) {
116 socket->levelcxx . . $ nftnl_expr_socket#level. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_SOCKET_LEVEL]));
117 e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_SOCKET_LEVELcxx . . $ NFTNL_EXPR_SOCKET_LEVEL.);
118 }
119
120 return 0;
121 }
122
123 static const char *socket_key2str_arraycxx . . $ socket_key2str_array.[NFT_SOCKET_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:1125:9`! + 1] = {
124 [NFT_SOCKET_TRANSPARENTcxx . . $ NFT_SOCKET_TRANSPARENT.] = "transparent",
125 [NFT_SOCKET_MARKcxx . . $ NFT_SOCKET_MARK.] = "mark",
126 [NFT_SOCKET_WILDCARDcxx . . $ NFT_SOCKET_WILDCARD.] = "wildcard",
127 [NFT_SOCKET_CGROUPV2cxx . . $ NFT_SOCKET_CGROUPV2.] = "cgroupv2",
128 };
129
130 static const char *socket_key2strcxx . . $ socket_key2str(42624c5f3be67010).(uint8_tcxx . . $ uint8_t# key)
131 {
132 if (key < NFT_SOCKET_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:1125:9`! + 1)
133 return socket_key2str_arraycxx . . $ socket_key2str_array.[key];
134
135 return "unknown";
136 }
137
138 static int
139 nftnl_expr_socket_snprintfcxx . . $ nftnl_expr_socket_snprintf(2b4af69501aef780).(char *buf, size_t len,
140 uint32_tcxx . . $ uint32_t# flags, const struct nftnl_exprcxx . . $ nftnl_expr# *e)
141 {
142 struct nftnl_expr_socketcxx . . $ nftnl_expr_socket# *socket = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
143
144 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_SOCKET_DREGcxx . . $ NFTNL_EXPR_SOCKET_DREG.)) {
145 return snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, len, "load %s => reg %u ",
146 socket_key2strcxx . . $ socket_key2str(42624c5f3be67010).(socket->keycxx . . $ nftnl_expr_socket#key.), socket->dregcxx . . $ nftnl_expr_socket#dreg.);
147 }
148 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_SOCKET_LEVELcxx . . $ NFTNL_EXPR_SOCKET_LEVEL.))
149 return snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, len, "level %u ", socket->levelcxx . . $ nftnl_expr_socket#level.);
150
151 return 0;
152 }
153
154 static struct attr_policycxx . . $ attr_policy# socket_attr_policycxx . . $ socket_attr_policy.[__NFTNL_EXPR_SOCKET_MAXcxx . . $ __NFTNL_EXPR_SOCKET_MAX.] = {
155 [NFTNL_EXPR_SOCKET_KEYcxx . . $ NFTNL_EXPR_SOCKET_KEY.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) },
156 [NFTNL_EXPR_SOCKET_DREGcxx . . $ NFTNL_EXPR_SOCKET_DREG.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) },
157 [NFTNL_EXPR_SOCKET_LEVELcxx . . $ NFTNL_EXPR_SOCKET_LEVEL.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) },
158 };
159
160 struct expr_opscxx . . $ expr_ops# expr_ops_socketcxx . . $ expr_ops_socket. = {
161 .name = "socket",
162 .alloc_len = sizeof(struct nftnl_expr_socketcxx . . $ nftnl_expr_socket#),
163 .nftnl_max_attr = __NFTNL_EXPR_SOCKET_MAXcxx . . $ __NFTNL_EXPR_SOCKET_MAX. - 1,
164 .attr_policy = socket_attr_policycxx . . $ socket_attr_policy.,
165 .set = nftnl_expr_socket_setcxx . . $ nftnl_expr_socket_set(211371a3e3842127).,
166 .get = nftnl_expr_socket_getcxx . . $ nftnl_expr_socket_get(9de263c4f9e71c4b).,
167 .parse = nftnl_expr_socket_parsecxx . . $ nftnl_expr_socket_parse(ed61207049d8af9a).,
168 .build = nftnl_expr_socket_buildcxx . . $ nftnl_expr_socket_build(93c7528a9cf90b41).,
169 .output = nftnl_expr_socket_snprintfcxx . . $ nftnl_expr_socket_snprintf(2b4af69501aef780).,
170 };