| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */cxx . . $ `<file>/src/expr/numgen.c`/ |
| 2 | /* |
| 3 | * (C) 2016 by Laura Garcia <[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_ngcxx . . $ nftnl_expr_ng# { |
| 19 | enum nft_registerscxx . . $ nft_registers# dregcxx . . $ nftnl_expr_ng#dreg.; |
| 20 | unsigned int moduluscxx . . $ nftnl_expr_ng#modulus.; |
| 21 | enum nft_ng_typescxx . . $ nft_ng_types# typecxx . . $ nftnl_expr_ng#type.; |
| 22 | unsigned int offsetcxx . . $ nftnl_expr_ng#offset.; |
| 23 | }; |
| 24 | |
| 25 | static int |
| 26 | nftnl_expr_ng_setcxx . . $ nftnl_expr_ng_set(211371a3e3842127).(struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type, |
| 27 | const void *data, uint32_tcxx . . $ uint32_t# data_len, uint32_tcxx . . $ uint32_t# byteorder) |
| 28 | { |
| 29 | struct nftnl_expr_ngcxx . . $ nftnl_expr_ng# *ng = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 30 | |
| 31 | switch (type) { |
| 32 | case NFTNL_EXPR_NG_DREGcxx . . $ NFTNL_EXPR_NG_DREG.: |
| 33 | memcpycxx . . $ memcpy(cc817cb175f282d).(&ng->dregcxx . . $ nftnl_expr_ng#dreg., data, data_len); |
| 34 | break; |
| 35 | case NFTNL_EXPR_NG_MODULUScxx . . $ NFTNL_EXPR_NG_MODULUS.: |
| 36 | memcpycxx . . $ memcpy(cc817cb175f282d).(&ng->moduluscxx . . $ nftnl_expr_ng#modulus., data, data_len); |
| 37 | break; |
| 38 | case NFTNL_EXPR_NG_TYPEcxx . . $ NFTNL_EXPR_NG_TYPE.: |
| 39 | memcpycxx . . $ memcpy(cc817cb175f282d).(&ng->typecxx . . $ nftnl_expr_ng#type., data, data_len); |
| 40 | break; |
| 41 | case NFTNL_EXPR_NG_OFFSETcxx . . $ NFTNL_EXPR_NG_OFFSET.: |
| 42 | memcpycxx . . $ memcpy(cc817cb175f282d).(&ng->offsetcxx . . $ nftnl_expr_ng#offset., data, data_len); |
| 43 | break; |
| 44 | default: |
| 45 | return -1; |
| 46 | } |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static const void * |
| 51 | nftnl_expr_ng_getcxx . . $ nftnl_expr_ng_get(9de263c4f9e71c4b).(const struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type, |
| 52 | uint32_tcxx . . $ uint32_t# *data_len) |
| 53 | { |
| 54 | struct nftnl_expr_ngcxx . . $ nftnl_expr_ng# *ng = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 55 | |
| 56 | switch (type) { |
| 57 | case NFTNL_EXPR_NG_DREGcxx . . $ NFTNL_EXPR_NG_DREG.: |
| 58 | *data_len = sizeof(ng->dregcxx . . $ nftnl_expr_ng#dreg.); |
| 59 | return &ng->dregcxx . . $ nftnl_expr_ng#dreg.; |
| 60 | case NFTNL_EXPR_NG_MODULUScxx . . $ NFTNL_EXPR_NG_MODULUS.: |
| 61 | *data_len = sizeof(ng->moduluscxx . . $ nftnl_expr_ng#modulus.); |
| 62 | return &ng->moduluscxx . . $ nftnl_expr_ng#modulus.; |
| 63 | case NFTNL_EXPR_NG_TYPEcxx . . $ NFTNL_EXPR_NG_TYPE.: |
| 64 | *data_len = sizeof(ng->typecxx . . $ nftnl_expr_ng#type.); |
| 65 | return &ng->typecxx . . $ nftnl_expr_ng#type.; |
| 66 | case NFTNL_EXPR_NG_OFFSETcxx . . $ NFTNL_EXPR_NG_OFFSET.: |
| 67 | *data_len = sizeof(ng->offsetcxx . . $ nftnl_expr_ng#offset.); |
| 68 | return &ng->offsetcxx . . $ nftnl_expr_ng#offset.; |
| 69 | } |
| 70 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 71 | } |
| 72 | |
| 73 | static int nftnl_expr_ng_cbcxx . . $ nftnl_expr_ng_cb(de2ef18c48fd675c).(const struct nlattrcxx . . $ nlattr# *attr, void *data) |
| 74 | { |
| 75 | const struct nlattrcxx . . $ nlattr# **tb = data; |
| 76 | int type = mnl_attr_get_typecxx . . $ mnl_attr_get_type(1da47bcc4c4b975).(attr); |
| 77 | |
| 78 | if (mnl_attr_type_validcxx . . $ mnl_attr_type_valid(47df1cce1c7be855).(attr, NFTA_NG_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:1909:9`!) < 0) |
| 79 | return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!; |
| 80 | |
| 81 | switch (type) { |
| 82 | case NFTA_NG_DREGcxx . . $ NFTA_NG_DREG.: |
| 83 | case NFTA_NG_MODULUScxx . . $ NFTA_NG_MODULUS.: |
| 84 | case NFTA_NG_TYPEcxx . . $ NFTA_NG_TYPE.: |
| 85 | case NFTA_NG_OFFSETcxx . . $ NFTA_NG_OFFSET.: |
| 86 | if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_U32cxx . . $ MNL_TYPE_U32.) < 0) |
| 87 | abi_breakagecxx . . $ `include/utils.h:28:9`!(); |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | tb[type] = attr; |
| 92 | return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!; |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | nftnl_expr_ng_buildcxx . . $ nftnl_expr_ng_build(93c7528a9cf90b41).(struct nlmsghdrcxx . . $ nlmsghdr# *nlh, const struct nftnl_exprcxx . . $ nftnl_expr# *e) |
| 97 | { |
| 98 | struct nftnl_expr_ngcxx . . $ nftnl_expr_ng# *ng = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 99 | |
| 100 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_NG_DREGcxx . . $ NFTNL_EXPR_NG_DREG.)) |
| 101 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_NG_DREGcxx . . $ NFTA_NG_DREG., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(ng->dreg)); |
| 102 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_NG_MODULUScxx . . $ NFTNL_EXPR_NG_MODULUS.)) |
| 103 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_NG_MODULUScxx . . $ NFTA_NG_MODULUS., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(ng->modulus)); |
| 104 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_NG_TYPEcxx . . $ NFTNL_EXPR_NG_TYPE.)) |
| 105 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_NG_TYPEcxx . . $ NFTA_NG_TYPE., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(ng->type)); |
| 106 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_NG_OFFSETcxx . . $ NFTNL_EXPR_NG_OFFSET.)) |
| 107 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_NG_OFFSETcxx . . $ NFTA_NG_OFFSET., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(ng->offset)); |
| 108 | } |
| 109 | |
| 110 | static int |
| 111 | nftnl_expr_ng_parsecxx . . $ nftnl_expr_ng_parse(ed61207049d8af9a).(struct nftnl_exprcxx . . $ nftnl_expr# *e, struct nlattrcxx . . $ nlattr# *attr) |
| 112 | { |
| 113 | struct nftnl_expr_ngcxx . . $ nftnl_expr_ng# *ng = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 114 | struct nlattrcxx . . $ nlattr# *tb[NFTA_NG_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:1909:9`!+1] = {}; |
| 115 | int ret = 0; |
| 116 | |
| 117 | if (mnl_attr_parse_nestedcxx . . $ mnl_attr_parse_nested(f99c0e9fa94f99d9).(attr, nftnl_expr_ng_cbcxx . . $ nftnl_expr_ng_cb(de2ef18c48fd675c)., tb) < 0) |
| 118 | return -1; |
| 119 | |
| 120 | if (tb[NFTA_NG_DREGcxx . . $ NFTA_NG_DREG.]) { |
| 121 | ng->dregcxx . . $ nftnl_expr_ng#dreg. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_NG_DREG])); |
| 122 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_NG_DREGcxx . . $ NFTNL_EXPR_NG_DREG.); |
| 123 | } |
| 124 | if (tb[NFTA_NG_MODULUScxx . . $ NFTA_NG_MODULUS.]) { |
| 125 | ng->moduluscxx . . $ nftnl_expr_ng#modulus. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_NG_MODULUS])); |
| 126 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_NG_MODULUScxx . . $ NFTNL_EXPR_NG_MODULUS.); |
| 127 | } |
| 128 | if (tb[NFTA_NG_TYPEcxx . . $ NFTA_NG_TYPE.]) { |
| 129 | ng->typecxx . . $ nftnl_expr_ng#type. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_NG_TYPE])); |
| 130 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_NG_TYPEcxx . . $ NFTNL_EXPR_NG_TYPE.); |
| 131 | } |
| 132 | if (tb[NFTA_NG_OFFSETcxx . . $ NFTA_NG_OFFSET.]) { |
| 133 | ng->offsetcxx . . $ nftnl_expr_ng#offset. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_NG_OFFSET])); |
| 134 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_NG_OFFSETcxx . . $ NFTNL_EXPR_NG_OFFSET.); |
| 135 | } |
| 136 | |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | static int |
| 141 | nftnl_expr_ng_snprintfcxx . . $ nftnl_expr_ng_snprintf(2b4af69501aef780).(char *buf, size_t remain, |
| 142 | uint32_tcxx . . $ uint32_t# flags, const struct nftnl_exprcxx . . $ nftnl_expr# *e) |
| 143 | { |
| 144 | struct nftnl_expr_ngcxx . . $ nftnl_expr_ng# *ng = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 145 | int offset = 0, ret; |
| 146 | |
| 147 | switch (ng->typecxx . . $ nftnl_expr_ng#type.) { |
| 148 | case NFT_NG_INCREMENTALcxx . . $ NFT_NG_INCREMENTAL.: |
| 149 | ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, remain, "reg %u = inc mod %u ", |
| 150 | ng->dregcxx . . $ nftnl_expr_ng#dreg., ng->moduluscxx . . $ nftnl_expr_ng#modulus.); |
| 151 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset); |
| 152 | break; |
| 153 | case NFT_NG_RANDOMcxx . . $ NFT_NG_RANDOM.: |
| 154 | ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, remain, "reg %u = random mod %u ", |
| 155 | ng->dregcxx . . $ nftnl_expr_ng#dreg., ng->moduluscxx . . $ nftnl_expr_ng#modulus.); |
| 156 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset); |
| 157 | break; |
| 158 | default: |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | if (ng->offsetcxx . . $ nftnl_expr_ng#offset.) { |
| 163 | ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf + offset, remain, "offset %u ", ng->offsetcxx . . $ nftnl_expr_ng#offset.); |
| 164 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset); |
| 165 | } |
| 166 | |
| 167 | return offset; |
| 168 | } |
| 169 | |
| 170 | static struct attr_policycxx . . $ attr_policy# numgen_attr_policycxx . . $ numgen_attr_policy.[__NFTNL_EXPR_NG_MAXcxx . . $ __NFTNL_EXPR_NG_MAX.] = { |
| 171 | [NFTNL_EXPR_NG_DREGcxx . . $ NFTNL_EXPR_NG_DREG.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 172 | [NFTNL_EXPR_NG_MODULUScxx . . $ NFTNL_EXPR_NG_MODULUS.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 173 | [NFTNL_EXPR_NG_TYPEcxx . . $ NFTNL_EXPR_NG_TYPE.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 174 | [NFTNL_EXPR_NG_OFFSETcxx . . $ NFTNL_EXPR_NG_OFFSET.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 175 | }; |
| 176 | |
| 177 | struct expr_opscxx . . $ expr_ops# expr_ops_ngcxx . . $ expr_ops_ng. = { |
| 178 | .name = "numgen", |
| 179 | .alloc_len = sizeof(struct nftnl_expr_ngcxx . . $ nftnl_expr_ng#), |
| 180 | .nftnl_max_attr = __NFTNL_EXPR_NG_MAXcxx . . $ __NFTNL_EXPR_NG_MAX. - 1, |
| 181 | .attr_policy = numgen_attr_policycxx . . $ numgen_attr_policy., |
| 182 | .set = nftnl_expr_ng_setcxx . . $ nftnl_expr_ng_set(211371a3e3842127)., |
| 183 | .get = nftnl_expr_ng_getcxx . . $ nftnl_expr_ng_get(9de263c4f9e71c4b)., |
| 184 | .parse = nftnl_expr_ng_parsecxx . . $ nftnl_expr_ng_parse(ed61207049d8af9a)., |
| 185 | .build = nftnl_expr_ng_buildcxx . . $ nftnl_expr_ng_build(93c7528a9cf90b41)., |
| 186 | .output = nftnl_expr_ng_snprintfcxx . . $ nftnl_expr_ng_snprintf(2b4af69501aef780)., |
| 187 | }; |