Debian Codegraph @

src/expr/match.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */cxx . . $ `<file>/src/expr/match.c`/
2 /*
3 * (C) 2012 by Pablo Neira Ayuso <[email protected]>
4 *
5 * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
6 */
7
8 #include "internal.h"cxx . . $ `<file>/include/internal.h`/
9
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <string.h> /* for memcpy */
13 #include <arpa/inet.h>
14 #include <errno.h>
15 #include <libmnl/libmnl.h>
16
17 #include <linux/netfilter/nf_tables.h>cxx . . $ `<file>/include/linux/netfilter/nf_tables.h`/
18 #include <linux/netfilter/nf_tables_compat.h>cxx . . $ `<file>/include/linux/netfilter/nf_tables_compat.h`/
19
20 #include <libnftnl/expr.h>cxx . . $ `<file>/include/libnftnl/expr.h`/
21 #include <libnftnl/rule.h>cxx . . $ `<file>/include/libnftnl/rule.h`/
22
23 /* From include/linux/netfilter/x_tables.h */
24 #define XT_EXTENSION_MAXNAMELENcxx . . $ `src/expr/match.c:24:9`! 29
25
26 struct nftnl_expr_matchcxx . . $ nftnl_expr_match# {
27 char namecxx . . $ nftnl_expr_match#name.[XT_EXTENSION_MAXNAMELENcxx . . $ `src/expr/match.c:24:9`!];
28 uint32_tcxx . . $ uint32_t# revcxx . . $ nftnl_expr_match#rev.;
29 uint32_tcxx . . $ uint32_t# data_lencxx . . $ nftnl_expr_match#data_len.;
30 const void *datacxx . . $ nftnl_expr_match#data.;
31 };
32
33 static int
34 nftnl_expr_match_setcxx . . $ nftnl_expr_match_set(211371a3e3842127).(struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type,
35 const void *data, uint32_tcxx . . $ uint32_t# data_len, uint32_tcxx . . $ uint32_t# byteorder)
36 {
37 struct nftnl_expr_matchcxx . . $ nftnl_expr_match# *mt = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
38
39 switch(type) {
40 case NFTNL_EXPR_MT_NAMEcxx . . $ NFTNL_EXPR_MT_NAME.:
41 snprintfcxx . . $ snprintf(ea502d73e74c754c).(mt->namecxx . . $ nftnl_expr_match#name., sizeof(mt->namecxx . . $ nftnl_expr_match#name.), "%.*s", data_len,
42 (const char *)data);
43 break;
44 case NFTNL_EXPR_MT_REVcxx . . $ NFTNL_EXPR_MT_REV.:
45 memcpycxx . . $ memcpy(cc817cb175f282d).(&mt->revcxx . . $ nftnl_expr_match#rev., data, data_len);
46 break;
47 case NFTNL_EXPR_MT_INFOcxx . . $ NFTNL_EXPR_MT_INFO.:
48 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_MT_INFOcxx . . $ NFTNL_EXPR_MT_INFO.))
49 xfreecxx . . $ `include/utils.h:22:9`!(mt->data);
50
51 mt->datacxx . . $ nftnl_expr_match#data. = data;
52 mt->data_lencxx . . $ nftnl_expr_match#data_len. = data_len;
53 break;
54 }
55 return 0;
56 }
57
58 static const void *
59 nftnl_expr_match_getcxx . . $ nftnl_expr_match_get(9de263c4f9e71c4b).(const struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type,
60 uint32_tcxx . . $ uint32_t# *data_len)
61 {
62 struct nftnl_expr_matchcxx . . $ nftnl_expr_match# *mt = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
63
64 switch(type) {
65 case NFTNL_EXPR_MT_NAMEcxx . . $ NFTNL_EXPR_MT_NAME.:
66 *data_len = sizeof(mt->namecxx . . $ nftnl_expr_match#name.);
67 return mt->namecxx . . $ nftnl_expr_match#name.;
68 case NFTNL_EXPR_MT_REVcxx . . $ NFTNL_EXPR_MT_REV.:
69 *data_len = sizeof(mt->revcxx . . $ nftnl_expr_match#rev.);
70 return &mt->revcxx . . $ nftnl_expr_match#rev.;
71 case NFTNL_EXPR_MT_INFOcxx . . $ NFTNL_EXPR_MT_INFO.:
72 *data_len = mt->data_lencxx . . $ nftnl_expr_match#data_len.;
73 return mt->datacxx . . $ nftnl_expr_match#data.;
74 }
75 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
76 }
77
78 static int nftnl_expr_match_cbcxx . . $ nftnl_expr_match_cb(de2ef18c48fd675c).(const struct nlattrcxx . . $ nlattr# *attr, void *data)
79 {
80 const struct nlattrcxx . . $ nlattr# **tb = data;
81 int type = mnl_attr_get_typecxx . . $ mnl_attr_get_type(1da47bcc4c4b975).(attr);
82
83 if (mnl_attr_type_validcxx . . $ mnl_attr_type_valid(47df1cce1c7be855).(attr, NFTA_MATCH_MAXcxx . . $ `include/linux/netfilter/nf_tables_compat.h:32:9`!) < 0)
84 return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!;
85
86 switch(type) {
87 case NFTA_MATCH_NAMEcxx . . $ NFTA_MATCH_NAME.:
88 if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_NUL_STRINGcxx . . $ MNL_TYPE_NUL_STRING.) < 0)
89 abi_breakagecxx . . $ `include/utils.h:28:9`!();
90 break;
91 case NFTA_MATCH_REVcxx . . $ NFTA_MATCH_REV.:
92 if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_U32cxx . . $ MNL_TYPE_U32.) < 0)
93 abi_breakagecxx . . $ `include/utils.h:28:9`!();
94 break;
95 case NFTA_MATCH_INFOcxx . . $ NFTA_MATCH_INFO.:
96 if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_BINARYcxx . . $ MNL_TYPE_BINARY.) < 0)
97 abi_breakagecxx . . $ `include/utils.h:28:9`!();
98 break;
99 }
100
101 tb[type] = attr;
102 return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!;
103 }
104
105 static void
106 nftnl_expr_match_buildcxx . . $ nftnl_expr_match_build(93c7528a9cf90b41).(struct nlmsghdrcxx . . $ nlmsghdr# *nlh, const struct nftnl_exprcxx . . $ nftnl_expr# *e)
107 {
108 struct nftnl_expr_matchcxx . . $ nftnl_expr_match# *mt = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
109
110 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_MT_NAMEcxx . . $ NFTNL_EXPR_MT_NAME.))
111 mnl_attr_put_strzcxx . . $ mnl_attr_put_strz(eb76286d4bfc1a8d).(nlh, NFTA_MATCH_NAMEcxx . . $ NFTA_MATCH_NAME., mt->namecxx . . $ nftnl_expr_match#name.);
112 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_MT_REVcxx . . $ NFTNL_EXPR_MT_REV.))
113 mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_MATCH_REVcxx . . $ NFTA_MATCH_REV., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(mt->rev));
114 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_MT_INFOcxx . . $ NFTNL_EXPR_MT_INFO.))
115 mnl_attr_putcxx . . $ mnl_attr_put(612d908c39ff741d).(nlh, NFTA_MATCH_INFOcxx . . $ NFTA_MATCH_INFO., mt->data_lencxx . . $ nftnl_expr_match#data_len., mt->datacxx . . $ nftnl_expr_match#data.);
116 }
117
118 static int nftnl_expr_match_parsecxx . . $ nftnl_expr_match_parse(ed61207049d8af9a).(struct nftnl_exprcxx . . $ nftnl_expr# *e, struct nlattrcxx . . $ nlattr# *attr)
119 {
120 struct nftnl_expr_matchcxx . . $ nftnl_expr_match# *match = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
121 struct nlattrcxx . . $ nlattr# *tb[NFTA_MATCH_MAXcxx . . $ `include/linux/netfilter/nf_tables_compat.h:32:9`!+1] = {};
122
123 if (mnl_attr_parse_nestedcxx . . $ mnl_attr_parse_nested(f99c0e9fa94f99d9).(attr, nftnl_expr_match_cbcxx . . $ nftnl_expr_match_cb(de2ef18c48fd675c)., tb) < 0)
124 return -1;
125
126 if (tb[NFTA_MATCH_NAMEcxx . . $ NFTA_MATCH_NAME.]) {
127 snprintfcxx . . $ snprintf(ea502d73e74c754c).(match->namecxx . . $ nftnl_expr_match#name., XT_EXTENSION_MAXNAMELENcxx . . $ `src/expr/match.c:24:9`!, "%s",
128 mnl_attr_get_strcxx . . $ mnl_attr_get_str(c24cebeef6d84d59).(tb[NFTA_MATCH_NAMEcxx . . $ NFTA_MATCH_NAME.]));
129
130 match->namecxx . . $ nftnl_expr_match#name.[XT_EXTENSION_MAXNAMELENcxx . . $ `src/expr/match.c:24:9`!-1] = '\0';
131 e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_MT_NAMEcxx . . $ NFTNL_EXPR_MT_NAME.);
132 }
133
134 if (tb[NFTA_MATCH_REVcxx . . $ NFTA_MATCH_REV.]) {
135 match->revcxx . . $ nftnl_expr_match#rev. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_MATCH_REV]));
136 e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_MT_REVcxx . . $ NFTNL_EXPR_MT_REV.);
137 }
138
139 if (tb[NFTA_MATCH_INFOcxx . . $ NFTA_MATCH_INFO.]) {
140 uint32_tcxx . . $ uint32_t# len = mnl_attr_get_payload_lencxx . . $ mnl_attr_get_payload_len(1da47bcc4c4b975).(tb[NFTA_MATCH_INFOcxx . . $ NFTA_MATCH_INFO.]);
141 void *match_data;
142
143 if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_MT_INFOcxx . . $ NFTNL_EXPR_MT_INFO.))
144 xfreecxx . . $ `include/utils.h:22:9`!(match->data);
145
146 match_data = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, len);
147 if (match_data == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
148 return -1;
149
150 memcpycxx . . $ memcpy(cc817cb175f282d).(match_data, mnl_attr_get_payloadcxx . . $ mnl_attr_get_payload(cbbcdcb61d3bc7e8).(tb[NFTA_MATCH_INFOcxx . . $ NFTA_MATCH_INFO.]), len);
151
152 match->datacxx . . $ nftnl_expr_match#data. = match_data;
153 match->data_lencxx . . $ nftnl_expr_match#data_len. = len;
154
155 e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_MT_INFOcxx . . $ NFTNL_EXPR_MT_INFO.);
156 }
157
158 return 0;
159 }
160
161 static int
162 nftnl_expr_match_snprintfcxx . . $ nftnl_expr_match_snprintf(2b4af69501aef780).(char *buf, size_t len,
163 uint32_tcxx . . $ uint32_t# flags, const struct nftnl_exprcxx . . $ nftnl_expr# *e)
164 {
165 struct nftnl_expr_matchcxx . . $ nftnl_expr_match# *match = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
166
167 return snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, len, "name %s rev %u ", match->namecxx . . $ nftnl_expr_match#name., match->revcxx . . $ nftnl_expr_match#rev.);
168 }
169
170 static void nftnl_expr_match_freecxx . . $ nftnl_expr_match_free(cbe1a28527736226).(const struct nftnl_exprcxx . . $ nftnl_expr# *e)
171 {
172 struct nftnl_expr_matchcxx . . $ nftnl_expr_match# *match = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e);
173
174 xfreecxx . . $ `include/utils.h:22:9`!(match->data);
175 }
176
177 static struct attr_policycxx . . $ attr_policy# match_attr_policycxx . . $ match_attr_policy.[__NFTNL_EXPR_MT_MAXcxx . . $ __NFTNL_EXPR_MT_MAX.] = {
178 [NFTNL_EXPR_MT_NAMEcxx . . $ NFTNL_EXPR_MT_NAME.] = { .maxlen = XT_EXTENSION_MAXNAMELENcxx . . $ `src/expr/match.c:24:9`! },
179 [NFTNL_EXPR_MT_REVcxx . . $ NFTNL_EXPR_MT_REV.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) },
180 [NFTNL_EXPR_MT_INFOcxx . . $ NFTNL_EXPR_MT_INFO.] = { .maxlen = 0 },
181 };
182
183 struct expr_opscxx . . $ expr_ops# expr_ops_matchcxx . . $ expr_ops_match. = {
184 .name = "match",
185 .alloc_len = sizeof(struct nftnl_expr_matchcxx . . $ nftnl_expr_match#),
186 .nftnl_max_attr = __NFTNL_EXPR_MT_MAXcxx . . $ __NFTNL_EXPR_MT_MAX. - 1,
187 .attr_policy = match_attr_policycxx . . $ match_attr_policy.,
188 .free = nftnl_expr_match_freecxx . . $ nftnl_expr_match_free(cbe1a28527736226).,
189 .set = nftnl_expr_match_setcxx . . $ nftnl_expr_match_set(211371a3e3842127).,
190 .get = nftnl_expr_match_getcxx . . $ nftnl_expr_match_get(9de263c4f9e71c4b).,
191 .parse = nftnl_expr_match_parsecxx . . $ nftnl_expr_match_parse(ed61207049d8af9a).,
192 .build = nftnl_expr_match_buildcxx . . $ nftnl_expr_match_build(93c7528a9cf90b41).,
193 .output = nftnl_expr_match_snprintfcxx . . $ nftnl_expr_match_snprintf(2b4af69501aef780).,
194 };