| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */cxx . . $ `<file>/src/expr/data_reg.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 <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 <netinet/in.h> |
| 15 | |
| 16 | #include <libmnl/libmnl.h> |
| 17 | #include <linux/netfilter.h>cxx . . $ `<file>/include/linux/netfilter.h`/ |
| 18 | #include <linux/netfilter/nf_tables.h>cxx . . $ `<file>/include/linux/netfilter/nf_tables.h`/ |
| 19 | #include <libnftnl/expr.h>cxx . . $ `<file>/include/libnftnl/expr.h`/ |
| 20 | #include <libnftnl/rule.h>cxx . . $ `<file>/include/libnftnl/rule.h`/ |
| 21 | #include "internal.h"cxx . . $ `<file>/include/internal.h`/ |
| 22 | |
| 23 | static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! big_endian_hostcxx . . $ big_endian_host(c9857a73d68e59ba).(void) |
| 24 | { |
| 25 | uint16_tcxx . . $ uint16_t# v = 1; |
| 26 | |
| 27 | return v == htonscxx . . $ `<external>/9581057695539925927/in.h:431:12`!(v); |
| 28 | } |
| 29 | |
| 30 | static int __reg_value_snprintfcxx . . $ __reg_value_snprintf(b8c13622a345ccf6).(char *buf, size_t remain, |
| 31 | uint8_tcxx . . $ uint8_t# *data, size_t datalen, |
| 32 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! reverse, const char *pfx) |
| 33 | { |
| 34 | int offset = 0, ret, i, idx; |
| 35 | const char *sep = ""; |
| 36 | |
| 37 | for (i = 0; i < datalen; i++) { |
| 38 | if ((i % 4) == 0) { |
| 39 | ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf + offset, remain, "%s%s", sep, pfx); |
| 40 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset); |
| 41 | sep = " "; |
| 42 | } |
| 43 | if (reverse) |
| 44 | idx = datalen - i - 1; |
| 45 | else |
| 46 | idx = i; |
| 47 | |
| 48 | ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf + offset, remain, "%.2x", data[idx]); |
| 49 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset); |
| 50 | } |
| 51 | |
| 52 | return offset; |
| 53 | } |
| 54 | |
| 55 | static int |
| 56 | nftnl_data_reg_value_snprintf_defaultcxx . . $ nftnl_data_reg_value_snprintf_default(a07d7734d8bed56).(char *buf, size_t remain, |
| 57 | const union nftnl_data_regcxx . . $ nftnl_data_reg# *reg, |
| 58 | uint32_tcxx . . $ uint32_t# flags) |
| 59 | { |
| 60 | uint32_tcxx . . $ uint32_t# byteorder = big_endian_hostcxx . . $ big_endian_host(c9857a73d68e59ba).() ? 0 : reg->byteordercxx . . $ nftnl_data_reg#$anonymous_type_2#byteorder.; |
| 61 | const char *pfx = flags & DATA_F_NOPFXcxx . . $ DATA_F_NOPFX. ? "" : "0x"; |
| 62 | int offset = 0, ret, i, pos = 0; |
| 63 | |
| 64 | for (i = 0; i < array_sizecxx . . $ `include/utils.h:69:9`!(reg->sizes); i++) { |
| 65 | int curlen = reg->sizescxx . . $ nftnl_data_reg#$anonymous_type_2#sizes.[i] ?: reg->lencxx . . $ nftnl_data_reg#$anonymous_type_2#len.; |
| 66 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! reverse = byteorder & (1 << i); |
| 67 | |
| 68 | if (i > 0) { |
| 69 | ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf + offset, remain, " . "); |
| 70 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset); |
| 71 | } |
| 72 | |
| 73 | ret = __reg_value_snprintfcxx . . $ __reg_value_snprintf(b8c13622a345ccf6).(buf + offset, remain, |
| 74 | (void *)®->valcxx . . $ nftnl_data_reg#$anonymous_type_2#val.[pos], |
| 75 | curlen, reverse, pfx); |
| 76 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset); |
| 77 | |
| 78 | pos += div_round_upcxx . . $ `include/utils.h:24:9`!(curlen, sizeof(uint32_t)); |
| 79 | if (pos >= reg->lencxx . . $ nftnl_data_reg#$anonymous_type_2#len. / sizeof(uint32_tcxx . . $ uint32_t#)) |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | return offset; |
| 84 | } |
| 85 | |
| 86 | static int |
| 87 | nftnl_data_reg_verdict_snprintf_defcxx . . $ nftnl_data_reg_verdict_snprintf_def(a07d7734d8bed56).(char *buf, size_t size, |
| 88 | const union nftnl_data_regcxx . . $ nftnl_data_reg# *reg, |
| 89 | uint32_tcxx . . $ uint32_t# flags) |
| 90 | { |
| 91 | int remain = size, offset = 0, ret = 0; |
| 92 | |
| 93 | ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, size, "%s", nftnl_verdict2strcxx . . $ nftnl_verdict2str(81a3ae90da26a711).(reg->verdictcxx . . $ nftnl_data_reg#$anonymous_type_3#verdict.)); |
| 94 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset); |
| 95 | |
| 96 | if (reg->chaincxx . . $ nftnl_data_reg#$anonymous_type_3#chain. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 97 | ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf + offset, remain, " -> %s", reg->chaincxx . . $ nftnl_data_reg#$anonymous_type_3#chain.); |
| 98 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset); |
| 99 | } |
| 100 | |
| 101 | return offset; |
| 102 | } |
| 103 | |
| 104 | int nftnl_data_reg_snprintfcxx . . $ nftnl_data_reg_snprintf(92088fbd4b6db60b).(char *buf, size_t size, |
| 105 | const union nftnl_data_regcxx . . $ nftnl_data_reg# *reg, |
| 106 | uint32_tcxx . . $ uint32_t# flags, int reg_type) |
| 107 | { |
| 108 | switch(reg_type) { |
| 109 | case DATA_VALUEcxx . . $ DATA_VALUE.: |
| 110 | return nftnl_data_reg_value_snprintf_defaultcxx . . $ nftnl_data_reg_value_snprintf_default(a07d7734d8bed56).(buf, size, |
| 111 | reg, flags); |
| 112 | case DATA_VERDICTcxx . . $ DATA_VERDICT.: |
| 113 | case DATA_CHAINcxx . . $ DATA_CHAIN.: |
| 114 | return nftnl_data_reg_verdict_snprintf_defcxx . . $ nftnl_data_reg_verdict_snprintf_def(a07d7734d8bed56).(buf, size, |
| 115 | reg, flags); |
| 116 | default: |
| 117 | return -1; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | static int nftnl_data_parse_cbcxx . . $ nftnl_data_parse_cb(de2ef18c48fd675c).(const struct nlattrcxx . . $ nlattr# *attr, void *data) |
| 122 | { |
| 123 | const struct nlattrcxx . . $ nlattr# **tb = data; |
| 124 | int type = mnl_attr_get_typecxx . . $ mnl_attr_get_type(1da47bcc4c4b975).(attr); |
| 125 | |
| 126 | if (mnl_attr_type_validcxx . . $ mnl_attr_type_valid(47df1cce1c7be855).(attr, NFTA_DATA_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:519:9`!) < 0) |
| 127 | return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!; |
| 128 | |
| 129 | switch(type) { |
| 130 | case NFTA_DATA_VALUEcxx . . $ NFTA_DATA_VALUE.: |
| 131 | if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_BINARYcxx . . $ MNL_TYPE_BINARY.) < 0) |
| 132 | abi_breakagecxx . . $ `include/utils.h:28:9`!(); |
| 133 | break; |
| 134 | case NFTA_DATA_VERDICTcxx . . $ NFTA_DATA_VERDICT.: |
| 135 | if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_NESTEDcxx . . $ MNL_TYPE_NESTED.) < 0) |
| 136 | abi_breakagecxx . . $ `include/utils.h:28:9`!(); |
| 137 | break; |
| 138 | } |
| 139 | tb[type] = attr; |
| 140 | return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!; |
| 141 | } |
| 142 | |
| 143 | static int nftnl_verdict_parse_cbcxx . . $ nftnl_verdict_parse_cb(de2ef18c48fd675c).(const struct nlattrcxx . . $ nlattr# *attr, void *data) |
| 144 | { |
| 145 | const struct nlattrcxx . . $ nlattr# **tb = data; |
| 146 | int type = mnl_attr_get_typecxx . . $ mnl_attr_get_type(1da47bcc4c4b975).(attr); |
| 147 | |
| 148 | if (mnl_attr_type_validcxx . . $ mnl_attr_type_valid(47df1cce1c7be855).(attr, NFTA_VERDICT_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:538:9`!) < 0) |
| 149 | return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!; |
| 150 | |
| 151 | switch(type) { |
| 152 | case NFTA_VERDICT_CODEcxx . . $ NFTA_VERDICT_CODE.: |
| 153 | case NFTA_VERDICT_CHAIN_IDcxx . . $ NFTA_VERDICT_CHAIN_ID.: |
| 154 | if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_U32cxx . . $ MNL_TYPE_U32.) < 0) |
| 155 | abi_breakagecxx . . $ `include/utils.h:28:9`!(); |
| 156 | break; |
| 157 | case NFTA_VERDICT_CHAINcxx . . $ NFTA_VERDICT_CHAIN.: |
| 158 | if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_STRINGcxx . . $ MNL_TYPE_STRING.) < 0) |
| 159 | abi_breakagecxx . . $ `include/utils.h:28:9`!(); |
| 160 | break; |
| 161 | } |
| 162 | tb[type] = attr; |
| 163 | return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!; |
| 164 | } |
| 165 | |
| 166 | static int |
| 167 | nftnl_parse_verdictcxx . . $ nftnl_parse_verdict(d4a0c8b8a6fd0fd8).(union nftnl_data_regcxx . . $ nftnl_data_reg# *data, const struct nlattrcxx . . $ nlattr# *attr, int *type) |
| 168 | { |
| 169 | struct nlattrcxx . . $ nlattr# *tb[NFTA_VERDICT_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:538:9`!+1]; |
| 170 | |
| 171 | if (mnl_attr_parse_nestedcxx . . $ mnl_attr_parse_nested(f99c0e9fa94f99d9).(attr, nftnl_verdict_parse_cbcxx . . $ nftnl_verdict_parse_cb(de2ef18c48fd675c)., tb) < 0) |
| 172 | return -1; |
| 173 | |
| 174 | if (!tb[NFTA_VERDICT_CODEcxx . . $ NFTA_VERDICT_CODE.]) |
| 175 | return -1; |
| 176 | |
| 177 | data->verdictcxx . . $ nftnl_data_reg#$anonymous_type_3#verdict. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_VERDICT_CODE])); |
| 178 | |
| 179 | switch(data->verdictcxx . . $ nftnl_data_reg#$anonymous_type_3#verdict.) { |
| 180 | case NF_ACCEPTcxx . . $ `include/linux/netfilter.h:11:9`!: |
| 181 | case NF_DROPcxx . . $ `include/linux/netfilter.h:10:9`!: |
| 182 | case NF_QUEUEcxx . . $ `include/linux/netfilter.h:13:9`!: |
| 183 | case NFT_CONTINUEcxx . . $ NFT_CONTINUE.: |
| 184 | case NFT_BREAKcxx . . $ NFT_BREAK.: |
| 185 | case NFT_RETURNcxx . . $ NFT_RETURN.: |
| 186 | if (type) |
| 187 | *type = DATA_VERDICTcxx . . $ DATA_VERDICT.; |
| 188 | data->lencxx . . $ nftnl_data_reg#$anonymous_type_2#len. = sizeof(data->verdictcxx . . $ nftnl_data_reg#$anonymous_type_3#verdict.); |
| 189 | break; |
| 190 | case NFT_JUMPcxx . . $ NFT_JUMP.: |
| 191 | case NFT_GOTOcxx . . $ NFT_GOTO.: |
| 192 | if (!tb[NFTA_VERDICT_CHAINcxx . . $ NFTA_VERDICT_CHAIN.]) |
| 193 | return -1; |
| 194 | |
| 195 | data->chaincxx . . $ nftnl_data_reg#$anonymous_type_3#chain. = strdupcxx . . $ strdup(a8b8e32347182212).(mnl_attr_get_strcxx . . $ mnl_attr_get_str(c24cebeef6d84d59).(tb[NFTA_VERDICT_CHAINcxx . . $ NFTA_VERDICT_CHAIN.])); |
| 196 | if (!data->chaincxx . . $ nftnl_data_reg#$anonymous_type_3#chain.) |
| 197 | return -1; |
| 198 | |
| 199 | if (type) |
| 200 | *type = DATA_CHAINcxx . . $ DATA_CHAIN.; |
| 201 | break; |
| 202 | default: |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static int |
| 210 | __nftnl_parse_datacxx . . $ __nftnl_parse_data(d0716bd0108d93b6).(union nftnl_data_regcxx . . $ nftnl_data_reg# *data, const struct nlattrcxx . . $ nlattr# *attr) |
| 211 | { |
| 212 | void *orig = mnl_attr_get_payloadcxx . . $ mnl_attr_get_payload(cbbcdcb61d3bc7e8).(attr); |
| 213 | uint32_tcxx . . $ uint32_t# data_len = mnl_attr_get_payload_lencxx . . $ mnl_attr_get_payload_len(1da47bcc4c4b975).(attr); |
| 214 | |
| 215 | if (data_len == 0) |
| 216 | return -1; |
| 217 | |
| 218 | if (data_len > sizeof(data->valcxx . . $ nftnl_data_reg#$anonymous_type_2#val.)) |
| 219 | return -1; |
| 220 | |
| 221 | memcpycxx . . $ memcpy(cc817cb175f282d).(data->valcxx . . $ nftnl_data_reg#$anonymous_type_2#val., orig, data_len); |
| 222 | data->lencxx . . $ nftnl_data_reg#$anonymous_type_2#len. = data_len; |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | int nftnl_parse_datacxx . . $ nftnl_parse_data(1f43ec2decb5ffec).(union nftnl_data_regcxx . . $ nftnl_data_reg# *data, struct nlattrcxx . . $ nlattr# *attr, int *type) |
| 228 | { |
| 229 | struct nlattrcxx . . $ nlattr# *tb[NFTA_DATA_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:519:9`!+1] = {}; |
| 230 | int ret = 0; |
| 231 | |
| 232 | if (mnl_attr_parse_nestedcxx . . $ mnl_attr_parse_nested(f99c0e9fa94f99d9).(attr, nftnl_data_parse_cbcxx . . $ nftnl_data_parse_cb(de2ef18c48fd675c)., tb) < 0) |
| 233 | return -1; |
| 234 | |
| 235 | if (tb[NFTA_DATA_VALUEcxx . . $ NFTA_DATA_VALUE.]) { |
| 236 | if (type) |
| 237 | *type = DATA_VALUEcxx . . $ DATA_VALUE.; |
| 238 | |
| 239 | ret = __nftnl_parse_datacxx . . $ __nftnl_parse_data(d0716bd0108d93b6).(data, tb[NFTA_DATA_VALUEcxx . . $ NFTA_DATA_VALUE.]); |
| 240 | if (ret < 0) |
| 241 | return ret; |
| 242 | } |
| 243 | if (tb[NFTA_DATA_VERDICTcxx . . $ NFTA_DATA_VERDICT.]) |
| 244 | ret = nftnl_parse_verdictcxx . . $ nftnl_parse_verdict(d4a0c8b8a6fd0fd8).(data, tb[NFTA_DATA_VERDICTcxx . . $ NFTA_DATA_VERDICT.], type); |
| 245 | |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | int nftnl_data_cpycxx . . $ nftnl_data_cpy(fe1838cfef5a2dc9).(union nftnl_data_regcxx . . $ nftnl_data_reg# *dreg, const void *src, |
| 250 | uint32_tcxx . . $ uint32_t# len, uint32_tcxx . . $ uint32_t# byteorder, uint8_tcxx . . $ uint8_t# *sizes) |
| 251 | { |
| 252 | int ret = 0; |
| 253 | |
| 254 | if (len > sizeof(dreg->valcxx . . $ nftnl_data_reg#$anonymous_type_2#val.)) { |
| 255 | len = sizeof(dreg->valcxx . . $ nftnl_data_reg#$anonymous_type_2#val.); |
| 256 | ret = -1; |
| 257 | } |
| 258 | |
| 259 | memcpycxx . . $ memcpy(cc817cb175f282d).(dreg->valcxx . . $ nftnl_data_reg#$anonymous_type_2#val., src, len); |
| 260 | dreg->lencxx . . $ nftnl_data_reg#$anonymous_type_2#len. = len; |
| 261 | dreg->byteordercxx . . $ nftnl_data_reg#$anonymous_type_2#byteorder. = byteorder; |
| 262 | if (sizes) |
| 263 | memcpycxx . . $ memcpy(cc817cb175f282d).(dreg->sizescxx . . $ nftnl_data_reg#$anonymous_type_2#sizes., sizes, sizeof(dreg->sizescxx . . $ nftnl_data_reg#$anonymous_type_2#sizes.)); |
| 264 | else |
| 265 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(dreg->sizescxx . . $ nftnl_data_reg#$anonymous_type_2#sizes., 0, sizeof(dreg->sizescxx . . $ nftnl_data_reg#$anonymous_type_2#sizes.)); |
| 266 | return ret; |
| 267 | } |