| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 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> |
| 15 | |
| 16 | #include "internal.h" |
| 17 | #include <libmnl/libmnl.h> |
| 18 | #include <libnftnl/expr.h> |
| 19 | #include <libnftnl/rule.h> |
| 20 | |
| 21 | struct nftnl_expr_counter { |
| 22 | uint64_t pkts; |
| 23 | uint64_t bytes; |
| 24 | }; |
| 25 | |
| 26 | static int |
| 27 | nftnl_expr_counter_set(struct nftnl_expr *e, uint16_t type, |
| 28 | const void *data, uint32_t data_len, uint32_t byteorder) |
| 29 | { |
| 30 | struct nftnl_expr_counter *ctr = nftnl_expr_data(e); |
| 31 | |
| 32 | switch(type) { |
| 33 | case NFTNL_EXPR_CTR_BYTES: |
| 34 | memcpy(&ctr->bytes, data, data_len); |
| 35 | break; |
| 36 | case NFTNL_EXPR_CTR_PACKETS: |
| 37 | memcpy(&ctr->pkts, data, data_len); |
| 38 | break; |
| 39 | } |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static const void * |
| 44 | nftnl_expr_counter_get(const struct nftnl_expr *e, uint16_t type, |
| 45 | uint32_t *data_len) |
| 46 | { |
| 47 | struct nftnl_expr_counter *ctr = nftnl_expr_data(e); |
| 48 | |
| 49 | switch(type) { |
| 50 | case NFTNL_EXPR_CTR_BYTES: |
| 51 | *data_len = sizeof(ctr->bytes); |
| 52 | return &ctr->bytes; |
| 53 | case NFTNL_EXPR_CTR_PACKETS: |
| 54 | *data_len = sizeof(ctr->pkts); |
| 55 | return &ctr->pkts; |
| 56 | } |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | static int nftnl_expr_counter_cb(const struct nlattr *attr, void *data) |
| 61 | { |
| 62 | const struct nlattr **tb = data; |
| 63 | int type = mnl_attr_get_type(attr); |
| 64 | |
| 65 | if (mnl_attr_type_valid(attr, NFTA_COUNTER_MAX) < 0) |
| 66 | return MNL_CB_OK; |
| 67 | |
| 68 | switch(type) { |
| 69 | case NFTA_COUNTER_BYTES: |
| 70 | case NFTA_COUNTER_PACKETS: |
| 71 | if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0) |
| 72 | abi_breakage(); |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | tb[type] = attr; |
| 77 | return MNL_CB_OK; |
| 78 | } |
| 79 | |
| 80 | static void |
| 81 | nftnl_expr_counter_build(struct nlmsghdr *nlh, const struct nftnl_expr *e) |
| 82 | { |
| 83 | struct nftnl_expr_counter *ctr = nftnl_expr_data(e); |
| 84 | |
| 85 | if (e->flags & (1 << NFTNL_EXPR_CTR_BYTES)) |
| 86 | mnl_attr_put_u64(nlh, NFTA_COUNTER_BYTES, htobe64(ctr->bytes)); |
| 87 | if (e->flags & (1 << NFTNL_EXPR_CTR_PACKETS)) |
| 88 | mnl_attr_put_u64(nlh, NFTA_COUNTER_PACKETS, htobe64(ctr->pkts)); |
| 89 | } |
| 90 | |
| 91 | static int |
| 92 | nftnl_expr_counter_parse(struct nftnl_expr *e, struct nlattr *attr) |
| 93 | { |
| 94 | struct nftnl_expr_counter *ctr = nftnl_expr_data(e); |
| 95 | struct nlattr *tb[NFTA_COUNTER_MAX+1] = {}; |
| 96 | |
| 97 | if (mnl_attr_parse_nested(attr, nftnl_expr_counter_cb, tb) < 0) |
| 98 | return -1; |
| 99 | |
| 100 | if (tb[NFTA_COUNTER_BYTES]) { |
| 101 | ctr->bytes = be64toh(mnl_attr_get_u64(tb[NFTA_COUNTER_BYTES])); |
| 102 | e->flags |= (1 << NFTNL_EXPR_CTR_BYTES); |
| 103 | } |
| 104 | if (tb[NFTA_COUNTER_PACKETS]) { |
| 105 | ctr->pkts = be64toh(mnl_attr_get_u64(tb[NFTA_COUNTER_PACKETS])); |
| 106 | e->flags |= (1 << NFTNL_EXPR_CTR_PACKETS); |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int nftnl_expr_counter_snprintf(char *buf, size_t len, |
| 113 | uint32_t flags, |
| 114 | const struct nftnl_expr *e) |
| 115 | { |
| 116 | struct nftnl_expr_counter *ctr = nftnl_expr_data(e); |
| 117 | |
| 118 | return snprintf(buf, len, "pkts %"PRIu64" bytes %"PRIu64" ", |
| 119 | ctr->pkts, ctr->bytes); |
| 120 | } |
| 121 | |
| 122 | static struct attr_policy counter_attr_policy[__NFTNL_EXPR_CTR_MAX] = { |
| 123 | [NFTNL_EXPR_CTR_PACKETS] = { .maxlen = sizeof(uint64_t) }, |
| 124 | [NFTNL_EXPR_CTR_BYTES] = { .maxlen = sizeof(uint64_t) }, |
| 125 | }; |
| 126 | |
| 127 | struct expr_ops expr_ops_counter = { |
| 128 | .name = "counter", |
| 129 | .alloc_len = sizeof(struct nftnl_expr_counter), |
| 130 | .nftnl_max_attr = __NFTNL_EXPR_CTR_MAX - 1, |
| 131 | .attr_policy = counter_attr_policy, |
| 132 | .set = nftnl_expr_counter_set, |
| 133 | .get = nftnl_expr_counter_get, |
| 134 | .parse = nftnl_expr_counter_parse, |
| 135 | .build = nftnl_expr_counter_build, |
| 136 | .output = nftnl_expr_counter_snprintf, |
| 137 | }; |