| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */cxx . . $ `<file>/src/expr/inner.c`/ |
| 2 | /* |
| 3 | * (C) 2012-2022 by Pablo Neira Ayuso <[email protected]> |
| 4 | */ |
| 5 | |
| 6 | #include "internal.h"cxx . . $ `<file>/include/internal.h`/ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdint.h> |
| 10 | #include <string.h> |
| 11 | #include <limits.h> |
| 12 | #include <arpa/inet.h> |
| 13 | #include <errno.h> |
| 14 | #include <libmnl/libmnl.h> |
| 15 | |
| 16 | #include <linux/netfilter/nf_tables.h>cxx . . $ `<file>/include/linux/netfilter/nf_tables.h`/ |
| 17 | |
| 18 | #include <libnftnl/expr.h>cxx . . $ `<file>/include/libnftnl/expr.h`/ |
| 19 | #include <libnftnl/rule.h>cxx . . $ `<file>/include/libnftnl/rule.h`/ |
| 20 | |
| 21 | struct nftnl_expr_innercxx . . $ nftnl_expr_inner# { |
| 22 | uint32_tcxx . . $ uint32_t# typecxx . . $ nftnl_expr_inner#type.; |
| 23 | uint32_tcxx . . $ uint32_t# flagscxx . . $ nftnl_expr_inner#flags.; |
| 24 | uint32_tcxx . . $ uint32_t# hdrsizecxx . . $ nftnl_expr_inner#hdrsize.; |
| 25 | struct nftnl_exprcxx . . $ nftnl_expr# *exprcxx . . $ nftnl_expr_inner#expr.; |
| 26 | }; |
| 27 | |
| 28 | static void nftnl_expr_inner_freecxx . . $ nftnl_expr_inner_free(cbe1a28527736226).(const struct nftnl_exprcxx . . $ nftnl_expr# *e) |
| 29 | { |
| 30 | struct nftnl_expr_innercxx . . $ nftnl_expr_inner# *inner = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 31 | |
| 32 | if (inner->exprcxx . . $ nftnl_expr_inner#expr.) |
| 33 | nftnl_expr_freecxx . . $ nftnl_expr_free(cbe1a28527736226).(inner->exprcxx . . $ nftnl_expr_inner#expr.); |
| 34 | } |
| 35 | |
| 36 | static int |
| 37 | nftnl_expr_inner_setcxx . . $ nftnl_expr_inner_set(211371a3e3842127).(struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type, |
| 38 | const void *data, uint32_tcxx . . $ uint32_t# data_len, uint32_tcxx . . $ uint32_t# byteorder) |
| 39 | { |
| 40 | struct nftnl_expr_innercxx . . $ nftnl_expr_inner# *inner = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 41 | |
| 42 | switch(type) { |
| 43 | case NFTNL_EXPR_INNER_TYPEcxx . . $ NFTNL_EXPR_INNER_TYPE.: |
| 44 | memcpycxx . . $ memcpy(cc817cb175f282d).(&inner->typecxx . . $ nftnl_expr_inner#type., data, data_len); |
| 45 | break; |
| 46 | case NFTNL_EXPR_INNER_FLAGScxx . . $ NFTNL_EXPR_INNER_FLAGS.: |
| 47 | memcpycxx . . $ memcpy(cc817cb175f282d).(&inner->flagscxx . . $ nftnl_expr_inner#flags., data, data_len); |
| 48 | break; |
| 49 | case NFTNL_EXPR_INNER_HDRSIZEcxx . . $ NFTNL_EXPR_INNER_HDRSIZE.: |
| 50 | memcpycxx . . $ memcpy(cc817cb175f282d).(&inner->hdrsizecxx . . $ nftnl_expr_inner#hdrsize., data, data_len); |
| 51 | break; |
| 52 | case NFTNL_EXPR_INNER_EXPRcxx . . $ NFTNL_EXPR_INNER_EXPR.: |
| 53 | if (inner->exprcxx . . $ nftnl_expr_inner#expr.) |
| 54 | nftnl_expr_freecxx . . $ nftnl_expr_free(cbe1a28527736226).(inner->exprcxx . . $ nftnl_expr_inner#expr.); |
| 55 | |
| 56 | inner->exprcxx . . $ nftnl_expr_inner#expr. = (void *)data; |
| 57 | break; |
| 58 | } |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static const void * |
| 63 | nftnl_expr_inner_getcxx . . $ nftnl_expr_inner_get(9de263c4f9e71c4b).(const struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type, |
| 64 | uint32_tcxx . . $ uint32_t# *data_len) |
| 65 | { |
| 66 | struct nftnl_expr_innercxx . . $ nftnl_expr_inner# *inner = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 67 | |
| 68 | switch(type) { |
| 69 | case NFTNL_EXPR_INNER_FLAGScxx . . $ NFTNL_EXPR_INNER_FLAGS.: |
| 70 | *data_len = sizeof(inner->flagscxx . . $ nftnl_expr_inner#flags.); |
| 71 | return &inner->flagscxx . . $ nftnl_expr_inner#flags.; |
| 72 | case NFTNL_EXPR_INNER_TYPEcxx . . $ NFTNL_EXPR_INNER_TYPE.: |
| 73 | *data_len = sizeof(inner->typecxx . . $ nftnl_expr_inner#type.); |
| 74 | return &inner->typecxx . . $ nftnl_expr_inner#type.; |
| 75 | case NFTNL_EXPR_INNER_HDRSIZEcxx . . $ NFTNL_EXPR_INNER_HDRSIZE.: |
| 76 | *data_len = sizeof(inner->hdrsizecxx . . $ nftnl_expr_inner#hdrsize.); |
| 77 | return &inner->hdrsizecxx . . $ nftnl_expr_inner#hdrsize.; |
| 78 | case NFTNL_EXPR_INNER_EXPRcxx . . $ NFTNL_EXPR_INNER_EXPR.: |
| 79 | return inner->exprcxx . . $ nftnl_expr_inner#expr.; |
| 80 | } |
| 81 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 82 | } |
| 83 | |
| 84 | static void |
| 85 | nftnl_expr_inner_buildcxx . . $ nftnl_expr_inner_build(93c7528a9cf90b41).(struct nlmsghdrcxx . . $ nlmsghdr# *nlh, const struct nftnl_exprcxx . . $ nftnl_expr# *e) |
| 86 | { |
| 87 | struct nftnl_expr_innercxx . . $ nftnl_expr_inner# *inner = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 88 | struct nlattrcxx . . $ nlattr# *nest; |
| 89 | |
| 90 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_INNER_NUMcxx . . $ NFTA_INNER_NUM., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(0)); |
| 91 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_INNER_TYPEcxx . . $ NFTNL_EXPR_INNER_TYPE.)) |
| 92 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_INNER_TYPEcxx . . $ NFTA_INNER_TYPE., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(inner->type)); |
| 93 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_INNER_FLAGScxx . . $ NFTNL_EXPR_INNER_FLAGS.)) |
| 94 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_INNER_FLAGScxx . . $ NFTA_INNER_FLAGS., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(inner->flags)); |
| 95 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_INNER_HDRSIZEcxx . . $ NFTNL_EXPR_INNER_HDRSIZE.)) |
| 96 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_INNER_HDRSIZEcxx . . $ NFTA_INNER_HDRSIZE., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(inner->hdrsize)); |
| 97 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_INNER_EXPRcxx . . $ NFTNL_EXPR_INNER_EXPR.)) { |
| 98 | nest = mnl_attr_nest_startcxx . . $ mnl_attr_nest_start(c8ed348a075426e7).(nlh, NFTA_INNER_EXPRcxx . . $ NFTA_INNER_EXPR.); |
| 99 | nftnl_expr_build_payloadcxx . . $ nftnl_expr_build_payload(efa2a5c645f672a6).(nlh, inner->exprcxx . . $ nftnl_expr_inner#expr.); |
| 100 | mnl_attr_nest_endcxx . . $ mnl_attr_nest_end(564f73625c62ba58).(nlh, nest); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | static int nftnl_inner_parse_cbcxx . . $ nftnl_inner_parse_cb(de2ef18c48fd675c).(const struct nlattrcxx . . $ nlattr# *attr, void *data) |
| 105 | { |
| 106 | const struct nlattrcxx . . $ nlattr# **tb = data; |
| 107 | int type = mnl_attr_get_typecxx . . $ mnl_attr_get_type(1da47bcc4c4b975).(attr); |
| 108 | |
| 109 | if (mnl_attr_type_validcxx . . $ mnl_attr_type_valid(47df1cce1c7be855).(attr, NFTA_INNER_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:848:9`!) < 0) |
| 110 | return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!; |
| 111 | |
| 112 | switch(type) { |
| 113 | case NFTA_INNER_NUMcxx . . $ NFTA_INNER_NUM.: |
| 114 | case NFTA_INNER_TYPEcxx . . $ NFTA_INNER_TYPE.: |
| 115 | case NFTA_INNER_HDRSIZEcxx . . $ NFTA_INNER_HDRSIZE.: |
| 116 | case NFTA_INNER_FLAGScxx . . $ NFTA_INNER_FLAGS.: |
| 117 | if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_U32cxx . . $ MNL_TYPE_U32.) < 0) |
| 118 | abi_breakagecxx . . $ `include/utils.h:28:9`!(); |
| 119 | break; |
| 120 | case NFTA_INNER_EXPRcxx . . $ NFTA_INNER_EXPR.: |
| 121 | if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_NESTEDcxx . . $ MNL_TYPE_NESTED.) < 0) |
| 122 | abi_breakagecxx . . $ `include/utils.h:28:9`!(); |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | tb[type] = attr; |
| 127 | |
| 128 | return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!; |
| 129 | } |
| 130 | |
| 131 | static int |
| 132 | nftnl_expr_inner_parsecxx . . $ nftnl_expr_inner_parse(ed61207049d8af9a).(struct nftnl_exprcxx . . $ nftnl_expr# *e, struct nlattrcxx . . $ nlattr# *attr) |
| 133 | { |
| 134 | struct nftnl_expr_innercxx . . $ nftnl_expr_inner# *inner = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 135 | struct nlattrcxx . . $ nlattr# *tb[NFTA_INNER_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:848:9`! + 1] = {}; |
| 136 | struct nftnl_exprcxx . . $ nftnl_expr# *expr; |
| 137 | int err; |
| 138 | |
| 139 | err = mnl_attr_parse_nestedcxx . . $ mnl_attr_parse_nested(f99c0e9fa94f99d9).(attr, nftnl_inner_parse_cbcxx . . $ nftnl_inner_parse_cb(de2ef18c48fd675c)., tb); |
| 140 | if (err < 0) |
| 141 | return err; |
| 142 | |
| 143 | if (tb[NFTA_INNER_HDRSIZEcxx . . $ NFTA_INNER_HDRSIZE.]) { |
| 144 | inner->hdrsizecxx . . $ nftnl_expr_inner#hdrsize. = |
| 145 | ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_INNER_HDRSIZE])); |
| 146 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_INNER_HDRSIZEcxx . . $ NFTNL_EXPR_INNER_HDRSIZE.); |
| 147 | } |
| 148 | if (tb[NFTA_INNER_FLAGScxx . . $ NFTA_INNER_FLAGS.]) { |
| 149 | inner->flagscxx . . $ nftnl_expr_inner#flags. = |
| 150 | ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_INNER_FLAGS])); |
| 151 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_INNER_FLAGScxx . . $ NFTNL_EXPR_INNER_FLAGS.); |
| 152 | } |
| 153 | if (tb[NFTA_INNER_TYPEcxx . . $ NFTA_INNER_TYPE.]) { |
| 154 | inner->typecxx . . $ nftnl_expr_inner#type. = |
| 155 | ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_INNER_TYPE])); |
| 156 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_INNER_TYPEcxx . . $ NFTNL_EXPR_INNER_TYPE.); |
| 157 | } |
| 158 | if (tb[NFTA_INNER_EXPRcxx . . $ NFTA_INNER_EXPR.]) { |
| 159 | expr = nftnl_expr_parsecxx . . $ nftnl_expr_parse(ea32a576744d0134).(tb[NFTA_INNER_EXPRcxx . . $ NFTA_INNER_EXPR.]); |
| 160 | if (!expr) |
| 161 | return -1; |
| 162 | |
| 163 | if (inner->exprcxx . . $ nftnl_expr_inner#expr.) |
| 164 | nftnl_expr_freecxx . . $ nftnl_expr_free(cbe1a28527736226).(inner->exprcxx . . $ nftnl_expr_inner#expr.); |
| 165 | |
| 166 | inner->exprcxx . . $ nftnl_expr_inner#expr. = expr; |
| 167 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_INNER_EXPRcxx . . $ NFTNL_EXPR_INNER_EXPR.); |
| 168 | } |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int |
| 174 | nftnl_expr_inner_snprintfcxx . . $ nftnl_expr_inner_snprintf(2b4af69501aef780).(char *buf, size_t remain, uint32_tcxx . . $ uint32_t# flags, |
| 175 | const struct nftnl_exprcxx . . $ nftnl_expr# *e) |
| 176 | { |
| 177 | struct nftnl_expr_innercxx . . $ nftnl_expr_inner# *inner = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 178 | uint32_tcxx . . $ uint32_t# offset = 0; |
| 179 | int ret; |
| 180 | |
| 181 | ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, remain, "type %u hdrsize %u flags %x [", |
| 182 | inner->typecxx . . $ nftnl_expr_inner#type., inner->hdrsizecxx . . $ nftnl_expr_inner#hdrsize., inner->flagscxx . . $ nftnl_expr_inner#flags.); |
| 183 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset); |
| 184 | |
| 185 | ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf + offset, remain, " %s ", inner->exprcxx . . $ nftnl_expr_inner#expr.->opscxx . . $ nftnl_expr#ops.->namecxx . . $ expr_ops#name.); |
| 186 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset); |
| 187 | |
| 188 | ret = nftnl_expr_snprintfcxx . . $ nftnl_expr_snprintf(7e053fe52193f124).(buf + offset, remain, inner->exprcxx . . $ nftnl_expr_inner#expr., |
| 189 | NFTNL_OUTPUT_DEFAULTcxx . . $ NFTNL_OUTPUT_DEFAULT., 0); |
| 190 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset); |
| 191 | |
| 192 | ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf + offset, remain, "] "); |
| 193 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset); |
| 194 | |
| 195 | return offset; |
| 196 | } |
| 197 | |
| 198 | static struct attr_policycxx . . $ attr_policy# inner_attr_policycxx . . $ inner_attr_policy.[__NFTNL_EXPR_INNER_MAXcxx . . $ __NFTNL_EXPR_INNER_MAX.] = { |
| 199 | [NFTNL_EXPR_INNER_TYPEcxx . . $ NFTNL_EXPR_INNER_TYPE.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 200 | [NFTNL_EXPR_INNER_FLAGScxx . . $ NFTNL_EXPR_INNER_FLAGS.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 201 | [NFTNL_EXPR_INNER_HDRSIZEcxx . . $ NFTNL_EXPR_INNER_HDRSIZE.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 202 | [NFTNL_EXPR_INNER_EXPRcxx . . $ NFTNL_EXPR_INNER_EXPR.] = { .maxlen = 0 }, |
| 203 | }; |
| 204 | |
| 205 | struct expr_opscxx . . $ expr_ops# expr_ops_innercxx . . $ expr_ops_inner. = { |
| 206 | .name = "inner", |
| 207 | .alloc_len = sizeof(struct nftnl_expr_innercxx . . $ nftnl_expr_inner#), |
| 208 | .nftnl_max_attr = __NFTNL_EXPR_INNER_MAXcxx . . $ __NFTNL_EXPR_INNER_MAX. - 1, |
| 209 | .attr_policy = inner_attr_policycxx . . $ inner_attr_policy., |
| 210 | .free = nftnl_expr_inner_freecxx . . $ nftnl_expr_inner_free(cbe1a28527736226)., |
| 211 | .set = nftnl_expr_inner_setcxx . . $ nftnl_expr_inner_set(211371a3e3842127)., |
| 212 | .get = nftnl_expr_inner_getcxx . . $ nftnl_expr_inner_get(9de263c4f9e71c4b)., |
| 213 | .parse = nftnl_expr_inner_parsecxx . . $ nftnl_expr_inner_parse(ed61207049d8af9a)., |
| 214 | .build = nftnl_expr_inner_buildcxx . . $ nftnl_expr_inner_build(93c7528a9cf90b41)., |
| 215 | .output = nftnl_expr_inner_snprintfcxx . . $ nftnl_expr_inner_snprintf(2b4af69501aef780)., |
| 216 | }; |