| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */cxx . . $ `<file>/src/expr/counter.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 <arpa/inet.h> |
| 11 | #include <errno.h> |
| 12 | #include <inttypes.h> |
| 13 | |
| 14 | #include <linux/netfilter/nf_tables.h>cxx . . $ `<file>/include/linux/netfilter/nf_tables.h`/ |
| 15 | |
| 16 | #include "internal.h"cxx . . $ `<file>/include/internal.h`/ |
| 17 | #include <libmnl/libmnl.h> |
| 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_countercxx . . $ nftnl_expr_counter# { |
| 22 | uint64_tcxx . . $ uint64_t# pktscxx . . $ nftnl_expr_counter#pkts.; |
| 23 | uint64_tcxx . . $ uint64_t# bytescxx . . $ nftnl_expr_counter#bytes.; |
| 24 | }; |
| 25 | |
| 26 | static int |
| 27 | nftnl_expr_counter_setcxx . . $ nftnl_expr_counter_set(211371a3e3842127).(struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type, |
| 28 | const void *data, uint32_tcxx . . $ uint32_t# data_len, uint32_tcxx . . $ uint32_t# byteorder) |
| 29 | { |
| 30 | struct nftnl_expr_countercxx . . $ nftnl_expr_counter# *ctr = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 31 | |
| 32 | switch(type) { |
| 33 | case NFTNL_EXPR_CTR_BYTEScxx . . $ NFTNL_EXPR_CTR_BYTES.: |
| 34 | memcpycxx . . $ memcpy(cc817cb175f282d).(&ctr->bytescxx . . $ nftnl_expr_counter#bytes., data, data_len); |
| 35 | break; |
| 36 | case NFTNL_EXPR_CTR_PACKETScxx . . $ NFTNL_EXPR_CTR_PACKETS.: |
| 37 | memcpycxx . . $ memcpy(cc817cb175f282d).(&ctr->pktscxx . . $ nftnl_expr_counter#pkts., data, data_len); |
| 38 | break; |
| 39 | } |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static const void * |
| 44 | nftnl_expr_counter_getcxx . . $ nftnl_expr_counter_get(9de263c4f9e71c4b).(const struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type, |
| 45 | uint32_tcxx . . $ uint32_t# *data_len) |
| 46 | { |
| 47 | struct nftnl_expr_countercxx . . $ nftnl_expr_counter# *ctr = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 48 | |
| 49 | switch(type) { |
| 50 | case NFTNL_EXPR_CTR_BYTEScxx . . $ NFTNL_EXPR_CTR_BYTES.: |
| 51 | *data_len = sizeof(ctr->bytescxx . . $ nftnl_expr_counter#bytes.); |
| 52 | return &ctr->bytescxx . . $ nftnl_expr_counter#bytes.; |
| 53 | case NFTNL_EXPR_CTR_PACKETScxx . . $ NFTNL_EXPR_CTR_PACKETS.: |
| 54 | *data_len = sizeof(ctr->pktscxx . . $ nftnl_expr_counter#pkts.); |
| 55 | return &ctr->pktscxx . . $ nftnl_expr_counter#pkts.; |
| 56 | } |
| 57 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 58 | } |
| 59 | |
| 60 | static int nftnl_expr_counter_cbcxx . . $ nftnl_expr_counter_cb(de2ef18c48fd675c).(const struct nlattrcxx . . $ nlattr# *attr, void *data) |
| 61 | { |
| 62 | const struct nlattrcxx . . $ nlattr# **tb = data; |
| 63 | int type = mnl_attr_get_typecxx . . $ mnl_attr_get_type(1da47bcc4c4b975).(attr); |
| 64 | |
| 65 | if (mnl_attr_type_validcxx . . $ mnl_attr_type_valid(47df1cce1c7be855).(attr, NFTA_COUNTER_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:1274:9`!) < 0) |
| 66 | return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!; |
| 67 | |
| 68 | switch(type) { |
| 69 | case NFTA_COUNTER_BYTEScxx . . $ NFTA_COUNTER_BYTES.: |
| 70 | case NFTA_COUNTER_PACKETScxx . . $ NFTA_COUNTER_PACKETS.: |
| 71 | if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_U64cxx . . $ MNL_TYPE_U64.) < 0) |
| 72 | abi_breakagecxx . . $ `include/utils.h:28:9`!(); |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | tb[type] = attr; |
| 77 | return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!; |
| 78 | } |
| 79 | |
| 80 | static void |
| 81 | nftnl_expr_counter_buildcxx . . $ nftnl_expr_counter_build(93c7528a9cf90b41).(struct nlmsghdrcxx . . $ nlmsghdr# *nlh, const struct nftnl_exprcxx . . $ nftnl_expr# *e) |
| 82 | { |
| 83 | struct nftnl_expr_countercxx . . $ nftnl_expr_counter# *ctr = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 84 | |
| 85 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_CTR_BYTEScxx . . $ NFTNL_EXPR_CTR_BYTES.)) |
| 86 | mnl_attr_put_u64cxx . . $ mnl_attr_put_u64(6d753f4d486e3a2f).(nlh, NFTA_COUNTER_BYTEScxx . . $ NFTA_COUNTER_BYTES., htobe64cxx . . $ `<external>/15855202785537844739/endian.h:49:11`!(ctr->bytes)); |
| 87 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_CTR_PACKETScxx . . $ NFTNL_EXPR_CTR_PACKETS.)) |
| 88 | mnl_attr_put_u64cxx . . $ mnl_attr_put_u64(6d753f4d486e3a2f).(nlh, NFTA_COUNTER_PACKETScxx . . $ NFTA_COUNTER_PACKETS., htobe64cxx . . $ `<external>/15855202785537844739/endian.h:49:11`!(ctr->pkts)); |
| 89 | } |
| 90 | |
| 91 | static int |
| 92 | nftnl_expr_counter_parsecxx . . $ nftnl_expr_counter_parse(ed61207049d8af9a).(struct nftnl_exprcxx . . $ nftnl_expr# *e, struct nlattrcxx . . $ nlattr# *attr) |
| 93 | { |
| 94 | struct nftnl_expr_countercxx . . $ nftnl_expr_counter# *ctr = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 95 | struct nlattrcxx . . $ nlattr# *tb[NFTA_COUNTER_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:1274:9`!+1] = {}; |
| 96 | |
| 97 | if (mnl_attr_parse_nestedcxx . . $ mnl_attr_parse_nested(f99c0e9fa94f99d9).(attr, nftnl_expr_counter_cbcxx . . $ nftnl_expr_counter_cb(de2ef18c48fd675c)., tb) < 0) |
| 98 | return -1; |
| 99 | |
| 100 | if (tb[NFTA_COUNTER_BYTEScxx . . $ NFTA_COUNTER_BYTES.]) { |
| 101 | ctr->bytescxx . . $ nftnl_expr_counter#bytes. = be64tohcxx . . $ `<external>/15855202785537844739/endian.h:51:11`!(mnl_attr_get_u64(tb[NFTA_COUNTER_BYTES])); |
| 102 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_CTR_BYTEScxx . . $ NFTNL_EXPR_CTR_BYTES.); |
| 103 | } |
| 104 | if (tb[NFTA_COUNTER_PACKETScxx . . $ NFTA_COUNTER_PACKETS.]) { |
| 105 | ctr->pktscxx . . $ nftnl_expr_counter#pkts. = be64tohcxx . . $ `<external>/15855202785537844739/endian.h:51:11`!(mnl_attr_get_u64(tb[NFTA_COUNTER_PACKETS])); |
| 106 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_CTR_PACKETScxx . . $ NFTNL_EXPR_CTR_PACKETS.); |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int nftnl_expr_counter_snprintfcxx . . $ nftnl_expr_counter_snprintf(2b4af69501aef780).(char *buf, size_t len, |
| 113 | uint32_tcxx . . $ uint32_t# flags, |
| 114 | const struct nftnl_exprcxx . . $ nftnl_expr# *e) |
| 115 | { |
| 116 | struct nftnl_expr_countercxx . . $ nftnl_expr_counter# *ctr = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 117 | |
| 118 | return snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, len, "pkts %"PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:110:10`!" bytes %"PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:110:10`!" ", |
| 119 | ctr->pktscxx . . $ nftnl_expr_counter#pkts., ctr->bytescxx . . $ nftnl_expr_counter#bytes.); |
| 120 | } |
| 121 | |
| 122 | static struct attr_policycxx . . $ attr_policy# counter_attr_policycxx . . $ counter_attr_policy.[__NFTNL_EXPR_CTR_MAXcxx . . $ __NFTNL_EXPR_CTR_MAX.] = { |
| 123 | [NFTNL_EXPR_CTR_PACKETScxx . . $ NFTNL_EXPR_CTR_PACKETS.] = { .maxlen = sizeof(uint64_tcxx . . $ uint64_t#) }, |
| 124 | [NFTNL_EXPR_CTR_BYTEScxx . . $ NFTNL_EXPR_CTR_BYTES.] = { .maxlen = sizeof(uint64_tcxx . . $ uint64_t#) }, |
| 125 | }; |
| 126 | |
| 127 | struct expr_opscxx . . $ expr_ops# expr_ops_countercxx . . $ expr_ops_counter. = { |
| 128 | .name = "counter", |
| 129 | .alloc_len = sizeof(struct nftnl_expr_countercxx . . $ nftnl_expr_counter#), |
| 130 | .nftnl_max_attr = __NFTNL_EXPR_CTR_MAXcxx . . $ __NFTNL_EXPR_CTR_MAX. - 1, |
| 131 | .attr_policy = counter_attr_policycxx . . $ counter_attr_policy., |
| 132 | .set = nftnl_expr_counter_setcxx . . $ nftnl_expr_counter_set(211371a3e3842127)., |
| 133 | .get = nftnl_expr_counter_getcxx . . $ nftnl_expr_counter_get(9de263c4f9e71c4b)., |
| 134 | .parse = nftnl_expr_counter_parsecxx . . $ nftnl_expr_counter_parse(ed61207049d8af9a)., |
| 135 | .build = nftnl_expr_counter_buildcxx . . $ nftnl_expr_counter_build(93c7528a9cf90b41)., |
| 136 | .output = nftnl_expr_counter_snprintfcxx . . $ nftnl_expr_counter_snprintf(2b4af69501aef780)., |
| 137 | }; |