libnftnl 1.2.7
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
24static 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, data_len);
32 break;
33 case NFTNL_OBJ_QUOTA_CONSUMED:
34 memcpy(&quota->consumed, data, data_len);
35 break;
36 case NFTNL_OBJ_QUOTA_FLAGS:
37 memcpy(&quota->flags, data, data_len);
38 break;
39 }
40 return 0;
41}
42
43static const void *nftnl_obj_quota_get(const struct nftnl_obj *e,
44 uint16_t type, uint32_t *data_len)
45{
46 struct nftnl_obj_quota *quota = nftnl_obj_data(e);
47
48 switch (type) {
49 case NFTNL_OBJ_QUOTA_BYTES:
50 *data_len = sizeof(quota->bytes);
51 return &quota->bytes;
52 case NFTNL_OBJ_QUOTA_CONSUMED:
53 *data_len = sizeof(quota->consumed);
54 return &quota->consumed;
55 case NFTNL_OBJ_QUOTA_FLAGS:
56 *data_len = sizeof(quota->flags);
57 return &quota->flags;
58 }
59 return NULL;
60}
61
62static int nftnl_obj_quota_cb(const struct nlattr *attr, void *data)
63{
64 int type = mnl_attr_get_type(attr);
65 const struct nlattr **tb = data;
66
67 if (mnl_attr_type_valid(attr, NFTA_QUOTA_MAX) < 0)
68 return MNL_CB_OK;
69
70 switch(type) {
71 case NFTA_QUOTA_BYTES:
72 case NFTA_QUOTA_CONSUMED:
73 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
74 abi_breakage();
75 break;
76 case NFTA_QUOTA_FLAGS:
77 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
78 abi_breakage();
79 break;
80 }
81
82 tb[type] = attr;
83 return MNL_CB_OK;
84}
85
86static void
87nftnl_obj_quota_build(struct nlmsghdr *nlh, const struct nftnl_obj *e)
88{
89 struct nftnl_obj_quota *quota = nftnl_obj_data(e);
90
91 if (e->flags & (1 << NFTNL_OBJ_QUOTA_BYTES))
92 mnl_attr_put_u64(nlh, NFTA_QUOTA_BYTES, htobe64(quota->bytes));
93 if (e->flags & (1 << NFTNL_OBJ_QUOTA_CONSUMED))
94 mnl_attr_put_u64(nlh, NFTA_QUOTA_CONSUMED,
95 htobe64(quota->consumed));
96 if (e->flags & (1 << NFTNL_OBJ_QUOTA_FLAGS))
97 mnl_attr_put_u32(nlh, NFTA_QUOTA_FLAGS, htonl(quota->flags));
98}
99
100static int
101nftnl_obj_quota_parse(struct nftnl_obj *e, struct nlattr *attr)
102{
103 struct nftnl_obj_quota *quota = nftnl_obj_data(e);
104 struct nlattr *tb[NFTA_QUOTA_MAX + 1] = {};
105
106 if (mnl_attr_parse_nested(attr, nftnl_obj_quota_cb, tb) < 0)
107 return -1;
108
109 if (tb[NFTA_QUOTA_BYTES]) {
110 quota->bytes = be64toh(mnl_attr_get_u64(tb[NFTA_QUOTA_BYTES]));
111 e->flags |= (1 << NFTNL_OBJ_QUOTA_BYTES);
112 }
113 if (tb[NFTA_QUOTA_CONSUMED]) {
114 quota->consumed =
115 be64toh(mnl_attr_get_u64(tb[NFTA_QUOTA_CONSUMED]));
116 e->flags |= (1 << NFTNL_OBJ_QUOTA_CONSUMED);
117 }
118 if (tb[NFTA_QUOTA_FLAGS]) {
119 quota->flags = ntohl(mnl_attr_get_u32(tb[NFTA_QUOTA_FLAGS]));
120 e->flags |= (1 << NFTNL_OBJ_QUOTA_FLAGS);
121 }
122
123 return 0;
124}
125
126static int nftnl_obj_quota_snprintf(char *buf, size_t len,
127 uint32_t flags,
128 const struct nftnl_obj *e)
129{
130 struct nftnl_obj_quota *quota = nftnl_obj_data(e);
131
132 return snprintf(buf, len, "bytes %"PRIu64" flags %u ",
133 quota->bytes, quota->flags);
134}
135
136static struct attr_policy obj_quota_attr_policy[__NFTNL_OBJ_QUOTA_MAX] = {
137 [NFTNL_OBJ_QUOTA_BYTES] = { .maxlen = sizeof(uint64_t) },
138 [NFTNL_OBJ_QUOTA_CONSUMED] = { .maxlen = sizeof(uint64_t) },
139 [NFTNL_OBJ_QUOTA_FLAGS] = { .maxlen = sizeof(uint32_t) },
140};
141
142struct obj_ops obj_ops_quota = {
143 .name = "quota",
144 .type = NFT_OBJECT_QUOTA,
145 .alloc_len = sizeof(struct nftnl_obj_quota),
146 .nftnl_max_attr = __NFTNL_OBJ_QUOTA_MAX - 1,
147 .attr_policy = obj_quota_attr_policy,
148 .set = nftnl_obj_quota_set,
149 .get = nftnl_obj_quota_get,
150 .parse = nftnl_obj_quota_parse,
151 .build = nftnl_obj_quota_build,
152 .output = nftnl_obj_quota_snprintf,
153};