| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */cxx . . $ `<file>/src/expr/target.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/target.c:24:9`! 29 |
| 25 | |
| 26 | struct nftnl_expr_targetcxx . . $ nftnl_expr_target# { |
| 27 | char namecxx . . $ nftnl_expr_target#name.[XT_EXTENSION_MAXNAMELENcxx . . $ `src/expr/target.c:24:9`!]; |
| 28 | uint32_tcxx . . $ uint32_t# revcxx . . $ nftnl_expr_target#rev.; |
| 29 | uint32_tcxx . . $ uint32_t# data_lencxx . . $ nftnl_expr_target#data_len.; |
| 30 | const void *datacxx . . $ nftnl_expr_target#data.; |
| 31 | }; |
| 32 | |
| 33 | static int |
| 34 | nftnl_expr_target_setcxx . . $ nftnl_expr_target_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_targetcxx . . $ nftnl_expr_target# *tg = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 38 | |
| 39 | switch(type) { |
| 40 | case NFTNL_EXPR_TG_NAMEcxx . . $ NFTNL_EXPR_TG_NAME.: |
| 41 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(tg->namecxx . . $ nftnl_expr_target#name., sizeof(tg->namecxx . . $ nftnl_expr_target#name.), "%.*s", data_len, |
| 42 | (const char *) data); |
| 43 | break; |
| 44 | case NFTNL_EXPR_TG_REVcxx . . $ NFTNL_EXPR_TG_REV.: |
| 45 | memcpycxx . . $ memcpy(cc817cb175f282d).(&tg->revcxx . . $ nftnl_expr_target#rev., data, data_len); |
| 46 | break; |
| 47 | case NFTNL_EXPR_TG_INFOcxx . . $ NFTNL_EXPR_TG_INFO.: |
| 48 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_TG_INFOcxx . . $ NFTNL_EXPR_TG_INFO.)) |
| 49 | xfreecxx . . $ `include/utils.h:22:9`!(tg->data); |
| 50 | |
| 51 | tg->datacxx . . $ nftnl_expr_target#data. = data; |
| 52 | tg->data_lencxx . . $ nftnl_expr_target#data_len. = data_len; |
| 53 | break; |
| 54 | } |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static const void * |
| 59 | nftnl_expr_target_getcxx . . $ nftnl_expr_target_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_targetcxx . . $ nftnl_expr_target# *tg = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 63 | |
| 64 | switch(type) { |
| 65 | case NFTNL_EXPR_TG_NAMEcxx . . $ NFTNL_EXPR_TG_NAME.: |
| 66 | *data_len = sizeof(tg->namecxx . . $ nftnl_expr_target#name.); |
| 67 | return tg->namecxx . . $ nftnl_expr_target#name.; |
| 68 | case NFTNL_EXPR_TG_REVcxx . . $ NFTNL_EXPR_TG_REV.: |
| 69 | *data_len = sizeof(tg->revcxx . . $ nftnl_expr_target#rev.); |
| 70 | return &tg->revcxx . . $ nftnl_expr_target#rev.; |
| 71 | case NFTNL_EXPR_TG_INFOcxx . . $ NFTNL_EXPR_TG_INFO.: |
| 72 | *data_len = tg->data_lencxx . . $ nftnl_expr_target#data_len.; |
| 73 | return tg->datacxx . . $ nftnl_expr_target#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_target_cbcxx . . $ nftnl_expr_target_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_TARGET_MAXcxx . . $ `include/linux/netfilter/nf_tables_compat.h:17:9`!) < 0) |
| 84 | return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!; |
| 85 | |
| 86 | switch(type) { |
| 87 | case NFTA_TARGET_NAMEcxx . . $ NFTA_TARGET_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_TARGET_REVcxx . . $ NFTA_TARGET_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_TARGET_INFOcxx . . $ NFTA_TARGET_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_target_buildcxx . . $ nftnl_expr_target_build(93c7528a9cf90b41).(struct nlmsghdrcxx . . $ nlmsghdr# *nlh, const struct nftnl_exprcxx . . $ nftnl_expr# *e) |
| 107 | { |
| 108 | struct nftnl_expr_targetcxx . . $ nftnl_expr_target# *tg = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 109 | |
| 110 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_TG_NAMEcxx . . $ NFTNL_EXPR_TG_NAME.)) |
| 111 | mnl_attr_put_strzcxx . . $ mnl_attr_put_strz(eb76286d4bfc1a8d).(nlh, NFTA_TARGET_NAMEcxx . . $ NFTA_TARGET_NAME., tg->namecxx . . $ nftnl_expr_target#name.); |
| 112 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_TG_REVcxx . . $ NFTNL_EXPR_TG_REV.)) |
| 113 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_TARGET_REVcxx . . $ NFTA_TARGET_REV., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(tg->rev)); |
| 114 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_TG_INFOcxx . . $ NFTNL_EXPR_TG_INFO.)) |
| 115 | mnl_attr_putcxx . . $ mnl_attr_put(612d908c39ff741d).(nlh, NFTA_TARGET_INFOcxx . . $ NFTA_TARGET_INFO., tg->data_lencxx . . $ nftnl_expr_target#data_len., tg->datacxx . . $ nftnl_expr_target#data.); |
| 116 | } |
| 117 | |
| 118 | static int nftnl_expr_target_parsecxx . . $ nftnl_expr_target_parse(ed61207049d8af9a).(struct nftnl_exprcxx . . $ nftnl_expr# *e, struct nlattrcxx . . $ nlattr# *attr) |
| 119 | { |
| 120 | struct nftnl_expr_targetcxx . . $ nftnl_expr_target# *target = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 121 | struct nlattrcxx . . $ nlattr# *tb[NFTA_TARGET_MAXcxx . . $ `include/linux/netfilter/nf_tables_compat.h:17:9`!+1] = {}; |
| 122 | |
| 123 | if (mnl_attr_parse_nestedcxx . . $ mnl_attr_parse_nested(f99c0e9fa94f99d9).(attr, nftnl_expr_target_cbcxx . . $ nftnl_expr_target_cb(de2ef18c48fd675c)., tb) < 0) |
| 124 | return -1; |
| 125 | |
| 126 | if (tb[NFTA_TARGET_NAMEcxx . . $ NFTA_TARGET_NAME.]) { |
| 127 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(target->namecxx . . $ nftnl_expr_target#name., XT_EXTENSION_MAXNAMELENcxx . . $ `src/expr/target.c:24:9`!, "%s", |
| 128 | mnl_attr_get_strcxx . . $ mnl_attr_get_str(c24cebeef6d84d59).(tb[NFTA_TARGET_NAMEcxx . . $ NFTA_TARGET_NAME.])); |
| 129 | |
| 130 | target->namecxx . . $ nftnl_expr_target#name.[XT_EXTENSION_MAXNAMELENcxx . . $ `src/expr/target.c:24:9`!-1] = '\0'; |
| 131 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_TG_NAMEcxx . . $ NFTNL_EXPR_TG_NAME.); |
| 132 | } |
| 133 | |
| 134 | if (tb[NFTA_TARGET_REVcxx . . $ NFTA_TARGET_REV.]) { |
| 135 | target->revcxx . . $ nftnl_expr_target#rev. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_TARGET_REV])); |
| 136 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_TG_REVcxx . . $ NFTNL_EXPR_TG_REV.); |
| 137 | } |
| 138 | |
| 139 | if (tb[NFTA_TARGET_INFOcxx . . $ NFTA_TARGET_INFO.]) { |
| 140 | uint32_tcxx . . $ uint32_t# len = mnl_attr_get_payload_lencxx . . $ mnl_attr_get_payload_len(1da47bcc4c4b975).(tb[NFTA_TARGET_INFOcxx . . $ NFTA_TARGET_INFO.]); |
| 141 | void *target_data; |
| 142 | |
| 143 | if (target->datacxx . . $ nftnl_expr_target#data.) |
| 144 | xfreecxx . . $ `include/utils.h:22:9`!(target->data); |
| 145 | |
| 146 | target_data = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, len); |
| 147 | if (target_data == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) |
| 148 | return -1; |
| 149 | |
| 150 | memcpycxx . . $ memcpy(cc817cb175f282d).(target_data, mnl_attr_get_payloadcxx . . $ mnl_attr_get_payload(cbbcdcb61d3bc7e8).(tb[NFTA_TARGET_INFOcxx . . $ NFTA_TARGET_INFO.]), len); |
| 151 | |
| 152 | target->datacxx . . $ nftnl_expr_target#data. = target_data; |
| 153 | target->data_lencxx . . $ nftnl_expr_target#data_len. = len; |
| 154 | |
| 155 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_TG_INFOcxx . . $ NFTNL_EXPR_TG_INFO.); |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static int |
| 162 | nftnl_expr_target_snprintfcxx . . $ nftnl_expr_target_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_targetcxx . . $ nftnl_expr_target# *target = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 166 | |
| 167 | return snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, len, "name %s rev %u ", target->namecxx . . $ nftnl_expr_target#name., target->revcxx . . $ nftnl_expr_target#rev.); |
| 168 | } |
| 169 | |
| 170 | static void nftnl_expr_target_freecxx . . $ nftnl_expr_target_free(cbe1a28527736226).(const struct nftnl_exprcxx . . $ nftnl_expr# *e) |
| 171 | { |
| 172 | struct nftnl_expr_targetcxx . . $ nftnl_expr_target# *target = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 173 | |
| 174 | xfreecxx . . $ `include/utils.h:22:9`!(target->data); |
| 175 | } |
| 176 | |
| 177 | static struct attr_policycxx . . $ attr_policy# target_attr_policycxx . . $ target_attr_policy.[__NFTNL_EXPR_TG_MAXcxx . . $ __NFTNL_EXPR_TG_MAX.] = { |
| 178 | [NFTNL_EXPR_TG_NAMEcxx . . $ NFTNL_EXPR_TG_NAME.] = { .maxlen = XT_EXTENSION_MAXNAMELENcxx . . $ `src/expr/target.c:24:9`! }, |
| 179 | [NFTNL_EXPR_TG_REVcxx . . $ NFTNL_EXPR_TG_REV.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 180 | [NFTNL_EXPR_TG_INFOcxx . . $ NFTNL_EXPR_TG_INFO.] = { .maxlen = 0 }, |
| 181 | }; |
| 182 | |
| 183 | struct expr_opscxx . . $ expr_ops# expr_ops_targetcxx . . $ expr_ops_target. = { |
| 184 | .name = "target", |
| 185 | .alloc_len = sizeof(struct nftnl_expr_targetcxx . . $ nftnl_expr_target#), |
| 186 | .nftnl_max_attr = __NFTNL_EXPR_TG_MAXcxx . . $ __NFTNL_EXPR_TG_MAX. - 1, |
| 187 | .attr_policy = target_attr_policycxx . . $ target_attr_policy., |
| 188 | .free = nftnl_expr_target_freecxx . . $ nftnl_expr_target_free(cbe1a28527736226)., |
| 189 | .set = nftnl_expr_target_setcxx . . $ nftnl_expr_target_set(211371a3e3842127)., |
| 190 | .get = nftnl_expr_target_getcxx . . $ nftnl_expr_target_get(9de263c4f9e71c4b)., |
| 191 | .parse = nftnl_expr_target_parsecxx . . $ nftnl_expr_target_parse(ed61207049d8af9a)., |
| 192 | .build = nftnl_expr_target_buildcxx . . $ nftnl_expr_target_build(93c7528a9cf90b41)., |
| 193 | .output = nftnl_expr_target_snprintfcxx . . $ nftnl_expr_target_snprintf(2b4af69501aef780)., |
| 194 | }; |