libnftnl  1.2.1
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  memcpy(&quota->bytes, data, sizeof(quota->bytes));
32  break;
33  case NFTNL_OBJ_QUOTA_CONSUMED:
34  memcpy(&quota->consumed, data, sizeof(quota->consumed));
35  break;
36  case NFTNL_OBJ_QUOTA_FLAGS:
37  memcpy(&quota->flags, data, sizeof(quota->flags));
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 nftnl_obj_quota_snprintf(char *buf, size_t len,
129  uint32_t flags,
130  const struct nftnl_obj *e)
131 {
132  struct nftnl_obj_quota *quota = nftnl_obj_data(e);
133 
134  return snprintf(buf, len, "bytes %"PRIu64" flags %u ",
135  quota->bytes, quota->flags);
136 }
137 
138 struct obj_ops obj_ops_quota = {
139  .name = "quota",
140  .type = NFT_OBJECT_QUOTA,
141  .alloc_len = sizeof(struct nftnl_obj_quota),
142  .max_attr = NFTA_QUOTA_MAX,
143  .set = nftnl_obj_quota_set,
144  .get = nftnl_obj_quota_get,
145  .parse = nftnl_obj_quota_parse,
146  .build = nftnl_obj_quota_build,
147  .snprintf = nftnl_obj_quota_snprintf,
148 };