libnftnl 1.2.6
expr/counter.c
1/*
2 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
10 */
11
12#include <stdio.h>
13#include <stdint.h>
14#include <arpa/inet.h>
15#include <errno.h>
16#include <inttypes.h>
17
18#include <linux/netfilter/nf_tables.h>
19
20#include "internal.h"
21#include <libmnl/libmnl.h>
22#include <libnftnl/expr.h>
23#include <libnftnl/rule.h>
24
26 uint64_t pkts;
27 uint64_t bytes;
28};
29
30static int
31nftnl_expr_counter_set(struct nftnl_expr *e, uint16_t type,
32 const void *data, uint32_t data_len)
33{
34 struct nftnl_expr_counter *ctr = nftnl_expr_data(e);
35
36 switch(type) {
37 case NFTNL_EXPR_CTR_BYTES:
38 memcpy(&ctr->bytes, data, sizeof(ctr->bytes));
39 break;
40 case NFTNL_EXPR_CTR_PACKETS:
41 memcpy(&ctr->pkts, data, sizeof(ctr->pkts));
42 break;
43 default:
44 return -1;
45 }
46 return 0;
47}
48
49static const void *
50nftnl_expr_counter_get(const struct nftnl_expr *e, uint16_t type,
51 uint32_t *data_len)
52{
53 struct nftnl_expr_counter *ctr = nftnl_expr_data(e);
54
55 switch(type) {
56 case NFTNL_EXPR_CTR_BYTES:
57 *data_len = sizeof(ctr->bytes);
58 return &ctr->bytes;
59 case NFTNL_EXPR_CTR_PACKETS:
60 *data_len = sizeof(ctr->pkts);
61 return &ctr->pkts;
62 }
63 return NULL;
64}
65
66static int nftnl_expr_counter_cb(const struct nlattr *attr, void *data)
67{
68 const struct nlattr **tb = data;
69 int type = mnl_attr_get_type(attr);
70
71 if (mnl_attr_type_valid(attr, NFTA_COUNTER_MAX) < 0)
72 return MNL_CB_OK;
73
74 switch(type) {
75 case NFTA_COUNTER_BYTES:
76 case NFTA_COUNTER_PACKETS:
77 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
78 abi_breakage();
79 break;
80 }
81
82 tb[type] = attr;
83 return MNL_CB_OK;
84}
85
86static void
87nftnl_expr_counter_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
88{
89 struct nftnl_expr_counter *ctr = nftnl_expr_data(e);
90
91 if (e->flags & (1 << NFTNL_EXPR_CTR_BYTES))
92 mnl_attr_put_u64(nlh, NFTA_COUNTER_BYTES, htobe64(ctr->bytes));
93 if (e->flags & (1 << NFTNL_EXPR_CTR_PACKETS))
94 mnl_attr_put_u64(nlh, NFTA_COUNTER_PACKETS, htobe64(ctr->pkts));
95}
96
97static int
98nftnl_expr_counter_parse(struct nftnl_expr *e, struct nlattr *attr)
99{
100 struct nftnl_expr_counter *ctr = nftnl_expr_data(e);
101 struct nlattr *tb[NFTA_COUNTER_MAX+1] = {};
102
103 if (mnl_attr_parse_nested(attr, nftnl_expr_counter_cb, tb) < 0)
104 return -1;
105
106 if (tb[NFTA_COUNTER_BYTES]) {
107 ctr->bytes = be64toh(mnl_attr_get_u64(tb[NFTA_COUNTER_BYTES]));
108 e->flags |= (1 << NFTNL_EXPR_CTR_BYTES);
109 }
110 if (tb[NFTA_COUNTER_PACKETS]) {
111 ctr->pkts = be64toh(mnl_attr_get_u64(tb[NFTA_COUNTER_PACKETS]));
112 e->flags |= (1 << NFTNL_EXPR_CTR_PACKETS);
113 }
114
115 return 0;
116}
117
118static int nftnl_expr_counter_snprintf(char *buf, size_t len,
119 uint32_t flags,
120 const struct nftnl_expr *e)
121{
122 struct nftnl_expr_counter *ctr = nftnl_expr_data(e);
123
124 return snprintf(buf, len, "pkts %"PRIu64" bytes %"PRIu64" ",
125 ctr->pkts, ctr->bytes);
126}
127
128struct expr_ops expr_ops_counter = {
129 .name = "counter",
130 .alloc_len = sizeof(struct nftnl_expr_counter),
131 .max_attr = NFTA_COUNTER_MAX,
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};