libnftnl  1.0.8
obj/quota.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 "internal.h"
19 #include <libmnl/libmnl.h>
20 #include <libnftnl/object.h>
21 
22 #include "obj.h"
23 
24 static int nftnl_obj_quota_set(struct nftnl_obj *e, uint16_t type,
25  const void *data, uint32_t data_len)
26 {
27  struct nftnl_obj_quota *quota = nftnl_obj_data(e);
28 
29  switch (type) {
30  case NFTNL_OBJ_QUOTA_BYTES:
31  quota->bytes = *((uint64_t *)data);
32  break;
33  case NFTNL_OBJ_QUOTA_CONSUMED:
34  quota->consumed = *((uint64_t *)data);
35  break;
36  case NFTNL_OBJ_QUOTA_FLAGS:
37  quota->flags = *((uint32_t *)data);
38  break;
39  default:
40  return -1;
41  }
42  return 0;
43 }
44 
45 static const void *nftnl_obj_quota_get(const struct nftnl_obj *e,
46  uint16_t type, uint32_t *data_len)
47 {
48  struct nftnl_obj_quota *quota = nftnl_obj_data(e);
49 
50  switch (type) {
51  case NFTNL_OBJ_QUOTA_BYTES:
52  *data_len = sizeof(quota->bytes);
53  return &quota->bytes;
54  case NFTNL_OBJ_QUOTA_CONSUMED:
55  *data_len = sizeof(quota->consumed);
56  return &quota->consumed;
57  case NFTNL_OBJ_QUOTA_FLAGS:
58  *data_len = sizeof(quota->flags);
59  return &quota->flags;
60  }
61  return NULL;
62 }
63 
64 static int nftnl_obj_quota_cb(const struct nlattr *attr, void *data)
65 {
66  int type = mnl_attr_get_type(attr);
67  const struct nlattr **tb = data;
68 
69  if (mnl_attr_type_valid(attr, NFTA_QUOTA_MAX) < 0)
70  return MNL_CB_OK;
71 
72  switch(type) {
73  case NFTA_QUOTA_BYTES:
74  case NFTA_QUOTA_CONSUMED:
75  if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
76  abi_breakage();
77  break;
78  case NFTA_QUOTA_FLAGS:
79  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
80  abi_breakage();
81  break;
82  }
83 
84  tb[type] = attr;
85  return MNL_CB_OK;
86 }
87 
88 static void
89 nftnl_obj_quota_build(struct nlmsghdr *nlh, const struct nftnl_obj *e)
90 {
91  struct nftnl_obj_quota *quota = nftnl_obj_data(e);
92 
93  if (e->flags & (1 << NFTNL_OBJ_QUOTA_BYTES))
94  mnl_attr_put_u64(nlh, NFTA_QUOTA_BYTES, htobe64(quota->bytes));
95  if (e->flags & (1 << NFTNL_OBJ_QUOTA_CONSUMED))
96  mnl_attr_put_u64(nlh, NFTA_QUOTA_CONSUMED,
97  htobe64(quota->consumed));
98  if (e->flags & (1 << NFTNL_OBJ_QUOTA_FLAGS))
99  mnl_attr_put_u32(nlh, NFTA_QUOTA_FLAGS, htonl(quota->flags));
100 }
101 
102 static int
103 nftnl_obj_quota_parse(struct nftnl_obj *e, struct nlattr *attr)
104 {
105  struct nftnl_obj_quota *quota = nftnl_obj_data(e);
106  struct nlattr *tb[NFTA_QUOTA_MAX + 1] = {};
107 
108  if (mnl_attr_parse_nested(attr, nftnl_obj_quota_cb, tb) < 0)
109  return -1;
110 
111  if (tb[NFTA_QUOTA_BYTES]) {
112  quota->bytes = be64toh(mnl_attr_get_u64(tb[NFTA_QUOTA_BYTES]));
113  e->flags |= (1 << NFTNL_OBJ_QUOTA_BYTES);
114  }
115  if (tb[NFTA_QUOTA_CONSUMED]) {
116  quota->consumed =
117  be64toh(mnl_attr_get_u64(tb[NFTA_QUOTA_CONSUMED]));
118  e->flags |= (1 << NFTNL_OBJ_QUOTA_CONSUMED);
119  }
120  if (tb[NFTA_QUOTA_FLAGS]) {
121  quota->flags = ntohl(mnl_attr_get_u32(tb[NFTA_QUOTA_FLAGS]));
122  e->flags |= (1 << NFTNL_OBJ_QUOTA_FLAGS);
123  }
124 
125  return 0;
126 }
127 
128 static int
129 nftnl_obj_quota_json_parse(struct nftnl_obj *e, json_t *root,
130  struct nftnl_parse_err *err)
131 {
132 #ifdef JSON_PARSING
133  uint64_t bytes;
134  uint32_t flags;
135 
136  if (nftnl_jansson_parse_val(root, "bytes", NFTNL_TYPE_U64, &bytes,
137  err) == 0)
138  nftnl_obj_set_u64(e, NFTNL_OBJ_QUOTA_BYTES, bytes);
139  if (nftnl_jansson_parse_val(root, "consumed", NFTNL_TYPE_U64, &bytes,
140  err) == 0)
141  nftnl_obj_set_u64(e, NFTNL_OBJ_QUOTA_CONSUMED, bytes);
142  if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, &flags,
143  err) == 0)
144  nftnl_obj_set_u32(e, NFTNL_OBJ_QUOTA_FLAGS, flags);
145 
146  return 0;
147 #else
148  errno = EOPNOTSUPP;
149  return -1;
150 #endif
151 }
152 
153 static int nftnl_obj_quota_export(char *buf, size_t size,
154  const struct nftnl_obj *e, int type)
155 {
156  struct nftnl_obj_quota *quota = nftnl_obj_data(e);
157  NFTNL_BUF_INIT(b, buf, size);
158 
159  if (e->flags & (1 << NFTNL_OBJ_QUOTA_BYTES))
160  nftnl_buf_u64(&b, type, quota->bytes, BYTES);
161  if (e->flags & (1 << NFTNL_OBJ_QUOTA_CONSUMED))
162  nftnl_buf_u64(&b, type, quota->consumed, CONSUMED);
163  if (e->flags & (1 << NFTNL_OBJ_QUOTA_FLAGS))
164  nftnl_buf_u32(&b, type, quota->flags, FLAGS);
165 
166  return nftnl_buf_done(&b);
167 }
168 
169 static int nftnl_obj_quota_snprintf_default(char *buf, size_t len,
170  const struct nftnl_obj *e)
171 {
172  struct nftnl_obj_quota *quota = nftnl_obj_data(e);
173 
174  return snprintf(buf, len, "bytes %"PRIu64" flags %u ",
175  quota->bytes, quota->flags);
176 }
177 
178 static int nftnl_obj_quota_snprintf(char *buf, size_t len, uint32_t type,
179  uint32_t flags,
180  const struct nftnl_obj *e)
181 {
182  if (len)
183  buf[0] = '\0';
184 
185  switch (type) {
186  case NFTNL_OUTPUT_DEFAULT:
187  return nftnl_obj_quota_snprintf_default(buf, len, e);
188  case NFTNL_OUTPUT_XML:
189  case NFTNL_OUTPUT_JSON:
190  return nftnl_obj_quota_export(buf, len, e, type);
191  default:
192  break;
193  }
194  return -1;
195 }
196 
197 struct obj_ops obj_ops_quota = {
198  .name = "quota",
199  .type = NFT_OBJECT_QUOTA,
200  .alloc_len = sizeof(struct nftnl_obj_quota),
201  .max_attr = NFTA_QUOTA_MAX,
202  .set = nftnl_obj_quota_set,
203  .get = nftnl_obj_quota_get,
204  .parse = nftnl_obj_quota_parse,
205  .build = nftnl_obj_quota_build,
206  .snprintf = nftnl_obj_quota_snprintf,
207  .json_parse = nftnl_obj_quota_json_parse,
208 };