libnftnl  1.0.8
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 
30 static int
31 nftnl_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  ctr->bytes = *((uint64_t *)data);
39  break;
40  case NFTNL_EXPR_CTR_PACKETS:
41  ctr->pkts = *((uint64_t *)data);
42  break;
43  default:
44  return -1;
45  }
46  return 0;
47 }
48 
49 static const void *
50 nftnl_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 
66 static 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 
86 static void
87 nftnl_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 
97 static int
98 nftnl_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 
118 static int
119 nftnl_expr_counter_json_parse(struct nftnl_expr *e, json_t *root,
120  struct nftnl_parse_err *err)
121 {
122 #ifdef JSON_PARSING
123  uint64_t uval64;
124 
125  if (nftnl_jansson_parse_val(root, "pkts", NFTNL_TYPE_U64, &uval64,
126  err) == 0)
127  nftnl_expr_set_u64(e, NFTNL_EXPR_CTR_PACKETS, uval64);
128 
129  if (nftnl_jansson_parse_val(root, "bytes", NFTNL_TYPE_U64, &uval64,
130  err) == 0)
131  nftnl_expr_set_u64(e, NFTNL_EXPR_CTR_BYTES, uval64);
132 
133  return 0;
134 #else
135  errno = EOPNOTSUPP;
136  return -1;
137 #endif
138 }
139 
140 static int nftnl_expr_counter_export(char *buf, size_t size,
141  const struct nftnl_expr *e, int type)
142 {
143  struct nftnl_expr_counter *ctr = nftnl_expr_data(e);
144  NFTNL_BUF_INIT(b, buf, size);
145 
146  if (e->flags & (1 << NFTNL_EXPR_CTR_PACKETS))
147  nftnl_buf_u64(&b, type, ctr->pkts, PKTS);
148  if (e->flags & (1 << NFTNL_EXPR_CTR_BYTES))
149  nftnl_buf_u64(&b, type, ctr->bytes, BYTES);
150 
151  return nftnl_buf_done(&b);
152 }
153 
154 static int nftnl_expr_counter_snprintf_default(char *buf, size_t len,
155  const struct nftnl_expr *e)
156 {
157  struct nftnl_expr_counter *ctr = nftnl_expr_data(e);
158 
159  return snprintf(buf, len, "pkts %"PRIu64" bytes %"PRIu64" ",
160  ctr->pkts, ctr->bytes);
161 }
162 
163 static int nftnl_expr_counter_snprintf(char *buf, size_t len, uint32_t type,
164  uint32_t flags,
165  const struct nftnl_expr *e)
166 {
167  switch (type) {
168  case NFTNL_OUTPUT_DEFAULT:
169  return nftnl_expr_counter_snprintf_default(buf, len, e);
170  case NFTNL_OUTPUT_XML:
171  case NFTNL_OUTPUT_JSON:
172  return nftnl_expr_counter_export(buf, len, e, type);
173  default:
174  break;
175  }
176  return -1;
177 }
178 
179 static bool nftnl_expr_counter_cmp(const struct nftnl_expr *e1,
180  const struct nftnl_expr *e2)
181 {
182  struct nftnl_expr_counter *c1 = nftnl_expr_data(e1);
183  struct nftnl_expr_counter *c2 = nftnl_expr_data(e2);
184  bool eq = true;
185 
186  if (e1->flags & (1 << NFTNL_EXPR_CTR_PACKETS))
187  eq &= (c1->pkts == c2->pkts);
188  if (e1->flags & (1 << NFTNL_EXPR_CTR_BYTES))
189  eq &= (c1->pkts == c2->pkts);
190 
191  return eq;
192 }
193 
194 struct expr_ops expr_ops_counter = {
195  .name = "counter",
196  .alloc_len = sizeof(struct nftnl_expr_counter),
197  .max_attr = NFTA_COUNTER_MAX,
198  .cmp = nftnl_expr_counter_cmp,
199  .set = nftnl_expr_counter_set,
200  .get = nftnl_expr_counter_get,
201  .parse = nftnl_expr_counter_parse,
202  .build = nftnl_expr_counter_build,
203  .snprintf = nftnl_expr_counter_snprintf,
204  .json_parse = nftnl_expr_counter_json_parse,
205 };