libnftnl  1.2.8
obj/counter.c
1 /*
2  * (C) 2012-2016 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 
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <arpa/inet.h>
13 #include <errno.h>
14 #include <inttypes.h>
15 
16 #include <linux/netfilter/nf_tables.h>
17 
18 #include <libmnl/libmnl.h>
19 #include <libnftnl/object.h>
20 
21 #include "internal.h"
22 #include "obj.h"
23 
24 static int
25 nftnl_obj_counter_set(struct nftnl_obj *e, uint16_t type,
26  const void *data, uint32_t data_len)
27 {
28  struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
29 
30  switch(type) {
31  case NFTNL_OBJ_CTR_BYTES:
32  memcpy(&ctr->bytes, data, data_len);
33  break;
34  case NFTNL_OBJ_CTR_PKTS:
35  memcpy(&ctr->pkts, data, data_len);
36  break;
37  }
38  return 0;
39 }
40 
41 static const void *
42 nftnl_obj_counter_get(const struct nftnl_obj *e, uint16_t type,
43  uint32_t *data_len)
44 {
45  struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
46 
47  switch(type) {
48  case NFTNL_OBJ_CTR_BYTES:
49  *data_len = sizeof(ctr->bytes);
50  return &ctr->bytes;
51  case NFTNL_OBJ_CTR_PKTS:
52  *data_len = sizeof(ctr->pkts);
53  return &ctr->pkts;
54  }
55  return NULL;
56 }
57 
58 static int nftnl_obj_counter_cb(const struct nlattr *attr, void *data)
59 {
60  const struct nlattr **tb = data;
61  int type = mnl_attr_get_type(attr);
62 
63  if (mnl_attr_type_valid(attr, NFTA_COUNTER_MAX) < 0)
64  return MNL_CB_OK;
65 
66  switch(type) {
67  case NFTA_COUNTER_BYTES:
68  case NFTA_COUNTER_PACKETS:
69  if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
70  abi_breakage();
71  break;
72  }
73 
74  tb[type] = attr;
75  return MNL_CB_OK;
76 }
77 
78 static void
79 nftnl_obj_counter_build(struct nlmsghdr *nlh, const struct nftnl_obj *e)
80 {
81  struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
82 
83  if (e->flags & (1 << NFTNL_OBJ_CTR_BYTES))
84  mnl_attr_put_u64(nlh, NFTA_COUNTER_BYTES, htobe64(ctr->bytes));
85  if (e->flags & (1 << NFTNL_OBJ_CTR_PKTS))
86  mnl_attr_put_u64(nlh, NFTA_COUNTER_PACKETS, htobe64(ctr->pkts));
87 }
88 
89 static int
90 nftnl_obj_counter_parse(struct nftnl_obj *e, struct nlattr *attr)
91 {
92  struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
93  struct nlattr *tb[NFTA_COUNTER_MAX+1] = {};
94 
95  if (mnl_attr_parse_nested(attr, nftnl_obj_counter_cb, tb) < 0)
96  return -1;
97 
98  if (tb[NFTA_COUNTER_BYTES]) {
99  ctr->bytes = be64toh(mnl_attr_get_u64(tb[NFTA_COUNTER_BYTES]));
100  e->flags |= (1 << NFTNL_OBJ_CTR_BYTES);
101  }
102  if (tb[NFTA_COUNTER_PACKETS]) {
103  ctr->pkts = be64toh(mnl_attr_get_u64(tb[NFTA_COUNTER_PACKETS]));
104  e->flags |= (1 << NFTNL_OBJ_CTR_PKTS);
105  }
106 
107  return 0;
108 }
109 
110 static int nftnl_obj_counter_snprintf(char *buf, size_t len, uint32_t flags,
111  const struct nftnl_obj *e)
112 {
113  struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
114 
115  return snprintf(buf, len, "pkts %"PRIu64" bytes %"PRIu64" ",
116  ctr->pkts, ctr->bytes);
117 }
118 
119 static struct attr_policy obj_ctr_attr_policy[__NFTNL_OBJ_CTR_MAX] = {
120  [NFTNL_OBJ_CTR_BYTES] = { .maxlen = sizeof(uint64_t) },
121  [NFTNL_OBJ_CTR_PKTS] = { .maxlen = sizeof(uint64_t) },
122 };
123 
124 struct obj_ops obj_ops_counter = {
125  .name = "counter",
126  .type = NFT_OBJECT_COUNTER,
127  .alloc_len = sizeof(struct nftnl_obj_counter),
128  .nftnl_max_attr = __NFTNL_OBJ_CTR_MAX - 1,
129  .attr_policy = obj_ctr_attr_policy,
130  .set = nftnl_obj_counter_set,
131  .get = nftnl_obj_counter_get,
132  .parse = nftnl_obj_counter_parse,
133  .build = nftnl_obj_counter_build,
134  .output = nftnl_obj_counter_snprintf,
135 };